1 | /******************************************************************************************* |
2 | * |
3 | * raylib [text] example - Sprite font loading |
4 | * |
5 | * Loaded sprite fonts have been generated following XNA SpriteFont conventions: |
6 | * - Characters must be ordered starting with character 32 (Space) |
7 | * - Every character must be contained within the same Rectangle height |
8 | * - Every character and every line must be separated the same distance |
9 | * - Rectangles must be defined by a MAGENTA color background |
10 | * |
11 | * If following this constraints, a font can be provided just by an image, |
12 | * this is quite handy to avoid additional information files (like BMFonts use). |
13 | * |
14 | * This example has been created using raylib 1.0 (www.raylib.com) |
15 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
16 | * |
17 | * Copyright (c) 2014 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 - sprite font loading" ); |
31 | |
32 | const char msg1[50] = "THIS IS A custom SPRITE FONT..." ; |
33 | const char msg2[50] = "...and this is ANOTHER CUSTOM font..." ; |
34 | const char msg3[50] = "...and a THIRD one! GREAT! :D" ; |
35 | |
36 | // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) |
37 | Font font1 = LoadFont("resources/custom_mecha.png" ); // Font loading |
38 | Font font2 = LoadFont("resources/custom_alagard.png" ); // Font loading |
39 | Font font3 = LoadFont("resources/custom_jupiter_crash.png" ); // Font loading |
40 | |
41 | Vector2 fontPosition1 = { screenWidth/2 - MeasureTextEx(font1, msg1, font1.baseSize, -3).x/2, |
42 | screenHeight/2 - font1.baseSize/2 - 80 }; |
43 | |
44 | Vector2 fontPosition2 = { screenWidth/2 - MeasureTextEx(font2, msg2, font2.baseSize, -2).x/2, |
45 | screenHeight/2 - font2.baseSize/2 - 10 }; |
46 | |
47 | Vector2 fontPosition3 = { screenWidth/2 - MeasureTextEx(font3, msg3, font3.baseSize, 2).x/2, |
48 | screenHeight/2 - font3.baseSize/2 + 50 }; |
49 | |
50 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
51 | //-------------------------------------------------------------------------------------- |
52 | |
53 | // Main game loop |
54 | while (!WindowShouldClose()) // Detect window close button or ESC key |
55 | { |
56 | // Update |
57 | //---------------------------------------------------------------------------------- |
58 | // TODO: Update variables here... |
59 | //---------------------------------------------------------------------------------- |
60 | |
61 | // Draw |
62 | //---------------------------------------------------------------------------------- |
63 | BeginDrawing(); |
64 | |
65 | ClearBackground(RAYWHITE); |
66 | |
67 | DrawTextEx(font1, msg1, fontPosition1, font1.baseSize, -3, WHITE); |
68 | DrawTextEx(font2, msg2, fontPosition2, font2.baseSize, -2, WHITE); |
69 | DrawTextEx(font3, msg3, fontPosition3, font3.baseSize, 2, WHITE); |
70 | |
71 | EndDrawing(); |
72 | //---------------------------------------------------------------------------------- |
73 | } |
74 | |
75 | // De-Initialization |
76 | //-------------------------------------------------------------------------------------- |
77 | UnloadFont(font1); // Font unloading |
78 | UnloadFont(font2); // Font unloading |
79 | UnloadFont(font3); // Font unloading |
80 | |
81 | CloseWindow(); // Close window and OpenGL context |
82 | //-------------------------------------------------------------------------------------- |
83 | |
84 | return 0; |
85 | } |