-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise.cpp
More file actions
405 lines (310 loc) · 13.6 KB
/
Copy pathexercise.cpp
File metadata and controls
405 lines (310 loc) · 13.6 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
I will try to make this into a framework
**/
#define GL_SILENCE_DEPRECATION
#define STB_IMAGE_IMPLEMENTATION
#ifdef _WINDOWS
#include <GL/glew.h>
#endif
/**
* below is a convenient debug tool
*/
#define LOG(argument) std::cout << argument << '\n'
/**
File path adjusted to relative, only compatible with Debian-based linux
because the packages for glm and sdl are under "SDL2" directory
for all other OS, remove "include/" from headers
**/
#define GL_GLEXT_PROTOTYPES 1
#include "SDL2/SDL.h"
#include "SDL2/SDL_opengl.h"
#include "include/glm/mat4x4.hpp"
#include "include/glm/gtc/matrix_transform.hpp"
#include "include/ShaderProgram.h"
#include "include/stb_image.h"
#include <vector>
/**
* in the future we could change shaders
* note we also have two sprites used here
* they all have to be absolute paths
* same as other headers, for other OS/envrionments, remove all path components before "include" but keep "include/" itsef
*/
const char V_SHADER_PATH[] = "/home/ren/projects/myGames/include/shaders/vertex_textured.glsl",
F_SHADER_PATH[] = "/home/ren/projects/myGames/include/shaders/fragment_textured.glsl";
const char PLAYER_SPRITE_FILEPATH[] = "/home/ren/projects/myGames/include/assets/sonic.png",
FRAME_SPRITE_FILEPATH[] = "/home/ren/projects/myGames/include/assets/frame.png",
FONT_SPRITE_FILEPATH[] = "/home/ren/projects/myGames/include/assets/font1.png";
enum Coordinate
{
x_coordinate,
y_coordinate
};
const int WINDOW_WIDTH = 640,
WINDOW_HEIGHT = 480;
//const float BG_RED = 0.9608f,
// BG_BLUE = 0.9608f,
// BG_GREEN = 0.9608f,
// BG_OPACITY = 1.0f;
const float BG_RED = 0.08f,
BG_BLUE = 0.08f,
BG_GREEN = 0.08f,
BG_OPACITY = 1.0f;
const int VIEWPORT_X = 0,
VIEWPORT_Y = 0,
VIEWPORT_WIDTH = WINDOW_WIDTH,
VIEWPORT_HEIGHT = WINDOW_HEIGHT;
const float MILLISECONDS_IN_SECOND = 1000.0;
const float DEGREES_PER_SECOND = 90.0f;
const glm::vec3 ORIGIN = glm::vec3(0.0f, 0.0f, 0.0f),
DOUBLE = glm::vec3(2.0f, 2.0f, 0.0f);
const int NUMBER_OF_TEXTURES = 1;
const GLint LEVEL_OF_DETAIL = 0;
const GLint TEXTURE_BORDER = 0;
const int CHARACTER_SHEET_ROWS = 1,
CHARACTER_SHEET_COLS = 4,
FRAME_ROWS = 1,
FRAME_COLS = 1;
const int FONTBANK_SIZE = 16,
FRAMES_PER_SECOND = 4;
SDL_Window* g_display_window;
bool g_game_is_running = true;
bool g_is_growing = true;
ShaderProgram g_shader_program;
glm::mat4 g_view_matrix,
g_character_model_matrix,
g_frame_model_matrix,
g_projection_matrix;
float g_previous_ticks = 0.0f;
GLuint g_character_texture_id,
g_frame_texture_id,
g_text_texture_id;
// ———— PART 1 ———— //
bool g_character_rotation = true;
int g_character_index = 0;
float g_animation_time = 0.0f;
// ———— PART 1 ———— //
void draw_text(ShaderProgram *program, GLuint font_texture_id, std::string text, float screen_size, float spacing, glm::vec3 position)
{
// Scale the size of the fontbank in the UV-plane
// We will use this for spacing and positioning
float width = 1.0f / FONTBANK_SIZE;
float height = 1.0f / FONTBANK_SIZE;
// Instead of having a single pair of arrays, we'll have a series of pairs—one for each character
// Don't forget to include <vector>!
std::vector<float> vertices;
std::vector<float> texture_coordinates;
// For every character...
for (int i = 0; i < text.size(); i++) {
// 1. Get their index in the spritesheet, as well as their offset (i.e. their position
// relative to the whole sentence)
int spritesheet_index = (int) text[i]; // ascii value of character
float offset = (screen_size + spacing) * i;
// 2. Using the spritesheet index, we can calculate our U- and V-coordinates
float u_coordinate = (float) (spritesheet_index % FONTBANK_SIZE) / FONTBANK_SIZE;
float v_coordinate = (float) (spritesheet_index / FONTBANK_SIZE) / FONTBANK_SIZE;
// 3. Inset the current pair in both vectors
vertices.insert(vertices.end(), {
offset + (-0.5f * screen_size), 0.5f * screen_size,
offset + (-0.5f * screen_size), -0.5f * screen_size,
offset + (0.5f * screen_size), 0.5f * screen_size,
offset + (0.5f * screen_size), -0.5f * screen_size,
offset + (0.5f * screen_size), 0.5f * screen_size,
offset + (-0.5f * screen_size), -0.5f * screen_size,
});
texture_coordinates.insert(texture_coordinates.end(), {
u_coordinate, v_coordinate,
u_coordinate, v_coordinate + height,
u_coordinate + width, v_coordinate,
u_coordinate + width, v_coordinate + height,
u_coordinate + width, v_coordinate,
u_coordinate, v_coordinate + height,
});
}
// 4. And render all of them using the pairs
glm::mat4 model_matrix = glm::mat4(1.0f);
model_matrix = glm::translate(model_matrix, position);
program->set_model_matrix(model_matrix);
glUseProgram(program->get_program_id());
glVertexAttribPointer(program->get_position_attribute(), 2, GL_FLOAT, false, 0, vertices.data());
glEnableVertexAttribArray(program->get_position_attribute());
glVertexAttribPointer(program->get_tex_coordinate_attribute(), 2, GL_FLOAT, false, 0, texture_coordinates.data());
glEnableVertexAttribArray(program->get_tex_coordinate_attribute());
glBindTexture(GL_TEXTURE_2D, font_texture_id);
glDrawArrays(GL_TRIANGLES, 0, (int) (text.size() * 6));
glDisableVertexAttribArray(program->get_position_attribute());
glDisableVertexAttribArray(program->get_tex_coordinate_attribute());
}
void draw_sprite_from_texture_atlas(ShaderProgram *program, GLuint texture_id, int index, int rows, int cols)
{
// Step 1: Calculate the UV location of the indexed frame
float u_coord = (float) (index % cols) / (float) cols;
float v_coord = (float) (index / cols) / (float) rows;
// Step 2: Calculate its UV size
float width = 1.0f / (float) cols;
float height = 1.0f / (float) rows;
// Step 3: Match the texture coordinates to the vertices
float tex_coords[] =
{
u_coord, v_coord + height, u_coord + width, v_coord + height, u_coord + width, v_coord,
u_coord, v_coord + height, u_coord + width, v_coord, u_coord, v_coord
};
float vertices[] =
{
-0.5, -0.5, 0.5, -0.5, 0.5, 0.5,
-0.5, -0.5, 0.5, 0.5, -0.5, 0.5
};
// Step 4: Render stuff
glBindTexture(GL_TEXTURE_2D, texture_id);
glVertexAttribPointer(program->get_position_attribute(), 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(program->get_position_attribute());
glVertexAttribPointer(program->get_tex_coordinate_attribute(), 2, GL_FLOAT, false, 0, tex_coords);
glEnableVertexAttribArray(program->get_tex_coordinate_attribute());
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(program->get_position_attribute());
glDisableVertexAttribArray(program->get_tex_coordinate_attribute());
}
GLuint load_texture(const char* filepath)
{
// STEP 1: Loading the image file
int width, height, number_of_components;
unsigned char* image = stbi_load(filepath, &width, &height, &number_of_components, STBI_rgb_alpha);
if (image == NULL)
{
LOG("Unable to load image. Make sure the path is correct.");
assert(false);
}
// STEP 2: Generating and binding a texture ID to our image
GLuint textureID;
glGenTextures(NUMBER_OF_TEXTURES, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, LEVEL_OF_DETAIL, GL_RGBA, width, height, TEXTURE_BORDER, GL_RGBA, GL_UNSIGNED_BYTE, image);
// STEP 3: Setting our texture filter parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// STEP 4: Releasing our file from memory and returning our texture id
stbi_image_free(image);
// Setting our texture wrapping modes
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // the last argument can change depending on what you are looking for
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
return textureID;
}
void initialise()
{
// Initialise video and joystick subsystems
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
g_display_window = SDL_CreateWindow("Hello, Animation!",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(g_display_window);
SDL_GL_MakeCurrent(g_display_window, context);
#ifdef _WINDOWS
glewInit();
#endif
glViewport(VIEWPORT_X, VIEWPORT_Y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
g_shader_program.load(V_SHADER_PATH, F_SHADER_PATH);
// ————— INSTANTIATING VIEW AND PROJ MATRICES ———— //
g_view_matrix = glm::mat4(1.0f); // Defines the position (location and orientation) of the camera
g_projection_matrix = glm::ortho(-5.0f, 5.0f, -3.75f, 3.75f, -1.0f, 1.0f); // Defines the characteristics of your camera, such as clip planes, field of view, projection method etc.
// ————— CHARACTER ———— //
g_character_model_matrix = glm::mat4(1.0f);
g_shader_program.set_projection_matrix(g_projection_matrix);
g_shader_program.set_view_matrix(g_view_matrix);
glUseProgram(g_shader_program.get_program_id());
g_character_texture_id = load_texture(PLAYER_SPRITE_FILEPATH);
// ————— FRAME ———— //
g_frame_model_matrix = glm::mat4(1.0f);
glUseProgram(g_shader_program.get_program_id());
g_frame_texture_id = load_texture(FRAME_SPRITE_FILEPATH);
// ———— TEXT ———— //
glUseProgram(g_shader_program.get_program_id());
g_text_texture_id = load_texture(FONT_SPRITE_FILEPATH);
// ————— GENERAL ———— //
glClearColor(BG_RED, BG_BLUE, BG_GREEN, BG_OPACITY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void process_input()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type) {
case SDL_WINDOWEVENT_CLOSE:
case SDL_QUIT:
g_game_is_running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_q:
g_game_is_running = false;
break;
// ———— PART 2 ———— //
case SDLK_s:
g_character_rotation = not g_character_rotation;
break;
// ———— PART 2 ———— //
default:
break;
}
default:
break;
}
}
}
void update()
{
// ———— DELTA TIME CALCULATIONS ———— //
float ticks = (float) SDL_GetTicks() / MILLISECONDS_IN_SECOND; // get the current number of ticks
float delta_time = ticks - g_previous_ticks; // the delta time is the difference from the last frame
g_previous_ticks = ticks;
// ———— PART 3 ———— //
g_animation_time += delta_time;
float seconds_per_frame = (float) 1 / FRAMES_PER_SECOND;
if (g_character_rotation) {
if (g_animation_time >= seconds_per_frame) {
g_animation_time = 0.0f;
g_character_index++;
g_character_index %= 4; //because we only have 4 characters
}
}
// ———— PART 3 ———— //
// ———— RESETTING MODEL MATRICES ———— //
g_character_model_matrix = glm::mat4(1.0f);
g_frame_model_matrix = glm::mat4(1.0f);
g_character_model_matrix = glm::translate(g_character_model_matrix, ORIGIN);
g_frame_model_matrix = glm::translate(g_frame_model_matrix, ORIGIN);
g_frame_model_matrix = glm::scale(g_frame_model_matrix, DOUBLE);
g_character_model_matrix = glm::scale(g_character_model_matrix, DOUBLE * 0.75f);
}
void render() {
glClear(GL_COLOR_BUFFER_BIT);
g_shader_program.set_model_matrix(g_character_model_matrix);
// ———— PART 4 ———— //
draw_sprite_from_texture_atlas(&g_shader_program, g_character_texture_id, g_character_index, CHARACTER_SHEET_ROWS, CHARACTER_SHEET_COLS);
// ———— PART 4 ———— //
g_shader_program.set_model_matrix(g_frame_model_matrix);
draw_sprite_from_texture_atlas(&g_shader_program, g_frame_texture_id, 0, FRAME_ROWS, FRAME_COLS);
draw_text(&g_shader_program, g_text_texture_id, std::string("PRESS S TO"), 0.25f, 0.0f, glm::vec3(-1.25f, 2.0f, 0.0f));
draw_text(&g_shader_program, g_text_texture_id, std::string("CHOOSE YOUR CHARACTER"), 0.25f, 0.01f, glm::vec3(-2.5f, 1.5f, 0.0f));
if(! g_character_rotation){
draw_text(&g_shader_program, g_text_texture_id, std::string("YOU MADE A CHOICE"), 0.25f, 0.01f, glm::vec3(-2.0f, -1.5f, 0.0f));
}
SDL_GL_SwapWindow(g_display_window);
}
void shutdown() { SDL_Quit(); }
/**
Start here—we can see the general structure of a game loop without worrying too much about the details yet.
*/
int main(int argc, char* argv[])
{
initialise();
while (g_game_is_running)
{
process_input();
update();
render();
}
shutdown();
return 0;
}