From 0603e59cae0da924d7fe811b078972e04cdfe1e9 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 18 Nov 2016 14:05:49 +0100 Subject: Review examples and added new ones --- examples/text_ttf_loading.c | 125 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 examples/text_ttf_loading.c (limited to 'examples/text_ttf_loading.c') diff --git a/examples/text_ttf_loading.c b/examples/text_ttf_loading.c new file mode 100644 index 00000000..1135619e --- /dev/null +++ b/examples/text_ttf_loading.c @@ -0,0 +1,125 @@ +/******************************************************************************************* +* +* 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 (Ray San - raysan@raysanweb.com) +* +********************************************************************************************/ + +#include "raylib.h" +#include + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [text] example - ttf loading"); + + const char msg1[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); + + float fontSize = font.size; + Vector2 fontPosition = { 40, screenHeight/2 + 50 }; + Vector2 textSize; + + 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 not supported in font because there are not mipmap levels + SetTextureFilter(font.texture, FILTER_TRILINEAR); + //currentFontFilter = 2; + } + + textSize = MeasureTextEx(font, msg1, 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, msg1, 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); + + 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 -- cgit v1.2.3 From bdbb1eb90133c882ee84421b1b024b286a76d5ce Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 21 Nov 2016 19:49:54 +0100 Subject: Added new text sample: text_bmfont_unordered BMFont loading has been improved to support unordered chars and extended characters (up to 255) --- examples/Makefile | 5 + examples/resources/fonts/pixantiqua.fnt | 188 ++++++++++++++++++++++++++++++ examples/resources/fonts/pixantiqua_0.png | Bin 0 -> 4531 bytes examples/text_bmfont_unordered.c | 65 +++++++++++ examples/text_bmfont_unordered.png | Bin 0 -> 18713 bytes examples/text_ttf_loading.c | 1 - 6 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 examples/resources/fonts/pixantiqua.fnt create mode 100644 examples/resources/fonts/pixantiqua_0.png create mode 100644 examples/text_bmfont_unordered.c create mode 100644 examples/text_bmfont_unordered.png (limited to 'examples/text_ttf_loading.c') diff --git a/examples/Makefile b/examples/Makefile index da29e915..2cb75ff9 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -229,6 +229,7 @@ EXAMPLES = \ text_font_select \ text_writing_anim \ text_ttf_loading \ + text_bmfont_unordered \ models_geometric_shapes \ models_box_collisions \ models_billboard \ @@ -418,6 +419,10 @@ text_writing_anim: text_writing_anim.c text_ttf_loading: text_ttf_loading.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +# compile [text] example - text bmfont unordered +text_bmfont_unordered: text_bmfont_unordered.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + # compile [models] example - basic geometric 3d shapes models_geometric_shapes: models_geometric_shapes.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) diff --git a/examples/resources/fonts/pixantiqua.fnt b/examples/resources/fonts/pixantiqua.fnt new file mode 100644 index 00000000..971b9b0b --- /dev/null +++ b/examples/resources/fonts/pixantiqua.fnt @@ -0,0 +1,188 @@ +info face="PixAntiqua" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=2,2,2,2 spacing=2,2 outline=0 +common lineHeight=32 base=27 scaleW=512 scaleH=512 pages=1 packed=0 alphaChnl=0 redChnl=4 greenChnl=4 blueChnl=4 +page id=0 file="pixantiqua_0.png" +chars count=184 +char id=32 x=9 y=304 width=7 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=33 x=391 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=34 x=240 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=35 x=468 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=36 x=152 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=37 x=176 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=38 x=303 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=39 x=495 y=266 width=8 height=36 xoffset=-3 yoffset=-2 xadvance=5 page=0 chnl=15 +char id=40 x=256 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=199 x=432 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=200 x=126 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=201 x=147 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=202 x=288 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=203 x=189 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=204 x=468 y=228 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=205 x=486 y=228 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=206 x=0 y=266 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=207 x=72 y=266 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=208 x=329 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=209 x=277 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=210 x=182 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=211 x=26 y=76 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=41 x=272 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=42 x=288 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=43 x=414 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=44 x=378 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=45 x=414 y=228 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=46 x=443 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=47 x=392 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=48 x=485 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=49 x=450 y=228 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=50 x=21 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=51 x=42 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=59 x=456 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=60 x=168 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=61 x=309 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=62 x=336 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=63 x=315 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=64 x=364 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=65 x=390 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=66 x=120 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=67 x=144 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=68 x=168 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=69 x=294 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=52 x=488 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=53 x=63 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=54 x=24 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=55 x=48 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=56 x=72 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=57 x=96 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=58 x=404 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=70 x=252 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=71 x=192 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=72 x=78 y=76 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=78 x=78 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=79 x=355 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=80 x=264 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=81 x=381 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=82 x=288 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=83 x=312 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=91 x=144 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=92 x=108 y=266 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=93 x=304 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=94 x=34 y=0 width=32 height=36 xoffset=-3 yoffset=-2 xadvance=29 page=0 chnl=15 +char id=95 x=231 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=96 x=442 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=97 x=408 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=98 x=432 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=99 x=210 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=84 x=336 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=85 x=360 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=86 x=0 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=87 x=68 y=0 width=30 height=36 xoffset=-3 yoffset=-2 xadvance=27 page=0 chnl=15 +char id=88 x=26 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=89 x=384 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=90 x=84 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=100 x=456 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=101 x=480 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=102 x=54 y=266 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=103 x=0 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=104 x=24 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=105 x=469 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=106 x=18 y=266 width=16 height=36 xoffset=-8 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=107 x=48 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=108 x=417 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=109 x=161 y=0 width=27 height=36 xoffset=-3 yoffset=-2 xadvance=24 page=0 chnl=15 +char id=110 x=72 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=111 x=96 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=117 x=192 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=118 x=216 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=119 x=248 y=0 width=27 height=36 xoffset=-3 yoffset=-2 xadvance=24 page=0 chnl=15 +char id=120 x=240 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=121 x=264 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=122 x=288 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=123 x=432 y=228 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=124 x=365 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=125 x=378 y=228 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=126 x=393 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=127 x=132 y=0 width=27 height=36 xoffset=-3 yoffset=-2 xadvance=24 page=0 chnl=15 +char id=160 x=0 y=304 width=7 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=161 x=352 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=162 x=351 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=163 x=336 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=165 x=360 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=167 x=384 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=169 x=433 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=170 x=224 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=171 x=105 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=172 x=0 y=0 width=32 height=36 xoffset=-3 yoffset=-2 xadvance=29 page=0 chnl=15 +char id=173 x=494 y=38 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=174 x=52 y=76 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=175 x=52 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=176 x=126 y=266 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=177 x=435 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=178 x=320 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=179 x=336 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=181 x=459 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=112 x=120 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=113 x=144 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=114 x=396 y=228 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=115 x=168 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=116 x=36 y=266 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=182 x=408 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=183 x=498 y=190 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=185 x=192 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=186 x=208 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=187 x=477 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=191 x=456 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=192 x=407 y=0 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=193 x=234 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=194 x=416 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=195 x=156 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=196 x=130 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=197 x=104 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=198 x=190 y=0 width=27 height=36 xoffset=-3 yoffset=-2 xadvance=24 page=0 chnl=15 +char id=212 x=0 y=76 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=213 x=338 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=214 x=312 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=215 x=357 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=216 x=286 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=217 x=456 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=218 x=480 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=219 x=0 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=220 x=24 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=221 x=48 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=222 x=260 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=223 x=72 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=224 x=96 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=225 x=120 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=226 x=144 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=227 x=168 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=228 x=192 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=229 x=216 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=230 x=219 y=0 width=27 height=36 xoffset=-3 yoffset=-2 xadvance=24 page=0 chnl=15 +char id=231 x=372 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=73 x=90 y=266 width=16 height=36 xoffset=-3 yoffset=-2 xadvance=13 page=0 chnl=15 +char id=74 x=216 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=75 x=240 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=76 x=273 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=77 x=100 y=0 width=30 height=36 xoffset=-3 yoffset=-2 xadvance=27 page=0 chnl=15 +char id=232 x=312 y=152 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=233 x=240 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=234 x=264 y=190 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=235 x=104 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=236 x=430 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=237 x=482 y=266 width=11 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=238 x=160 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=11 page=0 chnl=15 +char id=239 x=176 y=266 width=14 height=36 xoffset=-3 yoffset=-2 xadvance=8 page=0 chnl=15 +char id=240 x=128 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=241 x=200 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=242 x=224 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=243 x=248 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=244 x=272 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=245 x=296 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=246 x=320 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=247 x=330 y=190 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=248 x=208 y=38 width=24 height=36 xoffset=-3 yoffset=-2 xadvance=21 page=0 chnl=15 +char id=249 x=344 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=250 x=368 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=251 x=416 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=252 x=440 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=253 x=464 y=76 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 +char id=254 x=0 y=228 width=19 height=36 xoffset=-3 yoffset=-2 xadvance=16 page=0 chnl=15 +char id=255 x=0 y=114 width=22 height=36 xoffset=-3 yoffset=-2 xadvance=19 page=0 chnl=15 diff --git a/examples/resources/fonts/pixantiqua_0.png b/examples/resources/fonts/pixantiqua_0.png new file mode 100644 index 00000000..2aa2870f Binary files /dev/null and b/examples/resources/fonts/pixantiqua_0.png differ diff --git a/examples/text_bmfont_unordered.c b/examples/text_bmfont_unordered.c new file mode 100644 index 00000000..b29c5f8b --- /dev/null +++ b/examples/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/examples/text_bmfont_unordered.png b/examples/text_bmfont_unordered.png new file mode 100644 index 00000000..c6767567 Binary files /dev/null and b/examples/text_bmfont_unordered.png differ diff --git a/examples/text_ttf_loading.c b/examples/text_ttf_loading.c index 1135619e..b614023f 100644 --- a/examples/text_ttf_loading.c +++ b/examples/text_ttf_loading.c @@ -10,7 +10,6 @@ ********************************************************************************************/ #include "raylib.h" -#include int main() { -- cgit v1.2.3 From b8481369f7209804d70220a29ba2efbd6171d7a9 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 22 Nov 2016 12:15:58 +0100 Subject: Reviewed some lua examples and added new ones --- examples/audio_module_playing.lua | 31 ++----- examples/core_3d_camera_first_person.lua | 11 ++- examples/core_3d_camera_free.lua | 11 +-- examples/core_3d_picking.lua | 8 +- examples/core_input_gamepad.lua | 134 +++++++++++++++++++++++++++---- examples/core_world_screen.lua | 7 +- examples/models_billboard.lua | 9 +-- examples/models_cubicmap.lua | 8 +- examples/models_heightmap.lua | 7 +- examples/rlua_execute_file.c | 4 +- examples/shaders_custom_uniform.lua | 14 ++-- examples/shaders_model_shader.c | 4 +- examples/shaders_model_shader.lua | 6 +- examples/shaders_postprocessing.lua | 10 +-- examples/shaders_standard_lighting.lua | 10 +-- examples/text_bmfont_unordered.lua | 57 +++++++++++++ examples/text_ttf_loading.c | 16 ++-- examples/text_ttf_loading.lua | 118 +++++++++++++++++++++++++++ src/rlua.h | 77 +++++++++++------- 19 files changed, 402 insertions(+), 140 deletions(-) create mode 100644 examples/text_bmfont_unordered.lua create mode 100644 examples/text_ttf_loading.lua (limited to 'examples/text_ttf_loading.c') diff --git a/examples/audio_module_playing.lua b/examples/audio_module_playing.lua index 3c5ad641..7c675424 100644 --- a/examples/audio_module_playing.lua +++ b/examples/audio_module_playing.lua @@ -37,13 +37,6 @@ for i = MAX_CIRCLES, 1, -1 do circles[i].color = colors[GetRandomValue(1, 14)] end --- Load postprocessing bloom shader -local shader = LoadShader("resources/shaders/glsl330/base.vs", - "resources/shaders/glsl330/bloom.fs") - --- Create a RenderTexture2D to be used for render to texture -local target = LoadRenderTexture(screenWidth, screenHeight) - local xm = LoadMusicStream("resources/audio/mini1111.xm") PlayMusicStream(xm) @@ -83,22 +76,11 @@ while not WindowShouldClose() do -- Detect window close button or ESC key --------------------------------------------------------------------------------------- BeginDrawing() - ClearBackground(BLACK) - - BeginTextureMode(target) -- Enable drawing to texture - - for i = MAX_CIRCLES, 1, -1 do - DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha)) - end - - EndTextureMode() -- End drawing to texture (now we have a texture available for next passes) + ClearBackground(RAYWHITE) - BeginShaderMode(shader) - - -- NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) - DrawTextureRec(target.texture, Rectangle(0, 0, target.texture.width, -target.texture.height), Vector2(0, 0), WHITE) - - EndShaderMode() + for i = MAX_CIRCLES, 1, -1 do + DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha)) + end -- Draw time bar DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY) @@ -111,10 +93,7 @@ end -- De-Initialization ------------------------------------------------------------------------------------------- -UnloadShader(shader) -- Unload shader -UnloadRenderTexture(target) -- Unload render texture - -UnloadMusicStream(xm) -- Unload music stream buffers from RAM +UnloadMusicStream(xm) -- Unload music stream buffers from RAM CloseAudioDevice() -- Close audio device (music streaming is automatically stopped) diff --git a/examples/core_3d_camera_first_person.lua b/examples/core_3d_camera_first_person.lua index 800c3c2a..22ccdc5c 100644 --- a/examples/core_3d_camera_first_person.lua +++ b/examples/core_3d_camera_first_person.lua @@ -19,7 +19,7 @@ local screenHeight = 450 InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person") -- Define the camera to look into our 3d world (position, target, up vector) -local camera = Camera(Vector3(0.0, 10.0, 10.0), Vector3(0.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), 60.0) +local camera = Camera(Vector3(4.0, 2.0, 4.0), Vector3(0.0, 1.8, 0.0), Vector3(0.0, 1.0, 0.0), 60.0) -- Generates some random columns local heights = {} @@ -34,17 +34,16 @@ end local playerPosition = Vector3(4.0, 2.0, 4.0) -- Define player position -SetCameraMode(CameraMode.FIRST_PERSON) -- Set a first person camera mode -SetCameraFovy(camera.fovy) -- Set internal camera field-of-view Y +SetCameraMode(camera, CameraMode.FIRST_PERSON) -- Set a first person camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- -- Main game loop -while not WindowShouldClose() do -- Detect window close button or ESC key +while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera, playerPosition = UpdateCameraPlayer(camera, playerPosition) -- Update camera and player position + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/core_3d_camera_free.lua b/examples/core_3d_camera_free.lua index 244aad6b..57fa7a12 100644 --- a/examples/core_3d_camera_free.lua +++ b/examples/core_3d_camera_free.lua @@ -18,26 +18,23 @@ InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free") -- Define the camera to look into our 3d world local camera = {} -camera.position = Vector3(0.0, 10.0, 10.0) -- Camera position +camera.position = Vector3(10.0, 10.0, 10.0) -- Camera position camera.target = Vector3(0.0, 0.0, 0.0) -- Camera looking at point camera.up = Vector3(0.0, 1.0, 0.0) -- Camera up vector (rotation towards target) camera.fovy = 45.0 -- Camera field-of-view Y local cubePosition = Vector3(0.0, 0.0, 0.0) -SetCameraMode(CameraMode.FREE) -- Set a free camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our camera position -SetCameraTarget(camera.target) -- Set internal camera target to match our camera target -SetCameraFovy(camera.fovy) -- Set internal camera field-of-view Y +SetCameraMode(camera, CameraMode.FREE) -- Set a free camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- -- Main game loop while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/core_3d_picking.lua b/examples/core_3d_picking.lua index 1adee67c..230f5756 100644 --- a/examples/core_3d_picking.lua +++ b/examples/core_3d_picking.lua @@ -30,18 +30,16 @@ local ray = Ray(Vector3(0, 0, 0), Vector3(0, 0, 0)) -- Picking line ray local collision = false -SetCameraMode(CameraMode.FREE) -- Set a free camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our camera position -SetCameraFovy(camera.fovy) -- Set internal camera field-of-view Y +SetCameraMode(camera, CameraMode.FREE) -- Set a free camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- -- Main game loop while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera if (IsMouseButtonPressed(MOUSE.LEFT_BUTTON)) then -- NOTE: This function is NOT WORKING properly! diff --git a/examples/core_input_gamepad.lua b/examples/core_input_gamepad.lua index 78d9b84e..ade3f00f 100644 --- a/examples/core_input_gamepad.lua +++ b/examples/core_input_gamepad.lua @@ -19,8 +19,8 @@ local screenHeight = 450 InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input") -local ballPosition = Vector2(screenWidth/2, screenHeight/2) -local gamepadMovement = Vector2(0, 0) +local texPs3Pad = LoadTexture("resources/ps3.png") +local texXboxPad = LoadTexture("resources/xbox.png") SetTargetFPS(60) -- Set target frames-per-second ------------------------------------------------------------------------------------------- @@ -29,18 +29,7 @@ SetTargetFPS(60) -- Set target frames-per-second while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - if (IsGamepadAvailable(GAMEPAD.PLAYER1)) then - gamepadMovement.x = GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LEFT_X) - gamepadMovement.y = GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LEFT_Y) - - ballPosition.x = ballPosition.x + gamepadMovement.x - ballPosition.y = ballPosition.y - gamepadMovement.y - - if (IsGamepadButtonPressed(GAMEPAD.PLAYER1, GAMEPAD.BUTTON_A)) then - ballPosition.x = screenWidth/2 - ballPosition.y = screenHeight/2 - end - end + -- ... --------------------------------------------------------------------------------------- -- Draw @@ -49,9 +38,117 @@ while not WindowShouldClose() do -- Detect window close button or ESC key ClearBackground(RAYWHITE) - DrawText("move the ball with gamepad", 10, 10, 20, DARKGRAY) + if (IsGamepadAvailable(GAMEPAD.PLAYER1)) then + DrawText(string.format("GP1: %s", GetGamepadName(GAMEPAD.PLAYER1)), 10, 10, 10, BLACK) + + if (IsGamepadName(GAMEPAD.PLAYER1, "Xbox 360 Controller")) then + DrawTexture(texXboxPad, 0, 0, DARKGRAY) + + -- Draw buttons: xbox home + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_HOME)) then DrawCircle(394, 89, 19, RED) end + + -- Draw buttons: basic + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_START)) then DrawCircle(436, 150, 9, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_SELECT)) then DrawCircle(352, 150, 9, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_X)) then DrawCircle(501, 151, 15, BLUE) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_A)) then DrawCircle(536, 187, 15, LIME) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_B)) then DrawCircle(572, 151, 15, MAROON) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_Y)) then DrawCircle(536, 115, 15, GOLD) end + + -- Draw buttons: d-pad + DrawRectangle(317, 202, 19, 71, BLACK) + DrawRectangle(293, 228, 69, 19, BLACK) + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_UP)) then DrawRectangle(317, 202, 19, 26, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_DOWN)) then DrawRectangle(317, 202 + 45, 19, 26, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_LEFT)) then DrawRectangle(292, 228, 25, 19, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_RIGHT)) then DrawRectangle(292 + 44, 228, 26, 19, RED) end + + -- Draw buttons: left-right back + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_LB)) then DrawCircle(259, 61, 20, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_RB)) then DrawCircle(536, 61, 20, RED) end + + -- Draw axis: left joystick + DrawCircle(259, 152, 39, BLACK) + DrawCircle(259, 152, 34, LIGHTGRAY) + DrawCircle(259 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LEFT_X)*20), + 152 - (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LEFT_Y)*20), 25, BLACK) + + -- Draw axis: right joystick + DrawCircle(461, 237, 38, BLACK) + DrawCircle(461, 237, 33, LIGHTGRAY) + DrawCircle(461 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_RIGHT_X)*20), + 237 - (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_RIGHT_Y)*20), 25, BLACK) - DrawCircleV(ballPosition, 50, MAROON) + -- Draw axis: left-right triggers + DrawRectangle(170, 30, 15, 70, GRAY) + DrawRectangle(604, 30, 15, 70, GRAY) + DrawRectangle(170, 30, 15, (((1.0 + GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LT))/2.0)*70), RED) + DrawRectangle(604, 30, 15, (((1.0 + GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_RT))/2.0)*70), RED) + + --DrawText(FormatText("Xbox axis LT: %02.02f", GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LT)), 10, 40, 10, BLACK) + --DrawText(FormatText("Xbox axis RT: %02.02f", GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_RT)), 10, 60, 10, BLACK) + elseif (IsGamepadName(GAMEPAD.PLAYER1, "PLAYSTATION(R)3 Controller")) then + DrawTexture(texPs3Pad, 0, 0, DARKGRAY) + + -- Draw buttons: ps + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_PS)) then DrawCircle(396, 222, 13, RED) end + + -- Draw buttons: basic + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_SELECT)) then DrawRectangle(328, 170, 32, 13, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_START)) then DrawTriangle((Vector2){ 436, 168 }, (Vector2){ 436, 185 }, (Vector2){ 464, 177 }, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_TRIANGLE)) then DrawCircle(557, 144, 13, LIME) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_CIRCLE)) then DrawCircle(586, 173, 13, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_CROSS)) then DrawCircle(557, 203, 13, VIOLET) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_SQUARE)) then DrawCircle(527, 173, 13, PINK) end + + -- Draw buttons: d-pad + DrawRectangle(225, 132, 24, 84, BLACK) + DrawRectangle(195, 161, 84, 25, BLACK) + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_UP)) then DrawRectangle(225, 132, 24, 29, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_DOWN)) then DrawRectangle(225, 132 + 54, 24, 30, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_LEFT)) then DrawRectangle(195, 161, 30, 25, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_RIGHT)) then DrawRectangle(195 + 54, 161, 30, 25, RED) end + + -- Draw buttons: left-right back buttons + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_L1)) then DrawCircle(239, 82, 20, RED) end + if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_R1)) then DrawCircle(557, 82, 20, RED) end + + -- Draw axis: left joystick + DrawCircle(319, 255, 35, BLACK) + DrawCircle(319, 255, 31, LIGHTGRAY) + DrawCircle(319 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_LEFT_X)*20), + 255 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_LEFT_Y)*20), 25, BLACK) + + -- Draw axis: right joystick + DrawCircle(475, 255, 35, BLACK) + DrawCircle(475, 255, 31, LIGHTGRAY) + DrawCircle(475 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_RIGHT_X)*20), + 255 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_RIGHT_Y)*20), 25, BLACK) + + -- Draw axis: left-right triggers + DrawRectangle(169, 48, 15, 70, GRAY) + DrawRectangle(611, 48, 15, 70, GRAY) + DrawRectangle(169, 48, 15, (((1.0 - GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_L2))/2.0)*70), RED) + DrawRectangle(611, 48, 15, (((1.0 - GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_R2))/2.0)*70), RED) + else + DrawText("- GENERIC GAMEPAD -", 280, 180, 20, GRAY) + + -- TODO: Draw generic gamepad + end + + DrawText(string.format("DETECTED AXIS [%i]:", GetGamepadAxisCount(GAMEPAD.PLAYER1)), 10, 50, 10, MAROON) + + for i = 1, GetGamepadAxisCount(GAMEPAD.PLAYER1) do -- Iterate along all the rectangles + DrawText(string.format("AXIS %i: %.02f", i, GetGamepadAxisMovement(GAMEPAD.PLAYER1, i)), 20, 70 + 20*i, 10, DARKGRAY) + end + + if (GetGamepadButtonPressed() ~= -1) then DrawText(string.format("DETECTED BUTTON: %i", GetGamepadButtonPressed()), 10, 430, 10, RED) + else DrawText("DETECTED BUTTON: NONE", 10, 430, 10, GRAY) end + else + DrawText("GP1: NOT DETECTED", 10, 10, 10, GRAY) + + DrawTexture(texXboxPad, 0, 0, LIGHTGRAY) + end EndDrawing() --------------------------------------------------------------------------------------- @@ -59,5 +156,8 @@ end -- De-Initialization ------------------------------------------------------------------------------------------- -CloseWindow() -- Close window and OpenGL context +UnloadTexture(texPs3Pad) -- Unload gamepad texture +UnloadTexture(texXboxPad) -- Unload gamepad texture + +CloseWindow() -- Close window and OpenGL context ------------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/examples/core_world_screen.lua b/examples/core_world_screen.lua index 51f2cdbf..48b617dd 100644 --- a/examples/core_world_screen.lua +++ b/examples/core_world_screen.lua @@ -23,10 +23,7 @@ local cubePosition = Vector3(0.0, 0.0, 0.0) local cubeScreenPosition = Vector2(0, 0) -SetCameraMode(CameraMode.FREE) -- Set a free camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our camera position -SetCameraTarget(camera.target) -- Set internal camera target to match our camera target -SetCameraFovy(camera.fovy) -- Set internal camera field-of-view Y +SetCameraMode(camera, CameraMode.FREE) -- Set a free camera mode SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ---------------------------------------------------------------------------------------- @@ -35,7 +32,7 @@ SetTargetFPS(60) -- Set our game to run at 60 frames-per- while not WindowShouldClose() do -- Detect window close button or ESC key -- Update ------------------------------------------------------------------------------------ - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera -- Calculate cube screen space position (with a little offset to be in top) cubeScreenPosition = GetWorldToScreen(Vector3(cubePosition.x, cubePosition.y + 2.5, cubePosition.z), camera) diff --git a/examples/models_billboard.lua b/examples/models_billboard.lua index 457198e6..9d81f6ce 100644 --- a/examples/models_billboard.lua +++ b/examples/models_billboard.lua @@ -22,19 +22,16 @@ local camera = Camera(Vector3(5.0, 4.0, 5.0), Vector3(0.0, 2.0, 0.0), Vector3(0. local bill = LoadTexture("resources/billboard.png") -- Our texture billboard local billPosition = Vector3(0.0, 2.0, 0.0) -- Position where draw billboard -SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our camera position -SetCameraTarget(camera.target) -- Set internal camera target to match our camera target -SetCameraFovy(camera.fovy) -- Set internal camera field-of-view Y +SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- -- Main game loop while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/models_cubicmap.lua b/examples/models_cubicmap.lua index bae3bac2..79faafc9 100644 --- a/examples/models_cubicmap.lua +++ b/examples/models_cubicmap.lua @@ -31,18 +31,16 @@ local mapPosition = Vector3(-16.0, 0.0, -8.0) -- Set model position UnloadImage(image) -- Unload cubesmap image from RAM, already uploaded to VRAM -SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our custom camera position -SetCameraFovy(camera.fovy) -- Set internal camera field-of-view Y +SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- -- Main game loop while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/models_heightmap.lua b/examples/models_heightmap.lua index 4240f8b7..efcbfb4b 100644 --- a/examples/models_heightmap.lua +++ b/examples/models_heightmap.lua @@ -27,17 +27,16 @@ local mapPosition = Vector3(-8.0, 0.0, -8.0) -- Set model position (d UnloadImage(image) -- Unload heightmap image from RAM, already uploaded to VRAM -SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our custom camera position +SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ---------------------------------------------------------------------------------------- -- Main game loop while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/rlua_execute_file.c b/examples/rlua_execute_file.c index f2d7114e..a91ce42f 100644 --- a/examples/rlua_execute_file.c +++ b/examples/rlua_execute_file.c @@ -66,6 +66,8 @@ int main() // ExecuteLuaFile("text_format_text.lua"); // OK! NOTE: Use lua string.format() instead of raylib FormatText() // ExecuteLuaFile("text_font_select.lua"); // OK! // ExecuteLuaFile("text_writing_anim.lua"); // OK! + // ExecuteLuaFile("text_ttf_loading.lua"); // ISSUE: Attempt to index a SpriteFont value (local 'font') + // ExecuteLuaFile("text_bmfont_unordered.lua"); // OK! // ExecuteLuaFile("models_geometric_shapes.lua"); // OK! // ExecuteLuaFile("models_box_collisions.lua"); // OK! // ExecuteLuaFile("models_billboard.lua"); // OK! @@ -81,7 +83,7 @@ int main() // ExecuteLuaFile("audio_music_stream.lua"); // OK! // ExecuteLuaFile("audio_module_playing.lua"); // OK! // ExecuteLuaFile("audio_raw_stream.lua"); // ERROR: UpdateAudioStream() - + // De-Initialization //-------------------------------------------------------------------------------------- CloseLuaDevice(); // Close Lua device and free resources diff --git a/examples/shaders_custom_uniform.lua b/examples/shaders_custom_uniform.lua index 3a8bbae5..dafd3b84 100644 --- a/examples/shaders_custom_uniform.lua +++ b/examples/shaders_custom_uniform.lua @@ -47,15 +47,13 @@ local swirlCenter = { screenWidth/2, screenHeight/2 } local target = LoadRenderTexture(screenWidth, screenHeight) -- Setup orbital camera -SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our camera position -SetCameraTarget(camera.target) -- Set internal camera target to match our camera target +SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- -- Main game loop -while not WindowShouldClose() do -- Detect window close button or ESC key +while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- local mousePosition = GetMousePosition() @@ -66,7 +64,7 @@ while not WindowShouldClose() do -- Detect window close button or ESC key -- Send new value to the shader to be used on drawing SetShaderValue(shader, swirlCenterLoc, swirlCenter) - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw @@ -75,13 +73,13 @@ while not WindowShouldClose() do -- Detect window close button or ESC key ClearBackground(RAYWHITE) - BeginTextureMode(target) -- Enable drawing to texture + BeginTextureMode(target) -- Enable drawing to texture Begin3dMode(camera) DrawModel(dwarf, position, 2.0, WHITE) -- Draw 3d model with texture - DrawGrid(10, 1.0) -- Draw a grid + DrawGrid(10, 1.0) -- Draw a grid End3dMode() diff --git a/examples/shaders_model_shader.c b/examples/shaders_model_shader.c index 26de4922..51e9c1b3 100644 --- a/examples/shaders_model_shader.c +++ b/examples/shaders_model_shader.c @@ -42,7 +42,7 @@ int main() Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode + SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -52,7 +52,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw diff --git a/examples/shaders_model_shader.lua b/examples/shaders_model_shader.lua index d1436a7e..38f0fd30 100644 --- a/examples/shaders_model_shader.lua +++ b/examples/shaders_model_shader.lua @@ -39,9 +39,7 @@ dwarf.material.texDiffuse = texture -- Bind texture to model local position = Vector3(0.0, 0.0, 0.0) -- Set model position -- Setup orbital camera -SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our camera position -SetCameraTarget(camera.target) -- Set internal camera target to match our camera target +SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- @@ -50,7 +48,7 @@ SetTargetFPS(60) -- Set our game to run at 60 frames-pe while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/shaders_postprocessing.lua b/examples/shaders_postprocessing.lua index f20f31ec..7dfac816 100644 --- a/examples/shaders_postprocessing.lua +++ b/examples/shaders_postprocessing.lua @@ -41,18 +41,16 @@ local shader = LoadShader("resources/shaders/glsl330/base.vs", local target = LoadRenderTexture(screenWidth, screenHeight) -- Setup orbital camera -SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our camera position -SetCameraTarget(camera.target) -- Set internal camera target to match our camera target +SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- -- Main game loop -while not WindowShouldClose() do -- Detect window close button or ESC key +while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/shaders_standard_lighting.lua b/examples/shaders_standard_lighting.lua index 2f3700ff..1d4dcfcf 100644 --- a/examples/shaders_standard_lighting.lua +++ b/examples/shaders_standard_lighting.lua @@ -60,18 +60,16 @@ pointLight.diffuse = Color(100, 100, 255, 255) pointLight.radius = 3.0 -- Setup orbital camera -SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode -SetCameraPosition(camera.position) -- Set internal camera position to match our camera position -SetCameraTarget(camera.target) -- Set internal camera target to match our camera target +SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode -SetTargetFPS(60) -- Set our game to run at 60 frames-per-second +SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ------------------------------------------------------------------------------------------- -- Main game loop -while not WindowShouldClose() do -- Detect window close button or ESC key +while not WindowShouldClose() do -- Detect window close button or ESC key -- Update --------------------------------------------------------------------------------------- - camera = UpdateCamera(camera) -- Update internal camera and our camera + camera = UpdateCamera(camera) -- Update camera --------------------------------------------------------------------------------------- -- Draw diff --git a/examples/text_bmfont_unordered.lua b/examples/text_bmfont_unordered.lua new file mode 100644 index 00000000..f324ca19 --- /dev/null +++ b/examples/text_bmfont_unordered.lua @@ -0,0 +1,57 @@ +------------------------------------------------------------------------------------------- +-- +-- raylib [text] example - BMFont unordered chars loading and drawing +-- +-- This example has been created using raylib 1.6 (www.raylib.com) +-- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +-- +-- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5) +-- +------------------------------------------------------------------------------------------- + +-- Initialization +------------------------------------------------------------------------------------------- +local screenWidth = 800 +local 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 +local msg = "ASCII extended characters:\n¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆ\nÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæ\nçèéêëìíîïðñòóôõö÷øùúûüýþÿ" + +-- NOTE: Loaded font has an unordered list of characters (chars in the range 32..255) +local font = LoadSpriteFont("resources/fonts/pixantiqua.fnt") -- BMFont (AngelCode) + +SetTargetFPS(60) +------------------------------------------------------------------------------------------- + +-- Main game loop +while not WindowShouldClose() do -- 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(string.format("Font base size: %i", font.size), 40, 80, 20, GRAY) + DrawText(string.format("Font chars number: %i", font.numChars), 40, 110, 20, GRAY) + + DrawTextEx(font, msg, Vector2(40, 180), font.size, 0, MAROON) + + EndDrawing() + --------------------------------------------------------------------------------------- +end + +-- De-Initialization +------------------------------------------------------------------------------------------- +UnloadSpriteFont(font) -- AngelCode SpriteFont unloading + +CloseWindow() -- Close window and OpenGL context +------------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/examples/text_ttf_loading.c b/examples/text_ttf_loading.c index b614023f..918209dd 100644 --- a/examples/text_ttf_loading.c +++ b/examples/text_ttf_loading.c @@ -20,17 +20,22 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [text] example - ttf loading"); - const char msg1[50] = "TTF SpriteFont"; + 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; @@ -59,12 +64,12 @@ int main() } else if (IsKeyPressed(KEY_THREE)) { - // NOTE: Trilinear filter not supported in font because there are not mipmap levels + // NOTE: Trilinear filter won't be noticed on 2D drawing SetTextureFilter(font.texture, FILTER_TRILINEAR); - //currentFontFilter = 2; + currentFontFilter = 2; } - textSize = MeasureTextEx(font, msg1, fontSize, 0); + textSize = MeasureTextEx(font, msg, fontSize, 0); if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10; else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10; @@ -94,7 +99,7 @@ int main() 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, msg1, fontPosition, fontSize, 0, BLACK); + 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); @@ -106,6 +111,7 @@ int main() 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(); //---------------------------------------------------------------------------------- diff --git a/examples/text_ttf_loading.lua b/examples/text_ttf_loading.lua new file mode 100644 index 00000000..26443212 --- /dev/null +++ b/examples/text_ttf_loading.lua @@ -0,0 +1,118 @@ +------------------------------------------------------------------------------------------- +-- +-- raylib [text] example - TTF loading and usage +-- +-- This example has been created using raylib 1.6 (www.raylib.com) +-- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +-- +-- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5) +-- +------------------------------------------------------------------------------------------- + +-- Initialization +------------------------------------------------------------------------------------------- +local screenWidth = 800; +local screenHeight = 450; + +InitWindow(screenWidth, screenHeight, "raylib [text] example - ttf loading") + +local msg = "TTF SpriteFont" + +-- NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) + +-- TTF SpriteFont loading with custom generation parameters +local 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 +--font.texture = GenTextureMipmaps(font.texture) -- ISSUE: attempt to index a SpriteFont value (local 'font') + +local fontSize = font.size +local fontPosition = Vector2(40, screenHeight/2 + 50) +local textSize + +SetTextureFilter(font.texture, TextureFilter.POINT) +local currentFontFilter = 0 -- Default: FILTER_POINT + +local count = 0 +local droppedFiles + +SetTargetFPS(60) +------------------------------------------------------------------------------------------- + +-- Main game loop +while not WindowShouldClose() do -- Detect window close button or ESC key + -- Update + --------------------------------------------------------------------------------------- + fontSize = fontSize + GetMouseWheelMove()*4.0 + + -- Choose font texture filter method + if (IsKeyPressed(KEY.ONE)) then + SetTextureFilter(font.texture, TextureFilter.POINT) + currentFontFilter = 0 + elseif (IsKeyPressed(KEY.TWO)) then + SetTextureFilter(font.texture, TextureFilter.BILINEAR) + currentFontFilter = 1 + elseif (IsKeyPressed(KEY.THREE)) then + -- NOTE: Trilinear filter won't be noticed on 2D drawing + SetTextureFilter(font.texture, TextureFilter.TRILINEAR) + currentFontFilter = 2 + end + + textSize = MeasureTextEx(font, msg, fontSize, 0) + + if (IsKeyDown(KEY.LEFT)) then fontPosition.x = fontPosition.x - 10 + elseif (IsKeyDown(KEY.RIGHT)) then fontPosition.x = fontPosition.x + 10 + end + + -- Load a dropped TTF file dynamically (at current fontSize) + if (IsFileDropped()) then + droppedFiles = GetDroppedFiles() + count = #droppedFiles + + if (count == 1) then -- Only support one ttf file dropped + UnloadSpriteFont(font) + font = LoadSpriteFontTTF(droppedFiles[1], fontSize, 0, 0) + ClearDroppedFiles() + end + end + --------------------------------------------------------------------------------------- + + -- 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(string.format("Font size: %02.02f", fontSize), 20, screenHeight - 50, 10, DARKGRAY) + DrawText(string.format("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) then DrawText("POINT", 570, 400, 20, BLACK) + elseif (currentFontFilter == 1) then DrawText("BILINEAR", 570, 400, 20, BLACK) + elseif (currentFontFilter == 2) then DrawText("TRILINEAR", 570, 400, 20, BLACK) + end + + EndDrawing() + --------------------------------------------------------------------------------------- +end + +-- De-Initialization +------------------------------------------------------------------------------------------- +UnloadSpriteFont(font) -- SpriteFont unloading + +ClearDroppedFiles() -- Clear internal buffers + +CloseWindow() -- Close window and OpenGL context +------------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/src/rlua.h b/src/rlua.h index c801a328..961ed1c1 100644 --- a/src/rlua.h +++ b/src/rlua.h @@ -2105,8 +2105,9 @@ int lua_ImageColorBrightness(lua_State* L) int lua_GenTextureMipmaps(lua_State* L) { Texture2D arg1 = LuaGetArgument_Texture2D(L, 1); - GenTextureMipmaps(arg1); - return 0; + GenTextureMipmaps(&arg1); + LuaPush_Texture2D(L, arg1); + return 1; } int lua_SetTextureFilter(lua_State* L) @@ -4096,24 +4097,36 @@ RLUADEF void InitLuaDevice(void) LuaSetEnum("RIGHT_BUTTON", 1); LuaSetEnum("MIDDLE_BUTTON", 2); LuaEndEnum("MOUSE"); - + LuaStartEnum(); LuaSetEnum("PLAYER1", 0); LuaSetEnum("PLAYER2", 1); LuaSetEnum("PLAYER3", 2); LuaSetEnum("PLAYER4", 3); - LuaSetEnum("PS3_BUTTON_A", 2); - LuaSetEnum("PS3_BUTTON_B", 1); - LuaSetEnum("PS3_BUTTON_X", 3); - LuaSetEnum("PS3_BUTTON_Y", 4); - LuaSetEnum("PS3_BUTTON_R1", 7); - LuaSetEnum("PS3_BUTTON_R2", 5); + LuaSetEnum("PS3_BUTTON_TRIANGLE", 0); + LuaSetEnum("PS3_BUTTON_CIRCLE", 1); + LuaSetEnum("PS3_BUTTON_CROSS", 2); + LuaSetEnum("PS3_BUTTON_SQUARE", 3); LuaSetEnum("PS3_BUTTON_L1", 6); - LuaSetEnum("PS3_BUTTON_L2", 8); + LuaSetEnum("PS3_BUTTON_R1", 7); + LuaSetEnum("PS3_BUTTON_L2", 4); + LuaSetEnum("PS3_BUTTON_R2", 5); + LuaSetEnum("PS3_BUTTON_START", 8); LuaSetEnum("PS3_BUTTON_SELECT", 9); - LuaSetEnum("PS3_BUTTON_START", 10); - + LuaSetEnum("PS3_BUTTON_UP", 24); + LuaSetEnum("PS3_BUTTON_RIGHT", 25); + LuaSetEnum("PS3_BUTTON_DOWN", 26); + LuaSetEnum("PS3_BUTTON_LEFT", 27); + LuaSetEnum("PS3_BUTTON_PS", 12); + LuaSetEnum("PS3_AXIS_LEFT_X", 0); + LuaSetEnum("PS3_AXIS_LEFT_Y", 1); + LuaSetEnum("PS3_AXIS_RIGHT_X", 2); + LuaSetEnum("PS3_AXIS_RIGHT_Y", 5); + LuaSetEnum("PS3_AXIS_L2", 3); // [1..-1] (pressure-level) + LuaSetEnum("PS3_AXIS_R2", 4); // [1..-1] (pressure-level) + +// Xbox360 USB Controller Buttons LuaSetEnum("XBOX_BUTTON_A", 0); LuaSetEnum("XBOX_BUTTON_B", 1); LuaSetEnum("XBOX_BUTTON_X", 2); @@ -4122,25 +4135,26 @@ RLUADEF void InitLuaDevice(void) LuaSetEnum("XBOX_BUTTON_RB", 5); LuaSetEnum("XBOX_BUTTON_SELECT", 6); LuaSetEnum("XBOX_BUTTON_START", 7); - -#if defined(PLATFORM_RPI) - LuaSetEnum("XBOX_AXIS_DPAD_X", 7); - LuaSetEnum("XBOX_AXIS_DPAD_Y", 6); - LuaSetEnum("XBOX_AXIS_RIGHT_X", 3); - LuaSetEnum("XBOX_AXIS_RIGHT_Y", 4); - LuaSetEnum("XBOX_AXIS_LT", 2); - LuaSetEnum("XBOX_AXIS_RT", 5); -#else LuaSetEnum("XBOX_BUTTON_UP", 10); + LuaSetEnum("XBOX_BUTTON_RIGHT", 11); LuaSetEnum("XBOX_BUTTON_DOWN", 12); LuaSetEnum("XBOX_BUTTON_LEFT", 13); - LuaSetEnum("XBOX_BUTTON_RIGHT", 11); - LuaSetEnum("XBOX_AXIS_RIGHT_X", 4); - LuaSetEnum("XBOX_AXIS_RIGHT_Y", 3); - LuaSetEnum("XBOX_AXIS_LT_RT", 2); + LuaSetEnum("XBOX_BUTTON_HOME", 8); +#if defined(PLATFORM_RPI) + LuaSetEnum("XBOX_AXIS_LEFT_X", 0); // [-1..1] (left->right) + LuaSetEnum("XBOX_AXIS_LEFT_Y", 1); // [-1..1] (up->down) + LuaSetEnum("XBOX_AXIS_RIGHT_X", 3); // [-1..1] (left->right) + LuaSetEnum("XBOX_AXIS_RIGHT_Y", 4); // [-1..1] (up->down) + LuaSetEnum("XBOX_AXIS_LT", 2); // [-1..1] (pressure-level) + LuaSetEnum("XBOX_AXIS_RT", 5); // [-1..1] (pressure-level) +#else + LuaSetEnum("XBOX_AXIS_LEFT_X", 0); // [-1..1] (left->right) + LuaSetEnum("XBOX_AXIS_LEFT_Y", 1); // [1..-1] (up->down) + LuaSetEnum("XBOX_AXIS_RIGHT_X", 2); // [-1..1] (left->right) + LuaSetEnum("XBOX_AXIS_RIGHT_Y", 3); // [1..-1] (up->down) + LuaSetEnum("XBOX_AXIS_LT", 4); // [-1..1] (pressure-level) + LuaSetEnum("XBOX_AXIS_RT", 5); // [-1..1] (pressure-level) #endif - LuaSetEnum("XBOX_AXIS_LEFT_X", 0); - LuaSetEnum("XBOX_AXIS_LEFT_Y", 1); LuaEndEnum("GAMEPAD"); lua_pushglobaltable(L); @@ -4204,6 +4218,15 @@ RLUADEF void InitLuaDevice(void) LuaSetEnum("DIRECTIONAL", LIGHT_DIRECTIONAL); LuaSetEnum("SPOT", LIGHT_SPOT); LuaEndEnum("LightType"); + + LuaStartEnum(); + LuaSetEnum("POINT", FILTER_POINT); + LuaSetEnum("BILINEAR", FILTER_BILINEAR); + LuaSetEnum("TRILINEAR", FILTER_TRILINEAR); + LuaSetEnum("ANISOTROPIC_4X", FILTER_ANISOTROPIC_4X); + LuaSetEnum("ANISOTROPIC_8X", FILTER_ANISOTROPIC_8X); + LuaSetEnum("ANISOTROPIC_16X", FILTER_ANISOTROPIC_16X); + LuaEndEnum("TextureFilter"); LuaStartEnum(); LuaSetEnum("NONE", GESTURE_NONE); -- cgit v1.2.3 From 1a879ba08efe1877c26a76bfabd43f282d2b2a97 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 5 Feb 2017 02:59:39 +0100 Subject: Refactor SpriteFont struct Now it uses CharInfo data, this way, it's better aligned with the future RRES file format data layout for sprite font characters. --- examples/text_bmfont_ttf.c | 8 +- examples/text_bmfont_unordered.c | 6 +- examples/text_font_select.c | 6 +- examples/text_rbmf_fonts.c | 6 +- examples/text_sprite_fonts.c | 18 +-- examples/text_ttf_loading.c | 2 +- src/raylib.h | 20 ++-- src/rlua.h | 8 +- src/text.c | 230 +++++++++++++++++++++------------------ src/textures.c | 4 +- 10 files changed, 164 insertions(+), 144 deletions(-) (limited to 'examples/text_ttf_loading.c') diff --git a/examples/text_bmfont_ttf.c b/examples/text_bmfont_ttf.c index caece548..4d060915 100644 --- a/examples/text_bmfont_ttf.c +++ b/examples/text_bmfont_ttf.c @@ -29,8 +29,8 @@ int main() 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/examples/text_bmfont_unordered.c b/examples/text_bmfont_unordered.c index b29c5f8b..6fec3256 100644 --- a/examples/text_bmfont_unordered.c +++ b/examples/text_bmfont_unordered.c @@ -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/examples/text_font_select.c b/examples/text_font_select.c index fe586db8..5891bef7 100644 --- a/examples/text_font_select.c +++ b/examples/text_font_select.c @@ -41,7 +41,7 @@ int main() const char text[50] = "THIS is THE FONT you SELECTED!"; // Main text - Vector2 textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].size*3, 1); + Vector2 textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].baseSize*3, 1); Vector2 mousePoint; @@ -118,7 +118,7 @@ int main() } // Text measurement for better positioning on screen - textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].size*3, 1); + textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].baseSize*3, 1); //---------------------------------------------------------------------------------- // Draw @@ -140,7 +140,7 @@ int main() DrawText("NEXT", 700, positionY + 13, 20, btnNextOutColor); DrawTextEx(fonts[currentFont], text, (Vector2){ screenWidth/2 - textSize.x/2, - 260 + (70 - textSize.y)/2 }, fonts[currentFont].size*3, + 260 + (70 - textSize.y)/2 }, fonts[currentFont].baseSize*3, 1, colors[currentFont]); EndDrawing(); diff --git a/examples/text_rbmf_fonts.c b/examples/text_rbmf_fonts.c index b4bd851b..cd5da1fe 100644 --- a/examples/text_rbmf_fonts.c +++ b/examples/text_rbmf_fonts.c @@ -50,8 +50,8 @@ int main() 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; + 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 + 50*i; } Color colors[8] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD }; @@ -76,7 +76,7 @@ int main() for (int i = 0; i < 8; 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(); diff --git a/examples/text_sprite_fonts.c b/examples/text_sprite_fonts.c index c73eda85..bded266e 100644 --- a/examples/text_sprite_fonts.c +++ b/examples/text_sprite_fonts.c @@ -31,14 +31,14 @@ int main() 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/examples/text_ttf_loading.c b/examples/text_ttf_loading.c index 918209dd..10025c2f 100644 --- a/examples/text_ttf_loading.c +++ b/examples/text_ttf_loading.c @@ -31,7 +31,7 @@ int main() // 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; diff --git a/src/raylib.h b/src/raylib.h index 3c84ee72..f5b908db 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -370,15 +370,21 @@ typedef struct RenderTexture2D { Texture2D depth; // Depth buffer attachment texture } RenderTexture2D; +// SpriteFont character info +typedef struct CharInfo { + int value; // Character value (Unicode) + Rectangle rec; // Character rectangle in sprite font + int offsetX; // Character offset X when drawing + int offsetY; // Character offset Y when drawing + int advanceX; // Character advance position X +} CharInfo; + // SpriteFont type, includes texture and charSet array data typedef struct SpriteFont { Texture2D texture; // Font texture - int size; // Base size (default chars height) - int numChars; // Number of characters - int *charValues; // Characters values array - Rectangle *charRecs; // Characters rectangles within the texture - Vector2 *charOffsets; // Characters offsets (on drawing) - int *charAdvanceX; // Characters x advance (on drawing) + int baseSize; // Base size (default chars height) + int charsCount; // Number of characters + CharInfo *chars; // Characters info data } SpriteFont; // Camera type, defines a camera position/orientation in 3d space @@ -825,7 +831,7 @@ RLAPI void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle dest //------------------------------------------------------------------------------------ RLAPI SpriteFont GetDefaultFont(void); // Get the default SpriteFont RLAPI SpriteFont LoadSpriteFont(const char *fileName); // Load SpriteFont from file into GPU memory (VRAM) -RLAPI SpriteFont LoadSpriteFontTTF(const char *fileName, int fontSize, int numChars, int *fontChars); // Load SpriteFont from TTF font file with generation parameters +RLAPI SpriteFont LoadSpriteFontTTF(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load SpriteFont from TTF font file with generation parameters RLAPI void UnloadSpriteFont(SpriteFont spriteFont); // Unload SpriteFont from GPU memory (VRAM) RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font) diff --git a/src/rlua.h b/src/rlua.h index e9ed2c3c..80dede41 100644 --- a/src/rlua.h +++ b/src/rlua.h @@ -16,7 +16,7 @@ * * The following types: * Image, Texture2D, RenderTexture2D, SpriteFont -* are immutable, and you can only read their non-pointer arguments (e.g. sprfnt.size). +* are immutable, and you can only read their non-pointer arguments (e.g. sprfnt.baseSize). * * All other object types are opaque, that is, you cannot access or * change their fields directly. @@ -293,8 +293,8 @@ static int LuaIndexSpriteFont(lua_State* L) lua_pushinteger(L, img.size); else if (!strcmp(key, "texture")) LuaPush_Texture2D(L, img.texture); - else if (!strcmp(key, "numChars")) - lua_pushinteger(L, img.numChars); + else if (!strcmp(key, "charsCount")) + lua_pushinteger(L, img.charsCount); else return 0; return 1; @@ -2203,7 +2203,7 @@ int lua_LoadSpriteFontTTF(lua_State* L) int arg2 = LuaGetArgument_int(L, 2); int arg3 = LuaGetArgument_int(L, 3); int arg4 = LuaGetArgument_int(L, 4); - //LoadSpriteFontTTF(const char *fileName, int fontSize, int numChars, int *fontChars); + //LoadSpriteFontTTF(const char *fileName, int fontSize, int charsCount, int *fontChars); SpriteFont result = LoadSpriteFontTTF(arg1, arg2, arg3, &arg4); LuaPush_SpriteFont(L, result); return 1; diff --git a/src/text.c b/src/text.c index e3d22fbd..af07bb69 100644 --- a/src/text.c +++ b/src/text.c @@ -79,7 +79,7 @@ static int GetCharIndex(SpriteFont font, int letter); static SpriteFont LoadImageFont(Image image, Color key, int firstChar); // Load a Image font file (XNA style) static SpriteFont LoadRBMF(const char *fileName); // Load a rBMF font file (raylib BitMap Font) static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) -static SpriteFont LoadTTF(const char *fileName, int fontSize, int numChars, int *fontChars); // Load spritefont from TTF data +static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load spritefont from TTF data extern void LoadDefaultFont(void); extern void UnloadDefaultFont(void); @@ -92,7 +92,7 @@ extern void LoadDefaultFont(void) // NOTE: Using UTF8 encoding table for Unicode U+0000..U+00FF Basic Latin + Latin-1 Supplement // http://www.utf8-chartable.de/unicode-utf8-table.pl - defaultFont.numChars = 224; // Number of chars included in our default font + defaultFont.charsCount = 224; // Number of chars included in our default font // Default font is directly defined here (data generated from a sprite font image) // This way, we reconstruct SpriteFont without creating large global variables @@ -189,29 +189,27 @@ extern void LoadDefaultFont(void) defaultFont.texture = LoadTextureFromImage(image); UnloadImage(image); - // Reconstruct charSet using charsWidth[], charsHeight, charsDivisor, numChars + // Reconstruct charSet using charsWidth[], charsHeight, charsDivisor, charsCount //------------------------------------------------------------------------------ - defaultFont.charValues = (int *)malloc(defaultFont.numChars*sizeof(int)); - defaultFont.charRecs = (Rectangle *)malloc(defaultFont.numChars*sizeof(Rectangle)); // Allocate space for our character rectangle data - // This memory should be freed at end! --> Done on CloseWindow() - - defaultFont.charOffsets = (Vector2 *)malloc(defaultFont.numChars*sizeof(Vector2)); - defaultFont.charAdvanceX = (int *)malloc(defaultFont.numChars*sizeof(int)); + + // Allocate space for our characters info data + // NOTE: This memory should be freed at end! --> CloseWindow() + defaultFont.chars = (CharInfo *)malloc(defaultFont.charsCount*sizeof(CharInfo)); int currentLine = 0; int currentPosX = charsDivisor; int testPosX = charsDivisor; - for (int i = 0; i < defaultFont.numChars; i++) + for (int i = 0; i < defaultFont.charsCount; i++) { - defaultFont.charValues[i] = 32 + i; // First char is 32 + defaultFont.chars[i].value = 32 + i; // First char is 32 - defaultFont.charRecs[i].x = currentPosX; - defaultFont.charRecs[i].y = charsDivisor + currentLine*(charsHeight + charsDivisor); - defaultFont.charRecs[i].width = charsWidth[i]; - defaultFont.charRecs[i].height = charsHeight; + defaultFont.chars[i].rec.x = currentPosX; + defaultFont.chars[i].rec.y = charsDivisor + currentLine*(charsHeight + charsDivisor); + defaultFont.chars[i].rec.width = charsWidth[i]; + defaultFont.chars[i].rec.height = charsHeight; - testPosX += (defaultFont.charRecs[i].width + charsDivisor); + testPosX += (defaultFont.chars[i].rec.width + charsDivisor); if (testPosX >= defaultFont.texture.width) { @@ -219,17 +217,18 @@ extern void LoadDefaultFont(void) currentPosX = 2*charsDivisor + charsWidth[i]; testPosX = currentPosX; - defaultFont.charRecs[i].x = charsDivisor; - defaultFont.charRecs[i].y = charsDivisor + currentLine*(charsHeight + charsDivisor); + defaultFont.chars[i].rec.x = charsDivisor; + defaultFont.chars[i].rec.y = charsDivisor + currentLine*(charsHeight + charsDivisor); } else currentPosX = testPosX; // NOTE: On default font character offsets and xAdvance are not required - defaultFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; - defaultFont.charAdvanceX[i] = 0; + defaultFont.chars[i].offsetX = 0; + defaultFont.chars[i].offsetY = 0; + defaultFont.chars[i].advanceX = 0; } - defaultFont.size = defaultFont.charRecs[0].height; + defaultFont.baseSize = defaultFont.chars[0].rec.height; TraceLog(INFO, "[TEX ID %i] Default font loaded successfully", defaultFont.texture.id); } @@ -237,10 +236,7 @@ extern void LoadDefaultFont(void) extern void UnloadDefaultFont(void) { UnloadTexture(defaultFont.texture); - free(defaultFont.charValues); - free(defaultFont.charRecs); - free(defaultFont.charOffsets); - free(defaultFont.charAdvanceX); + free(defaultFont.chars); } // Get the default font, useful to be used with extended parameters @@ -260,9 +256,35 @@ SpriteFont LoadSpriteFont(const char *fileName) SpriteFont spriteFont = { 0 }; // Check file extension - if (strcmp(GetExtension(fileName),"rbmf") == 0) spriteFont = LoadRBMF(fileName); + if (strcmp(GetExtension(fileName),"rbmf") == 0) spriteFont = LoadRBMF(fileName); // TODO: DELETE... SOON... else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadSpriteFontTTF(fileName, DEFAULT_TTF_FONTSIZE, 0, NULL); else if (strcmp(GetExtension(fileName),"fnt") == 0) spriteFont = LoadBMFont(fileName); + else if (strcmp(GetExtension(fileName),"rres") == 0) + { + // TODO: Read multiple resource blocks from file (RRES_FONT_IMAGE, RRES_FONT_CHARDATA) + RRESData rres = LoadResource(fileName); + + // Load sprite font texture + if (rres.type == RRES_FONT_IMAGE) + { + // NOTE: Parameters for RRES_FONT_IMAGE type are: width, height, format, mipmaps + Image image = LoadImagePro(rres.data, rres.param1, rres.param2, rres.param3); + spriteFont.texture = LoadTextureFromImage(image); + UnloadImage(image); + } + + // Load sprite characters data + if (rres.type == RRES_FONT_CHARDATA) + { + // NOTE: Parameters for RRES_FONT_CHARDATA type are: fontSize, charsCount + spriteFont.baseSize = rres.param1; + spriteFont.charsCount = rres.param2; + spriteFont.chars = rres.data; + } + + // TODO: Do not free rres.data memory (chars info data!) + UnloadResource(rres); + } else { Image image = LoadImage(fileName); @@ -283,13 +305,13 @@ SpriteFont LoadSpriteFont(const char *fileName) // Load SpriteFont from TTF font file with generation parameters // NOTE: You can pass an array with desired characters, those characters should be available in the font // if array is NULL, default char set is selected 32..126 -SpriteFont LoadSpriteFontTTF(const char *fileName, int fontSize, int numChars, int *fontChars) +SpriteFont LoadSpriteFontTTF(const char *fileName, int fontSize, int charsCount, int *fontChars) { SpriteFont spriteFont = { 0 }; if (strcmp(GetExtension(fileName),"ttf") == 0) { - if ((fontChars == NULL) || (numChars == 0)) + if ((fontChars == NULL) || (charsCount == 0)) { int totalChars = 95; // Default charset [32..126] @@ -299,7 +321,7 @@ SpriteFont LoadSpriteFontTTF(const char *fileName, int fontSize, int numChars, i spriteFont = LoadTTF(fileName, fontSize, totalChars, defaultFontChars); } - else spriteFont = LoadTTF(fileName, fontSize, numChars, fontChars); + else spriteFont = LoadTTF(fileName, fontSize, charsCount, fontChars); } if (spriteFont.texture.id == 0) @@ -318,10 +340,7 @@ void UnloadSpriteFont(SpriteFont spriteFont) if (spriteFont.texture.id != defaultFont.texture.id) { UnloadTexture(spriteFont.texture); - free(spriteFont.charValues); - free(spriteFont.charRecs); - free(spriteFont.charOffsets); - free(spriteFont.charAdvanceX); + free(spriteFont.chars); TraceLog(DEBUG, "Unloaded sprite font data"); } @@ -357,7 +376,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float unsigned char letter; // Current character int index; // Index position in sprite font - scaleFactor = fontSize/spriteFont.size; + scaleFactor = fontSize/spriteFont.baseSize; // NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly // written in C code files (codified by default as UTF-8) @@ -367,7 +386,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float if ((unsigned char)text[i] == '\n') { // NOTE: Fixed line spacing of 1.5 lines - textOffsetY += (int)((spriteFont.size + spriteFont.size/2)*scaleFactor); + textOffsetY += (int)((spriteFont.baseSize + spriteFont.baseSize/2)*scaleFactor); textOffsetX = 0; } else @@ -388,14 +407,14 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float } else index = GetCharIndex(spriteFont, (int)text[i]); - DrawTexturePro(spriteFont.texture, spriteFont.charRecs[index], - (Rectangle){ position.x + textOffsetX + spriteFont.charOffsets[index].x*scaleFactor, - position.y + textOffsetY + spriteFont.charOffsets[index].y*scaleFactor, - spriteFont.charRecs[index].width*scaleFactor, - spriteFont.charRecs[index].height*scaleFactor }, (Vector2){ 0, 0 }, 0.0f, tint); + DrawTexturePro(spriteFont.texture, spriteFont.chars[index].rec, + (Rectangle){ position.x + textOffsetX + spriteFont.chars[index].offsetX*scaleFactor, + position.y + textOffsetY + spriteFont.chars[index].offsetY*scaleFactor, + spriteFont.chars[index].rec.width*scaleFactor, + spriteFont.chars[index].rec.height*scaleFactor }, (Vector2){ 0, 0 }, 0.0f, tint); - if (spriteFont.charAdvanceX[index] == 0) textOffsetX += (int)(spriteFont.charRecs[index].width*scaleFactor + spacing); - else textOffsetX += (int)(spriteFont.charAdvanceX[index]*scaleFactor + spacing); + if (spriteFont.chars[index].advanceX == 0) textOffsetX += (int)(spriteFont.chars[index].rec.width*scaleFactor + spacing); + else textOffsetX += (int)(spriteFont.chars[index].advanceX*scaleFactor + spacing); } } } @@ -466,8 +485,8 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i float textWidth = 0; float tempTextWidth = 0; // Used to count longer text line width - float textHeight = (float)spriteFont.size; - float scaleFactor = fontSize/(float)spriteFont.size; + float textHeight = (float)spriteFont.baseSize; + float scaleFactor = fontSize/(float)spriteFont.baseSize; for (int i = 0; i < len; i++) { @@ -477,15 +496,15 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i { int index = GetCharIndex(spriteFont, (int)text[i]); - if (spriteFont.charAdvanceX[index] != 0) textWidth += spriteFont.charAdvanceX[index]; - else textWidth += (spriteFont.charRecs[index].width + spriteFont.charOffsets[index].x); + if (spriteFont.chars[index].advanceX != 0) textWidth += spriteFont.chars[index].advanceX; + else textWidth += (spriteFont.chars[index].rec.width + spriteFont.chars[index].offsetX); } else { if (tempTextWidth < textWidth) tempTextWidth = textWidth; lenCounter = 0; textWidth = 0; - textHeight += ((float)spriteFont.size*1.5f); // NOTE: Fixed line spacing of 1.5 lines + textHeight += ((float)spriteFont.baseSize*1.5f); // NOTE: Fixed line spacing of 1.5 lines } if (tempLen < lenCounter) tempLen = lenCounter; @@ -518,9 +537,9 @@ static int GetCharIndex(SpriteFont font, int letter) #if defined(UNORDERED_CHARSET) int index = 0; - for (int i = 0; i < font.numChars; i++) + for (int i = 0; i < font.charsCount; i++) { - if (font.charValues[i] == letter) + if (font.chars[i].value == letter) { index = i; break; @@ -621,28 +640,26 @@ static SpriteFont LoadImageFont(Image image, Color key, int firstChar) SpriteFont spriteFont = { 0 }; spriteFont.texture = LoadTextureFromImage(fontClear); // Convert processed image to OpenGL texture - spriteFont.numChars = index; + spriteFont.charsCount = index; UnloadImage(fontClear); // Unload processed image once converted to texture // We got tempCharValues and tempCharsRecs populated with chars data // Now we move temp data to sized charValues and charRecs arrays - spriteFont.charRecs = (Rectangle *)malloc(spriteFont.numChars*sizeof(Rectangle)); - spriteFont.charValues = (int *)malloc(spriteFont.numChars*sizeof(int)); - spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2)); - spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int)); + spriteFont.chars = (CharInfo *)malloc(spriteFont.charsCount*sizeof(CharInfo)); - for (int i = 0; i < spriteFont.numChars; i++) + for (int i = 0; i < spriteFont.charsCount; i++) { - spriteFont.charValues[i] = tempCharValues[i]; - spriteFont.charRecs[i] = tempCharRecs[i]; + spriteFont.chars[i].value = tempCharValues[i]; + spriteFont.chars[i].rec = tempCharRecs[i]; // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) - spriteFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; - spriteFont.charAdvanceX[i] = 0; + spriteFont.chars[i].offsetX = 0; + spriteFont.chars[i].offsetY = 0; + spriteFont.chars[i].advanceX = 0; } - spriteFont.size = spriteFont.charRecs[0].height; + spriteFont.baseSize = spriteFont.chars[0].rec.height; TraceLog(INFO, "Image file loaded correctly as SpriteFont"); @@ -672,6 +689,8 @@ static SpriteFont LoadRBMF(const char *fileName) SpriteFont spriteFont = { 0 }; + // REMOVE SOON!!! +/* rbmfInfoHeader rbmfHeader; unsigned int *rbmfFileData = NULL; unsigned char *rbmfCharWidthData = NULL; @@ -737,29 +756,27 @@ static SpriteFont LoadRBMF(const char *fileName) //TraceLog(INFO, "[%s] Starting chars set reconstruction", fileName); // Get characters data using rbmfCharWidthData, rbmfHeader.charHeight, charsDivisor, rbmfHeader.numChars - spriteFont.charValues = (int *)malloc(spriteFont.numChars*sizeof(int)); - spriteFont.charRecs = (Rectangle *)malloc(spriteFont.numChars*sizeof(Rectangle)); - spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2)); - spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int)); + spriteFont.chars = (CharInfo *)malloc(spriteFont.charsCount*sizeof(CharInfo)); int currentLine = 0; int currentPosX = charsDivisor; int testPosX = charsDivisor; - for (int i = 0; i < spriteFont.numChars; i++) + for (int i = 0; i < spriteFont.charsCount; i++) { - spriteFont.charValues[i] = (int)rbmfHeader.firstChar + i; + spriteFont.chars[i].value = (int)rbmfHeader.firstChar + i; - spriteFont.charRecs[i].x = currentPosX; - spriteFont.charRecs[i].y = charsDivisor + currentLine*((int)rbmfHeader.charHeight + charsDivisor); - spriteFont.charRecs[i].width = (int)rbmfCharWidthData[i]; - spriteFont.charRecs[i].height = (int)rbmfHeader.charHeight; + spriteFont.chars[i].rec.x = currentPosX; + spriteFont.chars[i].rec.y = charsDivisor + currentLine*((int)rbmfHeader.charHeight + charsDivisor); + spriteFont.chars[i].rec.width = (int)rbmfCharWidthData[i]; + spriteFont.chars[i].rec.height = (int)rbmfHeader.charHeight; // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) - spriteFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; - spriteFont.charAdvanceX[i] = 0; + spriteFont.chars[i].offsetX = 0; + spriteFont.chars[i].offsetY = 0; + spriteFont.chars[i].advanceX = 0; - testPosX += (spriteFont.charRecs[i].width + charsDivisor); + testPosX += (spriteFont.chars[i].rec.width + charsDivisor); if (testPosX > spriteFont.texture.width) { @@ -767,13 +784,13 @@ static SpriteFont LoadRBMF(const char *fileName) currentPosX = 2*charsDivisor + (int)rbmfCharWidthData[i]; testPosX = currentPosX; - spriteFont.charRecs[i].x = charsDivisor; - spriteFont.charRecs[i].y = charsDivisor + currentLine*(rbmfHeader.charHeight + charsDivisor); + spriteFont.chars[i].rec.x = charsDivisor; + spriteFont.chars[i].rec.y = charsDivisor + currentLine*(rbmfHeader.charHeight + charsDivisor); } else currentPosX = testPosX; } - spriteFont.size = spriteFont.charRecs[0].height; + spriteFont.baseSize = spriteFont.charRecs[0].height; TraceLog(INFO, "[%s] rBMF file loaded correctly as SpriteFont", fileName); } @@ -782,6 +799,7 @@ static SpriteFont LoadRBMF(const char *fileName) free(rbmfFileData); // Now we can free loaded data from RAM memory free(rbmfCharWidthData); +*/ return spriteFont; } @@ -800,7 +818,7 @@ static SpriteFont LoadBMFont(const char *fileName) int fontSize = 0; int texWidth, texHeight; char texFileName[128]; - int numChars = 0; + int charsCount = 0; int base; // Useless data @@ -834,9 +852,9 @@ static SpriteFont LoadBMFont(const char *fileName) fgets(buffer, MAX_BUFFER_SIZE, fntFile); searchPoint = strstr(buffer, "count"); - sscanf(searchPoint, "count=%i", &numChars); + sscanf(searchPoint, "count=%i", &charsCount); - TraceLog(DEBUG, "[%s] Font num chars: %i", fileName, numChars); + TraceLog(DEBUG, "[%s] Font num chars: %i", fileName, charsCount); // Compose correct path using route of .fnt file (fileName) and texFileName char *texPath = NULL; @@ -868,12 +886,9 @@ static SpriteFont LoadBMFont(const char *fileName) } else font.texture = LoadTextureFromImage(imFont); - font.size = fontSize; - font.numChars = numChars; - font.charValues = (int *)malloc(numChars*sizeof(int)); - font.charRecs = (Rectangle *)malloc(numChars*sizeof(Rectangle)); - font.charOffsets = (Vector2 *)malloc(numChars*sizeof(Vector2)); - font.charAdvanceX = (int *)malloc(numChars*sizeof(int)); + font.baseSize = fontSize; + font.charsCount = charsCount; + font.chars = (CharInfo *)malloc(charsCount*sizeof(CharInfo)); UnloadImage(imFont); @@ -881,17 +896,18 @@ static SpriteFont LoadBMFont(const char *fileName) int charId, charX, charY, charWidth, charHeight, charOffsetX, charOffsetY, charAdvanceX; - for (int i = 0; i < numChars; i++) + for (int i = 0; i < charsCount; i++) { fgets(buffer, MAX_BUFFER_SIZE, fntFile); sscanf(buffer, "char id=%i x=%i y=%i width=%i height=%i xoffset=%i yoffset=%i xadvance=%i", &charId, &charX, &charY, &charWidth, &charHeight, &charOffsetX, &charOffsetY, &charAdvanceX); // Save data properly in sprite font - font.charValues[i] = charId; - font.charRecs[i] = (Rectangle){ charX, charY, charWidth, charHeight }; - font.charOffsets[i] = (Vector2){ (float)charOffsetX, (float)charOffsetY }; - font.charAdvanceX[i] = charAdvanceX; + font.chars[i].value = charId; + font.chars[i].rec = (Rectangle){ charX, charY, charWidth, charHeight }; + font.chars[i].offsetX = charOffsetX; + font.chars[i].offsetY = charOffsetY; + font.chars[i].advanceX = charAdvanceX; } fclose(fntFile); @@ -908,21 +924,21 @@ static SpriteFont LoadBMFont(const char *fileName) // Generate a sprite font from TTF file data (font size required) // TODO: Review texture packing method and generation (use oversampling) -static SpriteFont LoadTTF(const char *fileName, int fontSize, int numChars, int *fontChars) +static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, int *fontChars) { // NOTE: Font texture size is predicted (being as much conservative as possible) // Predictive method consist of supposing same number of chars by line-column (sqrtf) // and a maximum character width of 3/4 of fontSize... it worked ok with all my tests... // Calculate next power-of-two value - float guessSize = ceilf((float)fontSize*3/4)*ceilf(sqrtf((float)numChars)); + float guessSize = ceilf((float)fontSize*3/4)*ceilf(sqrtf((float)charsCount)); int textureSize = (int)powf(2, ceilf(logf((float)guessSize)/logf(2))); // Calculate next POT TraceLog(INFO, "TTF spritefont loading: Predicted texture size: %ix%i", textureSize, textureSize); unsigned char *ttfBuffer = (unsigned char *)malloc(1 << 25); unsigned char *dataBitmap = (unsigned char *)malloc(textureSize*textureSize*sizeof(unsigned char)); // One channel bitmap returned! - stbtt_bakedchar *charData = (stbtt_bakedchar *)malloc(sizeof(stbtt_bakedchar)*numChars); + stbtt_bakedchar *charData = (stbtt_bakedchar *)malloc(sizeof(stbtt_bakedchar)*charsCount); SpriteFont font = { 0 }; @@ -941,7 +957,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int numChars, int // NOTE: Using stb_truetype crappy packing method, no guarante the font fits the image... // TODO: Replace this function by a proper packing method and support random chars order, // we already receive a list (fontChars) with the ordered expected characters - int result = stbtt_BakeFontBitmap(ttfBuffer, 0, fontSize, dataBitmap, textureSize, textureSize, fontChars[0], numChars, charData); + int result = stbtt_BakeFontBitmap(ttfBuffer, 0, fontSize, dataBitmap, textureSize, textureSize, fontChars[0], charsCount, charData); //if (result > 0) TraceLog(INFO, "TTF spritefont loading: first unused row of generated bitmap: %i", result); if (result < 0) TraceLog(WARNING, "TTF spritefont loading: Not all the characters fit in the font"); @@ -973,24 +989,22 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int numChars, int UnloadImage(image); // Unloads dataGrayAlpha - font.size = fontSize; - font.numChars = numChars; - font.charValues = (int *)malloc(font.numChars*sizeof(int)); - font.charRecs = (Rectangle *)malloc(font.numChars*sizeof(Rectangle)); - font.charOffsets = (Vector2 *)malloc(font.numChars*sizeof(Vector2)); - font.charAdvanceX = (int *)malloc(font.numChars*sizeof(int)); + font.baseSize = fontSize; + font.charsCount = charsCount; + font.chars = (CharInfo *)malloc(font.charsCount*sizeof(CharInfo)); - for (int i = 0; i < font.numChars; i++) + for (int i = 0; i < font.charsCount; i++) { - font.charValues[i] = fontChars[i]; + font.chars[i].value = fontChars[i]; - font.charRecs[i].x = (int)charData[i].x0; - font.charRecs[i].y = (int)charData[i].y0; - font.charRecs[i].width = (int)charData[i].x1 - (int)charData[i].x0; - font.charRecs[i].height = (int)charData[i].y1 - (int)charData[i].y0; + font.chars[i].rec.x = (int)charData[i].x0; + font.chars[i].rec.y = (int)charData[i].y0; + font.chars[i].rec.width = (int)charData[i].x1 - (int)charData[i].x0; + font.chars[i].rec.height = (int)charData[i].y1 - (int)charData[i].y0; - font.charOffsets[i] = (Vector2){ charData[i].xoff, charData[i].yoff }; - font.charAdvanceX[i] = (int)charData[i].xadvance; + font.chars[i].offsetX = charData[i].xoff; + font.chars[i].offsetY = charData[i].yoff; + font.chars[i].advanceX = (int)charData[i].xadvance; } free(charData); diff --git a/src/textures.c b/src/textures.c index 9d865aa1..ce978b6c 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1064,7 +1064,7 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing int length = strlen(text); int posX = 0; - Vector2 imSize = MeasureTextEx(font, text, font.size, spacing); + Vector2 imSize = MeasureTextEx(font, text, font.baseSize, spacing); // NOTE: GetTextureData() not available in OpenGL ES Image imFont = GetTextureData(font.texture); @@ -1080,7 +1080,7 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing for (int i = 0; i < length; i++) { - Rectangle letterRec = font.charRecs[(int)text[i] - 32]; + Rectangle letterRec = font.chars[(int)text[i] - 32].rec; for (int y = letterRec.y; y < (letterRec.y + letterRec.height); y++) { -- cgit v1.2.3