From 424d3ca8d9c5d612606444b2a2099cfad37f1888 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 14 May 2019 15:34:23 +0200 Subject: examples review Redesigns, deletes and renames Also noted authors propertly on contributed examples --- examples/text/text_bmfont_ttf.c | 40 ++++++--- examples/text/text_bmfont_ttf.png | Bin 19542 -> 20086 bytes examples/text/text_bmfont_unordered.c | 65 --------------- examples/text/text_bmfont_unordered.png | Bin 18713 -> 0 bytes examples/text/text_draw_inside_rectangle.c | 120 --------------------------- examples/text/text_draw_inside_rectangle.png | Bin 17844 -> 0 bytes examples/text/text_rectangle_bounds.c | 120 +++++++++++++++++++++++++++ examples/text/text_rectangle_bounds.png | Bin 0 -> 17844 bytes examples/text/text_unicode.c | 2 + 9 files changed, 149 insertions(+), 198 deletions(-) delete mode 100644 examples/text/text_bmfont_unordered.c delete mode 100644 examples/text/text_bmfont_unordered.png delete mode 100644 examples/text/text_draw_inside_rectangle.c delete mode 100644 examples/text/text_draw_inside_rectangle.png create mode 100644 examples/text/text_rectangle_bounds.c create mode 100644 examples/text/text_rectangle_bounds.png (limited to 'examples/text') diff --git a/examples/text/text_bmfont_ttf.c b/examples/text/text_bmfont_ttf.c index f71f5ddb..ca26b2cf 100644 --- a/examples/text/text_bmfont_ttf.c +++ b/examples/text/text_bmfont_ttf.c @@ -20,17 +20,20 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [text] example - bmfont and ttf sprite fonts loading"); - const char msgBm[64] = "THIS IS AN AngelCode SPRITE FONT"; - const char msgTtf[64] = "THIS SPRITE FONT has been GENERATED from a TTF"; + // Define characters to draw + // NOTE: raylib supports UTF-8 encoding, following list is actually codified as UTF8 internally + const char msg[256] = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHI\nJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmn\nopqrstuvwxyz{|}~¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓ\nÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷\nøùúûüýþÿ"; // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) - Font fontBm = LoadFont("resources/bmfont.fnt"); // BMFont (AngelCode) - Font fontTtf = LoadFont("resources/pixantiqua.ttf"); // TTF font - - Vector2 fontPosition; - - fontPosition.x = screenWidth/2 - MeasureTextEx(fontBm, msgBm, fontBm.baseSize, 0).x/2; - fontPosition.y = screenHeight/2 - fontBm.baseSize/2 - 80; + + // BMFont (AngelCode) : Font data and image atlas have been generated using external program + Font fontBm = LoadFont("resources/pixantiqua.fnt"); + + // TTF font : Font data and atlas are generated directly from TTF + // NOTE: We define a font base size of 32 pixels tall and up-to 250 characters + Font fontTtf = LoadFontEx("resources/pixantiqua.ttf", 32, 0, 250); + + bool useTtf = false; SetTargetFPS(60); //-------------------------------------------------------------------------------------- @@ -40,7 +43,8 @@ int main() { // Update //---------------------------------------------------------------------------------- - // TODO: Update variables here... + if (IsKeyDown(KEY_SPACE)) useTtf = true; + else useTtf = false; //---------------------------------------------------------------------------------- // Draw @@ -48,9 +52,19 @@ int main() BeginDrawing(); ClearBackground(RAYWHITE); - - DrawTextEx(fontBm, msgBm, fontPosition, fontBm.baseSize, 0, MAROON); - DrawTextEx(fontTtf, msgTtf, (Vector2){ 75.0f, 240.0f }, fontTtf.baseSize*0.8f, 2, LIME); + + DrawText("Press SPACE to use TTF generated font", 20, 20, 20, LIGHTGRAY); + + if (!useTtf) + { + DrawTextEx(fontBm, msg, (Vector2){ 20.0f, 100.0f }, fontBm.baseSize, 2, MAROON); + DrawText("Using BMFont (Angelcode) imported", 20, GetScreenHeight() - 30, 20, GRAY); + } + else + { + DrawTextEx(fontTtf, msg, (Vector2){ 20.0f, 100.0f }, fontTtf.baseSize, 2, LIME); + DrawText("Using TTF font generated", 20, GetScreenHeight() - 30, 20, GRAY); + } EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/examples/text/text_bmfont_ttf.png b/examples/text/text_bmfont_ttf.png index 8305d36b..a99027ad 100644 Binary files a/examples/text/text_bmfont_ttf.png and b/examples/text/text_bmfont_ttf.png differ diff --git a/examples/text/text_bmfont_unordered.c b/examples/text/text_bmfont_unordered.c deleted file mode 100644 index a5147619..00000000 --- a/examples/text/text_bmfont_unordered.c +++ /dev/null @@ -1,65 +0,0 @@ -/******************************************************************************************* -* -* raylib [text] example - BMFont unordered chars loading and drawing -* -* This example has been created using raylib 1.4 (www.raylib.com) -* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) -* -* Copyright (c) 2016 Ramon Santamaria (@raysan5) -* -********************************************************************************************/ - -#include "raylib.h" - -int main() -{ - // Initialization - //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; - - InitWindow(screenWidth, screenHeight, "raylib [text] example - bmfont unordered loading and drawing"); - - // NOTE: Using chars outside the [32..127] limits! - // NOTE: If a character is not found in the font, it just renders a space - const char msg[256] = "ASCII extended characters:\n¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆ\nÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæ\nçèéêëìíîïðñòóôõö÷øùúûüýþÿ"; - - // NOTE: Loaded font has an unordered list of characters (chars in the range 32..255) - Font font = LoadFont("resources/pixantiqua.fnt"); // BMFont (AngelCode) - - SetTargetFPS(60); - //-------------------------------------------------------------------------------------- - - // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key - { - // Update - //---------------------------------------------------------------------------------- - // TODO: Update variables here... - //---------------------------------------------------------------------------------- - - // Draw - //---------------------------------------------------------------------------------- - BeginDrawing(); - - ClearBackground(RAYWHITE); - - DrawText("Font name: PixAntiqua", 40, 50, 20, GRAY); - DrawText(FormatText("Font base size: %i", font.baseSize), 40, 80, 20, GRAY); - DrawText(FormatText("Font chars number: %i", font.charsCount), 40, 110, 20, GRAY); - - DrawTextEx(font, msg, (Vector2){ 40, 180 }, font.baseSize, 0, MAROON); - - EndDrawing(); - //---------------------------------------------------------------------------------- - } - - // De-Initialization - //-------------------------------------------------------------------------------------- - UnloadFont(font); // AngelCode Font unloading - - CloseWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- - - return 0; -} \ No newline at end of file diff --git a/examples/text/text_bmfont_unordered.png b/examples/text/text_bmfont_unordered.png deleted file mode 100644 index c6767567..00000000 Binary files a/examples/text/text_bmfont_unordered.png and /dev/null differ diff --git a/examples/text/text_draw_inside_rectangle.c b/examples/text/text_draw_inside_rectangle.c deleted file mode 100644 index e60fa5e5..00000000 --- a/examples/text/text_draw_inside_rectangle.c +++ /dev/null @@ -1,120 +0,0 @@ -/******************************************************************************************* -* -* raylib [text] example - Draw text inside a rectangle -* -* This example has been created using raylib 2.3 (www.raylib.com) -* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) -* -* Copyright (c) 2018 Vlad Adrian (@demizdor) -* -********************************************************************************************/ - -#include "raylib.h" - -int main() -{ - // Initialization - //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; - - InitWindow(screenWidth, screenHeight, "raylib [text] example - draw text inside a rectangle"); - - char text[] = "Text cannot escape\tthis container\t...word wrap also works when active so here's\ - a long text for testing.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\ - tempor incididunt ut labore et dolore magna aliqua. Nec ullamcorper sit amet risus nullam eget felis eget."; - - bool resizing = false; - bool wordWrap = true; - - Rectangle container = { 25, 25, screenWidth - 50, screenHeight - 250}; - Rectangle resizer = { container.x + container.width - 17, container.y + container.height - 17, 14, 14 }; - - // Minimum width and heigh for the container rectangle - const int minWidth = 60; - const int minHeight = 60; - const int maxWidth = screenWidth - 50; - const int maxHeight = screenHeight - 160; - - Vector2 lastMouse = { 0, 0 }; // Stores last mouse coordinates - - Color borderColor = MAROON; // Container border color - - Font font = GetFontDefault(); // Get default system font - - SetTargetFPS(60); - //-------------------------------------------------------------------------------------- - - // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key - { - // Update - //---------------------------------------------------------------------------------- - if (IsKeyPressed(KEY_SPACE)) wordWrap = !wordWrap; - - Vector2 mouse = GetMousePosition(); - - // Check if the mouse is inside the container and toggle border color - if (CheckCollisionPointRec(mouse, container)) borderColor = Fade(MAROON, 0.4f); - else if (!resizing) borderColor = MAROON; - - // Container resizing logic - if (resizing) - { - if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) resizing = false; - - int width = container.width + (mouse.x - lastMouse.x); - container.width = (width > minWidth)? ((width < maxWidth)? width : maxWidth) : minWidth; - - int height = container.height + (mouse.y - lastMouse.y); - container.height = (height > minHeight)? ((height < maxHeight)? height : maxHeight) : minHeight; - } - else - { - // Check if we're resizing - if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, resizer)) resizing = true; - } - - // Move resizer rectangle properly - resizer.x = container.x + container.width - 17; - resizer.y = container.y + container.height - 17; - - lastMouse = mouse; // Update mouse - //---------------------------------------------------------------------------------- - - // Draw - //---------------------------------------------------------------------------------- - BeginDrawing(); - - ClearBackground(RAYWHITE); - - DrawRectangleLinesEx(container, 3, borderColor); // Draw container border - - // Draw text in container (add some padding) - DrawTextRec(font, text, - (Rectangle){ container.x + 4, container.y + 4, container.width - 4, container.height - 4 }, - 20.0f, 2.0f, wordWrap, GRAY); - - DrawRectangleRec(resizer, borderColor); // Draw the resize box - - // Draw info - DrawText("Word Wrap: ", 313, screenHeight-115, 20, BLACK); - if (wordWrap) DrawText("ON", 447, screenHeight - 115, 20, RED); - else DrawText("OFF", 447, screenHeight - 115, 20, BLACK); - DrawText("Press [SPACE] to toggle word wrap", 218, screenHeight - 91, 20, GRAY); - - DrawRectangle(0, screenHeight - 54, screenWidth, 54, GRAY); - DrawText("Click hold & drag the to resize the container", 155, screenHeight - 38, 20, RAYWHITE); - DrawRectangleRec((Rectangle){ 382, screenHeight - 34, 12, 12 }, MAROON); - - EndDrawing(); - //---------------------------------------------------------------------------------- - } - - // De-Initialization - //-------------------------------------------------------------------------------------- - CloseWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- - - return 0; -} \ No newline at end of file diff --git a/examples/text/text_draw_inside_rectangle.png b/examples/text/text_draw_inside_rectangle.png deleted file mode 100644 index f46b1096..00000000 Binary files a/examples/text/text_draw_inside_rectangle.png and /dev/null differ diff --git a/examples/text/text_rectangle_bounds.c b/examples/text/text_rectangle_bounds.c new file mode 100644 index 00000000..d4cd240a --- /dev/null +++ b/examples/text/text_rectangle_bounds.c @@ -0,0 +1,120 @@ +/******************************************************************************************* +* +* raylib [text] example - Draw text inside a rectangle +* +* This example has been created using raylib 2.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [text] example - draw text inside a rectangle"); + + char text[] = "Text cannot escape\tthis container\t...word wrap also works when active so here's\ + a long text for testing.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\ + tempor incididunt ut labore et dolore magna aliqua. Nec ullamcorper sit amet risus nullam eget felis eget."; + + bool resizing = false; + bool wordWrap = true; + + Rectangle container = { 25, 25, screenWidth - 50, screenHeight - 250}; + Rectangle resizer = { container.x + container.width - 17, container.y + container.height - 17, 14, 14 }; + + // Minimum width and heigh for the container rectangle + const int minWidth = 60; + const int minHeight = 60; + const int maxWidth = screenWidth - 50; + const int maxHeight = screenHeight - 160; + + Vector2 lastMouse = { 0, 0 }; // Stores last mouse coordinates + Color borderColor = MAROON; // Container border color + Font font = GetFontDefault(); // Get default system font + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyPressed(KEY_SPACE)) wordWrap = !wordWrap; + + Vector2 mouse = GetMousePosition(); + + // Check if the mouse is inside the container and toggle border color + if (CheckCollisionPointRec(mouse, container)) borderColor = Fade(MAROON, 0.4f); + else if (!resizing) borderColor = MAROON; + + // Container resizing logic + if (resizing) + { + if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) resizing = false; + + int width = container.width + (mouse.x - lastMouse.x); + container.width = (width > minWidth)? ((width < maxWidth)? width : maxWidth) : minWidth; + + int height = container.height + (mouse.y - lastMouse.y); + container.height = (height > minHeight)? ((height < maxHeight)? height : maxHeight) : minHeight; + } + else + { + // Check if we're resizing + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, resizer)) resizing = true; + } + + // Move resizer rectangle properly + resizer.x = container.x + container.width - 17; + resizer.y = container.y + container.height - 17; + + lastMouse = mouse; // Update mouse + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawRectangleLinesEx(container, 3, borderColor); // Draw container border + + // Draw text in container (add some padding) + DrawTextRec(font, text, + (Rectangle){ container.x + 4, container.y + 4, container.width - 4, container.height - 4 }, + 20.0f, 2.0f, wordWrap, GRAY); + + DrawRectangleRec(resizer, borderColor); // Draw the resize box + + // Draw info + DrawText("Word Wrap: ", 313, screenHeight-115, 20, BLACK); + if (wordWrap) DrawText("ON", 447, screenHeight - 115, 20, RED); + else DrawText("OFF", 447, screenHeight - 115, 20, BLACK); + DrawText("Press [SPACE] to toggle word wrap", 218, screenHeight - 91, 20, GRAY); + + DrawRectangle(0, screenHeight - 54, screenWidth, 54, GRAY); + DrawText("Click hold & drag the to resize the container", 155, screenHeight - 38, 20, RAYWHITE); + DrawRectangleRec((Rectangle){ 382, screenHeight - 34, 12, 12 }, MAROON); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/text/text_rectangle_bounds.png b/examples/text/text_rectangle_bounds.png new file mode 100644 index 00000000..f46b1096 Binary files /dev/null and b/examples/text/text_rectangle_bounds.png differ diff --git a/examples/text/text_unicode.c b/examples/text/text_unicode.c index b6c6db30..6b456a75 100644 --- a/examples/text/text_unicode.c +++ b/examples/text/text_unicode.c @@ -5,6 +5,8 @@ * This example has been created using raylib 2.5 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * +* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5) +* * Copyright (c) 2019 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5) * ********************************************************************************************/ -- cgit v1.2.3