aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 2bf6386..16c88d7 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,22 +1,31 @@
-#define SDL_MAIN_HANDLED // Prevents SDL from shitting the Windows cross-compiler
+#define SDL_MAIN_HANDLED // Prevents SDL from shitting the Windows cross-compiler and causing a "WinMain" error
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
-#include <iostream>
#include "lua/lua.hpp"
+#include <unistd.h>
+#include <iostream>
#define WIDTH 1280
#define HEIGHT 720
#define KEYSYM event.key.keysym.sym
+SDL_Window* window = nullptr;
+SDL_Renderer* renderer = nullptr;
+SDL_Event event;
+
SDL_Rect rect = {WIDTH/2, HEIGHT/2, 50, 50};
+SDL_Texture* background_texture = nullptr;
+// const char* bgImagePath;
-constexpr char* LUA_FILE = R"(
+
+constexpr char* LUA_FILE_MAIN_MENU = R"(
movementSpeed = 3;
function Setup()
+ setBgImage("assets/backgrounds/city-background-1.png")
end
function Update()
@@ -63,6 +72,28 @@ int l_keycode_from_string(lua_State* L) {
return 1;
}
+int l_set_background_image(lua_State* L) {
+ const char* bgImagePath = luaL_checkstring(L, 1);
+ SDL_Surface* temp_surface = IMG_Load(bgImagePath);
+ if (!temp_surface) {
+ std::cerr << "Failed to load image: " << IMG_GetError() << std::endl;
+ return 0;
+ }
+
+ if (background_texture) {
+ SDL_DestroyTexture(background_texture);
+ }
+
+ background_texture = SDL_CreateTextureFromSurface(renderer, temp_surface);
+ SDL_FreeSurface(temp_surface);
+
+ if (!background_texture) {
+ std::cerr << "Failed to create texture: " << SDL_GetError() << std::endl;
+ }
+
+ return 0;
+}
+
void push_keys_to_lua(lua_State* L, bool keys[SDL_NUM_SCANCODES]) {
lua_newtable(L); // create a new table on the stack
@@ -91,11 +122,8 @@ int main() {
luaL_openlibs(L);
lua_register(L, "movePlayer", l_move_rect);
lua_register(L, "getKeycodeByName", l_keycode_from_string);
- luaL_dostring(L, LUA_FILE);
-
- SDL_Window* window = nullptr;
- SDL_Renderer* renderer = nullptr;
- SDL_Event event;
+ lua_register(L, "setBgImage", l_set_background_image);
+ luaL_dostring(L, LUA_FILE_MAIN_MENU);
bool keys[SDL_NUM_SCANCODES] = {false};
bool running = true;
@@ -107,6 +135,8 @@ int main() {
SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer);
SDL_SetWindowTitle(window, "Showdown of the Sticks");
+ IMG_Init(IMG_INIT_PNG);
+
call_lua_function(L, "Setup");
while (running) {
@@ -131,6 +161,8 @@ int main() {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
+ SDL_RenderCopy(renderer, background_texture, nullptr, nullptr);
+
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
@@ -139,5 +171,12 @@ int main() {
}
lua_close(L);
+ if (background_texture) {
+ SDL_DestroyTexture(background_texture);
+ }
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+
return 0;
}