-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMode.cpp
More file actions
275 lines (241 loc) · 8.61 KB
/
GameMode.cpp
File metadata and controls
275 lines (241 loc) · 8.61 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "GameMode.hpp"
#include "CratesMode.hpp"
#include "MenuMode.hpp"
#include "Load.hpp"
#include "MeshBuffer.hpp"
#include "gl_errors.hpp" //helper for dumpping OpenGL error messages
#include "read_chunk.hpp" //helper for reading a vector of structures from a file
#include "data_path.hpp" //helper to get paths relative to executable
#include "compile_program.hpp" //helper to compile opengl shader programs
#include "draw_text.hpp" //helper to... um.. draw text
#include "vertex_color_program.hpp"
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <fstream>
#include <map>
#include <cstddef>
#include <random>
MeshBuffer::Mesh tile_mesh;
MeshBuffer::Mesh cursor_mesh;
MeshBuffer::Mesh doll_mesh;
MeshBuffer::Mesh egg_mesh;
MeshBuffer::Mesh cube_mesh;
MeshBuffer::Mesh star_mesh;
Load< MeshBuffer > meshes(LoadTagDefault, [](){
MeshBuffer const *ret = new MeshBuffer(data_path("meshes.pnc"));
tile_mesh = ret->lookup("Tile");
cursor_mesh = ret->lookup("Cursor");
doll_mesh = ret->lookup("Doll");
egg_mesh = ret->lookup("Egg");
cube_mesh = ret->lookup("Cube");
return ret;
});
Load< MeshBuffer > stickochet_meshes(LoadTagDefault, [](){
MeshBuffer const *stickochet = new MeshBuffer(data_path("stickochet.pnc"));
star_mesh = stickochet->lookup("Star");
return stickochet;
});
Load< GLuint > meshes_for_vertex_color_program(LoadTagDefault, [](){
return new GLuint(meshes->make_vao_for_program(vertex_color_program->program));
});
GameMode::GameMode() {
//----------------
//set up game board with meshes and rolls:
board_meshes.reserve(board_size.x * board_size.y);
board_rotations.reserve(board_size.x * board_size.y);
std::mt19937 mt(0xbead1234);
std::vector< MeshBuffer::Mesh const * > meshes{ &doll_mesh, &star_mesh, &cube_mesh };
for (uint32_t i = 0; i < board_size.x * board_size.y; ++i) {
board_meshes.emplace_back(meshes[mt()%meshes.size()]);
board_rotations.emplace_back(glm::quat());
}
}
GameMode::~GameMode() {
}
bool GameMode::handle_event(SDL_Event const &evt, glm::uvec2 const &window_size) {
//ignore any keys that are the result of automatic key repeat:
if (evt.type == SDL_KEYDOWN && evt.key.repeat) {
return false;
}
//handle tracking the state of WSAD for roll control:
if (evt.type == SDL_KEYDOWN || evt.type == SDL_KEYUP) {
if (evt.key.keysym.scancode == SDL_SCANCODE_W) {
controls.roll_up = (evt.type == SDL_KEYDOWN);
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_S) {
controls.roll_down = (evt.type == SDL_KEYDOWN);
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_A) {
controls.roll_left = (evt.type == SDL_KEYDOWN);
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_D) {
controls.roll_right = (evt.type == SDL_KEYDOWN);
return true;
}
}
//move cursor on L/R/U/D press:
if (evt.type == SDL_KEYDOWN && evt.key.repeat == 0) {
if (evt.key.keysym.scancode == SDL_SCANCODE_LEFT) {
if (cursor.x > 0) {
cursor.x -= 1;
}
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_RIGHT) {
if (cursor.x + 1 < board_size.x) {
cursor.x += 1;
}
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_UP) {
if (cursor.y + 1 < board_size.y) {
cursor.y += 1;
}
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_DOWN) {
if (cursor.y > 0) {
cursor.y -= 1;
}
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
//open pause menu on 'ESCAPE':
show_pause_menu();
return true;
}
}
return false;
}
void GameMode::update(float elapsed) {
//if the roll keys are pressed, rotate everything on the same row or column as the cursor:
glm::quat dr = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
float amt = elapsed * 1.0f;
if (controls.roll_left) {
dr = glm::angleAxis(amt, glm::vec3(0.0f, 1.0f, 0.0f)) * dr;
}
if (controls.roll_right) {
dr = glm::angleAxis(-amt, glm::vec3(0.0f, 1.0f, 0.0f)) * dr;
}
if (controls.roll_up) {
dr = glm::angleAxis(amt, glm::vec3(1.0f, 0.0f, 0.0f)) * dr;
}
if (controls.roll_down) {
dr = glm::angleAxis(-amt, glm::vec3(1.0f, 0.0f, 0.0f)) * dr;
}
if (dr != glm::quat()) {
for (uint32_t x = 0; x < board_size.x; ++x) {
glm::quat &r = board_rotations[cursor.y * board_size.x + x];
r = glm::normalize(dr * r);
}
for (uint32_t y = 0; y < board_size.y; ++y) {
if (y != cursor.y) {
glm::quat &r = board_rotations[y * board_size.x + cursor.x];
r = glm::normalize(dr * r);
}
}
}
}
void GameMode::draw(glm::uvec2 const &drawable_size) {
//set up basic OpenGL state:
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Set up a transformation matrix to fit the board in the window:
glm::mat4 world_to_clip;
{
float aspect = float(drawable_size.x) / float(drawable_size.y);
//want scale such that board * scale fits in [-aspect,aspect]x[-1.0,1.0] screen box:
float scale = glm::min(
2.0f * aspect / float(board_size.x),
2.0f / float(board_size.y)
);
//center of board will be placed at center of screen:
glm::vec2 center = 0.5f * glm::vec2(board_size);
//NOTE: glm matrices are specified in column-major order
world_to_clip = glm::mat4(
scale / aspect, 0.0f, 0.0f, 0.0f,
0.0f, scale, 0.0f, 0.0f,
0.0f, 0.0f,-1.0f, 0.0f,
-(scale / aspect) * center.x, -scale * center.y, 0.0f, 1.0f
);
}
//set up graphics pipeline to use data from the meshes and the simple shading program:
glBindVertexArray(*meshes_for_vertex_color_program);
glUseProgram(vertex_color_program->program);
glUniform3fv(vertex_color_program->sun_color_vec3, 1, glm::value_ptr(glm::vec3(0.81f, 0.81f, 0.76f)));
glUniform3fv(vertex_color_program->sun_direction_vec3, 1, glm::value_ptr(glm::normalize(glm::vec3(-0.2f, 0.2f, 1.0f))));
glUniform3fv(vertex_color_program->sky_color_vec3, 1, glm::value_ptr(glm::vec3(0.2f, 0.2f, 0.3f)));
glUniform3fv(vertex_color_program->sky_direction_vec3, 1, glm::value_ptr(glm::vec3(0.0f, 1.0f, 0.0f)));
//helper function to draw a given mesh with a given transformation:
auto draw_mesh = [&](MeshBuffer::Mesh const &mesh, glm::mat4 const &object_to_world) {
//set up the matrix uniforms:
if (vertex_color_program->object_to_clip_mat4 != -1U) {
glm::mat4 object_to_clip = world_to_clip * object_to_world;
glUniformMatrix4fv(vertex_color_program->object_to_clip_mat4, 1, GL_FALSE, glm::value_ptr(object_to_clip));
}
if (vertex_color_program->object_to_light_mat4x3 != -1U) {
glUniformMatrix4x3fv(vertex_color_program->object_to_light_mat4x3, 1, GL_FALSE, glm::value_ptr(object_to_world));
}
if (vertex_color_program->normal_to_light_mat3 != -1U) {
//NOTE: if there isn't any non-uniform scaling in the object_to_world matrix, then the inverse transpose is the matrix itself, and computing it wastes some CPU time:
glm::mat3 normal_to_world = glm::inverse(glm::transpose(glm::mat3(object_to_world)));
glUniformMatrix3fv(vertex_color_program->normal_to_light_mat3, 1, GL_FALSE, glm::value_ptr(normal_to_world));
}
//draw the mesh:
glDrawArrays(GL_TRIANGLES, mesh.start, mesh.count);
};
for (uint32_t y = 0; y < board_size.y; ++y) {
for (uint32_t x = 0; x < board_size.x; ++x) {
draw_mesh(tile_mesh,
glm::mat4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
x+0.5f, y+0.5f,-0.5f, 1.0f
)
);
draw_mesh(*board_meshes[y*board_size.x+x],
glm::mat4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
x+0.5f, y+0.5f, 0.0f, 1.0f
)
* glm::mat4_cast(board_rotations[y*board_size.x+x])
);
}
}
draw_mesh(cursor_mesh,
glm::mat4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
cursor.x+0.5f, cursor.y+0.5f, 0.0f, 1.0f
)
);
if (Mode::current.get() == this) {
glDisable(GL_DEPTH_TEST);
std::string message = "PRESS ESC FOR MENU";
float height = 0.06f;
float width = text_width(message, height);
draw_text(message, glm::vec2(-0.5f * width,-0.99f), height, glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));
draw_text(message, glm::vec2(-0.5f * width,-1.0f), height, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
glUseProgram(0);
}
GL_ERRORS();
}
void GameMode::show_pause_menu() {
std::shared_ptr< MenuMode > menu = std::make_shared< MenuMode >();
std::shared_ptr< Mode > game = shared_from_this();
menu->background = game;
menu->choices.emplace_back("PAUSED");
menu->choices.emplace_back("RESUME", [game](){
Mode::set_current(game);
});
menu->choices.emplace_back("CRATES", [game](){
Mode::set_current(std::make_shared< CratesMode >());
});
menu->choices.emplace_back("QUIT", [](){
Mode::set_current(nullptr);
});
menu->selected = 1;
Mode::set_current(menu);
}