2016-11-18 16:05:49 +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)
|
|
|
|
*
|
2017-03-19 14:52:13 +03:00
|
|
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
2016-11-18 16:05:49 +03:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
int screenWidth = 800;
|
|
|
|
int screenHeight = 450;
|
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [text] example - ttf loading");
|
|
|
|
|
2018-05-04 17:59:48 +03:00
|
|
|
const char msg[50] = "TTF Font";
|
2016-11-18 16:05:49 +03:00
|
|
|
|
|
|
|
// NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
|
|
|
|
|
2018-05-04 17:59:48 +03:00
|
|
|
// TTF Font loading with custom generation parameters
|
|
|
|
Font font = LoadFontEx("resources/KAISG.ttf", 96, 0, 0);
|
2016-11-22 14:15:58 +03:00
|
|
|
|
|
|
|
// Generate mipmap levels to use trilinear filtering
|
|
|
|
// NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR
|
|
|
|
GenTextureMipmaps(&font.texture);
|
2016-11-18 16:05:49 +03:00
|
|
|
|
2017-02-05 04:59:39 +03:00
|
|
|
float fontSize = font.baseSize;
|
2018-07-12 12:29:41 +03:00
|
|
|
Vector2 fontPosition = { 40, screenHeight/2 - 80 };
|
2016-11-18 16:05:49 +03:00
|
|
|
Vector2 textSize;
|
|
|
|
|
2016-11-22 14:15:58 +03:00
|
|
|
SetTextureFilter(font.texture, FILTER_POINT);
|
2016-11-18 16:05:49 +03:00
|
|
|
int currentFontFilter = 0; // FILTER_POINT
|
|
|
|
|
2017-05-16 16:23:01 +03:00
|
|
|
// NOTE: Drag and drop support only available for desktop platforms: Windows, Linux, OSX
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2016-11-18 16:05:49 +03:00
|
|
|
int count = 0;
|
|
|
|
char **droppedFiles;
|
2017-05-16 16:23:01 +03:00
|
|
|
#endif
|
2016-11-18 16:05:49 +03:00
|
|
|
|
|
|
|
SetTargetFPS(60);
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
fontSize += GetMouseWheelMove()*4.0f;
|
|
|
|
|
|
|
|
// Choose font texture filter method
|
|
|
|
if (IsKeyPressed(KEY_ONE))
|
|
|
|
{
|
|
|
|
SetTextureFilter(font.texture, FILTER_POINT);
|
|
|
|
currentFontFilter = 0;
|
|
|
|
}
|
|
|
|
else if (IsKeyPressed(KEY_TWO))
|
|
|
|
{
|
|
|
|
SetTextureFilter(font.texture, FILTER_BILINEAR);
|
|
|
|
currentFontFilter = 1;
|
|
|
|
}
|
|
|
|
else if (IsKeyPressed(KEY_THREE))
|
|
|
|
{
|
2016-11-22 14:15:58 +03:00
|
|
|
// NOTE: Trilinear filter won't be noticed on 2D drawing
|
2016-11-18 16:05:49 +03:00
|
|
|
SetTextureFilter(font.texture, FILTER_TRILINEAR);
|
2016-11-22 14:15:58 +03:00
|
|
|
currentFontFilter = 2;
|
2016-11-18 16:05:49 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 14:15:58 +03:00
|
|
|
textSize = MeasureTextEx(font, msg, fontSize, 0);
|
2016-11-18 16:05:49 +03:00
|
|
|
|
|
|
|
if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10;
|
|
|
|
else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10;
|
|
|
|
|
2017-05-16 16:23:01 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2016-11-18 16:05:49 +03:00
|
|
|
// Load a dropped TTF file dynamically (at current fontSize)
|
|
|
|
if (IsFileDropped())
|
|
|
|
{
|
|
|
|
droppedFiles = GetDroppedFiles(&count);
|
|
|
|
|
|
|
|
if (count == 1) // Only support one ttf file dropped
|
|
|
|
{
|
2018-05-04 17:59:48 +03:00
|
|
|
UnloadFont(font);
|
|
|
|
font = LoadFontEx(droppedFiles[0], fontSize, 0, 0);
|
2016-11-18 16:05:49 +03:00
|
|
|
ClearDroppedFiles();
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 16:23:01 +03:00
|
|
|
#endif
|
2016-11-18 16:05:49 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
|
|
|
DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY);
|
|
|
|
DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY);
|
|
|
|
DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY);
|
|
|
|
DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY);
|
|
|
|
|
2016-11-22 14:15:58 +03:00
|
|
|
DrawTextEx(font, msg, fontPosition, fontSize, 0, BLACK);
|
2016-11-18 16:05:49 +03:00
|
|
|
|
|
|
|
// TODO: It seems texSize measurement is not accurate due to chars offsets...
|
|
|
|
//DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED);
|
|
|
|
|
|
|
|
DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY);
|
|
|
|
DrawText(FormatText("Font size: %02.02f", fontSize), 20, screenHeight - 50, 10, DARKGRAY);
|
|
|
|
DrawText(FormatText("Text size: [%02.02f, %02.02f]", textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY);
|
|
|
|
DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY);
|
|
|
|
|
|
|
|
if (currentFontFilter == 0) DrawText("POINT", 570, 400, 20, BLACK);
|
|
|
|
else if (currentFontFilter == 1) DrawText("BILINEAR", 570, 400, 20, BLACK);
|
2016-11-22 14:15:58 +03:00
|
|
|
else if (currentFontFilter == 2) DrawText("TRILINEAR", 570, 400, 20, BLACK);
|
2016-11-18 16:05:49 +03:00
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2017-05-16 16:23:01 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
ClearDroppedFiles(); // Clear internal buffers
|
|
|
|
#endif
|
2018-05-04 17:59:48 +03:00
|
|
|
UnloadFont(font); // Font unloading
|
2016-11-18 16:05:49 +03:00
|
|
|
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|