aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp61
1 files changed, 16 insertions, 45 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 16c88d7..f5c399d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,12 +1,16 @@
-#define SDL_MAIN_HANDLED // Prevents SDL from shitting the Windows cross-compiler and causing a "WinMain" error
+// TOTL Studios (TM)
+// Showdown of the Sticks - developed by Arslaan Pathan ([email protected])
+#include "lua/lua.hpp"
+#include <iostream>
+// Prevents SDL from shitting the Windows cross-compiler and causing a "WinMain" error
+// At least that's what I think - I'm not sure what this does but if I remove it the code won't work
+// If it ain't broke don't fix it
+#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
-#include "lua/lua.hpp"
-#include <unistd.h>
-#include <iostream>
#define WIDTH 1280
#define HEIGHT 720
@@ -18,31 +22,6 @@ SDL_Event event;
SDL_Rect rect = {WIDTH/2, HEIGHT/2, 50, 50};
SDL_Texture* background_texture = nullptr;
-// const char* bgImagePath;
-
-
-constexpr char* LUA_FILE_MAIN_MENU = R"(
-movementSpeed = 3;
-
-function Setup()
- setBgImage("assets/backgrounds/city-background-1.png")
-end
-
-function Update()
- if keys[getKeycodeByName("RIGHT")] or keys[getKeycodeByName("D")] then
- movePlayer(movementSpeed, 0)
- end
- if keys[getKeycodeByName("LEFT")] or keys[getKeycodeByName("A")] then
- movePlayer(movementSpeed * -1, 0)
- end
- if keys[getKeycodeByName("DOWN")] or keys[getKeycodeByName("S")] then
- movePlayer(0, movementSpeed)
- end
- if keys[getKeycodeByName("UP")] or keys[getKeycodeByName("W")] then
- movePlayer(0, movementSpeed * -1)
- end
-end
-)";
int l_move_rect(lua_State* L) {
int dx = luaL_checkinteger(L, 1);
@@ -54,24 +33,16 @@ int l_move_rect(lua_State* L) {
int l_keycode_from_string(lua_State* L) {
const char* keystr = luaL_checkstring(L, 1);
-
- int scancode = -2;
-
- if (strcmp(keystr, "RIGHT") == 0) scancode = SDL_SCANCODE_RIGHT;
- else if (strcmp(keystr, "LEFT") == 0) scancode = SDL_SCANCODE_LEFT;
- else if (strcmp(keystr, "UP") == 0) scancode = SDL_SCANCODE_UP;
- else if (strcmp(keystr, "DOWN") == 0) scancode = SDL_SCANCODE_DOWN;
- else if (strcmp(keystr, "D") == 0) scancode = SDL_SCANCODE_D;
- else if (strcmp(keystr, "A") == 0) scancode = SDL_SCANCODE_A;
- else if (strcmp(keystr, "S") == 0) scancode = SDL_SCANCODE_S;
- else if (strcmp(keystr, "W") == 0) scancode = SDL_SCANCODE_W;
-
- scancode += 1;
-
- lua_pushinteger(L, scancode);
+ SDL_Scancode sc = SDL_GetScancodeFromName(keystr);
+ if (sc == SDL_SCANCODE_UNKNOWN) {
+ lua_pushinteger(L, -1);
+ } else {
+ lua_pushinteger(L, sc + 1);
+ }
return 1;
}
+
int l_set_background_image(lua_State* L) {
const char* bgImagePath = luaL_checkstring(L, 1);
SDL_Surface* temp_surface = IMG_Load(bgImagePath);
@@ -123,7 +94,7 @@ int main() {
lua_register(L, "movePlayer", l_move_rect);
lua_register(L, "getKeycodeByName", l_keycode_from_string);
lua_register(L, "setBgImage", l_set_background_image);
- luaL_dostring(L, LUA_FILE_MAIN_MENU);
+ luaL_dofile(L, "assets/scripts/mainMenu.lua");
bool keys[SDL_NUM_SCANCODES] = {false};
bool running = true;