-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.cpp
328 lines (307 loc) · 10.3 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
// Template, UU version
// IGAD/NHTV/UU - Jacco Bikker - 2006-2017
// 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 : 4273)
#pragma warning (disable : 4311) // pointer truncation from HANDLE to long
#endif
#include "precomp.h"
namespace Tmpl8 {
double timer::inv_freq = 1;
// 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::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( NULL, TEMPLATE_VERSION );
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
void 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();
freopen( "CON", "w", stdout );
freopen( "CON", "w", stderr );
}
#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, SCRWIDTH, SCRHEIGHT, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL );
glBindTexture(GL_TEXTURE_2D, 0);
if (glGetError()) return false;
}
const int sizeMemory = 4 * SCRWIDTH * SCRHEIGHT;
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, SCRWIDTH * SCRHEIGHT * 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, SCRWIDTH, SCRHEIGHT );
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( SCRWIDTH, SCRHEIGHT, 0, SCRWIDTH );
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, SCRWIDTH, SCRHEIGHT, 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 )
{
#ifdef _MSC_VER
redirectIO();
#endif
printf( "application started.\n" );
SDL_Init( SDL_INIT_VIDEO );
#ifdef ADVANCEDGL
#ifdef FULLSCREEN
window = SDL_CreateWindow( TEMPLATE_VERSION, 100, 100, SCRWIDTH, SCRHEIGHT, SDL_WINDOW_FULLSCREEN|SDL_WINDOW_OPENGL );
#else
window = SDL_CreateWindow( TEMPLATE_VERSION, 100, 100, SCRWIDTH, SCRHEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL );
#endif
SDL_GLContext glContext = SDL_GL_CreateContext( window);
init();
ShowCursor( false );
#else
#ifdef FULLSCREEN
window = SDL_CreateWindow( TEMPLATE_VERSION, 100, 100, SCRWIDTH, SCRHEIGHT, SDL_WINDOW_FULLSCREEN );
#else
window = SDL_CreateWindow( TEMPLATE_VERSION, 100, 100, SCRWIDTH, SCRHEIGHT, SDL_WINDOW_SHOWN );
#endif
surface = new Surface( SCRWIDTH, SCRHEIGHT );
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, SCRWIDTH, SCRHEIGHT );
#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(), SCRWIDTH * SCRHEIGHT * 4 );
}
else
{
unsigned char* t = (unsigned char*)target;
for( int i = 0; i < SCRHEIGHT; i++ )
{
memcpy( t, surface->GetBuffer() + i * SCRWIDTH, SCRWIDTH * 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
game->Tick( t.elapsed() );
t.reset();
// 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 1;
}