2018-07-15 21:04:12 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [text] example - TTF loading and usage
|
|
|
|
*
|
|
|
|
* This example has been created using raylib 1.3.0 (www.raylib.com)
|
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
2019-05-17 02:17:40 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
#define GLSL_VERSION 330
|
|
|
|
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
|
|
|
|
#define GLSL_VERSION 100
|
|
|
|
#endif
|
|
|
|
|
BREAKING CHANGE: Read description
Changes:
- Font structure has been redesigned, CharInfo structure contained character rectangle within font texture, it has not much sense, considering that it was an information relative to the font atlas generated and not the character itself, so character rectangles have been moved out from CharInfo to Font.
- CharInfo included a data parameters to contain character pixel data (usually grayscale), generated on TTF font generation. It was inconsistent with other fonts types, so, now CharInfo includes directly an Image of the glyph.
- REDESIGNED: GenImageFontAtlas(), additional recs parameter added, loaded and filled inside the function to export atlas characters rectangles, instead of silently modify the input CharInfo data.
- REVIEWED: ImageTextEx(), funtion retrieved the font atlas image from the GPU, that was slow and problematic in several platforms. Now it uses directly the CharInfo image. Support for unicode codepoints has also been added.
- REDESIGNED: ImageDraw(), now it includes an additional parameter, the color tint, not only it could be useful for several situations but also function signature is more consistent with similar functions.
- ADDED: ImageFromImage() to generate a new image from a piece of another image.
- REVIEWED: GetNextCodepoint(), renamed parameters to be more clear.
Also all examples and games that were affected by those changes have been reviewed.
2019-07-24 16:05:14 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
int main(void)
|
2018-07-15 21:04:12 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2018-07-15 21:04:12 +03:00
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [text] example - SDF fonts");
|
|
|
|
|
|
|
|
// NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
const char msg[50] = "Signed Distance Fields";
|
|
|
|
|
|
|
|
// Default font generation from TTF font
|
|
|
|
Font fontDefault = { 0 };
|
|
|
|
fontDefault.baseSize = 16;
|
|
|
|
fontDefault.charsCount = 95;
|
|
|
|
// Parameters > font size: 16, no chars array provided (0), chars count: 95 (autogenerate chars array)
|
2018-09-06 17:56:21 +03:00
|
|
|
fontDefault.chars = LoadFontData("resources/AnonymousPro-Bold.ttf", 16, 0, 95, FONT_DEFAULT);
|
2018-07-15 21:04:12 +03:00
|
|
|
// Parameters > chars count: 95, font size: 16, chars padding in image: 4 px, pack method: 0 (default)
|
BREAKING CHANGE: Read description
Changes:
- Font structure has been redesigned, CharInfo structure contained character rectangle within font texture, it has not much sense, considering that it was an information relative to the font atlas generated and not the character itself, so character rectangles have been moved out from CharInfo to Font.
- CharInfo included a data parameters to contain character pixel data (usually grayscale), generated on TTF font generation. It was inconsistent with other fonts types, so, now CharInfo includes directly an Image of the glyph.
- REDESIGNED: GenImageFontAtlas(), additional recs parameter added, loaded and filled inside the function to export atlas characters rectangles, instead of silently modify the input CharInfo data.
- REVIEWED: ImageTextEx(), funtion retrieved the font atlas image from the GPU, that was slow and problematic in several platforms. Now it uses directly the CharInfo image. Support for unicode codepoints has also been added.
- REDESIGNED: ImageDraw(), now it includes an additional parameter, the color tint, not only it could be useful for several situations but also function signature is more consistent with similar functions.
- ADDED: ImageFromImage() to generate a new image from a piece of another image.
- REVIEWED: GetNextCodepoint(), renamed parameters to be more clear.
Also all examples and games that were affected by those changes have been reviewed.
2019-07-24 16:05:14 +03:00
|
|
|
Image atlas = GenImageFontAtlas(fontDefault.chars, &fontDefault.recs, 95, 16, 4, 0);
|
2018-07-15 21:04:12 +03:00
|
|
|
fontDefault.texture = LoadTextureFromImage(atlas);
|
|
|
|
UnloadImage(atlas);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
// SDF font generation from TTF font
|
|
|
|
Font fontSDF = { 0 };
|
|
|
|
fontSDF.baseSize = 16;
|
|
|
|
fontSDF.charsCount = 95;
|
|
|
|
// Parameters > font size: 16, no chars array provided (0), chars count: 0 (defaults to 95)
|
2018-09-06 17:56:21 +03:00
|
|
|
fontSDF.chars = LoadFontData("resources/AnonymousPro-Bold.ttf", 16, 0, 0, FONT_SDF);
|
2018-07-15 21:04:12 +03:00
|
|
|
// Parameters > chars count: 95, font size: 16, chars padding in image: 0 px, pack method: 1 (Skyline algorythm)
|
BREAKING CHANGE: Read description
Changes:
- Font structure has been redesigned, CharInfo structure contained character rectangle within font texture, it has not much sense, considering that it was an information relative to the font atlas generated and not the character itself, so character rectangles have been moved out from CharInfo to Font.
- CharInfo included a data parameters to contain character pixel data (usually grayscale), generated on TTF font generation. It was inconsistent with other fonts types, so, now CharInfo includes directly an Image of the glyph.
- REDESIGNED: GenImageFontAtlas(), additional recs parameter added, loaded and filled inside the function to export atlas characters rectangles, instead of silently modify the input CharInfo data.
- REVIEWED: ImageTextEx(), funtion retrieved the font atlas image from the GPU, that was slow and problematic in several platforms. Now it uses directly the CharInfo image. Support for unicode codepoints has also been added.
- REDESIGNED: ImageDraw(), now it includes an additional parameter, the color tint, not only it could be useful for several situations but also function signature is more consistent with similar functions.
- ADDED: ImageFromImage() to generate a new image from a piece of another image.
- REVIEWED: GetNextCodepoint(), renamed parameters to be more clear.
Also all examples and games that were affected by those changes have been reviewed.
2019-07-24 16:05:14 +03:00
|
|
|
atlas = GenImageFontAtlas(fontSDF.chars, &fontSDF.recs, 95, 16, 0, 1);
|
2018-07-15 21:04:12 +03:00
|
|
|
fontSDF.texture = LoadTextureFromImage(atlas);
|
|
|
|
UnloadImage(atlas);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
// Load SDF required shader (we use default vertex shader)
|
2019-05-17 02:17:40 +03:00
|
|
|
Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/sdf.fs", GLSL_VERSION));
|
2018-07-15 21:04:12 +03:00
|
|
|
SetTextureFilter(fontSDF.texture, FILTER_BILINEAR); // Required for SDF font
|
|
|
|
|
|
|
|
Vector2 fontPosition = { 40, screenHeight/2 - 50 };
|
2019-05-27 01:18:15 +03:00
|
|
|
Vector2 textSize = { 0.0f, 0.0f };
|
2018-07-15 21:04:12 +03:00
|
|
|
float fontSize = 16.0f;
|
2019-05-20 17:36:42 +03:00
|
|
|
int currentFont = 0; // 0 - fontDefault, 1 - fontSDF
|
2018-07-15 21:04:12 +03:00
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
fontSize += GetMouseWheelMove()*8.0f;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
if (fontSize < 6) fontSize = 6;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
if (IsKeyDown(KEY_SPACE)) currentFont = 1;
|
|
|
|
else currentFont = 0;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
if (currentFont == 0) textSize = MeasureTextEx(fontDefault, msg, fontSize, 0);
|
|
|
|
else textSize = MeasureTextEx(fontSDF, msg, fontSize, 0);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
fontPosition.x = GetScreenWidth()/2 - textSize.x/2;
|
|
|
|
fontPosition.y = GetScreenHeight()/2 - textSize.y/2 + 80;
|
|
|
|
//----------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
ClearBackground(RAYWHITE);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
if (currentFont == 1)
|
|
|
|
{
|
|
|
|
// NOTE: SDF fonts require a custom SDf shader to compute fragment color
|
|
|
|
BeginShaderMode(shader); // Activate SDF font shader
|
|
|
|
DrawTextEx(fontSDF, msg, fontPosition, fontSize, 0, BLACK);
|
|
|
|
EndShaderMode(); // Activate our default shader for next drawings
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
DrawTexture(fontSDF.texture, 10, 10, BLACK);
|
|
|
|
}
|
2019-05-20 17:36:42 +03:00
|
|
|
else
|
2018-07-15 21:04:12 +03:00
|
|
|
{
|
|
|
|
DrawTextEx(fontDefault, msg, fontPosition, fontSize, 0, BLACK);
|
|
|
|
DrawTexture(fontDefault.texture, 10, 10, BLACK);
|
|
|
|
}
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:24:18 +03:00
|
|
|
if (currentFont == 1) DrawText("SDF!", 320, 20, 80, RED);
|
|
|
|
else DrawText("default font", 315, 40, 30, GRAY);
|
2018-07-15 21:04:12 +03:00
|
|
|
|
|
|
|
DrawText("FONT SIZE: 16.0", GetScreenWidth() - 240, 20, 20, DARKGRAY);
|
|
|
|
DrawText(FormatText("RENDER SIZE: %02.02f", fontSize), GetScreenWidth() - 240, 50, 20, DARKGRAY);
|
|
|
|
DrawText("Use MOUSE WHEEL to SCALE TEXT!", GetScreenWidth() - 240, 90, 10, DARKGRAY);
|
|
|
|
|
2019-05-31 17:42:16 +03:00
|
|
|
DrawText("HOLD SPACE to USE SDF FONT VERSION!", 340, GetScreenHeight() - 30, 20, MAROON);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
UnloadFont(fontDefault); // Default font unloading
|
|
|
|
UnloadFont(fontSDF); // SDF font unloading
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
UnloadShader(shader); // Unload SDF shader
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-15 21:04:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|