1 | /******************************************************************************************* |
2 | * |
3 | * raylib [texture] example - Image text drawing using TTF generated spritefont |
4 | * |
5 | * This example has been created using raylib 1.8 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2017 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | int main(void) |
15 | { |
16 | // Initialization |
17 | //-------------------------------------------------------------------------------------- |
18 | const int screenWidth = 800; |
19 | const int screenHeight = 450; |
20 | |
21 | InitWindow(screenWidth, screenHeight, "raylib [texture] example - image text drawing" ); |
22 | |
23 | Image parrots = LoadImage("resources/parrots.png" ); // Load image in CPU memory (RAM) |
24 | |
25 | // TTF Font loading with custom generation parameters |
26 | Font font = LoadFontEx("resources/KAISG.ttf" , 64, 0, 0); |
27 | |
28 | // Draw over image using custom font |
29 | ImageDrawTextEx(&parrots, font, "[Parrots font drawing]" , (Vector2){ 20.0f, 20.0f }, (float)font.baseSize, 0.0f, RED); |
30 | |
31 | Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM) |
32 | UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM |
33 | |
34 | Vector2 position = { (float)(screenWidth/2 - texture.width/2), (float)(screenHeight/2 - texture.height/2 - 20) }; |
35 | |
36 | bool showFont = false; |
37 | |
38 | SetTargetFPS(60); |
39 | //-------------------------------------------------------------------------------------- |
40 | |
41 | // Main game loop |
42 | while (!WindowShouldClose()) // Detect window close button or ESC key |
43 | { |
44 | // Update |
45 | //---------------------------------------------------------------------------------- |
46 | if (IsKeyDown(KEY_SPACE)) showFont = true; |
47 | else showFont = false; |
48 | //---------------------------------------------------------------------------------- |
49 | |
50 | // Draw |
51 | //---------------------------------------------------------------------------------- |
52 | BeginDrawing(); |
53 | |
54 | ClearBackground(RAYWHITE); |
55 | |
56 | if (!showFont) |
57 | { |
58 | // Draw texture with text already drawn inside |
59 | DrawTextureV(texture, position, WHITE); |
60 | |
61 | // Draw text directly using sprite font |
62 | DrawTextEx(font, "[Parrots font drawing]" , (Vector2){ position.x + 20, |
63 | position.y + 20 + 280 }, (float)font.baseSize, 0.0f, WHITE); |
64 | } |
65 | else DrawTexture(font.texture, screenWidth/2 - font.texture.width/2, 50, BLACK); |
66 | |
67 | DrawText("PRESS SPACE to SEE USED SPRITEFONT " , 290, 420, 10, DARKGRAY); |
68 | |
69 | EndDrawing(); |
70 | //---------------------------------------------------------------------------------- |
71 | } |
72 | |
73 | // De-Initialization |
74 | //-------------------------------------------------------------------------------------- |
75 | UnloadTexture(texture); // Texture unloading |
76 | |
77 | UnloadFont(font); // Unload custom spritefont |
78 | |
79 | CloseWindow(); // Close window and OpenGL context |
80 | //-------------------------------------------------------------------------------------- |
81 | |
82 | return 0; |
83 | } |