REVIEWED: examples: Replace TABS and Remove trailing spaces

This commit is contained in:
Ray
2025-11-19 13:18:10 +01:00
parent bd21d74914
commit 0b9f463e64
36 changed files with 440 additions and 447 deletions

View File

@@ -36,11 +36,11 @@ int main(void)
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [text] example - inline styling");
Vector2 textSize = { 0 }; // Measure text box for provided font and text
Color colRandom = RED; // Random color used on text
int frameCounter = 0; // Used to generate a new random color every certain frames
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@@ -50,7 +50,7 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
frameCounter++;
if ((frameCounter%20) == 0)
{
colRandom.r = (unsigned char)GetRandomValue(0, 255);
@@ -67,12 +67,12 @@ int main(void)
ClearBackground(RAYWHITE);
// Text inline styling strategy used: [ ] delimiters for format
// - Define foreground color: [cRRGGBBAA]
// - Define foreground color: [cRRGGBBAA]
// - Define background color: [bRRGGBBAA]
// - Reset formating: [r]
// Example: [bAA00AAFF][cFF0000FF]red text on gray background[r] normal text
DrawTextStyled(GetFontDefault(), "This changes the [cFF0000FF]foreground color[r] of provided text!!!",
DrawTextStyled(GetFontDefault(), "This changes the [cFF0000FF]foreground color[r] of provided text!!!",
(Vector2){ 100, 80 }, 20.0f, 2.0f, BLACK);
DrawTextStyled(GetFontDefault(), "This changes the [bFF00FFFF]background color[r] of provided text!!!",
@@ -80,11 +80,11 @@ int main(void)
DrawTextStyled(GetFontDefault(), "This changes the [c00ff00ff][bff0000ff]foreground and background colors[r]!!!",
(Vector2){ 100, 160 }, 20.0f, 2.0f, BLACK);
// Get pointer to formated text
const char *text = TextFormat("Let's be [c%02x%02x%02xFF]CREATIVE[r] !!!", colRandom.r, colRandom.g, colRandom.b);
DrawTextStyled(GetFontDefault(), text, (Vector2){ 100, 220 }, 40.0f, 2.0f, BLACK);
textSize = MeasureTextStyled(GetFontDefault(), text, 40.0f, 2.0f);
DrawRectangleLines(100, 220, (int)textSize.x, (int)textSize.y, GREEN);
@@ -108,13 +108,13 @@ int main(void)
static void DrawTextStyled(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color color)
{
// Text inline styling strategy used: [ ] delimiters for format
// - Define foreground color: [cRRGGBBAA]
// - Define foreground color: [cRRGGBBAA]
// - Define background color: [bRRGGBBAA]
// - Reset formating: [r]
// Example: [bAA00AAFF][cFF0000FF]red text on gray background[r] normal text
if (font.texture.id == 0) font = GetFontDefault();
int textLen = TextLength(text);
Color colFront = color;
@@ -144,14 +144,14 @@ static void DrawTextStyled(Font font, const char *text, Vector2 position, float
{
colFront = color;
colBack = BLANK;
i += 3; // Skip "[r]"
continue; // Do not draw characters
}
else if (((i + 1) < textLen) && ((text[i + 1] == 'c') || (text[i + 1] == 'b')))
{
i += 2; // Skip "[c" or "[b" to start parsing color
// Parse following color
char colHexText[9] = { 0 };
const char *textPtr = &text[i]; // Color should start here, let's see...
@@ -168,12 +168,12 @@ static void DrawTextStyled(Font font, const char *text, Vector2 position, float
}
else break; // Only affects while loop
}
// Convert hex color text into actual Color
unsigned int colHexValue = strtoul(colHexText, NULL, 16);
if (text[i - 1] == 'c') colFront = GetColor(colHexValue);
else if (text[i - 1] == 'b') colBack = GetColor(colHexValue);
i += (colHexCount + 1); // Skip color value retrieved and ']'
continue; // Do not draw characters
}
@@ -249,7 +249,7 @@ static Vector2 MeasureTextStyled(Font font, const char *text, float fontSize, fl
}
else break; // Only affects while loop
}
i += (colHexCount + 1); // Skip color value retrieved and ']'
continue; // Do not measure characters
}
@@ -260,7 +260,7 @@ static Vector2 MeasureTextStyled(Font font, const char *text, float fontSize, fl
if (font.glyphs[index].advanceX > 0) textWidth += font.glyphs[index].advanceX;
else textWidth += (font.recs[index].width + font.glyphs[index].offsetX);
validCodepointCounter++;
i += codepointByteCount;
}

View File

@@ -58,7 +58,7 @@ int main(void)
// Load font with default Unicode range: Basic ASCII [32-127]
font = LoadFont("resources/NotoSansTC-Regular.ttf");
// Add required ranges to loaded font
switch (unicodeRange)
{
@@ -128,11 +128,11 @@ int main(void)
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("ADD CODEPOINTS: [1][2][3][4]", 20, 20, 20, MAROON);
// Render test strings in different languages
DrawTextEx(font, "> English: Hello World!", (Vector2){ 50, 70 }, 32, 1, DARKGRAY); // English
DrawTextEx(font, "> Español: Hola mundo!", (Vector2){ 50, 120 }, 32, 1, DARKGRAY); // Spanish
@@ -141,7 +141,7 @@ int main(void)
DrawTextEx(font, "> 中文: 你好世界!", (Vector2){ 50, 270 }, 32, 1, DARKGRAY); // Chinese
DrawTextEx(font, "> 日本語: こんにちは世界!", (Vector2){ 50, 320 }, 32, 1, DARKGRAY); // Japanese
//DrawTextEx(font, "देवनागरी: होला मुंडो!", (Vector2){ 50, 350 }, 32, 1, DARKGRAY); // Devanagari (glyphs not available in font)
// Draw font texture scaled to screen
float atlasScale = 380.0f/font.texture.width;
DrawRectangleRec((Rectangle) { 400.0f, 16.0f, font.texture.width* atlasScale, font.texture.height* atlasScale }, BLACK);
@@ -161,7 +161,7 @@ int main(void)
DrawRectangle(0, 125, screenWidth, 200, GRAY);
DrawText("GENERATING FONT ATLAS...", 120, 210, 40, BLACK);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
@@ -184,10 +184,10 @@ static void AddCodepointRange(Font *font, const char *fontPath, int start, int s
{
int rangeSize = stop - start + 1;
int currentRangeSize = font->glyphCount;
// TODO: Load glyphs from provided vector font (if available),
// add them to existing font, regenerating font image and texture
int updatedCodepointCount = currentRangeSize + rangeSize;
int *updatedCodepoints = (int *)RL_CALLOC(updatedCodepointCount, sizeof(int));

View File

@@ -39,25 +39,25 @@ int main(void)
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [text] example - words alignment");
// Define the rectangle we will draw the text in
Rectangle textContainerRect = (Rectangle){ screenWidth/2-screenWidth/4, screenHeight/2-screenHeight/3, screenWidth/2, screenHeight*2/3 };
// Some text to display the current alignment
const char *textAlignNameH[] = { "Left", "Centre", "Right" };
const char *textAlignNameV[] = { "Top", "Middle", "Bottom" };
// Define the text we're going to draw in the rectangle
int wordIndex = 0;
int wordCount = 0;
char **words = TextSplit("raylib is a simple and easy-to-use library to enjoy videogames programming", ' ', &wordCount);
// Initialize the font size we're going to use
int fontSize = 40;
// And of course the font...
Font font = GetFontDefault();
// Intialize the alignment variables
TextAlignment hAlign = TEXT_ALIGN_CENTRE;
TextAlignment vAlign = TEXT_ALIGN_MIDDLE;
@@ -70,7 +70,7 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_LEFT)) {
hAlign = hAlign - 1;
if (hAlign < 0) hAlign = 0;
@@ -87,10 +87,10 @@ int main(void)
vAlign = vAlign + 1;
if (vAlign > 2) vAlign = 2;
}
// One word per second
wordIndex = (int)GetTime() % wordCount;
//----------------------------------------------------------------------------------
// Draw
@@ -103,16 +103,16 @@ int main(void)
DrawText(TextFormat("Alignment: Horizontal = %s, Vertical = %s", textAlignNameH[hAlign], textAlignNameV[vAlign]), 20, 40, 20, LIGHTGRAY);
DrawRectangleRec(textContainerRect, BLUE);
// Get the size of the text to draw
Vector2 textSize = MeasureTextEx(font, words[wordIndex], fontSize, fontSize*.1f);
// Calculate the top-left text position based on the rectangle and alignment
Vector2 textPos = (Vector2) {
textContainerRect.x + Lerp(0.0f, textContainerRect.width - textSize.x, ((float)hAlign) * 0.5f),
textContainerRect.y + Lerp(0.0f, textContainerRect.height - textSize.y, ((float)vAlign) * 0.5f)
};
// Draw the text
DrawTextEx(font, words[wordIndex], textPos, fontSize, fontSize*.1f, RAYWHITE);