From be39fd96344ee1f4f85ac5c63b4e5f9daf6e5171 Mon Sep 17 00:00:00 2001 From: jussi Date: Tue, 31 Oct 2023 15:24:11 +0200 Subject: GlyphInfo type. Some new text and core functions. --- src/core.c | 50 +++++++++++++++++++++++ src/lua_core.c | 25 +++++++++++- src/state.c | 1 - src/text.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 192 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index bd1ffe0..28b531d 100644 --- a/src/core.c +++ b/src/core.c @@ -125,6 +125,32 @@ int lcoreSetWindowSize( lua_State *L ) { return 0; } +/* +> RL.SetWindowOpacity( float opacity ) + +Set window opacity [0.0f..1.0f] (only PLATFORM_DESKTOP) +*/ +int lcoreSetWindowOpacity( lua_State *L ) { + float opacity = luaL_checknumber( L, 1 ); + + SetWindowOpacity( opacity ); + + return 0; +} + +/* +> windowHandle = RL.GetWindowHandle() + +Get native window handle. Return as lightuserdata + +- Success return lightuserdata +*/ +int lcoreGetWindowHandle( lua_State *L ) { + lua_pushlightuserdata( L, GetWindowHandle() ); + + return 1; +} + /* > RL.SetWindowMinSize( Vector2 size ) @@ -265,6 +291,30 @@ int lcoreSetWindowIcon( lua_State *L ) { return 0; } +/* +> RL.SetWindowIcons( Image{} images ) + +Set icon for window (multiple images, RGBA 32bit, only PLATFORM_DESKTOP) +*/ +int lcoreSetWindowIcons( lua_State *L ) { + int count = uluaGetTableLenIndex( L, 1 ); + Image images[ count ]; + + int t = 1; + int i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + images[i] = *uluaGetImage( L, lua_gettop( L ) ); + + i++; + lua_pop( L, 1 ); + } + SetWindowIcons( images, count ); + + return 0; +} + /* > RL.SetWindowTitle( string title ) diff --git a/src/lua_core.c b/src/lua_core.c index cb91583..90ed9ba 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -1504,6 +1504,8 @@ void luaRegister() { assingGlobalFunction( "SetWindowMonitor", lcoreSetWindowMonitor ); assingGlobalFunction( "SetWindowPosition", lcoreSetWindowPosition ); assingGlobalFunction( "SetWindowSize", lcoreSetWindowSize ); + assingGlobalFunction( "SetWindowOpacity", lcoreSetWindowOpacity ); + assingGlobalFunction( "GetWindowHandle", lcoreGetWindowHandle ); assingGlobalFunction( "SetWindowMinSize", lcoreSetWindowMinSize ); assingGlobalFunction( "GetMonitorPosition", lcoreGetMonitorPosition ); assingGlobalFunction( "GetMonitorSize", lcoreGetMonitorSize ); @@ -1514,6 +1516,7 @@ void luaRegister() { assingGlobalFunction( "ClearWindowState", lcoreClearWindowState ); assingGlobalFunction( "IsWindowResized", lcoreIsWindowResized ); assingGlobalFunction( "SetWindowIcon", lcoreSetWindowIcon ); + assingGlobalFunction( "SetWindowIcons", lcoreSetWindowIcons ); assingGlobalFunction( "SetWindowTitle", lcoreSetWindowTitle ); assingGlobalFunction( "GetMonitorCount", lcoreGetMonitorCount ); assingGlobalFunction( "GetCurrentMonitor", lcoreGetCurrentMonitor ); @@ -1936,9 +1939,15 @@ void luaRegister() { /* Drawing. */ assingGlobalFunction( "DrawFPS", ltextDrawFPS ); assingGlobalFunction( "DrawText", ltextDrawText ); + assingGlobalFunction( "DrawTextEx", ltextDrawTextEx ); assingGlobalFunction( "DrawTextPro", ltextDrawTextPro ); - /* Misc. */ + assingGlobalFunction( "DrawTextCodepoint", ltextDrawTextCodepoint ); + assingGlobalFunction( "DrawTextCodepoints", ltextDrawTextCodepoints ); + /* Font info functions. */ assingGlobalFunction( "MeasureText", ltextMeasureText ); + assingGlobalFunction( "GetGlyphIndex", ltextGetGlyphIndex ); + assingGlobalFunction( "GetGlyphInfo", ltextGetGlyphInfo ); + assingGlobalFunction( "GetGlyphAtlasRec", ltextGetGlyphAtlasRec ); assingGlobalFunction( "GetFontBaseSize", ltextGetFontBaseSize ); assingGlobalFunction( "GetFontGlyphCount", ltextGetFontGlyphCount ); assingGlobalFunction( "GetFontGlyphPadding", ltextGetFontGlyphPadding ); @@ -3129,6 +3138,20 @@ void uluaPushBoundingBox( lua_State *L, BoundingBox box ) { lua_rawseti( L, -2, 2 ); } +void uluaPushGlyphInfo( lua_State *L, GlyphInfo glyphInfo, Image *image ) { + lua_createtable( L, 4, 0 ); + lua_pushinteger( L, glyphInfo.value ); + lua_setfield( L, -2, "value" ); + lua_pushinteger( L, glyphInfo.offsetX ); + lua_setfield( L, -2, "offsetX" ); + lua_pushinteger( L, glyphInfo.offsetY ); + lua_setfield( L, -2, "offsetY" ); + lua_pushinteger( L, glyphInfo.advanceX ); + lua_setfield( L, -2, "advanceX" ); + lua_pushlightuserdata( L, image ); + lua_setfield( L, -2, "image" ); +} + void uluaPushBuffer( lua_State *L, Buffer buffer ) { Buffer *bufferP = lua_newuserdata( L, sizeof( Buffer ) ); *bufferP = buffer; diff --git a/src/state.c b/src/state.c index 3706561..228cbce 100644 --- a/src/state.c +++ b/src/state.c @@ -41,7 +41,6 @@ bool stateInit( int argn, const char **argc, const char *exePath ) { for ( int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++ ) { state->RLGLcurrentShaderLocs[i] = defaultShaderLocs[i]; - printf( "defaultShaderLocs[%d] %d\n", i, defaultShaderLocs[i] ); } return state->run; diff --git a/src/text.c b/src/text.c index e2135d1..c127ccf 100644 --- a/src/text.c +++ b/src/text.c @@ -42,7 +42,7 @@ int ltextLoadFont( lua_State *L ) { /* > font = RL.LoadFontEx( string fileName, int fontSize, int{} fontChars ) -Load font from file with extended parameters. Loading the default character set +Load font from file with extended parameters, use NULL for fontChars to load the default character set - Failure return nil - Success return Font @@ -67,7 +67,7 @@ int ltextLoadFontEx( lua_State *L ) { } uluaPushFont( L, LoadFontEx( lua_tostring( L, 1 ), fontSize, fontChars, glyphCount ) ); - return 0; + return 1; } uluaPushFont( L, LoadFontEx( lua_tostring( L, 1 ), fontSize, NULL, 0 ) ); @@ -142,11 +142,26 @@ int ltextDrawFPS( lua_State *L ) { } /* -> RL.DrawText( Font font, string text, Vector2 position, float fontSize, float spacing, Color tint ) +> RL.DrawText( string text, Vector2 position, float fontSize, Color tint ) -Draw text using font and additional parameters +Draw text (using default font) */ int ltextDrawText( lua_State *L ) { + Vector2 position = uluaGetVector2Index( L, 2 ); + float fontSize = luaL_checknumber( L, 3 ); + Color tint = uluaGetColorIndex( L, 4 ); + + DrawText( luaL_checkstring( L, 1 ), position.x, position.y, fontSize, tint ); + + return 0; +} + +/* +> RL.DrawTextEx( Font font, string text, Vector2 position, float fontSize, float spacing, Color tint ) + +Draw text using font and additional parameters +*/ +int ltextDrawTextEx( lua_State *L ) { Font *font = uluaGetFont( L, 1 ); Vector2 position = uluaGetVector2Index( L, 3 ); float fontSize = luaL_checknumber( L, 4 ); @@ -178,7 +193,54 @@ int ltextDrawTextPro( lua_State *L ) { } /* -## Text - Misc +> RL.DrawTextCodepoint( Font font, int codepoint, Vector2 position, float fontSize, Color tint ) + +Draw one character (codepoint) +*/ +int ltextDrawTextCodepoint( lua_State *L ) { + Font *font = uluaGetFont( L, 1 ); + int codepoint = luaL_checkinteger( L, 2 ); + Vector2 position = uluaGetVector2Index( L, 3 ); + float fontSize = luaL_checknumber( L, 4 ); + Color tint = uluaGetColorIndex( L, 5 ); + + DrawTextCodepoint( *font, codepoint, position, fontSize, tint ); + + return 0; +} + +/* +> RL.DrawTextCodepoints( Font font, int{} codepoints, Vector2 position, float fontSize, float spacing, Color tint ) + +Draw multiple character (codepoint) +*/ +int ltextDrawTextCodepoints( lua_State *L ) { + Font *font = uluaGetFont( L, 1 ); + Vector2 position = uluaGetVector2Index( L, 3 ); + float fontSize = luaL_checknumber( L, 4 ); + float spacing = luaL_checknumber( L, 5 ); + Color tint = uluaGetColorIndex( L, 6 ); + + int count = uluaGetTableLenIndex( L, 2 ); + int codepoints[ count ]; + + int t = 2; + int i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + codepoints[i] = lua_tointeger( L, -1 ); + + i++; + lua_pop( L, 1 ); + } + DrawTextCodepoints( *font, codepoints, count, position, fontSize, spacing, tint ); + + return 0; +} + +/* +## Text - Font info functions */ /* @@ -198,6 +260,56 @@ int ltextMeasureText( lua_State *L ) { return 1; } +/* +> index = RL.GetGlyphIndex( Font font, int codepoint ) + +Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found + +- Success return int +*/ +int ltextGetGlyphIndex( lua_State *L ) { + Font *font = uluaGetFont( L, 1 ); + int codepoint = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, GetGlyphIndex( *font, codepoint ) ); + + return 1; +} + +/* +> glyphInfo = RL.GetGlyphInfo( Font font, int codepoint ) + +Get glyph font info data for a codepoint (unicode character), fallback to '?' if not found. +Return Image as lightuserdata + +- Success return GlyphInfo +*/ +int ltextGetGlyphInfo( lua_State *L ) { + Font *font = uluaGetFont( L, 1 ); + int codepoint = luaL_checkinteger( L, 2 ); + + int id = GetGlyphIndex( *font, codepoint ); + uluaPushGlyphInfo( L, font->glyphs[id], &font->glyphs[id].image ); + + return 1; +} + +/* +> rect = RL.GetGlyphAtlasRec( Font font, int codepoint ) + +Get glyph rectangle in font atlas for a codepoint (unicode character), fallback to '?' if not found + +- Success return Rectangle +*/ +int ltextGetGlyphAtlasRec( lua_State *L ) { + Font *font = uluaGetFont( L, 1 ); + int codepoint = luaL_checkinteger( L, 2 ); + + uluaPushRectangle( L, GetGlyphAtlasRec( *font, codepoint ) ); + + return 1; +} + /* > baseSize = RL.GetFontBaseSize( Font font ) @@ -246,7 +358,7 @@ int ltextGetFontGlyphPadding( lua_State *L ) { /* > texture = RL.GetFontTexture( Font font ) -Get font texture atlas containing the glyphs. Returns as lightuserdata +Get font texture atlas containing the glyphs. Return as lightuserdata - Success return Texture */ -- cgit v1.2.3