Add renderer.text_scaled() Lua binding

lua_bindings.cpp:
- Added new lua_renderer_text_scaled() function
- Wraps renderer->draw_string_scaled() with proper scale support
- Registered as renderer.text_scaled(x, y, text, on, scale)
- Scale parameter defaults to 1 if omitted

Updated all 4 games to use text_scaled():
- simon_says.lua: All text now uses text_scaled with scale=2
- tic_tac_toe.lua: All text now uses text_scaled with scale=2
- memory_match.lua: All text now uses text_scaled with scale=2
- 2048.lua: All text now uses text_scaled with scale=2

This properly uses the C++ renderer's scaled text rendering instead of
workarounds in Lua, providing better performance and consistency.
This commit is contained in:
Adolfo Reyna
2026-02-12 21:31:15 -05:00
parent fa7d2fd32f
commit f398d62af2
5 changed files with 55 additions and 34 deletions

View File

@@ -125,6 +125,23 @@ static int lua_renderer_text(lua_State* L) {
return 0; return 0;
} }
// renderer.text_scaled(x, y, text, on, scale)
static int lua_renderer_text_scaled(lua_State* L) {
LuaGame* game = get_game(L);
if (!game) return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
const char* text = luaL_checkstring(L, 3);
bool on = lua_toboolean(L, 4);
int scale = lua_isnone(L, 5) ? 1 : luaL_checkinteger(L, 5);
game->renderer->set_text_color(on);
game->renderer->draw_string_scaled(x, y, text, scale, 1);
return 0;
}
// renderer.triangle(x0, y0, x1, y1, x2, y2, on, filled) // renderer.triangle(x0, y0, x1, y1, x2, y2, on, filled)
static int lua_renderer_triangle(lua_State* L) { static int lua_renderer_triangle(lua_State* L) {
LuaGame* game = get_game(L); LuaGame* game = get_game(L);
@@ -281,6 +298,10 @@ void lua_bindings_register(lua_State* L, LuaGame* game) {
lua_pushcfunction(L, lua_renderer_text); lua_pushcfunction(L, lua_renderer_text);
lua_settable(L, -3); lua_settable(L, -3);
lua_pushstring(L, "text_scaled");
lua_pushcfunction(L, lua_renderer_text_scaled);
lua_settable(L, -3);
lua_pushstring(L, "triangle"); lua_pushstring(L, "triangle");
lua_pushcfunction(L, lua_renderer_triangle); lua_pushcfunction(L, lua_renderer_triangle);
lua_settable(L, -3); lua_settable(L, -3);

View File

@@ -122,8 +122,8 @@ function draw()
local start_y = get_grid_start_y() local start_y = get_grid_start_y()
if state == STATE_MENU then if state == STATE_MENU then
renderer.text(game.width() / 2 - 15, game.height() / 2 - 30, "2048", true, 2) renderer.text_scaled(game.width() / 2 - 15, game.height() / 2 - 30, "2048", true, 2)
renderer.text(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true, 2)
elseif state == STATE_PLAYING or state == STATE_WIN or state == STATE_GAME_OVER then elseif state == STATE_PLAYING or state == STATE_WIN or state == STATE_GAME_OVER then
-- Draw grid -- Draw grid
@@ -143,27 +143,27 @@ function draw()
-- Draw value (simplified) -- Draw value (simplified)
local text = tostring(value) local text = tostring(value)
if string.len(text) <= 2 then if string.len(text) <= 2 then
renderer.text(tile_x + 2, tile_y + 2, text, false, 2) renderer.text_scaled(tile_x + 2, tile_y + 2, text, false, 2)
else else
renderer.text(tile_x + 1, tile_y + 2, text, false, 2) renderer.text_scaled(tile_x + 1, tile_y + 2, text, false, 2)
end end
end end
end end
end end
-- Draw score -- Draw score
renderer.text(10, 10, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(10, 10, "Score: " .. tostring(game.vars.score), true, 2)
if state == STATE_WIN then if state == STATE_WIN then
renderer.text(game.width() / 2 - 30, game.height() / 2 - 20, "YOU WIN!", true, 2) renderer.text_scaled(game.width() / 2 - 30, game.height() / 2 - 20, "YOU WIN!", true, 2)
renderer.text(game.width() / 2 - 60, game.height() / 2, "Tap up: Continue", true, 2) renderer.text_scaled(game.width() / 2 - 60, game.height() / 2, "Tap up: Continue", true, 2)
renderer.text(game.width() / 2 - 50, game.height() / 2 + 15, "Tap down: Menu", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 + 15, "Tap down: Menu", true, 2)
end end
if state == STATE_GAME_OVER then if state == STATE_GAME_OVER then
renderer.text(game.width() / 2 - 40, game.height() / 2, "GAME OVER", true, 2) renderer.text_scaled(game.width() / 2 - 40, game.height() / 2, "GAME OVER", true, 2)
renderer.text(game.width() / 2 - 30, game.height() / 2 + 15, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(game.width() / 2 - 30, game.height() / 2 + 15, "Score: " .. tostring(game.vars.score), true, 2)
renderer.text(game.width() / 2 - 60, game.height() / 2 + 30, "Tap to Menu", true, 2) renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 30, "Tap to Menu", true, 2)
end end
end end
end end

View File

@@ -134,9 +134,9 @@ function draw()
local start_y = get_grid_start_y() local start_y = get_grid_start_y()
if state == STATE_MENU then if state == STATE_MENU then
renderer.text(game.width() / 2 - 40, game.height() / 2 - 40, "MEMORY MATCH", true, 2) renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 40, "MEMORY MATCH", true, 2)
renderer.text(game.width() / 2 - 50, game.height() / 2 - 10, "Find all pairs", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 - 10, "Find all pairs", true, 2)
renderer.text(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Start", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Start", true, 2)
elseif state == STATE_PLAYING or state == STATE_GAME_OVER then elseif state == STATE_PLAYING or state == STATE_GAME_OVER then
-- Draw grid -- Draw grid
@@ -151,7 +151,7 @@ function draw()
-- Show card value -- Show card value
renderer.rect(x, y, card_size, card_size, true, true) renderer.rect(x, y, card_size, card_size, true, true)
local text = tostring(card.id) local text = tostring(card.id)
renderer.text(x + math.floor(card_size / 2 - 2), y + math.floor(card_size / 2 - 2), text, false, 2) renderer.text_scaled(x + math.floor(card_size / 2 - 2), y + math.floor(card_size / 2 - 2), text, false, 2)
else else
-- Face down (outline) -- Face down (outline)
renderer.rect(x, y, card_size, card_size, true, false) renderer.rect(x, y, card_size, card_size, true, false)
@@ -160,12 +160,12 @@ function draw()
end end
-- Draw stats -- Draw stats
renderer.text(10, 10, "Pairs: " .. tostring(game.vars.score) .. "/" .. tostring((GRID_COLS * GRID_ROWS) / 2), true, 2) renderer.text_scaled(10, 10, "Pairs: " .. tostring(game.vars.score) .. "/" .. tostring((GRID_COLS * GRID_ROWS) / 2), true, 2)
renderer.text(10, 25, "Moves: " .. tostring(game.vars.moves), true, 2) renderer.text_scaled(10, 25, "Moves: " .. tostring(game.vars.moves), true, 2)
if state == STATE_GAME_OVER then if state == STATE_GAME_OVER then
renderer.text(game.width() / 2 - 40, 5, "YOU WIN!", true, 2) renderer.text_scaled(game.width() / 2 - 40, 5, "YOU WIN!", true, 2)
renderer.text(game.width() / 2 - 50, game.height() - 20, "Tap to Menu", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() - 20, "Tap to Menu", true, 2)
end end
end end
end end

View File

@@ -148,9 +148,9 @@ function draw()
local buttons = get_buttons() local buttons = get_buttons()
if state == STATE_MENU then if state == STATE_MENU then
renderer.text(game.width() / 2 - 40, game.height() / 2 - 40, "SIMON SAYS", true, 2) renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 40, "SIMON SAYS", true, 2)
renderer.text(game.width() / 2 - 50, game.height() / 2 - 10, "Repeat the sequence", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 - 10, "Repeat the sequence", true, 2)
renderer.text(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Start", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Start", true, 2)
else else
-- Draw buttons with highlight -- Draw buttons with highlight
@@ -161,16 +161,16 @@ function draw()
end end
-- Draw score -- Draw score
renderer.text(10, 10, "Level: " .. tostring(game.vars.score + 1), true, 2) renderer.text_scaled(10, 10, "Level: " .. tostring(game.vars.score + 1), true, 2)
if state == STATE_PLAYING and game.vars.waiting_for_input then if state == STATE_PLAYING and game.vars.waiting_for_input then
renderer.text(game.width() / 2 - 40, game.height() - 20, "Your turn!", true, 2) renderer.text_scaled(game.width() / 2 - 40, game.height() - 20, "Your turn!", true, 2)
end end
if state == STATE_GAME_OVER then if state == STATE_GAME_OVER then
renderer.text(game.width() / 2 - 40, game.height() / 2 - 20, "GAME OVER", true, 2) renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 20, "GAME OVER", true, 2)
renderer.text(game.width() / 2 - 30, game.height() / 2, "Level: " .. tostring(game.vars.score + 1), true, 2) renderer.text_scaled(game.width() / 2 - 30, game.height() / 2, "Level: " .. tostring(game.vars.score + 1), true, 2)
renderer.text(game.width() / 2 - 60, game.height() / 2 + 20, "Tap to Restart", true, 2) renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 20, "Tap to Restart", true, 2)
end end
end end
end end

View File

@@ -128,9 +128,9 @@ function draw()
local cell_spacing = 2 local cell_spacing = 2
if state == STATE_MENU then if state == STATE_MENU then
renderer.text(game.width() / 2 - 50, game.height() / 2 - 30, "TIC-TAC-TOE", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 - 30, "TIC-TAC-TOE", true, 2)
renderer.text(game.width() / 2 - 50, game.height() / 2, "Play vs AI", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2, "Play vs AI", true, 2)
renderer.text(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Start", true, 2) renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Start", true, 2)
elseif state == STATE_PLAYING or state == STATE_GAME_OVER then elseif state == STATE_PLAYING or state == STATE_GAME_OVER then
-- Draw grid -- Draw grid
@@ -158,14 +158,14 @@ function draw()
if state == STATE_GAME_OVER then if state == STATE_GAME_OVER then
if game.vars.winner == 1 then if game.vars.winner == 1 then
renderer.text(game.width() / 2 - 30, 10, "YOU WIN!", true, 2) renderer.text_scaled(game.width() / 2 - 30, 10, "YOU WIN!", true, 2)
elseif game.vars.winner == 2 then elseif game.vars.winner == 2 then
renderer.text(game.width() / 2 - 30, 10, "AI WINS!", true, 2) renderer.text_scaled(game.width() / 2 - 30, 10, "AI WINS!", true, 2)
else else
renderer.text(game.width() / 2 - 20, 10, "DRAW!", true, 2) renderer.text_scaled(game.width() / 2 - 20, 10, "DRAW!", true, 2)
end end
renderer.text(game.width() / 2 - 60, game.height() - 15, "Tap to Menu", true, 2) renderer.text_scaled(game.width() / 2 - 60, game.height() - 15, "Tap to Menu", true, 2)
end end
end end
end end