-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
127 lines (97 loc) · 2.67 KB
/
main.c
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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
//SDL includes
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include "header.h"
void loadGLSLParameters();
//GLSL program variable
GLuint program;
//window
SDL_Window* glWindow = NULL;
//SDL context
SDL_GLContext glContext;
int main(int argc, char **argv){
//set initial window width and height - this will be overridden by window events if the window is resized etc
window.width = 800;
window.height = 600;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
fprintf(stderr, "failure of SDL_Init %s\n", SDL_GetError());
exit(1);
}
//set OpenGL 2.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
//SDL window
glWindow = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, window.width, window.height, SDL_WINDOW_OPENGL);
if(glWindow == NULL){
fprintf(stderr, "failure of SDL_CreateWindow %s\n", SDL_GetError());
exit(1);
}
glContext = SDL_GL_CreateContext(glWindow);
if(glContext == NULL){
fprintf(stderr, "failure of SDL_CreateWindow %s\n", SDL_GetError());
exit(1);
}
//Use Vsync - RESEARCH NEEDED on this
if(SDL_GL_SetSwapInterval( 1 ) < 0 ) {
fprintf(stderr,"Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError() );
exit(1);
}
//GLSL extensions - error check needed
loadExtensions();
glViewport(0, 0, window.width, window.height);
//shaders
if(SetupGLSL("shaders/vert.vs", "shaders/frag.fs", &program) == GL_FALSE){
fprintf(stderr,"SetupGLSL returned GL_FALSE\n");
exit(1);
}
glUseProgram(program);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
SDL_bool quit = SDL_FALSE;
SDL_Event event;
GLfloat x = 0.0f;
GLfloat z = 0.0f;
//While application is running
while(!quit)
{
//Handle events on queue
while(SDL_PollEvent( &event ) != 0)
{
//User requests quit
if(event.type == SDL_QUIT)
{
quit = SDL_TRUE;
}
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_LEFT: x -= 0.1; break;
case SDLK_RIGHT: x += 0.1; break;
case SDLK_UP: z += 0.1; break;
case SDLK_DOWN: z -= 0.1; break;
case SDLK_q: quit = SDL_TRUE; break;
}
}
}
setShaderUniform1f(program, "x", x);
setShaderUniform1f(program, "z", z);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
glRecti(-1, -1, 1, 1);
glFlush();
//GL 'swap buffers'
SDL_GL_SwapWindow( glWindow );
}
// disable GLSL program
glUseProgram(0);
//Destroy window
SDL_DestroyWindow(glWindow);
glWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
return 0;
}