-
Hello. Is there any examples to show CJK chars/fonts in raylib? |
Beta Was this translation helpful? Give feedback.
Answered by
raysan5
Jul 5, 2022
Replies: 2 comments 4 replies
-
I find the sample code:
|
Beta Was this translation helpful? Give feedback.
1 reply
-
Here a more complete version. Not tested but it should work: #include "raylib.h"
#include <stdlib.h> // Required for: calloc(), realloc(), free()
#include <string.h> // Required for: memcpy()
// Text to be displayed, must be UTF-8 (save this code file as UTF-8)
// NOTE: It can contain all the text to be rendered in the game to be scanned to
// get all the required codepoints
static char *text = "世界,你好!";
// Remove codepoint duplicates if requested
static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointResultCount);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [text] example - codepoints loading");
// Get codepoints from text
int codepointCount = 0;
int *codepoints = LoadCodepoints(text, &codepointCount);
// Removed duplicate codepoints to generate smaller font atlas
int codepointsNoDuplicatesCount = 0;
int *codepointsNoDuplicates = CodepointRemoveDuplicates(codepoints, codepointCount, &codepointsNoDuplicatesCount);
UnloadCodepoints(codepoints);
// Load font containing all the provided codepoints
Font font = LoadFontEx("./SimHei.ttf", 36, codepointsNoDuplicates, codepointsNoDuplicatesCount);
// Unload codepoints, atlas has already been generated
UnloadCodepoints(codepointsNoDuplicates);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextEx(font, text, (Vector2){ 50, 50 }, 32, 5, RED);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadFont(font); // Unload font
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
// Remove codepoint duplicates if requested
// WARNING: This process could be a bit slow if there text to process is very long
static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointResultCount)
{
int codepointsClearCount = codepointCount;
int *codepointsClear = (int *)RL_CALLOC(codepointCount, sizeof(int));
memcpy(codepointsClear, codepoints, codepointCount*sizeof(int));
// Remove duplicates
for (int i = 0; i < codepointsClearCount; i++)
{
for (int j = i + 1; j < codepointsClearCount; j++)
{
if (codepointsClear[i] == codepointsClear[j])
{
for (int k = j; k < codepointsClearCount; k++) codepointsClear[k] = codepointsClear[k + 1];
codepointsClearCount--;
j--;
}
}
}
// Resize array to number of resulting codepoints (lower or equal to input)
RL_REALLOC(codepointsClear, codepointsClearCount*sizeof(int));
*codepointResultCount = codepointsClearCount;
return codepointsClear;
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
wlxmhls
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here a more complete version. Not tested but it should work: