| 1 | /******************************************************************************************* |
| 2 | * |
| 3 | * raylib [text] example - Font loading |
| 4 | * |
| 5 | * raylib can load fonts from multiple file formats: |
| 6 | * |
| 7 | * - TTF/OTF > Sprite font atlas is generated on loading, user can configure |
| 8 | * some of the generation parameters (size, characters to include) |
| 9 | * - BMFonts > Angel code font fileformat, sprite font image must be provided |
| 10 | * together with the .fnt file, font generation cna not be configured |
| 11 | * - XNA Spritefont > Sprite font image, following XNA Spritefont conventions, |
| 12 | * Characters in image must follow some spacing and order rules |
| 13 | * |
| 14 | * This example has been created using raylib 2.6 (www.raylib.com) |
| 15 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
| 16 | * |
| 17 | * Copyright (c) 2016-2019 Ramon Santamaria (@raysan5) |
| 18 | * |
| 19 | ********************************************************************************************/ |
| 20 | |
| 21 | #include "raylib.h" |
| 22 | |
| 23 | int main(void) |
| 24 | { |
| 25 | // Initialization |
| 26 | //-------------------------------------------------------------------------------------- |
| 27 | const int screenWidth = 800; |
| 28 | const int screenHeight = 450; |
| 29 | |
| 30 | InitWindow(screenWidth, screenHeight, "raylib [text] example - font loading" ); |
| 31 | |
| 32 | // Define characters to draw |
| 33 | // NOTE: raylib supports UTF-8 encoding, following list is actually codified as UTF8 internally |
| 34 | const char msg[256] = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHI\nJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmn\nopqrstuvwxyz{|}~¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓ\nÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷\nøùúûüýþÿ" ; |
| 35 | |
| 36 | // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) |
| 37 | |
| 38 | // BMFont (AngelCode) : Font data and image atlas have been generated using external program |
| 39 | Font fontBm = LoadFont("resources/pixantiqua.fnt" ); |
| 40 | |
| 41 | // TTF font : Font data and atlas are generated directly from TTF |
| 42 | // NOTE: We define a font base size of 32 pixels tall and up-to 250 characters |
| 43 | Font fontTtf = LoadFontEx("resources/pixantiqua.ttf" , 32, 0, 250); |
| 44 | |
| 45 | bool useTtf = false; |
| 46 | |
| 47 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 48 | //-------------------------------------------------------------------------------------- |
| 49 | |
| 50 | // Main game loop |
| 51 | while (!WindowShouldClose()) // Detect window close button or ESC key |
| 52 | { |
| 53 | // Update |
| 54 | //---------------------------------------------------------------------------------- |
| 55 | if (IsKeyDown(KEY_SPACE)) useTtf = true; |
| 56 | else useTtf = false; |
| 57 | //---------------------------------------------------------------------------------- |
| 58 | |
| 59 | // Draw |
| 60 | //---------------------------------------------------------------------------------- |
| 61 | BeginDrawing(); |
| 62 | |
| 63 | ClearBackground(RAYWHITE); |
| 64 | |
| 65 | DrawText("Hold SPACE to use TTF generated font" , 20, 20, 20, LIGHTGRAY); |
| 66 | |
| 67 | if (!useTtf) |
| 68 | { |
| 69 | DrawTextEx(fontBm, msg, (Vector2){ 20.0f, 100.0f }, fontBm.baseSize, 2, MAROON); |
| 70 | DrawText("Using BMFont (Angelcode) imported" , 20, GetScreenHeight() - 30, 20, GRAY); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | DrawTextEx(fontTtf, msg, (Vector2){ 20.0f, 100.0f }, fontTtf.baseSize, 2, LIME); |
| 75 | DrawText("Using TTF font generated" , 20, GetScreenHeight() - 30, 20, GRAY); |
| 76 | } |
| 77 | |
| 78 | EndDrawing(); |
| 79 | //---------------------------------------------------------------------------------- |
| 80 | } |
| 81 | |
| 82 | // De-Initialization |
| 83 | //-------------------------------------------------------------------------------------- |
| 84 | UnloadFont(fontBm); // AngelCode Font unloading |
| 85 | UnloadFont(fontTtf); // TTF Font unloading |
| 86 | |
| 87 | CloseWindow(); // Close window and OpenGL context |
| 88 | //-------------------------------------------------------------------------------------- |
| 89 | |
| 90 | return 0; |
| 91 | } |