-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
207 lines (176 loc) · 4.21 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
// SDL2_OpenGL.cpp : Defines the entry point for the console application.
// MDL loader http://tfc.duke.free.fr/coding/mdl-specs-en.html ported to SDL2 by Memorix101
#include "stdafx.h"
#include <stdio.h>
#include<iostream>
#include <string>
#include "main.h"
#include "mdl.h"
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
// Request an OpenGL 3.1 context (should be core)
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
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 OpenGL 3.1 Renderer", 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());
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
//Create context
gContext = SDL_GL_CreateContext(gWindow);
if (gContext == NULL)
{
printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Use Vsync
if (SDL_GL_SetSwapInterval(1) < 0)
{
printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
}
//Initialize OpenGL
if (!initGL())
{
printf("Unable to initialize OpenGL!\n");
success = false;
}
}
}
}
return success;
}
bool initGL()
{
bool success = true;
GLenum error = GL_NO_ERROR;
//Initialize Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Check for error
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error initializing OpenGL! %s\n", gluErrorString(error));
success = false;
}
//Initialize Modelview Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Check for error
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error initializing OpenGL! %s\n", gluErrorString(error));
success = false;
}
//Initialize clear color
glClearColor(0.f, 0.f, 0.f, 1.f);
//Check for error
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error initializing OpenGL! %s\n", gluErrorString(error));
success = false;
}
return success;
}
void handleKeys(unsigned char key, int x, int y)
{
//Toggle quad
if (key == 'q')
{
gRenderQuad = !gRenderQuad;
}
}
void update()
{
//No per frame update needed
}
void close()
{
//Destroy window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main(int argc, char* args[])
{
//Start up SDL and create window
if (!init())
{
printf("Failed to initialize!\n");
}
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 requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
//Handle keypress with current mouse position
else if (e.type == SDL_TEXTINPUT)
{
int x = 0, y = 0;
SDL_GetMouseState(&x, &y);
handleKeys(e.text.text[0], x, y);
}
const Uint8 *state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_RETURN]) {
printf("<RETURN> is pressed.\n");
}
if (state[SDL_SCANCODE_UP]) {
std::cout << "CAM_Z" + std::to_string(cam_z) + " - " << "\n";
cam_z++;
}
if (state[SDL_SCANCODE_DOWN]) {
std::cout << "CAM_Z" + std::to_string(cam_z) + " - " << "\n";
cam_z--;
}
}
//Render quad
cleanup();
reshape(SCREEN_WIDTH, SCREEN_HEIGHT);
render();
//Update screen
SDL_GL_SwapWindow(gWindow);
}
//Disable text input
SDL_StopTextInput();
}
//Free resources and close SDL
close();
return 0;
}