aboutsummaryrefslogtreecommitdiff
path: root/docs/examples/src/text
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2017-04-17 16:42:01 +0200
committerraysan5 <raysan5@gmail.com>2017-04-17 16:42:01 +0200
commit881f134f4d2fb4419d50382284e19b4f8ca4660e (patch)
tree065658f8b462dd76837f849450bdd3895134121a /docs/examples/src/text
parent3e082f1d6251e366d7be6019d0950ea7a9e6b5b4 (diff)
downloadraylib-881f134f4d2fb4419d50382284e19b4f8ca4660e.tar.gz
raylib-881f134f4d2fb4419d50382284e19b4f8ca4660e.zip
Review and recompile web examples
Diffstat (limited to 'docs/examples/src/text')
-rw-r--r--docs/examples/src/text/text_bmfont_ttf.c12
-rw-r--r--docs/examples/src/text/text_bmfont_unordered.c8
-rw-r--r--docs/examples/src/text/text_input_box.c116
-rw-r--r--docs/examples/src/text/text_raylib_fonts.c (renamed from docs/examples/src/text/text_rbmf_fonts.c)58
-rw-r--r--docs/examples/src/text/text_sprite_fonts.c24
-rw-r--r--docs/examples/src/text/text_ttf_loading.c4
6 files changed, 172 insertions, 50 deletions
diff --git a/docs/examples/src/text/text_bmfont_ttf.c b/docs/examples/src/text/text_bmfont_ttf.c
index caece548..0778fd11 100644
--- a/docs/examples/src/text/text_bmfont_ttf.c
+++ b/docs/examples/src/text/text_bmfont_ttf.c
@@ -24,13 +24,13 @@ int main()
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
+ SpriteFont fontBm = LoadSpriteFont("resources/bmfont.fnt"); // BMFont (AngelCode)
+ SpriteFont fontTtf = LoadSpriteFont("resources/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;
+ fontPosition.x = screenWidth/2 - MeasureTextEx(fontBm, msgBm, fontBm.baseSize, 0).x/2;
+ fontPosition.y = screenHeight/2 - fontBm.baseSize/2 - 80;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@@ -49,8 +49,8 @@ int main()
ClearBackground(RAYWHITE);
- DrawTextEx(fontBm, msgBm, fontPosition, fontBm.size, 0, MAROON);
- DrawTextEx(fontTtf, msgTtf, (Vector2){ 75.0f, 240.0f }, fontTtf.size*0.8f, 2, LIME);
+ DrawTextEx(fontBm, msgBm, fontPosition, fontBm.baseSize, 0, MAROON);
+ DrawTextEx(fontTtf, msgTtf, (Vector2){ 75.0f, 240.0f }, fontTtf.baseSize*0.8f, 2, LIME);
EndDrawing();
//----------------------------------------------------------------------------------
diff --git a/docs/examples/src/text/text_bmfont_unordered.c b/docs/examples/src/text/text_bmfont_unordered.c
index b29c5f8b..01561bec 100644
--- a/docs/examples/src/text/text_bmfont_unordered.c
+++ b/docs/examples/src/text/text_bmfont_unordered.c
@@ -25,7 +25,7 @@ int main()
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)
+ SpriteFont font = LoadSpriteFont("resources/pixantiqua.fnt"); // BMFont (AngelCode)
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@@ -45,10 +45,10 @@ int main()
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);
+ 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.size, 0, MAROON);
+ DrawTextEx(font, msg, (Vector2){ 40, 180 }, font.baseSize, 0, MAROON);
EndDrawing();
//----------------------------------------------------------------------------------
diff --git a/docs/examples/src/text/text_input_box.c b/docs/examples/src/text/text_input_box.c
new file mode 100644
index 00000000..54eebf40
--- /dev/null
+++ b/docs/examples/src/text/text_input_box.c
@@ -0,0 +1,116 @@
+/*******************************************************************************************
+*
+* raylib [text] example - Input Box
+*
+* This example has been created using raylib 1.7 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2017 Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+
+#define MAX_INPUT_CHARS 9
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 800;
+ int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [text] example - input box");
+
+ char name[MAX_INPUT_CHARS + 1] = "\0"; // NOTE: One extra space required for line ending char '\0'
+ int letterCount = 0;
+
+ Rectangle textBox = { screenWidth/2 - 100, 180, 225, 50 };
+ bool mouseOnText = false;
+
+ int framesCounter = 0;
+
+ SetTargetFPS(60);
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ if (CheckCollisionPointRec(GetMousePosition(), textBox)) mouseOnText = true;
+ else mouseOnText = false;
+
+ if (mouseOnText)
+ {
+ int key = GetKeyPressed();
+
+ // NOTE: Only allow keys in range [32..125]
+ if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS))
+ {
+ name[letterCount] = (char)key;
+ letterCount++;
+ }
+
+ if (key == KEY_BACKSPACE)
+ {
+ letterCount--;
+ name[letterCount] = '\0';
+
+ if (letterCount < 0) letterCount = 0;
+ }
+ }
+
+ if (mouseOnText) framesCounter++;
+ else framesCounter = 0;
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ DrawText("PLACE MOUSE OVER INPUT BOX!", 240, 140, 20, GRAY);
+
+ DrawRectangleRec(textBox, LIGHTGRAY);
+ if (mouseOnText) DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, RED);
+ else DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, DARKGRAY);
+
+ DrawText(name, textBox.x + 5, textBox.y + 8, 40, MAROON);
+
+ DrawText(FormatText("INPUT CHARS: %i/%i", letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY);
+
+ if (mouseOnText)
+ {
+ if (letterCount < MAX_INPUT_CHARS)
+ {
+ // Draw blinking underscore char
+ if (((framesCounter/20)%2) == 0) DrawText("_", textBox.x + 8 + MeasureText(name, 40), textBox.y + 12, 40, MAROON);
+ }
+ else DrawText("Press BACKSPACE to delete chars...", 230, 300, 20, GRAY);
+ }
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
+// Check if any key is pressed
+// NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126
+bool IsAnyKeyPressed()
+{
+ bool keyPressed = false;
+ int key = GetKeyPressed();
+
+ if ((key >= 32) && (key <= 126)) keyPressed = true;
+
+ return keyPressed;
+} \ No newline at end of file
diff --git a/docs/examples/src/text/text_rbmf_fonts.c b/docs/examples/src/text/text_raylib_fonts.c
index b4bd851b..6d8ef2b6 100644
--- a/docs/examples/src/text/text_rbmf_fonts.c
+++ b/docs/examples/src/text/text_raylib_fonts.c
@@ -1,19 +1,21 @@
/*******************************************************************************************
*
-* raylib [text] example - raylib bitmap font (rbmf) loading and usage
+* raylib [text] example - raylib font 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)
+* This example has been created using raylib 1.7 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
-* Copyright (c) 2015 Ramon Santamaria (@raysan5)
+* Copyright (c) 2017 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
+#define MAX_FONTS 8
+
int main()
{
// Initialization
@@ -21,21 +23,21 @@ int main()
int screenWidth = 800;
int screenHeight = 450;
- InitWindow(screenWidth, screenHeight, "raylib [text] example - rBMF fonts");
+ InitWindow(screenWidth, screenHeight, "raylib [text] example - raylib fonts");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
- SpriteFont fonts[8];
+ SpriteFont fonts[MAX_FONTS];
- 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
+ fonts[0] = LoadSpriteFont("resources/fonts/alagard.png");
+ fonts[1] = LoadSpriteFont("resources/fonts/pixelplay.png");
+ fonts[2] = LoadSpriteFont("resources/fonts/mecha.png");
+ fonts[3] = LoadSpriteFont("resources/fonts/setback.png");
+ fonts[4] = LoadSpriteFont("resources/fonts/romulus.png");
+ fonts[5] = LoadSpriteFont("resources/fonts/pixantiqua.png");
+ fonts[6] = LoadSpriteFont("resources/fonts/alpha_beta.png");
+ fonts[7] = LoadSpriteFont("resources/fonts/jupiter_crash.png");
- const char *messages[8] = { "ALAGARD FONT designed by Hewett Tsoi",
+ const char *messages[MAX_FONTS] = { "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)",
@@ -44,17 +46,22 @@ int main()
"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 };
+ const int spacings[MAX_FONTS] = { 2, 4, 8, 4, 3, 4, 4, 1 };
- Vector2 positions[8];
+ Vector2 positions[MAX_FONTS];
- for (int i = 0; i < 8; i++)
+ for (int i = 0; i < MAX_FONTS; 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;
+ positions[i].x = screenWidth/2 - MeasureTextEx(fonts[i], messages[i], fonts[i].baseSize*2, spacings[i]).x/2;
+ positions[i].y = 60 + fonts[i].baseSize + 45*i;
}
- Color colors[8] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD };
+ // Small Y position corrections
+ positions[3].y += 8;
+ positions[4].y += 2;
+ positions[7].y -= 8;
+
+ Color colors[MAX_FONTS] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };
//--------------------------------------------------------------------------------------
// Main game loop
@@ -74,9 +81,9 @@ int main()
DrawText("free fonts included with raylib", 250, 20, 20, DARKGRAY);
DrawLine(220, 50, 590, 50, DARKGRAY);
- for (int i = 0; i < 8; i++)
+ for (int i = 0; i < MAX_FONTS; i++)
{
- DrawTextEx(fonts[i], messages[i], positions[i], fonts[i].size*2, spacings[i], colors[i]);
+ DrawTextEx(fonts[i], messages[i], positions[i], fonts[i].baseSize*2, spacings[i], colors[i]);
}
EndDrawing();
@@ -85,10 +92,9 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
- for (int i = 0; i < 8; i++)
- {
- UnloadSpriteFont(fonts[i]); // SpriteFont unloading
- }
+
+ // SpriteFonts unloading
+ for (int i = 0; i < MAX_FONTS; i++) UnloadSpriteFont(fonts[i]);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
diff --git a/docs/examples/src/text/text_sprite_fonts.c b/docs/examples/src/text/text_sprite_fonts.c
index c73eda85..aefbfd1f 100644
--- a/docs/examples/src/text/text_sprite_fonts.c
+++ b/docs/examples/src/text/text_sprite_fonts.c
@@ -25,20 +25,20 @@ int main()
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
+ SpriteFont font1 = LoadSpriteFont("resources/custom_mecha.png"); // SpriteFont loading
+ SpriteFont font2 = LoadSpriteFont("resources/custom_alagard.png"); // SpriteFont loading
+ SpriteFont font3 = LoadSpriteFont("resources/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;
+ fontPosition1.x = screenWidth/2 - MeasureTextEx(font1, msg1, font1.baseSize, -3).x/2;
+ fontPosition1.y = screenHeight/2 - font1.baseSize/2 - 80;
- fontPosition2.x = screenWidth/2 - MeasureTextEx(font2, msg2, font2.size, -2).x/2;
- fontPosition2.y = screenHeight/2 - font2.size/2 - 10;
+ fontPosition2.x = screenWidth/2 - MeasureTextEx(font2, msg2, font2.baseSize, -2).x/2;
+ fontPosition2.y = screenHeight/2 - font2.baseSize/2 - 10;
- fontPosition3.x = screenWidth/2 - MeasureTextEx(font3, msg3, font3.size, 2).x/2;
- fontPosition3.y = screenHeight/2 - font3.size/2 + 50;
+ fontPosition3.x = screenWidth/2 - MeasureTextEx(font3, msg3, font3.baseSize, 2).x/2;
+ fontPosition3.y = screenHeight/2 - font3.baseSize/2 + 50;
//--------------------------------------------------------------------------------------
@@ -56,9 +56,9 @@ int main()
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);
+ DrawTextEx(font1, msg1, fontPosition1, font1.baseSize, -3, WHITE);
+ DrawTextEx(font2, msg2, fontPosition2, font2.baseSize, -2, WHITE);
+ DrawTextEx(font3, msg3, fontPosition3, font3.baseSize, 2, WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
diff --git a/docs/examples/src/text/text_ttf_loading.c b/docs/examples/src/text/text_ttf_loading.c
index 33cda7f5..4e490399 100644
--- a/docs/examples/src/text/text_ttf_loading.c
+++ b/docs/examples/src/text/text_ttf_loading.c
@@ -25,13 +25,13 @@ int main()
// 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);
+ SpriteFont font = LoadSpriteFontTTF("resources/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;
+ float fontSize = font.baseSize;
Vector2 fontPosition = { 40, screenHeight/2 + 50 };
Vector2 textSize;