From 26a11a4b7f32a6fc2d131e4c78fe1ca40cc6ac8a Mon Sep 17 00:00:00 2001 From: jussi Date: Thu, 10 Mar 2022 17:53:43 +0200 Subject: Measure text. --- src/core.c | 11 +++++++++ src/lua_core.c | 4 +++ src/text.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 91 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index cd7316e..c5ae5c6 100644 --- a/src/core.c +++ b/src/core.c @@ -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 */ diff --git a/src/lua_core.c b/src/lua_core.c index ece4c97..e594a2f 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -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. */ diff --git a/src/text.c b/src/text.c index 1089c1b..ace5e20 100644 --- a/src/text.c +++ b/src/text.c @@ -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; +} -- cgit v1.2.3