Code: Select all
/'
*******************************************************************************************
*
* 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 once "raylib.bi"
#if defined( PLATFORM_DESKTOP )
#define GLSL_VERSION 330
#else '' PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#endif
const as integer _
screenWidth => 800, _
screenHeight => 450
InitWindow( _
screenWidth, _
screenHeight, "raylib [text] example - SDF fonts" )
dim as string _
msg => "Signed Distance Fields"
'const char msg[50] = "Signed Distance Fields";
'' Default font generation from TTF font
dim as Font _
fontDefault
with fontDefault
.baseSize => 16
.charsCount => 95
/'
Parameters:
font size: 16
no chars array provided (0)
chars count: 95 (autogenerate chars array)
'/
.chars => LoadFontData( _
"fonts/resources/AnonymousPro-Bold.ttf", _
16, 0, 95, FONT_DEFAULT )
/'
Parameters:
chars count: 95
font size: 16
chars padding in image: 4 px
pack method: 0 (default)
'/
dim as Image _
atlas => GenImageFontAtlas( _
fontDefault.chars, _
@fontDefault.recs, _
95, 16, 4, 0)
.texture => LoadTextureFromImage( atlas )
UnloadImage( atlas )
end with
'' SDF font generation from TTF font
dim as Font _
fontSDF
with fontSDF
.baseSize => 16
.charsCount => 95
/'
Parameters:
font size: 16
no chars array provided (0)
chars count: 0 (defaults to 95)
'/
.chars => LoadFontData( _
"fonts/resources/AnonymousPro-Bold.ttf", _
16, 0, 0, FONT_SDF )
/'
Parameters:
chars count: 95
font size: 16
chars padding in image: 0 px
pack method: 1 (Skyline algorythm)
'/
dim as Image _
atlas => GenImageFontAtlas( _
.chars, _
@.recs, _
95, 16, 0, 1 )
.texture => LoadTextureFromImage( atlas )
UnloadImage( atlas )
end with
'' Load SDF required shader (we use default vertex shader)
dim as Shader _
shader => LoadShader( _
0, _
FormatText( _
"fonts/resources/shaders/glsl%i/sdf.fs", GLSL_VERSION ) )
'' Required for SDF font
SetTextureFilter( _
fontSDF.texture, FILTER_BILINEAR)
var _
fontPosition => Vector2( _
40.0!, screenHeight / 2 - 50 ), _
textSize => Vector2( 0.0!, 0.0! )
dim as single _
fontSize => 16.0!
'' 0 - fontDefault, 1 - fontSDF
dim as integer _
currentFont => 0
SetTargetFPS( 60 )
do while( not WindowShouldClose() )
'' Update
fontSize +=> GetMouseWheelMove() * 8.0!
fontSize => iif( fontSize < 6.0!, 6.0!, fontSize )
currentFont => iif( IsKeyDown( KEY_SPACE ), 1, 0 )
if( currentFont = 0 ) then
textSize => MeasureTextEx( fontDefault, strPtr( msg ), fontSize, 0 )
else
textSize => MeasureTextEx( fontSDF, strPtr( msg ), fontSize, 0 )
end if
fontPosition.x => GetScreenWidth() / 2 - textSize.x / 2
fontPosition.y => GetScreenHeight() / 2 - textSize.y / 2 + 80
'' Render
BeginDrawing()
ClearBackground( RAYWHITE )
if( currentFont = 1 ) then
/'
NOTE: SDF fonts require a custom SDf shader to compute fragment color
Activate SDF font shader
'/
BeginShaderMode( shader )
DrawTextEx( _
fontSDF, strPtr( msg ), fontPosition, fontSize, 0, BLACK )
'' Activate our default shader for next drawings
EndShaderMode()
DrawTexture( _
fontSDF.texture, 10, 10, BLACK )
else
DrawTextEx( _
fontDefault, strPtr( msg ), fontPosition, fontSize, 0, BLACK )
DrawTexture( _
fontDefault.texture, 10, 10, BLACK )
end if
if( currentFont = 1 ) then
DrawText( "SDF!", 320, 20, 80, RED )
else
DrawText( "default font", 315, 40, 30, GRAY )
end if
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 )
DrawText( _
"HOLD SPACE to USE SDF FONT VERSION!", _
340, GetScreenHeight() - 30, 20, MAROON )
EndDrawing()
loop
UnloadFont( fontDefault )
UnloadFont( fontSDF )
UnloadShader( shader )
CloseWindow()
Note: you need resources to run this sample. These are located in the repo, in the
folder. Download the repo and just dump the folder on a local
one. Or simply change the code to point to the one on the repo. Whichever is fine.