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
|
#include <SDL2/SDL.h>
#include <iostream>
#include "lua/lua.hpp"
#define WIDTH 1280
#define HEIGHT 720
#define KEYSYM event.key.keysym.sym
SDL_Rect rect = {WIDTH/2, HEIGHT/2, 50, 50};
constexpr char* LUA_FILE = R"(
movementSpeed = 3;
function Setup()
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;
}
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);
luaL_dostring(L, LUA_FILE);
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
SDL_Event event;
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");
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_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
SDL_Delay(10);
}
lua_close(L);
return 0;
}
|