diff options
| author | Ray <raysan5@gmail.com> | 2017-04-09 23:46:47 +0200 |
|---|---|---|
| committer | Ray <raysan5@gmail.com> | 2017-04-09 23:46:47 +0200 |
| commit | f7bebf9861734c47d0840868f243b186a59a96ba (patch) | |
| tree | f5ca2d320c680ef5926b5455e4498646031cd5ac /docs/examples/src/text | |
| parent | 8374460c3986b16c68a6dea0643e9af541987d52 (diff) | |
| download | raylib-f7bebf9861734c47d0840868f243b186a59a96ba.tar.gz raylib-f7bebf9861734c47d0840868f243b186a59a96ba.zip | |
Working on web examples
Reorganizing folders
Review examples
Work on makefile and loader.html
Diffstat (limited to 'docs/examples/src/text')
| -rw-r--r-- | docs/examples/src/text/text_bmfont_ttf.c | 68 | ||||
| -rw-r--r-- | docs/examples/src/text/text_bmfont_unordered.c | 65 | ||||
| -rw-r--r-- | docs/examples/src/text/text_format_text.c | 62 | ||||
| -rw-r--r-- | docs/examples/src/text/text_rbmf_fonts.c | 97 | ||||
| -rw-r--r-- | docs/examples/src/text/text_sprite_fonts.c | 77 | ||||
| -rw-r--r-- | docs/examples/src/text/text_ttf_loading.c | 130 | ||||
| -rw-r--r-- | docs/examples/src/text/text_writing_anim.c | 62 |
7 files changed, 561 insertions, 0 deletions
diff --git a/docs/examples/src/text/text_bmfont_ttf.c b/docs/examples/src/text/text_bmfont_ttf.c new file mode 100644 index 00000000..caece548 --- /dev/null +++ b/docs/examples/src/text/text_bmfont_ttf.c @@ -0,0 +1,68 @@ +/******************************************************************************************* +* +* raylib [text] example - BMFont and TTF SpriteFonts loading +* +* 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 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"; + + // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) + SpriteFont fontBm = LoadSpriteFont("resources/fonts/bmfont.fnt"); // BMFont (AngelCode) + SpriteFont fontTtf = LoadSpriteFont("resources/fonts/pixantiqua.ttf"); // TTF font + + Vector2 fontPosition; + + fontPosition.x = screenWidth/2 - MeasureTextEx(fontBm, msgBm, fontBm.size, 0).x/2; + fontPosition.y = screenHeight/2 - fontBm.size/2 - 80; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update variables here... + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawTextEx(fontBm, msgBm, fontPosition, fontBm.size, 0, MAROON); + DrawTextEx(fontTtf, msgTtf, (Vector2){ 75.0f, 240.0f }, fontTtf.size*0.8f, 2, LIME); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadSpriteFont(fontBm); // AngelCode SpriteFont unloading + UnloadSpriteFont(fontTtf); // TTF SpriteFont unloading + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/docs/examples/src/text/text_bmfont_unordered.c b/docs/examples/src/text/text_bmfont_unordered.c new file mode 100644 index 00000000..b29c5f8b --- /dev/null +++ b/docs/examples/src/text/text_bmfont_unordered.c @@ -0,0 +1,65 @@ +/******************************************************************************************* +* +* 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) + SpriteFont font = LoadSpriteFont("resources/fonts/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.size), 40, 80, 20, GRAY); + DrawText(FormatText("Font chars number: %i", font.numChars), 40, 110, 20, GRAY); + + DrawTextEx(font, msg, (Vector2){ 40, 180 }, font.size, 0, MAROON); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadSpriteFont(font); // AngelCode SpriteFont unloading + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/docs/examples/src/text/text_format_text.c b/docs/examples/src/text/text_format_text.c new file mode 100644 index 00000000..ca28be74 --- /dev/null +++ b/docs/examples/src/text/text_format_text.c @@ -0,0 +1,62 @@ +/******************************************************************************************* +* +* raylib [text] example - Text formatting +* +* This example has been created using raylib 1.1 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [text] example - text formatting"); + + int score = 100020; + int hiscore = 200450; + int lives = 5; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText(FormatText("Score: %08i", score), 200, 80, 20, RED); + + DrawText(FormatText("HiScore: %08i", hiscore), 200, 120, 20, GREEN); + + DrawText(FormatText("Lives: %02i", lives), 200, 160, 40, BLUE); + + DrawText(FormatText("Elapsed Time: %02.02f ms", GetFrameTime()*1000), 200, 220, 20, BLACK); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/docs/examples/src/text/text_rbmf_fonts.c b/docs/examples/src/text/text_rbmf_fonts.c new file mode 100644 index 00000000..b4bd851b --- /dev/null +++ b/docs/examples/src/text/text_rbmf_fonts.c @@ -0,0 +1,97 @@ +/******************************************************************************************* +* +* raylib [text] example - raylib bitmap font (rbmf) loading and usage +* +* NOTE: raylib is distributed with some free to use fonts (even for commercial pourposes!) +* To view details and credits for those fonts, check raylib license file +* +* This example has been created using raylib 1.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [text] example - rBMF fonts"); + + // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) + SpriteFont fonts[8]; + + fonts[0] = LoadSpriteFont("resources/fonts/alagard.rbmf"); // rBMF font loading + fonts[1] = LoadSpriteFont("resources/fonts/pixelplay.rbmf"); // rBMF font loading + fonts[2] = LoadSpriteFont("resources/fonts/mecha.rbmf"); // rBMF font loading + fonts[3] = LoadSpriteFont("resources/fonts/setback.rbmf"); // rBMF font loading + fonts[4] = LoadSpriteFont("resources/fonts/romulus.rbmf"); // rBMF font loading + fonts[5] = LoadSpriteFont("resources/fonts/pixantiqua.rbmf"); // rBMF font loading + fonts[6] = LoadSpriteFont("resources/fonts/alpha_beta.rbmf"); // rBMF font loading + fonts[7] = LoadSpriteFont("resources/fonts/jupiter_crash.rbmf"); // rBMF font loading + + const char *messages[8] = { "ALAGARD FONT designed by Hewett Tsoi", + "PIXELPLAY FONT designed by Aleksander Shevchuk", + "MECHA FONT designed by Captain Falcon", + "SETBACK FONT designed by Brian Kent (AEnigma)", + "ROMULUS FONT designed by Hewett Tsoi", + "PIXANTIQUA FONT designed by Gerhard Grossmann", + "ALPHA_BETA FONT designed by Brian Kent (AEnigma)", + "JUPITER_CRASH FONT designed by Brian Kent (AEnigma)" }; + + const int spacings[8] = { 2, 4, 8, 4, 3, 4, 4, 1 }; + + Vector2 positions[8]; + + for (int i = 0; i < 8; i++) + { + positions[i].x = screenWidth/2 - MeasureTextEx(fonts[i], messages[i], fonts[i].size*2, spacings[i]).x/2; + positions[i].y = 60 + fonts[i].size + 50*i; + } + + Color colors[8] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD }; + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText("free fonts included with raylib", 250, 20, 20, DARKGRAY); + DrawLine(220, 50, 590, 50, DARKGRAY); + + for (int i = 0; i < 8; i++) + { + DrawTextEx(fonts[i], messages[i], positions[i], fonts[i].size*2, spacings[i], colors[i]); + } + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + for (int i = 0; i < 8; i++) + { + UnloadSpriteFont(fonts[i]); // SpriteFont unloading + } + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/docs/examples/src/text/text_sprite_fonts.c b/docs/examples/src/text/text_sprite_fonts.c new file mode 100644 index 00000000..c73eda85 --- /dev/null +++ b/docs/examples/src/text/text_sprite_fonts.c @@ -0,0 +1,77 @@ +/******************************************************************************************* +* +* raylib [text] example - SpriteFont loading and usage +* +* This example has been created using raylib 1.0 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [text] example - sprite fonts usage"); + + const char msg1[50] = "THIS IS A custom SPRITE FONT..."; + const char msg2[50] = "...and this is ANOTHER CUSTOM font..."; + const char msg3[50] = "...and a THIRD one! GREAT! :D"; + + // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) + SpriteFont font1 = LoadSpriteFont("resources/fonts/custom_mecha.png"); // SpriteFont loading + SpriteFont font2 = LoadSpriteFont("resources/fonts/custom_alagard.png"); // SpriteFont loading + SpriteFont font3 = LoadSpriteFont("resources/fonts/custom_jupiter_crash.png"); // SpriteFont loading + + Vector2 fontPosition1, fontPosition2, fontPosition3; + + fontPosition1.x = screenWidth/2 - MeasureTextEx(font1, msg1, font1.size, -3).x/2; + fontPosition1.y = screenHeight/2 - font1.size/2 - 80; + + fontPosition2.x = screenWidth/2 - MeasureTextEx(font2, msg2, font2.size, -2).x/2; + fontPosition2.y = screenHeight/2 - font2.size/2 - 10; + + fontPosition3.x = screenWidth/2 - MeasureTextEx(font3, msg3, font3.size, 2).x/2; + fontPosition3.y = screenHeight/2 - font3.size/2 + 50; + + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update variables here... + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawTextEx(font1, msg1, fontPosition1, font1.size, -3, WHITE); + DrawTextEx(font2, msg2, fontPosition2, font2.size, -2, WHITE); + DrawTextEx(font3, msg3, fontPosition3, font3.size, 2, WHITE); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadSpriteFont(font1); // SpriteFont unloading + UnloadSpriteFont(font2); // SpriteFont unloading + UnloadSpriteFont(font3); // SpriteFont unloading + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/docs/examples/src/text/text_ttf_loading.c b/docs/examples/src/text/text_ttf_loading.c new file mode 100644 index 00000000..33cda7f5 --- /dev/null +++ b/docs/examples/src/text/text_ttf_loading.c @@ -0,0 +1,130 @@ +/******************************************************************************************* +* +* raylib [text] example - TTF loading and usage +* +* This example has been created using raylib 1.3.0 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [text] example - ttf loading"); + + const char msg[50] = "TTF SpriteFont"; + + // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) + + // TTF SpriteFont loading with custom generation parameters + SpriteFont font = LoadSpriteFontTTF("resources/fonts/KAISG.ttf", 96, 0, 0); + + // Generate mipmap levels to use trilinear filtering + // NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR + GenTextureMipmaps(&font.texture); + + float fontSize = font.size; + Vector2 fontPosition = { 40, screenHeight/2 + 50 }; + Vector2 textSize; + + SetTextureFilter(font.texture, FILTER_POINT); + int currentFontFilter = 0; // FILTER_POINT + + int count = 0; + char **droppedFiles; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + fontSize += GetMouseWheelMove()*4.0f; + + // Choose font texture filter method + if (IsKeyPressed(KEY_ONE)) + { + SetTextureFilter(font.texture, FILTER_POINT); + currentFontFilter = 0; + } + else if (IsKeyPressed(KEY_TWO)) + { + SetTextureFilter(font.texture, FILTER_BILINEAR); + currentFontFilter = 1; + } + else if (IsKeyPressed(KEY_THREE)) + { + // NOTE: Trilinear filter won't be noticed on 2D drawing + SetTextureFilter(font.texture, FILTER_TRILINEAR); + currentFontFilter = 2; + } + + textSize = MeasureTextEx(font, msg, fontSize, 0); + + if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10; + else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10; + + // Load a dropped TTF file dynamically (at current fontSize) + if (IsFileDropped()) + { + droppedFiles = GetDroppedFiles(&count); + + if (count == 1) // Only support one ttf file dropped + { + UnloadSpriteFont(font); + font = LoadSpriteFontTTF(droppedFiles[0], fontSize, 0, 0); + ClearDroppedFiles(); + } + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY); + DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY); + DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY); + DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY); + + DrawTextEx(font, msg, fontPosition, fontSize, 0, BLACK); + + // TODO: It seems texSize measurement is not accurate due to chars offsets... + //DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED); + + DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY); + DrawText(FormatText("Font size: %02.02f", fontSize), 20, screenHeight - 50, 10, DARKGRAY); + DrawText(FormatText("Text size: [%02.02f, %02.02f]", textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY); + DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY); + + if (currentFontFilter == 0) DrawText("POINT", 570, 400, 20, BLACK); + else if (currentFontFilter == 1) DrawText("BILINEAR", 570, 400, 20, BLACK); + else if (currentFontFilter == 2) DrawText("TRILINEAR", 570, 400, 20, BLACK); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadSpriteFont(font); // SpriteFont unloading + + ClearDroppedFiles(); // Clear internal buffers + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/docs/examples/src/text/text_writing_anim.c b/docs/examples/src/text/text_writing_anim.c new file mode 100644 index 00000000..5563b561 --- /dev/null +++ b/docs/examples/src/text/text_writing_anim.c @@ -0,0 +1,62 @@ +/******************************************************************************************* +* +* raylib [text] example - Text Writing Animation +* +* 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 - text writing anim"); + + const char message[128] = "This sample illustrates a text writing\nanimation effect! Check it out! ;)"; + + int framesCounter = 0; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyDown(KEY_SPACE)) framesCounter += 8; + else framesCounter++; + + if (IsKeyPressed(KEY_ENTER)) framesCounter = 0; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText(SubText(message, 0, framesCounter/10), 210, 160, 20, MAROON); + + DrawText("PRESS [ENTER] to RESTART!", 240, 260, 20, LIGHTGRAY); + DrawText("PRESS [SPACE] to SPEED UP!", 239, 300, 20, LIGHTGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file |
