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
|
#include "stdafx.h"
/*Global variables for the game.*/
bool app = true;
bool keys[323] = {false};
bool btns[3] = {false};
Map *mainmap;
Actor_main *actor;
int ticks1, ticks2;
int oldclip=1, rightpressed =0, leftpressed = 0;
float vertical_speed = 0.0, falltime = 0.0;
bool jumplock;
/*Definition for our surfaces and such.*/
SDL_Surface *viewport, *backg, *buddy, *text; //our surfaces: the viewport, the background, the actor and the text
SDL_Event event; //our event handler
SDL_Rect src_rect, dst_rect; //our source rectangle and dest rectangle
SDL_Color white = {255,255,255,0}; //some colors
SDL_Color grey = {65,65,65,0};
SDL_Color magenta = {255,0,255,0};
Mix_Chunk *jumpfx; //Sound effect for the jump
Mix_Music *music; //background music
TTF_Font *font; //font
/*Definition of our functions */
void init();
void loadobjects();
void updateinput(); //belongs to input
void handleinput(); //belongs to input
void handlephysics(); //belongs to actor_main
void renderscene();
void freeobjects();
void deinit();
void jump(); //belongs to actor_main
/*My main man*/
int main(int argc, char *argv[])
{
//boring stuff.
std::cout << "Initializing... \n";
init();
std::cout << "Done! \n";
std::cout << "Loading Objects... \n";
loadobjects();
std::cout << "Done! \n";
//Play it again, Sam
Mix_PlayMusic(music, 0);
//It's aliiive, ALIIIVE!
while(app)
{
//I heard seaman 2 has ticks!
ticks1 = SDL_GetTicks();
//don't mind these fellas
updateinput();
handleinput();
handlephysics();
renderscene();
//Psh, Semantics!
ticks2 = SDL_GetTicks();
if ((ticks2-ticks1) < FRAME_TIME) SDL_Delay(FRAME_TIME - (ticks2-ticks1));
}
//freedom, yay!
freeobjects();
deinit();
return 0;
}
/* Initialization of components */
void init()
{
SDL_Init(SDL_INIT_EVERYTHING); //let's start SDL
TTF_Init(); //Also the TTF library
Mix_OpenAudio(44100, AUDIO_S16SYS, 1, 2048); //And finally the Audio system.
mainmap = new Map;
}
/* deinitialization of components */
void deinit()
{
TTF_Quit(); //bye bye TTF library
Mix_CloseAudio(); //bye bye Audio Mixer
SDL_Quit(); //bye bye SDL
}
/*Load our basic objects*/
void loadobjects()
{
viewport = SDL_SetVideoMode( SCREEN_WIDTH, //Define our viewport
SCREEN_HEIGHT,
SCREEN_DEPTH,
SCREEN_PROPS );
/**********LOAD SPRITES*******************************/
actor = new Actor_main(20,75,"data/mansprite.png");
/**********LOAD AUDIO STUFF***************************/
jumpfx = Mix_LoadWAV("data/evasion.ogg"); //este es el SFX de evasion.
Mix_VolumeChunk(jumpfx,128);
//music = Mix_LoadMUS("data/Air1_2.mid"); //este es el audio de fondo
//Mix_VolumeMusic(128);
/**********LOAD FONT STUFF*****************************/
font = TTF_OpenFont("data/fipps.ttf", 24); //algo del font ... hmm :P
text = TTF_RenderText_Solid(font, "Glitch Test", grey);
}
/*Automatic Updates*/
void updateinput() //this is our super pooler
{
while(SDL_PollEvent(&event)){
if (event.type == SDL_KEYDOWN) keys[event.key.keysym.sym] = true; //si esta presionada la tecla, entonces es TRUE!
if (event.type == SDL_KEYUP) keys[event.key.keysym.sym] = false; //si no esta presionada la tecla, entonces es FALSE!
if (event.type == SDL_MOUSEBUTTONDOWN) btns[event.button.button] = true;//igual para el mouse
if (event.type == SDL_MOUSEBUTTONUP) btns[event.button.button] = false;
}
}
/*Love Handles*/
void handleinput()
{
if(keys[SDLK_LEFT]){ //si estamos presionando la izquierda
if(leftpressed == 0){ //si no hay tiempo registrado
leftpressed = ticks1; //le damos un tiempo
}
if( (leftpressed > rightpressed || !keys[SDLK_RIGHT]) && actor->canMove(LEFT, actor->get_x(), actor->get_y(), mainmap) ){ //si presionamos la izquierda despues que la derecha o no esta la derecha
actor->set_x(actor->get_x() - BUDDY_SPEED);
oldclip=1;
}
}else{
leftpressed = 0;
}
/*******************************************************************/
if(keys[SDLK_RIGHT]){ //si estamos presionando la derecha
if(rightpressed == 0){ //si no hay tiempo registrado
rightpressed = ticks1; //le damos un tiempo
}
if( (leftpressed < rightpressed || !keys[SDLK_LEFT]) && actor->canMove(RIGHT, actor->get_x(), actor->get_y(), mainmap) ){ //si presionamos la derecha despues que la izquierda o no esta la izquierda
actor->set_x(actor->get_x() + BUDDY_SPEED);
oldclip=2;
}
}else{
rightpressed = 0; //si se ha soltado la tecla reseteamos rightpressed
}
/*****************************************************************/
if(keys[SDLK_LEFT] || keys[SDLK_RIGHT] || keys[SDLK_x]){ //cualquier movimiento pertinente, debemos animar.
actor->animate();
}else{
actor->set_clip_x(0); //en cuanto se detenga, pues lo devolvemos a donde debiera
}
/*************************************************************/
if(keys[SDLK_x]){ //z is for jumping.
jump(); //ahora si, estamos brincando
}else{ //si z no esta presionado
if(jumplock){ //y ya habiamos aterrizado
jumplock = false; //entonces jump vuelve a ser 0
}
if(vertical_speed < -1){
vertical_speed = -1;
}
}
if(event.type == SDL_QUIT) app = false; //si le pican a salirse, pues ... nos salimos.
}
void jump(){
if(vertical_speed == 0 && !jumplock && actor->canMove(UP, actor->get_x(), actor->get_y(), mainmap)){
vertical_speed -= JUMP_STRENGTH;
}
jumplock = true;
}
/*maneja la fisica del mono*/
void handlephysics()
{
if(actor->canMove(DOWN, actor->get_x(), actor->get_y(), mainmap)){
switch(oldclip){
case 1:
actor->set_clip_y(actor->get_h()*0); //depende de su direccion, escogemos el sprite de brinco
break;
case 2:
actor->set_clip_y(actor->get_h()*3);
break;
}
if(falltime == 0){
falltime = .5;
}else{
falltime += (ticks2-ticks1)/1000;
}
}else{
actor->set_clip_y(actor->get_h()*oldclip);
falltime = 0;
if(vertical_speed > 0){
vertical_speed = 0;
}
}
if(!actor->canMove(UP, actor->get_x(), actor->get_y(), mainmap) && vertical_speed < 0){
vertical_speed = 0;
}
while(!actor->canMove(DOWN, actor->get_x(), actor->get_y()+vertical_speed, mainmap) && vertical_speed > 0){
vertical_speed -= 1;
}
vertical_speed += (GRAVITY*falltime);
actor->set_y(actor->get_y() + vertical_speed);
}
/*Please Take One*/
void freeobjects()
{
SDL_FreeSurface(text);
TTF_CloseFont(font);
Mix_FreeChunk(jumpfx);
//Mix_FreeMusic(music);
}
/*Crime scene*/
void renderscene()
{
SDL_FillRect(viewport,NULL,0);
mainmap->drawmap(viewport);
Gfx::drawsurface(backg,0,0,viewport);
actor->draw(viewport);
Gfx::drawsurface(text,SCREEN_WIDTH/2-text->w/2,SCREEN_HEIGHT/2-text->h,viewport);
SDL_Flip(viewport);
}
|