/
main.cpp
466 lines (392 loc) · 9.77 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL.h>
#include <GL\glew.h>
#include <SDL_opengl.h>
#include <gl\GLU.h>
#include <stdio.h>
#include <string>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int LEVEL_WIDTH = 640;
const int LEVEL_HEIGHT = 480;
//The window we'll be rendering to
SDL_Window *gWindow = NULL;
//Shader loading utility programs
void printProgramLog(GLuint program);
void printShaderLog(GLuint shader);
//Graphics program
GLuint gProgramID = 0;
GLint gVertexPos2DLocation = -1;
GLuint gVBO = 0;
GLuint gIBO = 0;
//Starts up SDL and creates a window
bool init();
//Initializes matrices and clear color
bool initGL();
//Input handler
void handleKeys(unsigned char key, int x, int y);
//Per frame update
void update();
//Renders quad to the screen
void render();
//OpenGL context
SDL_GLContext gContext;
//Render flag
bool gRenderQuad = true;
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//Loads individual image
SDL_Surface* loadSurface(std::string path);
//Loads individual image as texture
SDL_Texture* loadTexture(std::string path);
void printProgramLog(GLuint program)
{
//Make sure name is shader
if (glIsProgram(program))
{
//Program log length
int infoLogLength = 0;
int maxLength = infoLogLength;
//Get info string length
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
//Allocate string
char* infoLog = new char[maxLength];
//Get info log
glGetProgramInfoLog(program, maxLength, &infoLogLength, infoLog);
if (infoLogLength > 0)
{
//Print Log
printf("%s\n", infoLog);
}
//Deallocate string
delete[] infoLog;
}
else
{
printf("Name %d is not a program\n", program);
}
}
void printShaderLog(GLuint shader)
{
//Make sure name is shader
if (glIsShader(shader))
{
//Shader log length
int infoLogLength = 0;
int maxLength = infoLogLength;
//Get info string length
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
//Allocate string
char* infoLog = new char[maxLength];
//Get info log
glGetShaderInfoLog(shader, maxLength, &infoLogLength, infoLog);
if (infoLogLength > 0)
{
//Print Log
printf("%s\n", infoLog);
}
//Deallocate string
delete[] infoLog;
}
else
{
printf("Name %d is not a shader\n", shader);
}
}
void handleKeys(unsigned char key, int x, int y)
{
//toggle quad
if (key == 'q')
{
gRenderQuad = !gRenderQuad;
}
}
void update()
{
//No per frame update needed
}
void render()
{
//Clear color buffer
glClear(GL_COLOR_BUFFER_BIT);
//Render quad
if (gRenderQuad)
{
//Bind program
glUseProgram(gProgramID);
//Enable vertex position
glEnableVertexAttribArray(gVertexPos2DLocation);
//Set vertex data
glBindBuffer(GL_ARRAY_BUFFER, gVBO);
glVertexAttribPointer(gVertexPos2DLocation, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), NULL);
//Set index data and render
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gIBO);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, NULL);
//Disable vertext position
glDisableVertexAttribArray(gVertexPos2DLocation);
//Unbind program
glUseProgram(NULL);
}
}
bool init()
{
//Initialization flag;
bool sucess = true;
//Initalize SDL
if (SDL_Init(SDL_INIT_VIDEO /*| SDL_INIT_TIMER /*| SDL_INIT_AUDIO*/) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
sucess = false;
}//end if
else
{
/*//Use OpenGL 2.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
*/
//Use OpenGL 3.1 core
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
//Create Window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
sucess = false;
}
else
{
//Create context
gContext = SDL_GL_CreateContext(gWindow);
if (gContext == NULL)
{
printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
sucess = false;
}
else
{
//Initalize GLEW
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if (glewError != GLEW_OK)
{
printf("Error initalizing GLEW! %s\n", glewGetErrorString(glewError));
}
//Use Vsync
if (SDL_GL_SetSwapInterval(1) < 0)
{
printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
sucess = false;
}
//Initalize OpenGL
if (!initGL())
{
printf("Unable to Initalize OpenGL!\n");
sucess = false;
}
}
#ifdef _SDL_IMAGE_H
//initialize PNG loading
int imageFlags = IMG_INIT_PNG;
if (!(IMG_Init(imageFlags)& imageFlags))
{
printf("SDL_image could not initialize! SDL_Image Error: %s\n", IMG_GetError());
sucess = false;
}//end image init else
#endif
#ifdef _SDL_TTF_H
//Initialize SDL_ttf
if (TTF_Init() == -1)
{
printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
sucess = false;
}
#endif
/*//Initalize SDL_mixer
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
printf("SDL_mixer could not initalize! SDL_mixer Error: %s\n", Mix_GetError());
sucess = false;
}*/
}//end else
}//end else
return sucess;
}//end init
bool initGL()
{
bool success = true;
//Generate program
gProgramID = glCreateProgram();
//Create vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
//Get vertex source
const GLchar* vertexShaderSource[] =
{
"#version 140\nin vec2 LVertexPos2D; void main(){gl_Position = vec4( LVertexPos2D.x, LVertexPos2D.y, 0, 1 ); }"
};
//Set vertex shource
glShaderSource(vertexShader, 1, vertexShaderSource, NULL);
//Compile vertex source
glCompileShader(vertexShader);
//Check vertex shader for errors
GLint vShaderCompiled = GL_FALSE;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vShaderCompiled);
if (vShaderCompiled != GL_TRUE)
{
printf("Unable to compile vertext shader %d!\n", vertexShader);
printShaderLog(vertexShader);
success = false;
}
else
{
//Attach vertext shader to program
glAttachShader(gProgramID, vertexShader);
//Create fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
//Get fragment source
const GLchar* fragmentShaderSource[] =
{
"#version 140\nout vec4 LFragment; void main(){LFragment = vec4( 1.0, 1.0, 1.0, 1.0);}"
};
//Set fragment source
glShaderSource(fragmentShader, 1, fragmentShaderSource, NULL);
//Compile fragment source
glCompileShader(fragmentShader);
//Check fragment shader for errors
GLint fShaderCompiled = GL_FALSE;
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fShaderCompiled);
if (fShaderCompiled != GL_TRUE)
{
printf("Unable to compile fragment shader %d!\n", fragmentShader);
printShaderLog(fragmentShader);
success = false;
}
else
{
//Attach fragment shader to program
glAttachShader(gProgramID, fragmentShader);
//Link program
glLinkProgram(gProgramID);
//Check for errors
GLint programSuccess = GL_TRUE;
glGetProgramiv(gProgramID, GL_LINK_STATUS, &programSuccess);
if (programSuccess != GL_TRUE)
{
printf("Error linking program %d\n", gProgramID);
printProgramLog(gProgramID);
success = false;
}
else
{
//Get vertext attribute location
gVertexPos2DLocation = glGetAttribLocation(gProgramID, "LVertexPos2D");
if (gVertexPos2DLocation == -1)
{
printf("LVertexPos2D is not a valid glsl program variable!\n");
success = false;
}
else
{
//Initalize clear color
glClearColor(0.f, 0.f, 0.f, 1.f);
//VBO data
GLfloat vertexData[] =
{
-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f
};
//IBO data
GLuint indexData[] = { 0,1,2,3 };
//Create VBO
glGenBuffers(1, &gVBO);
glBindBuffer(GL_ARRAY_BUFFER, gVBO);
glBufferData(GL_ARRAY_BUFFER, 2 * 4 * sizeof(GLfloat), vertexData, GL_STATIC_DRAW);
//Create IBO
glGenBuffers(1, &gIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), indexData, GL_STATIC_DRAW);
}//end gVertexPos2DLocation
}//end program success
}//end if fShaderCopiled
}//end vShader compiled
return success;
}
bool loadMedia()
{
bool success = true;
return success;
}//end loadMedia
void close()
{
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
#ifdef _SDL_TTF_H
TTF_Quit();
#endif
#ifdef _SDL_IMAGE_H
IMG_Quit();
#endif
SDL_Quit();
}//end close
int main( int argc, char* args[] )
{
//Start up SDL and create window
if (!init())
{
printf("Failed to initalize!\n");
}//end if
else
{
//Load Media
if (!loadMedia())
{
printf("Failed to load media!\n");
}//end if
else
{
// Main Loop Flag
bool quit = false;
//Event Handler
SDL_Event e;
//enable text input
SDL_StartTextInput();
//While application is running
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User request quite
if (e.type == SDL_QUIT)
{
quit = true;
}//end if
else if (e.type == SDL_TEXTINPUT)
{
int x = 0, y = 0;
SDL_GetMouseState(&x, &y);
handleKeys(e.text.text[0], x, y);
}
}//end while
//Render quad
render();
//Update screen
SDL_GL_SwapWindow(gWindow);
}//end main loop
SDL_StopTextInput();
}//end else
}//end if
//Free resources and close SDL
close();
system("pause");
return 0;
}// end main func