From 5348dbdef1c55c076c8603bad98d20c73085d12b Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Sun, 15 Jun 2025 11:10:31 +1200 Subject: Add proper ground/platforms using Rects - may be swapped for textures later --- src/main.cpp | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index c170abe..b2a48d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,7 +54,6 @@ SDL_Color veryDarkGrey = {30, 30, 47, 255}; const char *globalFontFile = "assets/fonts/OpenSans-Regular.ttf"; // Forward declarations -int l_move_player(lua_State *L); int l_keycode_from_string(lua_State *L); int l_set_background_image(lua_State *L); int l_queue_texture_for_render(lua_State *L); @@ -88,6 +87,11 @@ struct QueuedText SDL_Color color = {255, 255, 255, 255}; }; +struct QueuedRect { + SDL_Rect rect; + SDL_Color color; +}; + std::vector textureList = {}; std::map textureCache; @@ -96,17 +100,9 @@ std::vector buttonList = {}; std::vector textList = {}; std::map, TTF_Font *> textCache; -SDL_Rect rect = {WIDTH / 2, HEIGHT / 2, 50, 50}; -SDL_Texture *background_texture = nullptr; +std::vector rectList = {}; -int l_move_player(lua_State *L) -{ - int dx = luaL_checkinteger(L, 1); - int dy = luaL_checkinteger(L, 2); - rect.x += dx; - rect.y += dy; - return 0; -} +SDL_Texture *background_texture = nullptr; int l_keycode_from_string(lua_State *L) { @@ -137,6 +133,21 @@ int l_queue_button_for_render(lua_State *L) return 0; } +int l_queue_rect_for_render(lua_State* L) { + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); + int w = luaL_checkinteger(L, 3); + int h = luaL_checkinteger(L, 4); + Uint8 r = luaL_checkinteger(L, 5); + Uint8 g = luaL_checkinteger(L, 6); + Uint8 b = luaL_checkinteger(L, 7); + Uint8 a = luaL_checkinteger(L, 8); + SDL_Rect rect = {x, y, w, h}; + SDL_Color color = {r, g, b, a}; + rectList.push_back({rect, color}); + return 0; +} + int l_queue_text_for_render(lua_State *L) { std::string text = (std::string)luaL_checkstring(L, 1); @@ -313,7 +324,6 @@ void call_lua_function(lua_State *L, const char *func_name) void expose_c_functions() { - lua_register(L, "movePlayer", l_move_player); lua_register(L, "getKeycodeByName", l_keycode_from_string); lua_register(L, "setBgImage", l_set_background_image); lua_register(L, "queueTextureForRender", l_queue_texture_for_render); @@ -322,6 +332,7 @@ void expose_c_functions() lua_register(L, "queueTextForRender", l_queue_text_for_render); lua_register(L, "getTextWidth", l_get_text_width); lua_register(L, "quitGame", l_quit_game); + lua_register(L, "queueRectForRender", l_queue_rect_for_render); } int main() @@ -479,6 +490,12 @@ int main() } buttonList.clear(); + for (const auto& rect: rectList) { + SDL_SetRenderDrawColor(renderer, rect.color.r, rect.color.g, rect.color.b, rect.color.a); + SDL_RenderFillRect(renderer, &rect.rect); + } + rectList.clear(); + for (const auto &text : textList) { std::pair key = {text.font_file, text.font_size}; -- cgit v1.2.3