From 10a0e290ac07524dc129cc2b227b0f9e95df0f8e Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Mon, 16 Apr 2012 09:39:52 -0500 Subject: first commit --- oldmain.cpp | 255 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100755 oldmain.cpp (limited to 'oldmain.cpp') diff --git a/oldmain.cpp b/oldmain.cpp new file mode 100755 index 0000000..2605f6d --- /dev/null +++ b/oldmain.cpp @@ -0,0 +1,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); +} \ No newline at end of file -- cgit