-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cpp
199 lines (172 loc) · 4.56 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
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
/*This source code copyrighted by Lazy Foo' Productions (2004-2013)
and may not be redistributed without written permission.*/
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "Background.h"
#include "Player.h"
#include "Enemy.h"
#include "Timer.h"
#include <string>
//Screen attributes
const int SCREEN_WIDTH = 1000;
const int SCREEN_HEIGHT = 500;
const int SCREEN_BPP = 32;
SDL_Surface *screen = NULL;
Timer* update;
std::string toString(int number)
{
if (number == 0)
return "0";
std::string temp="";
std::string returnvalue="";
while (number>0)
{
temp+=number%10+48;
number/=10;
}
for (int i=0;i<(int)temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Initialize SDL_mixer
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
{
return false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//Set the window caption
SDL_WM_SetCaption( "SDL Runner Lab Progra 3", NULL );
//If everything initialized fine
return true;
}
void frameCap()
{
int frames_per_seccond = 15;
if(update->get_ticks() < 1000 / frames_per_seccond)
{
//Sleep the remaining frame time
SDL_Delay( ( 1000 / frames_per_seccond ) - update->get_ticks() );
}
update->start();
}
int main( int argc, char* args[] )
{
//Initialize
init();
update=new Timer();
update->start();
SDL_Surface * game_over = IMG_Load( "game_over.png" );
TTF_Font *font = TTF_OpenFont( "lazy.ttf", 30 );
SDL_Color textColor = { 0, 0, 0 };
SDL_Surface * score_surface = NULL;
Mix_Chunk *jump = Mix_LoadWAV( "jump.ogg" );
Background background(screen);
Player player(screen);
Enemy enemy(screen);
SDL_Event event;
//Quit flag
bool quit = false;
int score=0;
while( quit == false )
{
//If there's an event to handle
if( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the proper message surface
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE: quit = true; break;
case SDLK_UP:
player.jump();
Mix_PlayChannel( -1, jump, 0 );
break;
}
}
//If the user has Xed out the window
else if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
background.logic();
player.logic();
enemy.logic();
SDL_Rect offset;
offset.x = 0;
offset.y = 0;
SDL_Surface * score_surface = TTF_RenderText_Solid( font, toString(score+=5).c_str(), textColor );
SDL_BlitSurface( score_surface, NULL, screen, &offset );
SDL_Flip( screen );
SDL_FreeSurface( score_surface );
if(player.x-enemy.x<50
&& player.x-enemy.x>-50
&& player.y-enemy.y<50
&& player.y-enemy.y>-50
)
{
break;
}
background.render();
player.render();
enemy.render();
frameCap();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
while( quit == false )
{
//If there's an event to handle
if( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the proper message surface
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE: quit = true; break;
}
}
//If the user has Xed out the window
else if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
SDL_Rect offset;
offset.x = 0;
offset.y = 0;
SDL_BlitSurface( game_over, NULL, screen, &offset );
frameCap();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
//SDL_Quit();
return 0;
}