aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--assets/backgrounds/city-background-1.pngbin0 -> 414702 bytes
-rw-r--r--src/main.cpp55
3 files changed, 51 insertions, 9 deletions
diff --git a/Makefile b/Makefile
index b41c9e7..d493731 100644
--- a/Makefile
+++ b/Makefile
@@ -37,9 +37,10 @@ LUA_OBJ = $(patsubst src/lua/%.c, $(BUILD_DIR)/lua/%.o, $(LUA_SRC))
ifeq ($(UNAME_S),Darwin)
TARGET_DIR = $(BUILD_DIR)/dist/$(APP_NAME).app/Contents/MacOS
+ RESOURCES_DIR = $(BUILD_DIR)/dist/$(APP_NAME).app/Contents/Resources
TARGET = $(TARGET_DIR)/$(APP_NAME)
INFO_PLIST = $(BUILD_DIR)/dist/$(APP_NAME).app/Contents/Info.plist
- APP_ICNS = $(BUILD_DIR)/dist/$(APP_NAME).app/Contents/Resources/AppIcon.icns
+ APP_ICNS = $(RESOURCES_DIR)/AppIcon.icns
MAKE_APP = true
else
TARGET_DIR = $(BUILD_DIR)/dist
@@ -76,6 +77,7 @@ ifeq ($(MAKE_APP),true)
mkdir -p $(dir $(APP_ICNS))
cp MacShit/Info.plist $(INFO_PLIST)
cp MacShit/AppIcon.icns $(APP_ICNS)
+ cp -r assets $(RESOURCES_DIR)/assets
mkdir -p $(TARGET_DIR)
cp $(MACOS_SDL_DYLIB_LOCATION) $(TARGET_DIR)/libSDL2.dylib
cp $(MACOS_SDL_TTF_DYLIB_LOCATION) $(TARGET_DIR)/libSDL2_ttf.dylib
@@ -88,6 +90,7 @@ ifeq ($(MAKE_APP),true)
otool -l $(TARGET)
endif
ifeq ($(OS),Windows_NT)
+ cp -r assets $(TARGET_DIR)/assets
cp $(NT_SDL_DLL_LOCATION) $(TARGET_DIR)/SDL2.dll
cp $(NT_SDL_TTF_DLL_LOCATION) $(TARGET_DIR)/SDL2_ttf.dll
cp $(NT_SDL_IMAGE_DLL_LOCATION) $(TARGET_DIR)/SDL2_image.dll
diff --git a/assets/backgrounds/city-background-1.png b/assets/backgrounds/city-background-1.png
new file mode 100644
index 0000000..23dd4e7
--- /dev/null
+++ b/assets/backgrounds/city-background-1.png
Binary files differ
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;
}