aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 16c88d7ee7b8c17f5dc69b19469f4cd8e56c57bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#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 "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_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);
    int dy = luaL_checkinteger(L, 2);
    rect.x += dx;
    rect.y += dy;
    return 0;
}

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);
    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
    
    for (int i = 0; i < SDL_NUM_SCANCODES; i++) {
        lua_pushinteger(L, i + 1);
        lua_pushboolean(L, keys[i]);
        lua_settable(L, -3);
    }
}

void call_lua_function(lua_State* L, const char* func_name) {
    lua_getglobal(L, func_name);
    if (lua_isfunction(L, -1)) {
        if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
            std::cerr << "Lua Error in " << func_name << ": " << lua_tostring(L, -1) << std::endl;
            lua_pop(L, 1);
        }
    } else {
        lua_pop(L, 1);
    }
}


int main() {
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    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);

    bool keys[SDL_NUM_SCANCODES] = {false};
    bool running = true;

    push_keys_to_lua(L, keys);
    lua_setglobal(L, "keys");

    SDL_Init(SDL_INIT_EVERYTHING);
    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) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }

            if (event.type == SDL_KEYDOWN) {
                keys[event.key.keysym.scancode] = true;
            }
            if (event.type == SDL_KEYUP) {
                keys[event.key.keysym.scancode] = false;
            }
        }

        push_keys_to_lua(L, keys);
        lua_setglobal(L, "keys");

        call_lua_function(L, "Update");

        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);

        SDL_RenderPresent(renderer);
        SDL_Delay(10);
    }

    lua_close(L);
    if (background_texture) {
        SDL_DestroyTexture(background_texture);
    }
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}