aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: f5c399d1f4ada118ac12e23118316434127c444f (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
// 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>

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

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);
    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);
    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_dofile(L, "assets/scripts/mainMenu.lua");

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