-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.cpp
408 lines (374 loc) · 11.7 KB
/
template.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
// Template, BUAS version https://www.buas.nl/games
// IGAD/BUAS(NHTV)/UU - Jacco Bikker - 2006-2020
// Note:
// this version of the template uses SDL2 for all frame buffer interaction
// see: https://www.libsdl.org
#ifdef _MSC_VER
#pragma warning (disable : 4530) // complaint about exception handler
#pragma warning (disable : 4311) // pointer truncation from HANDLE to long
#endif
//#define FULLSCREEN
//#define ADVANCEDGL
#include "game.h"
#include <fcntl.h>
#include <io.h>
#include "template.h"
#include <corecrt_math.h>
#include <SDL.h>
#include "surface.h"
#include <cstdio>
#include <iostream>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef ADVANCEDGL
#define GLEW_BUILD
extern "C"
{
#include "glew.h"
}
#include "gl.h"
#include "wglext.h"
#endif
namespace Tmpl8 {
double timer::inv_freq = 1;
timer::timer(): start(get())
{
init();
}
float timer::elapsed() const
{
return (float)((get() - start) * inv_freq);
}
timer::value_type timer::get()
{
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return c.QuadPart;
}
double timer::to_time(const value_type vt)
{
return double(vt) * inv_freq;
}
void timer::reset()
{
start = get();
}
void timer::init()
{
LARGE_INTEGER f;
QueryPerformanceFrequency(&f);
inv_freq = 1000. / double(f.QuadPart);
}
// Math Stuff
// ----------------------------------------------------------------------------
vec3 normalize( const vec3& v ) { return v.normalized(); }
vec3 cross( const vec3& a, const vec3& b ) { return a.cross( b ); }
float dot( const vec3& a, const vec3& b ) { return a.dot( b ); }
vec3 operator * ( const float& s, const vec3& v ) { return vec3( v.x * s, v.y * s, v.z * s ); }
vec3 operator * ( const vec3& v, const float& s ) { return vec3( v.x * s, v.y * s, v.z * s ); }
vec4 operator * ( const float& s, const vec4& v ) { return vec4( v.x * s, v.y * s, v.z * s, v.w * s ); }
vec4 operator * ( const vec4& v, const float& s ) { return vec4( v.x * s, v.y * s, v.z * s, v.w * s ); }
vec4 operator * ( const vec4& v, const mat4& M )
{
vec4 mx( M.cell[0], M.cell[4], M.cell[8], M.cell[12] );
vec4 my( M.cell[1], M.cell[5], M.cell[9], M.cell[13] );
vec4 mz( M.cell[2], M.cell[6], M.cell[10], M.cell[14] );
vec4 mw( M.cell[3], M.cell[7], M.cell[11], M.cell[15] );
return v.x * mx + v.y * my + v.z * mz + v.w * mw;
}
mat4::mat4()
{
memset(cell, 0, 64);
cell[0] = cell[5] = cell[10] = cell[15] = 1;
}
mat4 mat4::identity()
{
mat4 r;
memset(r.cell, 0, 64);
r.cell[0] = r.cell[5] = r.cell[10] = r.cell[15] = 1.0f;
return r;
}
mat4 mat4::rotate( const vec3 l, const float a )
{
// http://inside.mines.edu/fs_home/gmurray/ArbitraryAxisRotation
mat4 M;
const float u = l.x, v = l.y, w = l.z, ca = cosf( a ), sa = sinf( a );
M.cell[0] = u * u + (v * v + w * w) * ca, M.cell[1] = u * v * (1 - ca) - w * sa;
M.cell[2] = u * w * (1 - ca) + v * sa, M.cell[4] = u * v * (1 - ca) + w * sa;
M.cell[5] = v * v + (u * u + w * w) * ca, M.cell[6] = v * w * (1 - ca) - u * sa;
M.cell[8] = u * w * (1 - ca) - v * sa, M.cell[9] = v * w * (1 - ca) + u * sa;
M.cell[10] = w * w + (u * u + v * v) * ca;
M.cell[3] = M.cell[7] = M.cell[11] = M.cell[12] = M.cell[13] = M.cell[14] = 0, M.cell[15] = 1;
return M;
}
mat4 mat4::rotatex( const float a )
{
mat4 M;
const float ca = cosf( a ), sa = sinf( a );
M.cell[5] = ca, M.cell[6] = -sa;
M.cell[9] = sa, M.cell[10] = ca;
return M;
}
mat4 mat4::rotatey( const float a )
{
mat4 M;
const float ca = cosf( a ), sa = sinf( a );
M.cell[0] = ca, M.cell[2] = sa;
M.cell[8] = -sa, M.cell[10] = ca;
return M;
}
mat4 mat4::rotatez( const float a )
{
mat4 M;
const float ca = cosf( a ), sa = sinf( a );
M.cell[0] = ca, M.cell[1] = -sa;
M.cell[4] = sa, M.cell[5] = ca;
return M;
}
void NotifyUser( char* s )
{
HWND hApp = FindWindow(nullptr, TemplateVersion);
MessageBox( hApp, s, "ERROR", MB_OK );
exit( 0 );
}
}
using namespace Tmpl8;
using namespace std;
#ifdef ADVANCEDGL
PFNGLGENBUFFERSPROC glGenBuffers = 0;
PFNGLBINDBUFFERPROC glBindBuffer = 0;
PFNGLBUFFERDATAPROC glBufferData = 0;
PFNGLMAPBUFFERPROC glMapBuffer = 0;
PFNGLUNMAPBUFFERPROC glUnmapBuffer = 0;
typedef BOOL(APIENTRY* PFNWGLSWAPINTERVALFARPROC)(int);
PFNWGLSWAPINTERVALFARPROC wglSwapIntervalEXT = 0;
unsigned int framebufferTexID[2];
GLuint fbPBO[2];
unsigned char* framedata = 0;
#endif
int ACTWIDTH, ACTHEIGHT;
static bool firstframe = true;
Surface* surface = 0;
Game* game = 0;
SDL_Window* window = 0;
#ifdef _MSC_VER
bool redirectIO()
{
CONSOLE_SCREEN_BUFFER_INFO coninfo;
AllocConsole();
GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &coninfo );
coninfo.dwSize.Y = 500;
SetConsoleScreenBufferSize( GetStdHandle( STD_OUTPUT_HANDLE ), coninfo.dwSize );
HANDLE h1 = GetStdHandle( STD_OUTPUT_HANDLE );
int h2 = _open_osfhandle( (intptr_t)h1, _O_TEXT );
FILE* fp = _fdopen( h2, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
h1 = GetStdHandle( STD_INPUT_HANDLE ), h2 = _open_osfhandle( (intptr_t)h1, _O_TEXT );
fp = _fdopen( h2, "r" ), *stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
h1 = GetStdHandle( STD_ERROR_HANDLE ), h2 = _open_osfhandle( (intptr_t)h1, _O_TEXT );
fp = _fdopen( h2, "w" ), *stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
ios::sync_with_stdio();
FILE* stream;
if ((stream = freopen("CON", "w", stdout)) == NULL)
return false;
if ((stream = freopen("CON", "w", stderr)) == NULL)
return false;
return true;
}
#endif
#ifdef ADVANCEDGL
bool createFBtexture()
{
glGenTextures( 2, framebufferTexID );
if (glGetError()) return false;
for ( int i = 0; i < 2; i++ )
{
glBindTexture( GL_TEXTURE_2D, framebufferTexID[i] );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, ScreenWidth, ScreenHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL );
glBindTexture(GL_TEXTURE_2D, 0);
if (glGetError()) return false;
}
const int sizeMemory = 4 * ScreenWidth * ScreenHeight;
glGenBuffers( 2, fbPBO );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[0] );
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, sizeMemory, NULL, GL_STREAM_DRAW_ARB );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[1] );
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, sizeMemory, NULL, GL_STREAM_DRAW_ARB );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
glBindTexture( GL_TEXTURE_2D, framebufferTexID[0] );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[0] );
framedata = (unsigned char*)glMapBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB );
if (!framedata) return false;
memset( framedata, 0, ScreenWidth * ScreenHeight * 4 );
return (glGetError() == 0);
}
bool init()
{
fbPBO[0] = fbPBO[1] = -1;
glGenBuffers = (PFNGLGENBUFFERSPROC)wglGetProcAddress( "glGenBuffersARB" );
glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress( "glBindBufferARB" );
glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress( "glBufferDataARB" );
glMapBuffer = (PFNGLMAPBUFFERPROC)wglGetProcAddress( "glMapBufferARB" );
glUnmapBuffer = (PFNGLUNMAPBUFFERPROC)wglGetProcAddress( "glUnmapBufferARB" );
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALFARPROC)wglGetProcAddress( "wglSwapIntervalEXT" );
if ((!glGenBuffers) || (!glBindBuffer) || (!glBufferData) || (!glMapBuffer) || (!glUnmapBuffer)) return false;
if (glGetError()) return false;
glViewport( 0, 0, ScreenWidth, ScreenHeight );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 1, 0, 1, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
glShadeModel( GL_SMOOTH );
if (!createFBtexture()) return false;
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
if (wglSwapIntervalEXT) wglSwapIntervalEXT( 0 );
surface = new Surface( ScreenWidth, ScreenHeight, 0, ScreenWidth );
return true;
}
void swap()
{
static int index = 0;
int nextindex;
glUnmapBuffer( GL_PIXEL_UNPACK_BUFFER_ARB );
glBindTexture( GL_TEXTURE_2D, framebufferTexID[index] );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[index] );
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, ScreenWidth, ScreenHeight, GL_BGRA, GL_UNSIGNED_BYTE, 0 );
nextindex = (index + 1) % 2;
index = (index + 1) % 2;
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[nextindex] );
framedata = (unsigned char*)glMapBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB );
glColor3f( 1.0f, 1.0f, 1.0f );
glBegin( GL_QUADS );
glNormal3f( 0, 0, 1 );
glTexCoord2f( 0.0f, 0.0f );
glVertex2f ( 0.0f, 1.0f );
glTexCoord2f( 1.0f, 0.0f );
glVertex2f ( 1.0f, 1.0f );
glTexCoord2f( 1.0f, 1.0f );
glVertex2f ( 1.0f, 0.0f );
glTexCoord2f( 0.0f, 1.0f );
glVertex2f ( 0.0f, 0.0f );
glEnd();
glBindTexture( GL_TEXTURE_2D, 0 );
SDL_GL_SwapWindow( window );
}
#endif
int main( int argc, char **argv )
{
SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, "0", SDL_HINT_OVERRIDE); //VSYNC off|Uncap fps
#ifdef _MSC_VER
if (!redirectIO())
return 1;
#endif
printf( "application started.\n" );
SDL_Init( SDL_INIT_VIDEO );
#ifdef ADVANCEDGL
#ifdef FULLSCREEN
window = SDL_CreateWindow(TemplateVersion, 100, 100, ScreenWidth, ScreenHeight, SDL_WINDOW_FULLSCREEN|SDL_WINDOW_OPENGL );
#else
window = SDL_CreateWindow(TemplateVersion, 100, 100, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL );
#endif
SDL_GLContext glContext = SDL_GL_CreateContext( window);
init();
ShowCursor( false );
#else
#ifdef FULLSCREEN
window = SDL_CreateWindow(TemplateVersion, 100, 100, ScreenWidth, ScreenHeight, SDL_WINDOW_FULLSCREEN );
#else
window = SDL_CreateWindow(TemplateVersion, 100, 100, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN );
#endif
surface = new Surface( ScreenWidth, ScreenHeight );
surface->Clear( 0 );
SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
SDL_Texture* frameBuffer = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, ScreenWidth, ScreenHeight );
#endif
int exitapp = 0;
game = new Game();
game->SetTarget( surface );
timer t;
t.reset();
while (!exitapp)
{
#ifdef ADVANCEDGL
swap();
surface->SetBuffer( (Pixel*)framedata );
#else
void* target = 0;
int pitch;
SDL_LockTexture( frameBuffer, NULL, &target, &pitch );
if (pitch == (surface->GetWidth() * 4))
{
memcpy( target, surface->GetBuffer(), ScreenWidth * ScreenHeight * 4 );
}
else
{
unsigned char* t = (unsigned char*)target;
for( int i = 0; i < ScreenHeight; i++ )
{
memcpy( t, surface->GetBuffer() + i * ScreenWidth, ScreenWidth * 4 );
t += pitch;
}
}
SDL_UnlockTexture( frameBuffer );
SDL_RenderCopy( renderer, frameBuffer, NULL, NULL );
SDL_RenderPresent( renderer );
#endif
if (firstframe)
{
game->Init();
firstframe = false;
}
// calculate frame time and pass it to game->Tick
float elapsedTime = t.elapsed();
t.reset();
game->Tick( elapsedTime );
// event loop
SDL_Event event;
while (SDL_PollEvent( &event ))
{
switch (event.type)
{
case SDL_QUIT:
exitapp = 1;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
exitapp = 1;
// find other keys here: http://sdl.beuc.net/sdl.wiki/SDLKey
}
game->KeyDown( event.key.keysym.scancode );
break;
case SDL_KEYUP:
game->KeyUp( event.key.keysym.scancode );
break;
case SDL_MOUSEMOTION:
game->MouseMove( event.motion.xrel, event.motion.yrel );
break;
case SDL_MOUSEBUTTONUP:
game->MouseUp( event.button.button );
break;
case SDL_MOUSEBUTTONDOWN:
game->MouseDown( event.button.button );
break;
default:
break;
}
}
}
game->Shutdown();
SDL_Quit();
return 0;
}