1 | /******************************************************************************************* |
2 | * |
3 | * raylib [text] example - Font filters |
4 | * |
5 | * After font loading, font texture atlas filter could be configured for a softer |
6 | * display of the font when scaling it to different sizes, that way, it's not required |
7 | * to generate multiple fonts at multiple sizes (as long as the scaling is not very different) |
8 | * |
9 | * This example has been created using raylib 1.3.0 (www.raylib.com) |
10 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
11 | * |
12 | * Copyright (c) 2015 Ramon Santamaria (@raysan5) |
13 | * |
14 | ********************************************************************************************/ |
15 | |
16 | #include "raylib.h" |
17 | |
18 | int main(void) |
19 | { |
20 | // Initialization |
21 | //-------------------------------------------------------------------------------------- |
22 | const int screenWidth = 800; |
23 | const int screenHeight = 450; |
24 | |
25 | InitWindow(screenWidth, screenHeight, "raylib [text] example - font filters" ); |
26 | |
27 | const char msg[50] = "Loaded Font" ; |
28 | |
29 | // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) |
30 | |
31 | // TTF Font loading with custom generation parameters |
32 | Font font = LoadFontEx("resources/KAISG.ttf" , 96, 0, 0); |
33 | |
34 | // Generate mipmap levels to use trilinear filtering |
35 | // NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR |
36 | GenTextureMipmaps(&font.texture); |
37 | |
38 | float fontSize = font.baseSize; |
39 | Vector2 fontPosition = { 40, screenHeight/2 - 80 }; |
40 | Vector2 textSize = { 0.0f, 0.0f }; |
41 | |
42 | // Setup texture scaling filter |
43 | SetTextureFilter(font.texture, FILTER_POINT); |
44 | int currentFontFilter = 0; // FILTER_POINT |
45 | |
46 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
47 | //-------------------------------------------------------------------------------------- |
48 | |
49 | // Main game loop |
50 | while (!WindowShouldClose()) // Detect window close button or ESC key |
51 | { |
52 | // Update |
53 | //---------------------------------------------------------------------------------- |
54 | fontSize += GetMouseWheelMove()*4.0f; |
55 | |
56 | // Choose font texture filter method |
57 | if (IsKeyPressed(KEY_ONE)) |
58 | { |
59 | SetTextureFilter(font.texture, FILTER_POINT); |
60 | currentFontFilter = 0; |
61 | } |
62 | else if (IsKeyPressed(KEY_TWO)) |
63 | { |
64 | SetTextureFilter(font.texture, FILTER_BILINEAR); |
65 | currentFontFilter = 1; |
66 | } |
67 | else if (IsKeyPressed(KEY_THREE)) |
68 | { |
69 | // NOTE: Trilinear filter won't be noticed on 2D drawing |
70 | SetTextureFilter(font.texture, FILTER_TRILINEAR); |
71 | currentFontFilter = 2; |
72 | } |
73 | |
74 | textSize = MeasureTextEx(font, msg, fontSize, 0); |
75 | |
76 | if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10; |
77 | else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10; |
78 | |
79 | // Load a dropped TTF file dynamically (at current fontSize) |
80 | if (IsFileDropped()) |
81 | { |
82 | int count = 0; |
83 | char **droppedFiles = GetDroppedFiles(&count); |
84 | |
85 | // NOTE: We only support first ttf file dropped |
86 | if (IsFileExtension(droppedFiles[0], ".ttf" )) |
87 | { |
88 | UnloadFont(font); |
89 | font = LoadFontEx(droppedFiles[0], fontSize, 0, 0); |
90 | ClearDroppedFiles(); |
91 | } |
92 | } |
93 | //---------------------------------------------------------------------------------- |
94 | |
95 | // Draw |
96 | //---------------------------------------------------------------------------------- |
97 | BeginDrawing(); |
98 | |
99 | ClearBackground(RAYWHITE); |
100 | |
101 | DrawText("Use mouse wheel to change font size" , 20, 20, 10, GRAY); |
102 | DrawText("Use KEY_RIGHT and KEY_LEFT to move text" , 20, 40, 10, GRAY); |
103 | DrawText("Use 1, 2, 3 to change texture filter" , 20, 60, 10, GRAY); |
104 | DrawText("Drop a new TTF font for dynamic loading" , 20, 80, 10, DARKGRAY); |
105 | |
106 | DrawTextEx(font, msg, fontPosition, fontSize, 0, BLACK); |
107 | |
108 | // TODO: It seems texSize measurement is not accurate due to chars offsets... |
109 | //DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED); |
110 | |
111 | DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY); |
112 | DrawText(FormatText("Font size: %02.02f" , fontSize), 20, screenHeight - 50, 10, DARKGRAY); |
113 | DrawText(FormatText("Text size: [%02.02f, %02.02f]" , textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY); |
114 | DrawText("CURRENT TEXTURE FILTER:" , 250, 400, 20, GRAY); |
115 | |
116 | if (currentFontFilter == 0) DrawText("POINT" , 570, 400, 20, BLACK); |
117 | else if (currentFontFilter == 1) DrawText("BILINEAR" , 570, 400, 20, BLACK); |
118 | else if (currentFontFilter == 2) DrawText("TRILINEAR" , 570, 400, 20, BLACK); |
119 | |
120 | EndDrawing(); |
121 | //---------------------------------------------------------------------------------- |
122 | } |
123 | |
124 | // De-Initialization |
125 | //-------------------------------------------------------------------------------------- |
126 | ClearDroppedFiles(); // Clear internal buffers |
127 | |
128 | UnloadFont(font); // Font unloading |
129 | |
130 | CloseWindow(); // Close window and OpenGL context |
131 | //-------------------------------------------------------------------------------------- |
132 | |
133 | return 0; |
134 | } |