forked from ixchow/15-466-f17-base1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
366 lines (305 loc) · 11.9 KB
/
main.cpp
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "load_save_png.hpp"
#include "GL.hpp"
#include <SDL.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <chrono>
#include <iostream>
#include <stdexcept>
static GLuint compile_shader(GLenum type, std::string const &source);
static GLuint link_program(GLuint vertex_shader, GLuint fragment_shader);
int main(int argc, char **argv) {
//Configuration:
struct {
std::string title = "Game1: Text/Tiles";
glm::uvec2 size = glm::uvec2(640, 480);
} config;
//------------ initialization ------------
//Initialize SDL library:
SDL_Init(SDL_INIT_VIDEO);
//Ask for an OpenGL context version 3.3, core profile, enable debug:
SDL_GL_ResetAttributes();
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
//create window:
SDL_Window *window = SDL_CreateWindow(
config.title.c_str(),
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
config.size.x, config.size.y,
SDL_WINDOW_OPENGL /*| SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI*/
);
if (!window) {
std::cerr << "Error creating SDL window: " << SDL_GetError() << std::endl;
return 1;
}
//Create OpenGL context:
SDL_GLContext context = SDL_GL_CreateContext(window);
if (!context) {
SDL_DestroyWindow(window);
std::cerr << "Error creating OpenGL context: " << SDL_GetError() << std::endl;
return 1;
}
#ifdef _WIN32
//On windows, load OpenGL extensions:
if (!init_gl_shims()) {
std::cerr << "ERROR: failed to initialize shims." << std::endl;
return 1;
}
#endif
//Set VSYNC + Late Swap (prevents crazy FPS):
if (SDL_GL_SetSwapInterval(-1) != 0) {
std::cerr << "NOTE: couldn't set vsync + late swap tearing (" << SDL_GetError() << ")." << std::endl;
if (SDL_GL_SetSwapInterval(1) != 0) {
std::cerr << "NOTE: couldn't set vsync (" << SDL_GetError() << ")." << std::endl;
}
}
//Hide mouse cursor (note: showing can be useful for debugging):
SDL_ShowCursor(SDL_DISABLE);
//------------ opengl objects / game assets ------------
//texture:
GLuint tex = 0;
glm::uvec2 tex_size = glm::uvec2(0,0);
{ //load texture 'tex':
std::vector< uint32_t > data;
if (!load_png("elements.png", &tex_size.x, &tex_size.y, &data, LowerLeftOrigin)) {
std::cerr << "Failed to load texture." << std::endl;
exit(1);
}
//create a texture object:
glGenTextures(1, &tex);
//bind texture object to GL_TEXTURE_2D:
glBindTexture(GL_TEXTURE_2D, tex);
//upload texture data from data:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_size.x, tex_size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data[0]);
//set texture sampling parameters:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
//shader program:
GLuint program = 0;
GLuint program_Position = 0;
GLuint program_TexCoord = 0;
GLuint program_Color = 0;
GLuint program_mvp = 0;
GLuint program_tex = 0;
{ //compile shader program:
GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER,
"#version 330\n"
"uniform mat4 mvp;\n"
"in vec4 Position;\n"
"in vec2 TexCoord;\n"
"in vec4 Color;\n"
"out vec2 texCoord;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = mvp * Position;\n"
" color = Color;\n"
" texCoord = TexCoord;\n"
"}\n"
);
GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER,
"#version 330\n"
"uniform sampler2D tex;\n"
"in vec4 color;\n"
"in vec2 texCoord;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = texture(tex, texCoord) * color;\n"
"}\n"
);
program = link_program(fragment_shader, vertex_shader);
//look up attribute locations:
program_Position = glGetAttribLocation(program, "Position");
if (program_Position == -1U) throw std::runtime_error("no attribute named Position");
program_TexCoord = glGetAttribLocation(program, "TexCoord");
if (program_TexCoord == -1U) throw std::runtime_error("no attribute named TexCoord");
program_Color = glGetAttribLocation(program, "Color");
if (program_Color == -1U) throw std::runtime_error("no attribute named Color");
//look up uniform locations:
program_mvp = glGetUniformLocation(program, "mvp");
if (program_mvp == -1U) throw std::runtime_error("no uniform named mvp");
program_tex = glGetUniformLocation(program, "tex");
if (program_tex == -1U) throw std::runtime_error("no uniform named tex");
}
//vertex buffer:
GLuint buffer = 0;
{ //create vertex buffer
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
}
struct Vertex {
Vertex(glm::vec2 const &Position_, glm::vec2 const &TexCoord_, glm::u8vec4 const &Color_) :
Position(Position_), TexCoord(TexCoord_), Color(Color_) { }
glm::vec2 Position;
glm::vec2 TexCoord;
glm::u8vec4 Color;
};
static_assert(sizeof(Vertex) == 20, "Vertex is nicely packed.");
//vertex array object:
GLuint vao = 0;
{ //create vao and set up binding:
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(program_Position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLbyte *)0);
glVertexAttribPointer(program_TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLbyte *)0 + sizeof(glm::vec2));
glVertexAttribPointer(program_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (GLbyte *)0 + sizeof(glm::vec2) + sizeof(glm::vec2));
glEnableVertexAttribArray(program_Position);
glEnableVertexAttribArray(program_TexCoord);
glEnableVertexAttribArray(program_Color);
}
//------------ sprite info ------------
struct SpriteInfo {
glm::vec2 min_uv = glm::vec2(0.0f);
glm::vec2 max_uv = glm::vec2(1.0f);
glm::vec2 rad = glm::vec2(0.5f);
};
auto load_sprite = [](std::string const &name) -> SpriteInfo {
SpriteInfo info;
//TODO: look up sprite name in table of sprite infos
return info;
};
//------------ game state ------------
glm::vec2 mouse = glm::vec2(0.0f, 0.0f); //mouse position in [-1,1]x[-1,1] coordinates
struct {
glm::vec2 at = glm::vec2(0.0f, 0.0f);
glm::vec2 radius = glm::vec2(10.0f, 10.0f);
} camera;
//correct radius for aspect ratio:
camera.radius.x = camera.radius.y * (float(config.size.x) / float(config.size.y));
//------------ game loop ------------
bool should_quit = false;
while (true) {
static SDL_Event evt;
while (SDL_PollEvent(&evt) == 1) {
//handle input:
if (evt.type == SDL_MOUSEMOTION) {
mouse.x = (evt.motion.x + 0.5f) / float(config.size.x) * 2.0f - 1.0f;
mouse.y = (evt.motion.y + 0.5f) / float(config.size.y) *-2.0f + 1.0f;
} else if (evt.type == SDL_MOUSEBUTTONDOWN) {
} else if (evt.type == SDL_KEYDOWN && evt.key.keysym.sym == SDLK_ESCAPE) {
should_quit = true;
} else if (evt.type == SDL_QUIT) {
should_quit = true;
break;
}
}
if (should_quit) break;
auto current_time = std::chrono::high_resolution_clock::now();
static auto previous_time = current_time;
float elapsed = std::chrono::duration< float >(current_time - previous_time).count();
previous_time = current_time;
{ //update game state:
(void)elapsed;
}
//draw output:
glClearColor(0.5, 0.5, 0.5, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
{ //draw game state:
std::vector< Vertex > verts;
//helper: add rectangle to verts:
auto rect = [&verts](glm::vec2 const &at, glm::vec2 const &rad, glm::u8vec4 const &tint) {
verts.emplace_back(at + glm::vec2(-rad.x,-rad.y), glm::vec2(0.0f, 0.0f), tint);
verts.emplace_back(verts.back());
verts.emplace_back(at + glm::vec2(-rad.x, rad.y), glm::vec2(0.0f, 1.0f), tint);
verts.emplace_back(at + glm::vec2( rad.x,-rad.y), glm::vec2(1.0f, 0.0f), tint);
verts.emplace_back(at + glm::vec2( rad.x, rad.y), glm::vec2(1.0f, 1.0f), tint);
verts.emplace_back(verts.back());
};
auto draw_sprite = [&verts](SpriteInfo const &sprite, glm::vec2 const &at, float angle = 0.0f) {
glm::vec2 min_uv = sprite.min_uv;
glm::vec2 max_uv = sprite.max_uv;
glm::vec2 rad = sprite.rad;
glm::u8vec4 tint = glm::u8vec4(0xff, 0xff, 0xff, 0xff);
glm::vec2 right = glm::vec2(std::cos(angle), std::sin(angle));
glm::vec2 up = glm::vec2(-right.y, right.x);
verts.emplace_back(at + right * -rad.x + up * -rad.y, glm::vec2(min_uv.x, min_uv.y), tint);
verts.emplace_back(verts.back());
verts.emplace_back(at + right * -rad.x + up * rad.y, glm::vec2(min_uv.x, max_uv.y), tint);
verts.emplace_back(at + right * rad.x + up * -rad.y, glm::vec2(max_uv.x, min_uv.y), tint);
verts.emplace_back(at + right * rad.x + up * rad.y, glm::vec2(max_uv.x, max_uv.y), tint);
verts.emplace_back(verts.back());
};
//Draw a sprite "player" at position (5.0, 2.0):
static SpriteInfo player = load_sprite("player"); //TODO: hoist
draw_sprite(player, glm::vec2(5.0, 2.0), 0.2f);
rect(glm::vec2(0.0f, 0.0f), glm::vec2(4.0f), glm::u8vec4(0xff, 0x00, 0x00, 0xff));
rect(mouse * camera.radius + camera.at, glm::vec2(4.0f), glm::u8vec4(0xff, 0xff, 0xff, 0x88));
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * verts.size(), &verts[0], GL_STREAM_DRAW);
glUseProgram(program);
glUniform1i(program_tex, 0);
glm::vec2 scale = 1.0f / camera.radius;
glm::vec2 offset = scale * -camera.at;
glm::mat4 mvp = glm::mat4(
glm::vec4(scale.x, 0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, scale.y, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 0.0f),
glm::vec4(offset.x, offset.y, 0.0f, 1.0f)
);
glUniformMatrix4fv(program_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
glBindTexture(GL_TEXTURE_2D, tex);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, verts.size());
}
SDL_GL_SwapWindow(window);
}
//------------ teardown ------------
SDL_GL_DeleteContext(context);
context = 0;
SDL_DestroyWindow(window);
window = NULL;
return 0;
}
static GLuint compile_shader(GLenum type, std::string const &source) {
GLuint shader = glCreateShader(type);
GLchar const *str = source.c_str();
GLint length = source.size();
glShaderSource(shader, 1, &str, &length);
glCompileShader(shader);
GLint compile_status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if (compile_status != GL_TRUE) {
std::cerr << "Failed to compile shader." << std::endl;
GLint info_log_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector< GLchar > info_log(info_log_length, 0);
GLsizei length = 0;
glGetShaderInfoLog(shader, info_log.size(), &length, &info_log[0]);
std::cerr << "Info log: " << std::string(info_log.begin(), info_log.begin() + length);
glDeleteShader(shader);
throw std::runtime_error("Failed to compile shader.");
}
return shader;
}
static GLuint link_program(GLuint fragment_shader, GLuint vertex_shader) {
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
GLint link_status = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &link_status);
if (link_status != GL_TRUE) {
std::cerr << "Failed to link shader program." << std::endl;
GLint info_log_length = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector< GLchar > info_log(info_log_length, 0);
GLsizei length = 0;
glGetProgramInfoLog(program, info_log.size(), &length, &info_log[0]);
std::cerr << "Info log: " << std::string(info_log.begin(), info_log.begin() + length);
throw std::runtime_error("Failed to link program");
}
return program;
}