]> git.r.bdr.sh - rbdr/pico-engine/blame_incremental - oldmain.cpp
first commit
[rbdr/pico-engine] / oldmain.cpp
... / ...
CommitLineData
1#include "stdafx.h"\r
2\r
3/*Global variables for the game.*/\r
4\r
5bool app = true;\r
6bool keys[323] = {false};\r
7bool btns[3] = {false};\r
8\r
9Map *mainmap;\r
10Actor_main *actor;\r
11int ticks1, ticks2;\r
12int oldclip=1, rightpressed =0, leftpressed = 0;\r
13float vertical_speed = 0.0, falltime = 0.0;\r
14bool jumplock;\r
15\r
16/*Definition for our surfaces and such.*/\r
17\r
18SDL_Surface *viewport, *backg, *buddy, *text; //our surfaces: the viewport, the background, the actor and the text\r
19SDL_Event event; //our event handler\r
20SDL_Rect src_rect, dst_rect; //our source rectangle and dest rectangle\r
21SDL_Color white = {255,255,255,0}; //some colors\r
22SDL_Color grey = {65,65,65,0};\r
23SDL_Color magenta = {255,0,255,0};\r
24Mix_Chunk *jumpfx; //Sound effect for the jump\r
25Mix_Music *music; //background music\r
26TTF_Font *font; //font\r
27\r
28/*Definition of our functions */\r
29\r
30void init();\r
31void loadobjects();\r
32void updateinput(); //belongs to input\r
33void handleinput(); //belongs to input\r
34void handlephysics(); //belongs to actor_main\r
35void renderscene();\r
36void freeobjects();\r
37void deinit();\r
38void jump(); //belongs to actor_main\r
39\r
40\r
41/*My main man*/\r
42int main(int argc, char *argv[])\r
43{\r
44\r
45 //boring stuff.\r
46 std::cout << "Initializing... \n";\r
47 init();\r
48 std::cout << "Done! \n";\r
49 std::cout << "Loading Objects... \n";\r
50 loadobjects();\r
51 std::cout << "Done! \n";\r
52 \r
53 //Play it again, Sam\r
54 Mix_PlayMusic(music, 0);\r
55 \r
56 //It's aliiive, ALIIIVE!\r
57 while(app)\r
58 {\r
59 \r
60 //I heard seaman 2 has ticks!\r
61 ticks1 = SDL_GetTicks();\r
62 \r
63 //don't mind these fellas\r
64 updateinput();\r
65 handleinput();\r
66 handlephysics();\r
67 \r
68 renderscene();\r
69 \r
70 //Psh, Semantics!\r
71 ticks2 = SDL_GetTicks();\r
72 if ((ticks2-ticks1) < FRAME_TIME) SDL_Delay(FRAME_TIME - (ticks2-ticks1));\r
73 }\r
74 \r
75 //freedom, yay!\r
76 freeobjects();\r
77 deinit();\r
78 \r
79 return 0;\r
80}\r
81\r
82/* Initialization of components */\r
83\r
84void init()\r
85{\r
86 SDL_Init(SDL_INIT_EVERYTHING); //let's start SDL\r
87 TTF_Init(); //Also the TTF library\r
88 Mix_OpenAudio(44100, AUDIO_S16SYS, 1, 2048); //And finally the Audio system.\r
89 mainmap = new Map;\r
90}\r
91\r
92/* deinitialization of components */\r
93\r
94void deinit()\r
95{\r
96 TTF_Quit(); //bye bye TTF library\r
97 Mix_CloseAudio(); //bye bye Audio Mixer\r
98 SDL_Quit(); //bye bye SDL\r
99}\r
100\r
101/*Load our basic objects*/\r
102\r
103void loadobjects()\r
104{\r
105 viewport = SDL_SetVideoMode( SCREEN_WIDTH, //Define our viewport\r
106 SCREEN_HEIGHT,\r
107 SCREEN_DEPTH,\r
108 SCREEN_PROPS ); \r
109 \r
110 /**********LOAD SPRITES*******************************/ \r
111 actor = new Actor_main(20,75,"data/mansprite.png");\r
112 \r
113 /**********LOAD AUDIO STUFF***************************/\r
114 \r
115 jumpfx = Mix_LoadWAV("data/evasion.ogg"); //este es el SFX de evasion.\r
116 Mix_VolumeChunk(jumpfx,128);\r
117 \r
118 //music = Mix_LoadMUS("data/Air1_2.mid"); //este es el audio de fondo\r
119 //Mix_VolumeMusic(128);\r
120 \r
121 /**********LOAD FONT STUFF*****************************/\r
122 \r
123 font = TTF_OpenFont("data/fipps.ttf", 24); //algo del font ... hmm :P\r
124 text = TTF_RenderText_Solid(font, "Glitch Test", grey); \r
125}\r
126\r
127/*Automatic Updates*/\r
128void updateinput() //this is our super pooler\r
129{\r
130 while(SDL_PollEvent(&event)){ \r
131 if (event.type == SDL_KEYDOWN) keys[event.key.keysym.sym] = true; //si esta presionada la tecla, entonces es TRUE!\r
132 if (event.type == SDL_KEYUP) keys[event.key.keysym.sym] = false; //si no esta presionada la tecla, entonces es FALSE!\r
133 if (event.type == SDL_MOUSEBUTTONDOWN) btns[event.button.button] = true;//igual para el mouse\r
134 if (event.type == SDL_MOUSEBUTTONUP) btns[event.button.button] = false; \r
135 }\r
136}\r
137\r
138/*Love Handles*/\r
139void handleinput()\r
140{\r
141 if(keys[SDLK_LEFT]){ //si estamos presionando la izquierda \r
142 if(leftpressed == 0){ //si no hay tiempo registrado\r
143 leftpressed = ticks1; //le damos un tiempo\r
144 } \r
145 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\r
146 actor->set_x(actor->get_x() - BUDDY_SPEED); \r
147 oldclip=1;\r
148 }\r
149 }else{\r
150 leftpressed = 0;\r
151 }\r
152 \r
153 /*******************************************************************/\r
154 \r
155 if(keys[SDLK_RIGHT]){ //si estamos presionando la derecha\r
156 \r
157 if(rightpressed == 0){ //si no hay tiempo registrado\r
158 rightpressed = ticks1; //le damos un tiempo\r
159 }\r
160 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\r
161 actor->set_x(actor->get_x() + BUDDY_SPEED);\r
162 oldclip=2;\r
163 }\r
164 }else{\r
165 rightpressed = 0; //si se ha soltado la tecla reseteamos rightpressed\r
166 }\r
167 \r
168 /*****************************************************************/\r
169 \r
170 if(keys[SDLK_LEFT] || keys[SDLK_RIGHT] || keys[SDLK_x]){ //cualquier movimiento pertinente, debemos animar.\r
171 actor->animate(); \r
172 }else{\r
173 actor->set_clip_x(0); //en cuanto se detenga, pues lo devolvemos a donde debiera\r
174 } \r
175 \r
176 /*************************************************************/ \r
177 if(keys[SDLK_x]){ //z is for jumping.\r
178 jump(); //ahora si, estamos brincando\r
179 }else{ //si z no esta presionado\r
180 if(jumplock){ //y ya habiamos aterrizado\r
181 jumplock = false; //entonces jump vuelve a ser 0\r
182 }\r
183 if(vertical_speed < -1){\r
184 vertical_speed = -1;\r
185 }\r
186 }\r
187 \r
188 if(event.type == SDL_QUIT) app = false; //si le pican a salirse, pues ... nos salimos.\r
189}\r
190\r
191void jump(){\r
192 if(vertical_speed == 0 && !jumplock && actor->canMove(UP, actor->get_x(), actor->get_y(), mainmap)){\r
193 vertical_speed -= JUMP_STRENGTH;\r
194 }\r
195 jumplock = true;\r
196}\r
197\r
198/*maneja la fisica del mono*/\r
199void handlephysics()\r
200{\r
201 \r
202 if(actor->canMove(DOWN, actor->get_x(), actor->get_y(), mainmap)){\r
203 switch(oldclip){\r
204 case 1:\r
205 actor->set_clip_y(actor->get_h()*0); //depende de su direccion, escogemos el sprite de brinco\r
206 break;\r
207 case 2: \r
208 actor->set_clip_y(actor->get_h()*3); \r
209 break; \r
210 }\r
211 if(falltime == 0){\r
212 falltime = .5;\r
213 }else{\r
214 falltime += (ticks2-ticks1)/1000;\r
215 }\r
216 }else{\r
217 actor->set_clip_y(actor->get_h()*oldclip);\r
218 falltime = 0;\r
219 if(vertical_speed > 0){\r
220 vertical_speed = 0;\r
221 }\r
222 }\r
223 \r
224 if(!actor->canMove(UP, actor->get_x(), actor->get_y(), mainmap) && vertical_speed < 0){\r
225 vertical_speed = 0;\r
226 }\r
227\r
228 while(!actor->canMove(DOWN, actor->get_x(), actor->get_y()+vertical_speed, mainmap) && vertical_speed > 0){\r
229 vertical_speed -= 1;\r
230 }\r
231 vertical_speed += (GRAVITY*falltime);\r
232 \r
233 \r
234 actor->set_y(actor->get_y() + vertical_speed);\r
235}\r
236\r
237/*Please Take One*/\r
238void freeobjects()\r
239{\r
240 SDL_FreeSurface(text);\r
241 TTF_CloseFont(font);\r
242 Mix_FreeChunk(jumpfx);\r
243 //Mix_FreeMusic(music); \r
244}\r
245\r
246/*Crime scene*/\r
247void renderscene()\r
248{\r
249 SDL_FillRect(viewport,NULL,0);\r
250 mainmap->drawmap(viewport);\r
251 Gfx::drawsurface(backg,0,0,viewport);\r
252 actor->draw(viewport);\r
253 Gfx::drawsurface(text,SCREEN_WIDTH/2-text->w/2,SCREEN_HEIGHT/2-text->h,viewport);\r
254 SDL_Flip(viewport);\r
255}