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 | |
| parent | debe4baa8c208458f847dd4c89c17f7cc39be559 (diff) | |
| download | reilua-enhanced-26a11a4b7f32a6fc2d131e4c78fe1ca40cc6ac8a.tar.gz reilua-enhanced-26a11a4b7f32a6fc2d131e4c78fe1ca40cc6ac8a.tar.bz2 reilua-enhanced-26a11a4b7f32a6fc2d131e4c78fe1ca40cc6ac8a.zip | |
Measure text.
| -rw-r--r-- | API.md | 30 | ||||
| -rw-r--r-- | examples/bunnymark/main.lua | 6 | ||||
| -rw-r--r-- | examples/gui/main.lua | 1 | ||||
| -rw-r--r-- | examples/window/main.lua | 9 | ||||
| -rw-r--r-- | include/core.h | 1 | ||||
| -rw-r--r-- | include/text.h | 3 | ||||
| -rw-r--r-- | src/core.c | 11 | ||||
| -rw-r--r-- | src/lua_core.c | 4 | ||||
| -rw-r--r-- | src/text.c | 78 |
9 files changed, 135 insertions, 8 deletions
@@ -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 --- diff --git a/examples/bunnymark/main.lua b/examples/bunnymark/main.lua index 50b8f11..8672887 100644 --- a/examples/bunnymark/main.lua +++ b/examples/bunnymark/main.lua @@ -10,7 +10,7 @@ Bunny.__index = Bunny function Bunny:new( pos, spd, col ) local bunny = {} - setmetatable( bunny,Bunny ) + setmetatable( bunny, Bunny ) bunny.position = pos bunny.speed = spd bunny.color = col @@ -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 diff --git a/examples/gui/main.lua b/examples/gui/main.lua index 079bd95..c799cdb 100644 --- a/examples/gui/main.lua +++ b/examples/gui/main.lua @@ -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 diff --git a/examples/window/main.lua b/examples/window/main.lua index ec25dd3..44d9fa7 100644 --- a/examples/window/main.lua +++ b/examples/window/main.lua @@ -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 diff --git a/include/core.h b/include/core.h index 7a0655a..cead79f 100644 --- a/include/core.h +++ b/include/core.h @@ -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 ); diff --git a/include/text.h b/include/text.h index c1e7b3e..a07b320 100644 --- a/include/text.h +++ b/include/text.h @@ -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 ); @@ -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; +} |
