-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.cpp
293 lines (274 loc) · 9.08 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
// Template, major revision 3, beta
// IGAD/NHTV - Jacco Bikker - 2006-2009
// Note:
// This version of the template attempts to setup a rendering surface in system RAM
// and copies it to VRAM using DMA. On recent systems, this yields extreme performance,
// and flips are almost instant. For older systems, there is a fall-back path that
// uses a more conventional approach offered by SDL. If your system uses this, the
// window caption will indicate this. In this case, you may want to tweak the video
// mode setup code for optimal performance.
#pragma warning (disable : 4530) // complaint about exception handler
#pragma warning (disable : 4273)
#pragma warning (disable : 4311) // pointer truncation from HANDLE to long
#include <windows.h>
extern "C"
{
#include "glew.h"
}
#include "gl.h"
#include "io.h"
#include <ios>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "SDL.h"
#include "SDL_syswm.h"
#include "wglext.h"
#include "../include/headers.h"
#include "fcntl.h"
int SCRWIDTH = -1;
int SCRHEIGHT = -1;
namespace Tmpl8 {
void NotifyUser( char* s )
{
HWND hApp = FindWindow( NULL, "Template" );
MessageBox( hApp, s, "ERROR", MB_OK );
exit( 0 );
}
}
using namespace Tmpl8;
using namespace std;
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;
static int SCRPITCH = 0;
int ACTWIDTH, ACTHEIGHT;
static bool FULLSCREEN = false, firstframe = true;
Surface* surface = 0;
Game* game = 0;
double lastftime = 0;
LARGE_INTEGER lasttime, ticksPS;
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 );
QueryPerformanceFrequency( &ticksPS );
surface = new Surface( SCRWIDTH, SCRHEIGHT, 0, SCRWIDTH );
surface->InitCharset();
return true;
}
void redirectIO()
{
static const WORD MAX_CONSOLE_LINES = 500;
int hConHandle;
long lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
AllocConsole();
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
&coninfo);
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
coninfo.dwSize);
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
ios::sync_with_stdio();
}
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_SwapBuffers();
}
int main( int argc, char **argv )
{
redirectIO();
SDL_Init( SDL_INIT_VIDEO );
int scr_width = GetSystemMetrics(SM_CXSCREEN);
int scr_height = GetSystemMetrics(SM_CYSCREEN);
if(scr_width < 1024 || scr_height < 768) {
MessageBox(NULL, "You need a resolution of at least 1024x768 to run this game.", "GameOfLife v1.0", MB_OK);
exit(0);
}
if (MessageBox(NULL, "Run in fullscreen?", "GameOfLife v1.0", MB_YESNO) == IDYES) {
SCRWIDTH = scr_width;
SCRHEIGHT = scr_height;
SDL_SetVideoMode(SCRWIDTH, SCRHEIGHT, 32, SDL_OPENGL | SDL_FULLSCREEN);
} else {
SCRHEIGHT = scr_height-75-(scr_height-75)%32;
SCRWIDTH = (scr_height/3)*4;
SDL_SetVideoMode(SCRWIDTH, SCRHEIGHT, 32, SDL_OPENGL);
}
SDL_EnableKeyRepeat( 0, 0 );
bool vbo = true;
if (!init())
{
SDL_SetVideoMode( SCRWIDTH, SCRHEIGHT, 32, SDL_HWSURFACE|SDL_DOUBLEBUF );
SDL_Surface* s = SDL_GetVideoSurface();
SCRHEIGHT = scr_height-75-(scr_height-75)%32;
SCRWIDTH = (scr_height/3)*4;
surface = new Surface( SCRWIDTH, SCRHEIGHT, (Pixel*)s->pixels, s->pitch );
surface->InitCharset();
vbo = false;
SDL_WM_SetCaption( "GameOfLife v1.0 by Alexander Dzhoganov", NULL );
}
else SDL_WM_SetCaption( "GameOfLife v1.0 by Alexander Dzhoganov", NULL );
SDL_EnableUNICODE(true);
int exitapp = 0;
game = new Game();
game->SetTarget( surface );
while (!exitapp)
{
if (vbo) // frame buffer swapping for VBO mode
{
swap();
surface->SetBuffer( (Pixel*)framedata );
}
else // frame buffer swapping for fall-back path
{
SDL_Surface* s = SDL_GetVideoSurface();
SDL_UpdateRect( s, 0, 0, 0, 0 );
framedata = (unsigned char*)s->pixels;
surface->SetPitch( s->pitch / 4 );
surface->SetBuffer( (Pixel*)framedata );
}
if (firstframe)
{
game->Init();
firstframe = false;
}
// calculate frame time and pass it to game->Tick
LARGE_INTEGER start, end;
QueryPerformanceCounter( &start );
game->Tick( (float)lastftime );
QueryPerformanceCounter( &end );
lastftime = float( end.QuadPart - start.QuadPart ) / float( ticksPS.QuadPart / 1000 );
// 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.unicode, event.key.keysym.scancode );
break;
case SDL_KEYUP:
game->KeyUp( event.key.keysym.unicode, event.key.keysym.scancode );
break;
case SDL_MOUSEMOTION:
game->MouseMove( event.motion.x, event.motion.y );
break;
case SDL_MOUSEBUTTONUP:
game->MouseUp( event.button.button );
break;
case SDL_MOUSEBUTTONDOWN:
game->MouseDown( event.button.button );
break;
default:
// more info on events in SDL: http://sdl.beuc.net/sdl.wiki/SDL_Event
break;
}
}
}
SDL_Quit();
return 1;
}