Measure text.

This commit is contained in:
jussi
2022-03-10 17:53:43 +02:00
parent debe4baa8c
commit 26a11a4b7f
9 changed files with 135 additions and 8 deletions

30
API.md
View File

@@ -641,6 +641,12 @@ Set title for window ( Only PLATFORM_DESKTOP )
---
> RL_lcoreCloseWindow()
Close window and unload OpenGL context and free all resources
---
## Core - Timing
---
@@ -1975,6 +1981,15 @@ Load font from file into GPU memory ( VRAM )
---
> font = RL_LoadFontFromImage( Image image, Color key, int firstChar )
Load font from Image ( XNA style )
- Failure return -1
- Success return int
---
> success = RL_UnloadFont( Font font )
Unload Font from GPU memory ( VRAM )
@@ -2006,7 +2021,7 @@ Draw text using font and additional parameters
---
> success = RL_DrawTextPro( Font font, const char text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint )
> success = RL_DrawTextPro( Font font, string text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint )
Draw text using Font and pro parameters ( rotation )
@@ -2015,6 +2030,19 @@ Draw text using Font and pro parameters ( rotation )
---
## Text - Misc
---
> size = RL_MeasureText( Font font, string text, float fontSize, float spacing )
Measure string size for Font
- Failure return false
- Success return Vector2
---
## Models - Basic
---

View File

@@ -24,7 +24,7 @@ local texSize = { 0, 0 }
local texBunny = -1
local bunnies = {}
function Bunny:update( texture )
function Bunny:update()
self.position[1] = self.position[1] + self.speed[1]
self.position[2] = self.position[2] + self.speed[2]
@@ -65,7 +65,7 @@ function process( delta )
end
-- Update bunnies
for i = 1, #bunnies do
bunnies[i]:update( texBunny )
bunnies[i]:update()
end
end

View File

@@ -31,6 +31,7 @@ function draw()
if RL_GuiButton( { 112, 16, 96, 32 }, "Button" ) then
print( "Button pressed!" )
RL_CloseWindow()
end
if windowOpen and RL_GuiWindowBox( { 300, 16, 200, 320 }, "Window" ) then

View File

@@ -1,5 +1,7 @@
local textColor = BLACK
local textPos = { 192, 200 }
local imageFont = -1
local text = "Congrats! You created your first window!"
function init()
RL_SetWindowTitle( "First window" )
@@ -7,8 +9,11 @@ end
function process( delta )
if RL_IsKeyPressed( KEY_ENTER ) then
local textSize = RL_MeasureText( 0, text, 20, 2 )
local winSize = RL_GetWindowSize()
textColor = BLUE
textPos = { 230, 230 }
textPos = { winSize[1] / 2 - textSize[1] / 2, winSize[2] / 2 - textSize[2] / 2 }
end
if RL_IsKeyPressed( KEY_SPACE ) then
@@ -19,5 +24,5 @@ end
function draw()
RL_ClearBackground( RAYWHITE )
RL_DrawText( 0, "Congrats! You created your first window!", textPos, 20, 2, textColor )
RL_DrawText( 0, text, textPos, 20, 2, textColor )
end

View File

@@ -16,6 +16,7 @@ int lcoreClearWindowState( lua_State *L );
int lcoreIsWindowResized( lua_State *L );
int lcoreSetWindowIcon( lua_State *L );
int lcoreSetWindowTitle( lua_State *L );
int lcoreCloseWindow( lua_State *L );
/* Timing. */
int lcoreSetTargetFPS( lua_State *L );
int lcoreGetFrameTime( lua_State *L );

View File

@@ -4,8 +4,11 @@
bool validFont( size_t id );
/* Loading. */
int ltextLoadFont( lua_State *L );
int ltextLoadFontFromImage( lua_State *L );
int ltextUnloadFont( lua_State *L );
/* Drawing. */
int ltextDrawFPS( lua_State *L );
int ltextDrawText( lua_State *L );
int ltextDrawTextPro( lua_State *L );
/* Misc. */
int ltextMeasureText( lua_State *L );

View File

@@ -301,6 +301,17 @@ int lcoreSetWindowTitle( lua_State *L ) {
return 1;
}
/*
> RL_lcoreCloseWindow()
Close window and unload OpenGL context and free all resources
*/
int lcoreCloseWindow( lua_State *L ) {
state->run = false;
return 0;
}
/*
## Core - Timing
*/

View File

@@ -397,6 +397,7 @@ void luaRegister() {
lua_register( L, "RL_IsWindowResized", lcoreIsWindowResized );
lua_register( L, "RL_SetWindowIcon", lcoreSetWindowIcon );
lua_register( L, "RL_SetWindowTitle", lcoreSetWindowTitle );
lua_register( L, "RL_CloseWindow", lcoreCloseWindow );
/* Timing. */
lua_register( L, "RL_SetTargetFPS", lcoreSetTargetFPS );
lua_register( L, "RL_GetFrameTime", lcoreGetFrameTime );
@@ -634,11 +635,14 @@ void luaRegister() {
/* Text. */
/* Loading. */
lua_register( L, "RL_LoadFont", ltextLoadFont );
lua_register( L, "RL_LoadFontFromImage", ltextLoadFontFromImage );
lua_register( L, "RL_UnloadFont", ltextUnloadFont );
/* Drawing. */
lua_register( L, "RL_DrawFPS", ltextDrawFPS );
lua_register( L, "RL_DrawText", ltextDrawText );
lua_register( L, "RL_DrawTextPro", ltextDrawTextPro );
/* Misc. */
lua_register( L, "RL_MeasureText", ltextMeasureText );
/* Audio. */
/* Sound. */

View File

@@ -1,6 +1,7 @@
#include "main.h"
#include "state.h"
#include "text.h"
#include "textures.h"
#include "lua_core.h"
static void checkFontRealloc( int i ) {
@@ -61,6 +62,46 @@ int ltextLoadFont( lua_State *L ) {
return 1;
}
/*
> font = RL_LoadFontFromImage( Image image, Color key, int firstChar )
Load font from Image ( XNA style )
- Failure return -1
- Success return int
*/
int ltextLoadFontFromImage( lua_State *L ) {
if ( !lua_isnumber( L, -3 ) || !lua_istable( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadFontFromImage( Image image, Color key, int firstChar )" );
lua_pushinteger( L, -1 );
return 1;
}
int i = 0;
for ( i = 0; i < state->fontCount; i++ ) {
if ( state->fonts[i] == NULL ) {
break;
}
}
int firstChar = lua_tointeger( L, -1 );
lua_pop( L, 1 );
Color key = uluaGetColor( L );
lua_pop( L, 1 );
size_t imageId = lua_tointeger( L, -1 );
if ( !validImage( imageId ) ) {
lua_pushboolean( L, false );
return 1;
}
state->fonts[i] = malloc( sizeof( Font ) );
*state->fonts[i] = LoadFontFromImage( *state->images[ imageId ], key, firstChar );
lua_pushinteger( L, i );
checkFontRealloc( i );
return 1;
}
/*
> success = RL_UnloadFont( Font font )
@@ -150,7 +191,7 @@ int ltextDrawText( lua_State *L ) {
}
/*
> success = RL_DrawTextPro( Font font, const char text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint )
> success = RL_DrawTextPro( Font font, string text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint )
Draw text using Font and pro parameters ( rotation )
@@ -160,7 +201,7 @@ Draw text using Font and pro parameters ( rotation )
int ltextDrawTextPro( lua_State *L ) {
if ( !lua_isnumber( L, -8 ) || !lua_isstring( L, -7 ) || !lua_istable( L, -6 ) || !lua_istable( L, -5 )
|| !lua_isnumber( L, -4 ) || !lua_isnumber( L, -3 ) || !lua_isnumber( L, -2 ) || !lua_istable( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_DrawTextPro( Font font, const char text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint )" );
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_DrawTextPro( Font font, string text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint )" );
lua_pushboolean( L, false );
return 1;
}
@@ -187,3 +228,36 @@ int ltextDrawTextPro( lua_State *L ) {
return 1;
}
/*
## Text - Misc
*/
/*
> size = RL_MeasureText( Font font, string text, float fontSize, float spacing )
Measure string size for Font
- Failure return false
- Success return Vector2
*/
int ltextMeasureText( lua_State *L ) {
if ( !lua_isnumber( L, -4 ) || !lua_isstring( L, -3 ) || !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_MeasureText( Font font, string text, float fontSize, float spacing )" );
lua_pushboolean( L, false );
return 1;
}
float spacing = lua_tonumber( L, -1 );
lua_pop( L, 1 );
float fontSize = lua_tonumber( L, -1 );
lua_pop( L, 1 );
size_t fontId = lua_tointeger( L, -2 );
if ( !validFont( fontId ) ) {
lua_pushboolean( L, false );
return 1;
}
uluaPushVector2( L, MeasureTextEx( *state->fonts[ fontId ], lua_tostring( L, -1 ), fontSize, spacing ) );
return 1;
}