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