-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·132 lines (101 loc) · 2.69 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
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include "timer.cpp"
#include "tools.cpp"
#include "game.cpp"
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 30;
Timer fps;
int frame = 0;
bool quit = false;
bool pauseGame = false;
bool gameOver = false;
bool runningGame = false;
bool firstRun = true;
int main( int argc, char* args[] ) {
SDL_Init( SDL_INIT_EVERYTHING );
TTF_Init( );
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_WM_SetCaption( "Twoxy 3", NULL );
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
// accelerometer input
SDL_Joystick *accelerometer = SDL_JoystickOpen( 0 );
// my game object
Game myGame;
// main loop
while( !quit ) {
if( !runningGame && firstRun ) {
// show intro screens
myGame.showIntroScreens( screen );
}
while( runningGame && !gameOver && !quit ) {
// clear screen
if( !pauseGame ) {
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
}
fps.start( );
// handle events
while( SDL_PollEvent( &event ) ) {
if( event.type == SDL_QUIT ) {
quit = true;
break;
}
myGame.handleInput( event, accelerometer );
}
if( !pauseGame ) {
// handle logic
myGame.handleMovement( );
// handle rendering
myGame.render( screen );
gameOver = myGame.checkState( );
// draw screen
SDL_Flip( screen );
}
frame++;
// frame limiter
if( fps.getTicks( ) < 1000 / FRAMES_PER_SECOND ) {
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.getTicks( ) );
}
}
if( gameOver ) {
runningGame = false;
myGame.showGameOverScreen( screen );
}
// handle events
while( SDL_PollEvent( &event ) ) {
if( event.type == SDL_QUIT ) {
quit = true;
break;
}
if( event.type == SDL_KEYDOWN || event.type == SDL_MOUSEBUTTONDOWN ) {
// here be dragons, that is some pretty messed up code here
if( event.key.keysym.sym == SDLK_RETURN || event.type == SDL_MOUSEBUTTONDOWN ) {
gameOver = false;
myGame.initialize( );
}
if( firstRun ) {
myGame.nextIntro( );
} else {
runningGame = true;
}
break;
}
if( event.key.keysym.sym == SDLK_ESCAPE ) {
quit = true;
break;
}
}
// draw screen
SDL_Flip( screen );
}
// free my stuff
SDL_FreeSurface( screen );
// cleanup sdl stuff
TTF_Quit( );
SDL_Quit( );
return 0;
}