-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDL_loop.cpp
More file actions
226 lines (176 loc) · 6.13 KB
/
SDL_loop.cpp
File metadata and controls
226 lines (176 loc) · 6.13 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
#include "render.h"
#include "SDL_loop.h"
// Screen dimension constants
const int SCREEN_WIDTH = 360;
const int SCREEN_HEIGHT = 360;
/**
*
* @param windowOut pass in a reference to a pointer
* @param screenSurfaceOut
* @return
*/
int init_window(SDL_Window **windowOut, SDL_Surface **screenSurfaceOut) {
// Create the window we'll render to
SDL_Window *window = NULL;
// The window will contain a single surface
SDL_Surface *screenSurface = NULL;
/*
* An SDL_Surface is a 2D image. A 2D image can be loaded from a file or
* be the image inside a window. (We are using the second use case)
*
* SDL_Surfaces are used for CPU rendering
*/
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) { // SDL_Init returns -1 upon error
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
// SDL_GetError() returns the most recent error. Remember it and use it everywhere!
return -1; // Logically this return statement makes sense but isn't in the tutorial?
}
// initialization was successful
window = SDL_CreateWindow("MyFirstWindow",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, //
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (window == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return -1;
}
// Get the window surface
screenSurface = SDL_GetWindowSurface(window);
*windowOut = window;
*screenSurfaceOut = screenSurface;
return 0;
}
void doLoop(SDL_Window *window, SDL_Surface *screenSurface) {
// Event loop
SDL_Event e;
bool quit = false;
// we are going to render before the actual loop (to be changed later) TODO
// Fill surface black
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00));
float camdownangle = 25.f;
// dummy data for the triangles
Tri triArray[] = {
{
{{0, 100, 0}, {255,0,0,255}},
{{75, 0, -75}, {0,255,0,255}},
{{-75, 0, -75}, {0,0,255,255}},
},
{
{{-75, 0, 75}, {255,0,0,255}},
{{0, 100, 0}, {0,255,0,255}},
{{75, 0, 75}, {0,0,255,255}},
},
{
{{75, 0, -75}, {255,0,0,255}},
{{75, 0, 75}, {0,255,0,255}},
{{0, 100, 0}, {0,0,255,255}},
},
{
{{0, 100, 0}, {255,0,0,255}},
{{-75, 0, 75}, {0,255,0,255}},
{{-75, 0, -75}, {0,0,255,255}},
},
{
{{0, -100, 0}, {255,0,0,255}},
{{75, 0, -75}, {0,255,0,255}},
{{-75, 0, -75}, {0,0,255,255}},
},
{
{{-75, 0, 75}, {255,0,0,255}},
{{0, -100, 0}, {0,255,0,255}},
{{75, 0, 75}, {0,0,255,255}},
},
{
{{75, 0, -75}, {255,0,0,255}},
{{75, 0, 75}, {0,255,0,255}},
{{0, -100, 0}, {0,0,255,255}},
},
{
{{0, -100, 0}, {255,0,0,255}},
{{-75, 0, 75}, {0,255,0,255}},
{{-75, 0, -75}, {0,0,255,255}},
}
};
int triCount = sizeof(triArray) / sizeof(Tri);
Mat4 camera = IDENTITY;
// tilt camera down
camera.values[1][1] = cosf(camdownangle);
camera.values[1][2] = -sinf(camdownangle);
camera.values[2][1] = sinf(camdownangle);
camera.values[2][2] = cosf(camdownangle);
// translate camera
camera.values[1][3] = 30.f; // y
camera.values[2][3] = 300.f; // z
Mat4 model = IDENTITY;
// render3D(triArray, triCount,screenSurface, camera);
// // Update the surface
// SDL_UpdateWindowSurface(window);
// do the main loop...
// continue to poll for events until a quit event is reached
float mvmt = 3.f;
// rotation matrix definitions
float deg = 0.01f;
Mat4 leftrot = {{
{cosf(deg),0,-sinf(deg),0},
{0, 1, 0, 0},
{sinf(deg), 0, cosf(deg),0},
{0,0,0,1},
}};
Mat4 rightrot = {{
{1,0,0,0},
{0, cosf(-deg), -sinf(-deg),0},
{0, sinf(-deg), cosf(-deg),0},
{0,0,0,1},
}};
Mat4 rot = multiply(leftrot, rightrot);
const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
while (!quit) {
// process all events in the event queue - SDL_PollEvent will return 0 upon emptying the queue
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT)
quit = true;
}
// refresh the screen
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00));
render3D(triArray, triCount,screenSurface, model, camera);
// Update the surface
SDL_UpdateWindowSurface(window);
// pan camera left/right controls
if (keyboardState[SDL_SCANCODE_LEFT]) {
camera.values[0][3] -= mvmt;
}
if (keyboardState[SDL_SCANCODE_RIGHT]) {
camera.values[0][3] += mvmt;
}
// if space is held, up/down controls z axis camera mvmnt.
// otherwise default to panning on y axis.
if (keyboardState[SDL_SCANCODE_SPACE]) {
if (keyboardState[SDL_SCANCODE_UP]) {
camera.values[2][3] += mvmt;
}
if (keyboardState[SDL_SCANCODE_DOWN]) {
camera.values[2][3] -= mvmt;
}
}
else {
if (keyboardState[SDL_SCANCODE_UP]) {
camera.values[1][3] += mvmt;
}
if (keyboardState[SDL_SCANCODE_DOWN]) {
camera.values[1][3] -= mvmt;
}
}
model = multiply(rot, model);
// printMatrix(camera);
}
}
int gameExit(SDL_Window *window, SDL_Surface *screenSurface) {
// destruct z buffer in renderer
destructZbuf(screenSurface->w, screenSurface->h);
// Destroy window
SDL_DestroyWindow(window);
// Quit SDL subsystems
SDL_Quit();
return 0;
}