forked from UnitecProgra3/Esquivador
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
221 lines (179 loc) · 4.62 KB
/
main.cpp
File metadata and controls
221 lines (179 loc) · 4.62 KB
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
/*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_ttf.h"
#include "Personaje.h"
#include "Enemigo.h"
#include "Fantasmita.h"
#include <string>
#include <vector>
using namespace std;
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *up = NULL;
SDL_Surface *down = NULL;
SDL_Surface *left = NULL;
SDL_Surface *right = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The font
TTF_Font *font = NULL;
//The color of the font
SDL_Color textColor = { 0, 0, 0 };
SDL_Surface *load_image( std::string filename )
{
return IMG_Load( filename.c_str() );
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Press an Arrow Key", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the background image
background = load_image( "background.png" );
//Open the font
font = TTF_OpenFont( "lazy.ttf", 72 );
//If there was a problem in loading the background
if( background == NULL )
{
return false;
}
//If there was an error in loading the font
if( font == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( up );
SDL_FreeSurface( down );
SDL_FreeSurface( left );
SDL_FreeSurface( right );
//Close the font
TTF_CloseFont( font );
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Render the text
up = TTF_RenderText_Solid( font, "Up", textColor );
down = TTF_RenderText_Solid( font, "Down", textColor );
left = TTF_RenderText_Solid( font, "Left", textColor );
right = TTF_RenderText_Solid( font, "Right", textColor );
Personaje personaje (20,50,load_image("personaje.png"));
vector<Enemigo*> enemigos;
enemigos.push_back(new Enemigo(100,200,load_image("enemigo.png")));
enemigos.push_back(new Enemigo(150,250,load_image("enemigo.png")));
enemigos.push_back(new Enemigo(200,300,load_image("enemigo.png")));
enemigos.push_back(new Fantasmita(200,300));
//While the user hasn't quit
while( quit == false )
{
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Apply the background
apply_surface( 0, 0, background, screen );
apply_surface( personaje.x, personaje.y, personaje.surface, screen);
for(int i=0;i<enemigos.size();i++)
{
apply_surface( enemigos[i]->x, enemigos[i]->y, enemigos[i]->surface, screen);
enemigos[i]->moverse();
}
//Get the keystates
Uint8 *keystates = SDL_GetKeyState( NULL );
//If up is pressed
if( keystates[ SDLK_UP ] )
{
personaje.y--;
}
//If down is pressed
if( keystates[ SDLK_DOWN ] )
{
personaje.y++;
}
//If left is pressed
if( keystates[ SDLK_LEFT ] )
{
personaje.x--;
}
//If right is pressed
if( keystates[ SDLK_RIGHT ] )
{
personaje.x++;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
//Clean up
clean_up();
return 0;
}