Repository navigation
Expand file tree
/
Copy pathsubhunt.cpp
More file actions
153 lines (106 loc) · 4.28 KB
/
Copy pathsubhunt.cpp
File metadata and controls
153 lines (106 loc) · 4.28 KB
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
#include "./includes/rendererV1.hpp"
#include <lua.hpp>
// TODO: make the background retangles relative to the screen size, also with spawning
class Subhunt: public groover::Renderer {
private:
void BackgroundDraw() {
for(int i=150; i<700; i+=100) {
this->SetColor(50 , (int)266 - i / 3, 200);
this->DrawRec(groover::point{0.0f, (float)i}, groover::point{(float)winWidth, (float)i+100});
}
}
void IttrRects() {
// we have a table of tables not rn so ignore
lua_pushvalue(L, -1);
lua_pushnil(L);
float points[4];
int counter = 0;
this->SetColor(83, 83, 83);
while(lua_next(L, -2) != 0) {
int key_v = lua_tonumber(L, -2);
float value = lua_tonumber(L, -1);
points[counter] = value;
counter += 1;
if (counter == 4) {
this->DrawRec(groover::point{ points[0], points[1] }, groover::point{ points[2], points[3] } );
counter = 0;
}
lua_pop(L, 1);
}
lua_pop(L, 1);
// Stack is now the same as it was on entry to this function
}
void IttrTris() {
lua_pushvalue(L, -1);
lua_pushnil(L);
float points[6];
int counter = 0;
while(lua_next(L, -2) != 0) {
int key_v = lua_tonumber(L, -2);
float value = lua_tonumber(L, -1);
points[counter] = value;
counter += 1;
if (counter == 6) {
this->DrawTriangle( groover::point{ points[0], points[1] },
groover::point{ points[2], points[3] },
groover::point{ points[4], points[5] } );
counter = 0;
}
lua_pop(L, 1);
}
lua_pop(L, 1);
// Stack is now the same as it was on entry to this function
}
public:
int key_send = -2;
lua_State* L;
bool Startup() override {
L = luaL_newstate();
luaL_openlibs(L);
// we have all lua libraries
luaL_dofile(L, "./lua/subhunt.lua");
// white background
this->SetBackgroundColor(255,255,255);
return true;
}
bool Update(float deltaTime) override {
this->BackgroundDraw();
lua_pushnumber(L, deltaTime);
lua_setglobal(L, "delta_time");
lua_pushnumber(L, winWidth);
lua_setglobal(L, "screen_width");
lua_pushnumber(L, winHeight);
lua_setglobal(L, "screen_height");
lua_getglobal(L, "main");
lua_pushinteger(L, key_send); // has to be after main lets lua know what key we are pressing
lua_pcall(L, 1, 0, 0);
// calling main function and setting delta_time, screen_width, screen_height
lua_getglobal(L, "get_rects"); // maybe need to function then
lua_call(L, 0, 1);
this->IttrRects();
lua_getglobal(L, "get_tris");
lua_call(L, 0, 1);
this->IttrTris();
lua_pop(L, lua_gettop(L)); // clean stack if extra stuff
return true;
}
};
Subhunt renderer;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if(key == GLFW_KEY_LEFT && (action == GLFW_PRESS | action == GLFW_REPEAT)) {
renderer.key_send = 0;
}
else if(key == GLFW_KEY_RIGHT && (action == GLFW_PRESS | action == GLFW_REPEAT)) {
renderer.key_send = 1;
}
else if(key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
renderer.key_send = 2;
}
else { renderer.key_send = -1; }
}
int main() {
renderer.CreateWin("Subhunt", 1200, 800, false);
glfwSetKeyCallback(renderer.window, key_callback);
renderer.Run();
return 0;
}