diff options
| author | jussi | 2023-10-28 14:15:20 +0300 |
|---|---|---|
| committer | jussi | 2023-10-28 14:15:20 +0300 |
| commit | af03c7364ea0dfe2c8bb269eb8a8f9b580f39633 (patch) | |
| tree | 8ef09eefe38c890112972768c6861432028d8945 /src | |
| parent | 23935aefca3212c989199cd7e195c02b01ef14ae (diff) | |
| download | reilua-enhanced-af03c7364ea0dfe2c8bb269eb8a8f9b580f39633.tar.gz reilua-enhanced-af03c7364ea0dfe2c8bb269eb8a8f9b580f39633.tar.bz2 reilua-enhanced-af03c7364ea0dfe2c8bb269eb8a8f9b580f39633.zip | |
New object type Font.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lua_core.c | 34 | ||||
| -rw-r--r-- | src/rgui.c | 22 | ||||
| -rw-r--r-- | src/state.c | 15 | ||||
| -rw-r--r-- | src/text.c | 303 | ||||
| -rw-r--r-- | src/textures.c | 2 |
5 files changed, 118 insertions, 258 deletions
diff --git a/src/lua_core.c b/src/lua_core.c index d388c88..385ec93 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -122,6 +122,24 @@ static void defineShader() { lua_setfield( L, -2, "__gc" ); } +/* Font. */ +static int gcFont( lua_State *L ) { + Font *font = luaL_checkudata ( L, 1, "Font" ); + printf( "gcFont\n" ); + + UnloadFont( *font ); +} + +static void defineFont() { + lua_State *L = state->luaState; + + luaL_newmetatable( L, "Font" ); + lua_pushvalue( L, -1 ); + lua_setfield( L, -2, "__index" ); + lua_pushcfunction( L, gcFont ); + lua_setfield( L, -2, "__gc" ); +} + /* Assing globals. */ static void assignGlobalInt( int value, const char *name ) { @@ -161,6 +179,9 @@ static void defineGlobals() { lua_setglobal( L, "RL" ); lua_getglobal( L, "RL" ); + uluaPushFont( L, GetFontDefault() ); + lua_setfield( L, -2, "fontDefault" ); + /*DOC_START*/ /* ConfigFlags */ assignGlobalInt( FLAG_VSYNC_HINT, "FLAG_VSYNC_HINT" ); @@ -1130,10 +1151,8 @@ bool luaInit( int argn, const char **argc ) { if ( L == NULL ) { TraceLog( LOG_WARNING, "%s", "Failed to init Lua" ); - return false; } - defineGlobals(); /* Define object types. */ defineBuffer(); defineImage(); @@ -1142,6 +1161,9 @@ bool luaInit( int argn, const char **argc ) { defineCamera2D(); defineCamera3D(); defineShader(); + defineFont(); + /* Define globals. */ + defineGlobals(); /* Set arguments. */ lua_getglobal( L, "RL" ); @@ -1733,10 +1755,10 @@ void luaRegister() { /* Text. */ /* Loading. */ + assingGlobalFunction( "GetFontDefault", ltextGetFontDefault ); assingGlobalFunction( "LoadFont", ltextLoadFont ); assingGlobalFunction( "LoadFontEx", ltextLoadFontEx ); assingGlobalFunction( "LoadFontFromImage", ltextLoadFontFromImage ); - assingGlobalFunction( "UnloadFont", ltextUnloadFont ); /* Drawing. */ assingGlobalFunction( "DrawFPS", ltextDrawFPS ); assingGlobalFunction( "DrawText", ltextDrawText ); @@ -2871,6 +2893,12 @@ void uluaPushShader( lua_State *L, Shader shader ) { luaL_setmetatable( L, "Shader" ); } +void uluaPushFont( lua_State *L, Font font ) { + Font *fontP = lua_newuserdata( L, sizeof( Font ) ); + *fontP = font; + luaL_setmetatable( L, "Font" ); +} + int uluaGetTableLen( lua_State *L ) { return uluaGetTableLenIndex( L, lua_gettop( L ) ); } @@ -129,26 +129,16 @@ int lguiGuiGetState( lua_State *L ) { */ /* -> success = RL.GuiSetFont( Font font ) +> RL.GuiSetFont( Font font ) -Set gui custom font ( global state ) - -- Failure return false -- Success return true +Set gui custom font (global state) */ int lguiGuiSetFont( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GuiSetFont( Font font )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t fontId = lua_tointeger( L, 1 ); - state->guiFont = fontId; + Font *font = luaL_checkudata( L, 1, "Font" ); - GuiSetFont( *state->fonts[ fontId ] ); - lua_pushboolean( L, true ); + GuiSetFont( *font ); - return 1; + return 0; } /* @@ -159,7 +149,7 @@ Get gui custom font ( global state ) - Success return int */ int lguiGuiGetFont( lua_State *L ) { - lua_pushinteger( L, state->guiFont ); + uluaPushFont( L, GuiGetFont() ); return 1; } diff --git a/src/state.c b/src/state.c index da065a5..010210e 100644 --- a/src/state.c +++ b/src/state.c @@ -16,12 +16,7 @@ bool stateInit( int argn, const char **argc, const char *exePath ) { state->run = true; state->resolution = (Vector2){ 800, 600 }; state->luaState = NULL; - state->guiFont = 0; state->logLevelInvalid = LOG_ERROR; - /* Fonts. */ - state->fontAlloc = ALLOC_PAGE_SIZE; - state->fontCount = 1; - state->fonts = malloc( state->fontAlloc * sizeof( Font* ) ); /* Waves. */ state->waveAlloc = ALLOC_PAGE_SIZE; state->waveCount = 0; @@ -65,7 +60,6 @@ bool stateInit( int argn, const char **argc, const char *exePath ) { /* The ones we want to save the first. */ if ( 0 < i ) { - state->fonts[i] = NULL; state->materials[i] = NULL; } } @@ -73,8 +67,6 @@ bool stateInit( int argn, const char **argc, const char *exePath ) { /* Has to be after InitWindod where opengl context is created. */ state->materials[0] = malloc( sizeof( Material ) ); *state->materials[0] = LoadMaterialDefault(); - state->fonts[0] = malloc( sizeof( Font ) ); - *state->fonts[0] = GetFontDefault(); if ( !IsWindowReady() ) { state->hasWindow = false; @@ -94,12 +86,6 @@ void stateInitInterpret( int argn, const char **argc ) { } void stateFree() { - for ( int i = 0; i < state->fontCount; ++i ) { - if ( state->fonts[i] != NULL ) { - UnloadFont( *state->fonts[i] ); - free( state->fonts[i] ); - } - } for ( int i = 0; i < state->waveCount; ++i ) { if ( state->waves[i] != NULL ) { UnloadWave( *state->waves[i] ); @@ -165,7 +151,6 @@ void stateFree() { if ( state->hasWindow ) { CloseWindow(); } - free( state->fonts ); free( state->waves ); free( state->sounds ); free( state->musics ); @@ -4,103 +4,77 @@ #include "textures.h" #include "lua_core.h" -static void checkFontRealloc( int i ) { - if ( i == state->fontCount ) { - state->fontCount++; - } - - if ( state->fontCount == state->fontAlloc ) { - state->fontAlloc += ALLOC_PAGE_SIZE; - state->fonts = realloc( state->fonts, state->fontAlloc * sizeof( Font* ) ); - - for ( i = state->fontCount; i < state->fontAlloc; i++ ) { - state->fonts[i] = NULL; - } - } -} - -bool validFont( size_t id ) { - if ( id < 0 || state->fontCount < id || state->fonts[ id ] == NULL ) { - TraceLog( state->logLevelInvalid, "%s %d", "Invalid font", id ); - return false; - } - else { - return true; - } -} +/* +## Text - Loading +*/ -static int newFont() { - int i = 0; +/* +> RL.GetFontDefault() - for ( i = 0; i < state->fontCount; i++ ) { - if ( state->fonts[i] == NULL ) { - break; - } - } - state->fonts[i] = malloc( sizeof( Font ) ); - checkFontRealloc( i ); +Get the default Font +*/ +int ltextGetFontDefault( lua_State *L ) { + uluaPushFont( L, GetFontDefault() ); - return i; + return 1; } /* -## Text - Loading -*/ - -/* > font = RL.LoadFont( string fileName ) -Load font from file into GPU memory ( VRAM ) +Load font from file into GPU memory (VRAM) -- Failure return -1 -- Success return int +- Failure return nil +- Success return Font */ int ltextLoadFont( lua_State *L ) { - if ( !lua_isstring( L, 1 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.LoadFont( string fileName )" ); - lua_pushinteger( L, -1 ); - return 1; - } - if ( FileExists( lua_tostring( L, 1 ) ) ) { - int i = newFont(); - *state->fonts[i] = LoadFont( lua_tostring( L, 1 ) ); - lua_pushinteger( L, i ); - return 1; - } - else { - lua_pushinteger( L, -1 ); + if ( FileExists( luaL_checkstring( L, 1 ) ) ) { + uluaPushFont( L, LoadFont( lua_tostring( L, 1 ) ) ); + return 1; } + TraceLog( state->logLevelInvalid, "Invalid file path '%s'", lua_tostring( L, 1 ) ); + lua_pushnil( L ); return 1; } /* -> font = RL.LoadFontEx( string fileName, int fontSize ) +> font = RL.LoadFontEx( string fileName, int fontSize, int fontChars{} ) Load font from file with extended parameters. Loading the default character set -- Failure return -1 -- Success return int +- Failure return nil +- Success return Font */ int ltextLoadFontEx( lua_State *L ) { - if ( !lua_isstring( L, 1 ) || !lua_isnumber( L, 2 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.LoadFontEx( string fileName, int fontSize )" ); - lua_pushinteger( L, -1 ); - return 1; - } - int fontSize = lua_tointeger( L, 2 ); + int fontSize = luaL_checkinteger( L, 2 ); + + if ( FileExists( luaL_checkstring( L, 1 ) ) ) { + if ( lua_istable( L, 3 ) ) { + int glyphCount = uluaGetTableLenIndex( L, 3 ); + int fontChars[ glyphCount ]; + + int t = lua_gettop( L ); + int i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + fontChars[i] = lua_tointeger( L, -1 ); + + i++; + lua_pop( L, 1 ); + } + uluaPushFont( L, LoadFontEx( lua_tostring( L, 1 ), fontSize, fontChars, glyphCount ) ); + + return 0; + } + uluaPushFont( L, LoadFontEx( lua_tostring( L, 1 ), fontSize, NULL, 0 ) ); - if ( FileExists( lua_tostring( L, 1 ) ) ) { - int i = newFont(); - *state->fonts[i] = LoadFontEx( lua_tostring( L, 1 ), fontSize, NULL, 0 ); - lua_pushinteger( L, i ); - return 1; - } - else { - lua_pushinteger( L, -1 ); return 1; } + TraceLog( state->logLevelInvalid, "Invalid file path '%s'", lua_tostring( L, 1 ) ); + lua_pushnil( L ); return 1; } @@ -110,44 +84,14 @@ int ltextLoadFontEx( lua_State *L ) { Load font from Image ( XNA style ) -- Failure return -1 -- Success return int +- Success return Font */ int ltextLoadFontFromImage( lua_State *L ) { Image *image = luaL_checkudata( L, 1, "Image" ); Color key = uluaGetColorIndex( L, 2 ); - int firstChar = lua_tointeger( L, 3 ); - - int i = newFont(); - *state->fonts[i] = LoadFontFromImage( *image, key, firstChar ); - lua_pushinteger( L, i ); - - return 1; -} - -/* -> success = RL.UnloadFont( Font font ) - -Unload Font from GPU memory ( VRAM ) + int firstChar = luaL_checkinteger( L, 3 ); -- Failure return false -- Success return true -*/ -int ltextUnloadFont( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.UnloadFont( Font font )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t FontId = lua_tointeger( L, 1 ); - - if ( !validFont( FontId ) ) { - lua_pushboolean( L, false ); - return 1; - } - UnloadFont( *state->fonts[ FontId ] ); - state->fonts[ FontId ] = NULL; - lua_pushboolean( L, true ); + uluaPushFont( L, LoadFontFromImage( *image, key, firstChar ) ); return 1; } @@ -157,89 +101,52 @@ int ltextUnloadFont( lua_State *L ) { */ /* -> success = RL.DrawFPS( Vector2 pos ) +> RL.DrawFPS( Vector2 pos ) Draw current FPS - -- Failure return false -- Success return true */ int ltextDrawFPS( lua_State *L ) { - if ( !lua_istable( L, 1 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.DrawFPS( Vector2 pos )" ); - lua_pushboolean( L, false ); - return 1; - } Vector2 pos = uluaGetVector2Index( L, 1 ); DrawFPS( pos.x, pos.y ); - lua_pushboolean( L, true ); - return 1; + return 0; } /* -> success = RL.DrawText( Font font, string text, Vector2 position, float fontSize, float spacing, Color tint ) +> RL.DrawText( Font font, string text, Vector2 position, float fontSize, float spacing, Color tint ) Draw text using font and additional parameters - -- Failure return false -- Success return true */ int ltextDrawText( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) || !lua_isstring( L, 2 ) || !lua_istable( L, 3 ) - || !lua_isnumber( L, 4 ) || !lua_isnumber( L, 5 ) || !lua_istable( L, 6 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.DrawText( Font font, string text, Vector2 position, float fontSize, float spacing, Color tint )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t fontId = lua_tointeger( L, 1 ); + Font *font = luaL_checkudata( L, 1, "Font" ); Vector2 position = uluaGetVector2Index( L, 3 ); - float fontSize = lua_tonumber( L, 4 ); - float spacing = lua_tonumber( L, 5 ); + float fontSize = luaL_checknumber( L, 4 ); + float spacing = luaL_checknumber( L, 5 ); Color tint = uluaGetColorIndex( L, 6 ); - if ( !validFont( fontId ) ) { - lua_pushboolean( L, false ); - return 1; - } - DrawTextEx( *state->fonts[ fontId ], lua_tostring( L, 2 ), position, fontSize, spacing, tint ); - lua_pushboolean( L, true ); + DrawTextEx( *font, luaL_checkstring( L, 2 ), position, fontSize, spacing, tint ); - return 1; + return 0; } /* -> success = RL.DrawTextPro( Font font, string text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint ) +> 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 ) - -- Failure return false -- Success return true +Draw text using Font and pro parameters (rotation) */ int ltextDrawTextPro( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) || !lua_isstring( L, 2 ) || !lua_istable( L, 3 ) || !lua_istable( L, 4 ) - || !lua_isnumber( L, 5 ) || !lua_isnumber( L, 6 ) || !lua_isnumber( L, 7 ) || !lua_istable( L, 8 ) ) { - TraceLog( state->logLevelInvalid, "%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; - } - size_t fontId = lua_tointeger( L, 1 ); + Font *font = luaL_checkudata( L, 1, "Font" ); Vector2 position = uluaGetVector2Index( L, 3 ); Vector2 origin = uluaGetVector2Index( L, 4 ); - float rotation = lua_tonumber( L, 5 ); - float fontSize = lua_tonumber( L, 6 ); - float spacing = lua_tonumber( L, 7 ); + float rotation = luaL_checknumber( L, 5 ); + float fontSize = luaL_checknumber( L, 6 ); + float spacing = luaL_checknumber( L, 7 ); Color tint = uluaGetColorIndex( L, 8 ); - if ( !validFont( fontId ) ) { - lua_pushboolean( L, false ); - return 1; - } - DrawTextPro( *state->fonts[ fontId ], lua_tostring( L, 2 ), position, origin, rotation, fontSize, spacing, tint ); - lua_pushboolean( L, true ); + DrawTextPro( *font, luaL_checkstring( L, 2 ), position, origin, rotation, fontSize, spacing, tint ); - return 1; + return 0; } /* @@ -251,24 +158,14 @@ int ltextDrawTextPro( lua_State *L ) { Measure string size for Font -- Failure return false - Success return Vector2 */ int ltextMeasureText( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) || !lua_isstring( L, 2 ) || !lua_isnumber( L, 3 ) || !lua_isnumber( L, 4 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.MeasureText( Font font, string text, float fontSize, float spacing )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t fontId = lua_tointeger( L, 1 ); - float fontSize = lua_tonumber( L, 3 ); - float spacing = lua_tonumber( L, 4 ); + Font *font = luaL_checkudata( L, 1, "Font" ); + float fontSize = luaL_checknumber( L, 3 ); + float spacing = luaL_checknumber( L, 4 ); - if ( !validFont( fontId ) ) { - lua_pushboolean( L, false ); - return 1; - } - uluaPushVector2( L, MeasureTextEx( *state->fonts[ fontId ], lua_tostring( L, 2 ), fontSize, spacing ) ); + uluaPushVector2( L, MeasureTextEx( *font, luaL_checkstring( L, 2 ), fontSize, spacing ) ); return 1; } @@ -276,24 +173,14 @@ int ltextMeasureText( lua_State *L ) { /* > baseSize = RL.GetFontBaseSize( Font font ) -Get font base size ( default chars height ) +Get font base size (default chars height) -- Failure return false - Success return int */ int ltextGetFontBaseSize( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GetFontBaseSize( Font font )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t fontId = lua_tointeger( L, 1 ); + Font *font = luaL_checkudata( L, 1, "Font" ); - if ( !validFont( fontId ) ) { - lua_pushboolean( L, false ); - return 1; - } - lua_pushinteger( L, state->fonts[ fontId ]->baseSize ); + lua_pushinteger( L, font->baseSize ); return 1; } @@ -303,22 +190,12 @@ int ltextGetFontBaseSize( lua_State *L ) { Get font number of glyph characters -- Failure return false - Success return int */ int ltextGetFontGlyphCount( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GetFontGlyphCount( Font font )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t fontId = lua_tointeger( L, 1 ); + Font *font = luaL_checkudata( L, 1, "Font" ); - if ( !validFont( fontId ) ) { - lua_pushboolean( L, false ); - return 1; - } - lua_pushinteger( L, state->fonts[ fontId ]->glyphCount ); + lua_pushinteger( L, font->glyphCount ); return 1; } @@ -328,47 +205,27 @@ int ltextGetFontGlyphCount( lua_State *L ) { Get font padding around the glyph characters -- Failure return false - Success return int */ int ltextGetFontGlyphPadding( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GetFontGlyphPadding( Font font )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t fontId = lua_tointeger( L, 1 ); + Font *font = luaL_checkudata( L, 1, "Font" ); - if ( !validFont( fontId ) ) { - lua_pushboolean( L, false ); - return 1; - } - lua_pushinteger( L, state->fonts[ fontId ]->glyphPadding ); + lua_pushinteger( L, font->glyphPadding ); return 1; } /* -> textureTable = RL.GetFontTexture( Font font ) +> texture = RL.GetFontTexture( Font font ) -Get font texture atlas containing the glyphs. NOTE! Texture in table form. +Get font texture atlas containing the glyphs. -- Failure return false -- Success return table +- Success return Texture */ int ltextGetFontTexture( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) ) { - TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.GetFontTexture( Font font )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t fontId = lua_tointeger( L, 1 ); + Font *font = luaL_checkudata( L, 1, "Font" ); - if ( !validFont( fontId ) ) { - lua_pushboolean( L, false ); - return 1; - } - uluaPushTexture( L, state->fonts[ fontId ]->texture ); + uluaPushTexture( L, font->texture ); return 1; } diff --git a/src/textures.c b/src/textures.c index 28cb1cb..2121d12 100644 --- a/src/textures.c +++ b/src/textures.c @@ -924,7 +924,7 @@ Load texture from file into GPU memory ( VRAM ) - Success return Texture */ int ltexturesLoadTexture( lua_State *L ) { - if ( FileExists( lua_tostring( L, 1 ) ) ) { + if ( FileExists( luaL_checkstring( L, 1 ) ) ) { uluaPushTexture( L, LoadTexture( lua_tostring( L, 1 ) ) ); return 1; |
