1 | /******************************************************************************************* |
2 | * |
3 | * raylib [text] example - TTF loading and usage |
4 | * |
5 | * This example has been created using raylib 1.3.0 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2015 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | #if defined(PLATFORM_DESKTOP) |
15 | #define GLSL_VERSION 330 |
16 | #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB |
17 | #define GLSL_VERSION 100 |
18 | #endif |
19 | |
20 | #include <stdlib.h> |
21 | |
22 | int main(void) |
23 | { |
24 | // Initialization |
25 | //-------------------------------------------------------------------------------------- |
26 | const int screenWidth = 800; |
27 | const int screenHeight = 450; |
28 | |
29 | InitWindow(screenWidth, screenHeight, "raylib [text] example - SDF fonts" ); |
30 | |
31 | // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) |
32 | |
33 | const char msg[50] = "Signed Distance Fields" ; |
34 | |
35 | // Default font generation from TTF font |
36 | Font fontDefault = { 0 }; |
37 | fontDefault.baseSize = 16; |
38 | fontDefault.charsCount = 95; |
39 | // Parameters > font size: 16, no chars array provided (0), chars count: 95 (autogenerate chars array) |
40 | fontDefault.chars = LoadFontData("resources/AnonymousPro-Bold.ttf" , 16, 0, 95, FONT_DEFAULT); |
41 | // Parameters > chars count: 95, font size: 16, chars padding in image: 4 px, pack method: 0 (default) |
42 | Image atlas = GenImageFontAtlas(fontDefault.chars, &fontDefault.recs, 95, 16, 4, 0); |
43 | fontDefault.texture = LoadTextureFromImage(atlas); |
44 | UnloadImage(atlas); |
45 | |
46 | // SDF font generation from TTF font |
47 | Font fontSDF = { 0 }; |
48 | fontSDF.baseSize = 16; |
49 | fontSDF.charsCount = 95; |
50 | // Parameters > font size: 16, no chars array provided (0), chars count: 0 (defaults to 95) |
51 | fontSDF.chars = LoadFontData("resources/AnonymousPro-Bold.ttf" , 16, 0, 0, FONT_SDF); |
52 | // Parameters > chars count: 95, font size: 16, chars padding in image: 0 px, pack method: 1 (Skyline algorythm) |
53 | atlas = GenImageFontAtlas(fontSDF.chars, &fontSDF.recs, 95, 16, 0, 1); |
54 | fontSDF.texture = LoadTextureFromImage(atlas); |
55 | UnloadImage(atlas); |
56 | |
57 | // Load SDF required shader (we use default vertex shader) |
58 | Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/sdf.fs" , GLSL_VERSION)); |
59 | SetTextureFilter(fontSDF.texture, FILTER_BILINEAR); // Required for SDF font |
60 | |
61 | Vector2 fontPosition = { 40, screenHeight/2 - 50 }; |
62 | Vector2 textSize = { 0.0f, 0.0f }; |
63 | float fontSize = 16.0f; |
64 | int currentFont = 0; // 0 - fontDefault, 1 - fontSDF |
65 | |
66 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
67 | //-------------------------------------------------------------------------------------- |
68 | |
69 | // Main game loop |
70 | while (!WindowShouldClose()) // Detect window close button or ESC key |
71 | { |
72 | // Update |
73 | //---------------------------------------------------------------------------------- |
74 | fontSize += GetMouseWheelMove()*8.0f; |
75 | |
76 | if (fontSize < 6) fontSize = 6; |
77 | |
78 | if (IsKeyDown(KEY_SPACE)) currentFont = 1; |
79 | else currentFont = 0; |
80 | |
81 | if (currentFont == 0) textSize = MeasureTextEx(fontDefault, msg, fontSize, 0); |
82 | else textSize = MeasureTextEx(fontSDF, msg, fontSize, 0); |
83 | |
84 | fontPosition.x = GetScreenWidth()/2 - textSize.x/2; |
85 | fontPosition.y = GetScreenHeight()/2 - textSize.y/2 + 80; |
86 | //---------------------------------------------------------------------------------- |
87 | |
88 | // Draw |
89 | //---------------------------------------------------------------------------------- |
90 | BeginDrawing(); |
91 | |
92 | ClearBackground(RAYWHITE); |
93 | |
94 | if (currentFont == 1) |
95 | { |
96 | // NOTE: SDF fonts require a custom SDf shader to compute fragment color |
97 | BeginShaderMode(shader); // Activate SDF font shader |
98 | DrawTextEx(fontSDF, msg, fontPosition, fontSize, 0, BLACK); |
99 | EndShaderMode(); // Activate our default shader for next drawings |
100 | |
101 | DrawTexture(fontSDF.texture, 10, 10, BLACK); |
102 | } |
103 | else |
104 | { |
105 | DrawTextEx(fontDefault, msg, fontPosition, fontSize, 0, BLACK); |
106 | DrawTexture(fontDefault.texture, 10, 10, BLACK); |
107 | } |
108 | |
109 | if (currentFont == 1) DrawText("SDF!" , 320, 20, 80, RED); |
110 | else DrawText("default font" , 315, 40, 30, GRAY); |
111 | |
112 | DrawText("FONT SIZE: 16.0" , GetScreenWidth() - 240, 20, 20, DARKGRAY); |
113 | DrawText(FormatText("RENDER SIZE: %02.02f" , fontSize), GetScreenWidth() - 240, 50, 20, DARKGRAY); |
114 | DrawText("Use MOUSE WHEEL to SCALE TEXT!" , GetScreenWidth() - 240, 90, 10, DARKGRAY); |
115 | |
116 | DrawText("HOLD SPACE to USE SDF FONT VERSION!" , 340, GetScreenHeight() - 30, 20, MAROON); |
117 | |
118 | EndDrawing(); |
119 | //---------------------------------------------------------------------------------- |
120 | } |
121 | |
122 | // De-Initialization |
123 | //-------------------------------------------------------------------------------------- |
124 | UnloadFont(fontDefault); // Default font unloading |
125 | UnloadFont(fontSDF); // SDF font unloading |
126 | |
127 | UnloadShader(shader); // Unload SDF shader |
128 | |
129 | CloseWindow(); // Close window and OpenGL context |
130 | //-------------------------------------------------------------------------------------- |
131 | |
132 | return 0; |
133 | } |