]>
git.r.bdr.sh - rbdr/pico-engine/blob - oldmain.cpp
3 /*Global variables for the game.*/
6 bool keys
[ 323 ] = { false };
7 bool btns
[ 3 ] = { false };
12 int oldclip
= 1 , rightpressed
= 0 , leftpressed
= 0 ;
13 float vertical_speed
= 0.0 , falltime
= 0.0 ;
16 /*Definition for our surfaces and such.*/
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
28 /*Definition of our functions */
32 void updateinput (); //belongs to input
33 void handleinput (); //belongs to input
34 void handlephysics (); //belongs to actor_main
38 void jump (); //belongs to actor_main
42 int main ( int argc
, char * argv
[])
46 std :: cout
<< "Initializing... \n " ;
48 std :: cout
<< "Done! \n " ;
49 std :: cout
<< "Loading Objects... \n " ;
51 std :: cout
<< "Done! \n " ;
54 Mix_PlayMusic ( music
, 0 );
56 //It's aliiive, ALIIIVE!
60 //I heard seaman 2 has ticks!
61 ticks1
= SDL_GetTicks ();
63 //don't mind these fellas
71 ticks2
= SDL_GetTicks ();
72 if (( ticks2
- ticks1
) < FRAME_TIME
) SDL_Delay ( FRAME_TIME
- ( ticks2
- ticks1
));
82 /* Initialization of components */
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.
92 /* deinitialization of components */
96 TTF_Quit (); //bye bye TTF library
97 Mix_CloseAudio (); //bye bye Audio Mixer
98 SDL_Quit (); //bye bye SDL
101 /*Load our basic objects*/
105 viewport
= SDL_SetVideoMode ( SCREEN_WIDTH
, //Define our viewport
110 /**********LOAD SPRITES*******************************/
111 actor
= new Actor_main ( 20 , 75 , "data/mansprite.png" );
113 /**********LOAD AUDIO STUFF***************************/
115 jumpfx
= Mix_LoadWAV ( "data/evasion.ogg" ); //este es el SFX de evasion.
116 Mix_VolumeChunk ( jumpfx
, 128 );
118 //music = Mix_LoadMUS("data/Air1_2.mid"); //este es el audio de fondo
119 //Mix_VolumeMusic(128);
121 /**********LOAD FONT STUFF*****************************/
123 font
= TTF_OpenFont ( "data/fipps.ttf" , 24 ); //algo del font ... hmm :P
124 text
= TTF_RenderText_Solid ( font
, "Glitch Test" , grey
);
127 /*Automatic Updates*/
128 void updateinput () //this is our super pooler
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 ;
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
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
);
153 /*******************************************************************/
155 if ( keys
[ SDLK_RIGHT
]){ //si estamos presionando la derecha
157 if ( rightpressed
== 0 ){ //si no hay tiempo registrado
158 rightpressed
= ticks1
; //le damos un tiempo
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
);
165 rightpressed
= 0 ; //si se ha soltado la tecla reseteamos rightpressed
168 /*****************************************************************/
170 if ( keys
[ SDLK_LEFT
] || keys
[ SDLK_RIGHT
] || keys
[ SDLK_x
]){ //cualquier movimiento pertinente, debemos animar.
173 actor
-> set_clip_x ( 0 ); //en cuanto se detenga, pues lo devolvemos a donde debiera
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
183 if ( vertical_speed
< - 1 ){
188 if ( event
. type
== SDL_QUIT
) app
= false ; //si le pican a salirse, pues ... nos salimos.
192 if ( vertical_speed
== 0 && ! jumplock
&& actor
-> canMove ( UP
, actor
-> get_x (), actor
-> get_y (), mainmap
)){
193 vertical_speed
-= JUMP_STRENGTH
;
198 /*maneja la fisica del mono*/
202 if ( actor
-> canMove ( DOWN
, actor
-> get_x (), actor
-> get_y (), mainmap
)){
205 actor
-> set_clip_y ( actor
-> get_h ()* 0 ); //depende de su direccion, escogemos el sprite de brinco
208 actor
-> set_clip_y ( actor
-> get_h ()* 3 );
214 falltime
+= ( ticks2
- ticks1
)/ 1000 ;
217 actor
-> set_clip_y ( actor
-> get_h ()* oldclip
);
219 if ( vertical_speed
> 0 ){
224 if (! actor
-> canMove ( UP
, actor
-> get_x (), actor
-> get_y (), mainmap
) && vertical_speed
< 0 ){
228 while (! actor
-> canMove ( DOWN
, actor
-> get_x (), actor
-> get_y ()+ vertical_speed
, mainmap
) && vertical_speed
> 0 ){
231 vertical_speed
+= ( GRAVITY
* falltime
);
234 actor
-> set_y ( actor
-> get_y () + vertical_speed
);
240 SDL_FreeSurface ( text
);
242 Mix_FreeChunk ( jumpfx
);
243 //Mix_FreeMusic(music);
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
);