diff options
| author | jussi | 2022-03-10 17:53:43 +0200 |
|---|---|---|
| committer | jussi | 2022-03-10 17:53:43 +0200 |
| commit | 26a11a4b7f32a6fc2d131e4c78fe1ca40cc6ac8a (patch) | |
| tree | b6fc68889bf5df58ba3455e6d64da6b2f78d38de /src | |
| parent | debe4baa8c208458f847dd4c89c17f7cc39be559 (diff) | |
| download | reilua-enhanced-26a11a4b7f32a6fc2d131e4c78fe1ca40cc6ac8a.tar.gz reilua-enhanced-26a11a4b7f32a6fc2d131e4c78fe1ca40cc6ac8a.tar.bz2 reilua-enhanced-26a11a4b7f32a6fc2d131e4c78fe1ca40cc6ac8a.zip | |
Measure text.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core.c | 11 | ||||
| -rw-r--r-- | src/lua_core.c | 4 | ||||
| -rw-r--r-- | src/text.c | 78 |
3 files changed, 91 insertions, 2 deletions
@@ -302,6 +302,17 @@ int lcoreSetWindowTitle( lua_State *L ) { } /* +> 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. */ @@ -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 ) { @@ -62,6 +63,46 @@ int ltextLoadFont( lua_State *L ) { } /* +> 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 ) Unload Font from GPU memory ( VRAM ) @@ -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; +} |
