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/text.c | |
| 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/text.c')
| -rw-r--r-- | src/text.c | 303 |
1 files changed, 80 insertions, 223 deletions
@@ -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; } |
