From 10a0e290ac07524dc129cc2b227b0f9e95df0f8e Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Mon, 16 Apr 2012 09:39:52 -0500 Subject: first commit --- main.cpp | 284 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100755 main.cpp (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp new file mode 100755 index 0000000..335db20 --- /dev/null +++ b/main.cpp @@ -0,0 +1,284 @@ +#include "stdafx.h" + +/*Global variables for the game.*/ + +bool app = true; //Structure to check app lifecycle +bool keys[323] = {false}; //Structures for keyboard/mouse status. +bool btns[3] = {false}; +int mouse[2] = {0,0}; + +char *musicpath; //NOTE: Kill this when we have an actual music system + +//basic systems. +Game_overwatch *overwatch; //Overwatch is the game controller +Map *mainmap; //NOTE: Must change +Audio *audio; //NOTE: Probably needs to change. + +SDL_Surface *pauseimage, *bgimage; //NOTE: This should probably change. +int ticks1, ticks2; //Timing for proper FPS +bool pauselock, gamepaused, dudelock; //Locking variables. Probably needs a better system for this. (semaphores?) + +char str[12]; //NOTE: String for debugging info. MUST change + +/*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_Color white = {255,255,255,0}; //some colors +SDL_Color grey = {65,65,65,0}; +SDL_Color magenta = {255,0,255,0}; +SDL_Color black = {34, 34, 34, 0}; +Mix_Music *music; //background music +TTF_Font *font; //font +CLuaVirtualMachine vm; //Lua VM. + + +/*Definition of our functions */ + +void init(); //Initialize Game objects +void loadobjects(); //Load Game Objects +void updateinput(); //Update Input +void handleinput(); //Act on input +void renderscene(); //Render Game Objects +void freeobjects(); //Free Game Objects +void deinit(); //Deinitialize +void pauseinput(); //Pause input handling (this will be better abstracted in a State machine) + + + +/*Entry point*/ +int main(int argc, char *argv[]) +{ + + +//When on apple, sit on top of the bundle resources directory. +#ifdef __APPLE__ + CFBundleRef mainBundle = CFBundleGetMainBundle(); + CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle); + char path[MAXPATHLEN]; + if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, MAXPATHLEN)) + { + // error! + } + CFRelease(resourcesURL); + + chdir(path); + std::cout << "Current Path: " << path << std::endl; +#endif + + //Initialize the Lua VM and debugger.. + vm.InitialiseVM (); + CLuaDebugger dbg (vm); + dbg.SetCount (10); + std::cout << ">>>Initialized Lua VM\n"; + + //load stuff + std::cout << "Initializing... \n"; + init(); + std::cout << "Done! \n"; + std::cout << "Loading Objects... \n"; + loadobjects(); + std::cout << "Done! \n"; + + //play the music (infinite loop) + Mix_PlayMusic(music, -1); + + //Main Application Loop + while(app) + { + if(!gamepaused){ + //Measure time + ticks1 = SDL_GetTicks(); + + //Check and update the inputs. + updateinput(); + handleinput(); + + //Temporary debugging of population counts. + sprintf(str, "%d", overwatch->get_population()); + SDL_FreeSurface(text); + text = TTF_RenderText_Blended(font, str, white); + + //Make everyone act, animate, handle physics, and do collision work + overwatch->act(viewport, mainmap, keys); + overwatch->animate(); + overwatch->handlephysics(mainmap, ticks1, ticks2); + overwatch->reset_collisions(); + overwatch->check_collisions(mainmap); + overwatch->dieloop(); + + //render the actual scene + renderscene(); + + //Measure time again and extend frame. + ticks2 = SDL_GetTicks(); + if ((ticks2-ticks1) < FRAME_TIME) SDL_Delay(FRAME_TIME - (ticks2-ticks1)); + }else{ + //Temporary pause menu. This is better abstracted with a FSM. + ticks1 = SDL_GetTicks(); + renderscene(); + updateinput(); + pauseinput(); + ticks2 = SDL_GetTicks(); + } + } + + //When the loop dies, free and deinitialize. + freeobjects(); + deinit(); + + return 0; +} + +/* Initialization of components */ + +void init() +{ + //Initialize subsystems: SDL, Fonts, Audio, Overwatch and Map. + SDL_Init(SDL_INIT_EVERYTHING); + TTF_Init(); + Mix_OpenAudio(44100, AUDIO_S16SYS, 1, 2048); + overwatch = new Game_overwatch; + mainmap = new Map; +} + +/* deinitialization of components */ + +void deinit() +{ + TTF_Quit(); + Mix_CloseAudio(); + SDL_Quit(); +} + +/*Load our basic objects*/ + +void loadobjects() +{ + viewport = SDL_SetVideoMode( SCREEN_WIDTH, //Define our viewport + SCREEN_HEIGHT, + SCREEN_DEPTH, + SCREEN_PROPS ); + std::cout << ">>Created Viewport... \n"; + + + /* + *NOTE: Actor and audio loading should be handled by the map system. + * Fonts should be handled by the yet inexistant Debug and Message systems. + */ + + + /**********LOAD SPRITES******************************/ + new Actor(220,220,"./sprites/picosprite.png", "./scripts/main_actor.lua", mainmap, audio, overwatch, vm); + + pauseimage = IMG_Load("./pauseimage.png"); + bgimage = IMG_Load("./picobg1.png"); + /**********LOAD AUDIO STUFF***************************/ + + music = Mix_LoadMUS("./bgm/newpicoambient.ogg"); //este es el audio de fondo + Mix_VolumeMusic(64); + std::cout << ">>Loaded BGM.\n"; + + /**********LOAD FONT STUFF*****************************/ + + font = TTF_OpenFont("./dejavubold.ttf", 18); + std::cout << ">>Loaded fonts.\n"; + +} + +/*Automatic Updates*/ +void updateinput() +{ + while(SDL_PollEvent(&event)){ + if (event.type == SDL_KEYDOWN) keys[event.key.keysym.sym] = true; //Set pressed keys to true, depressed to false + if (event.type == SDL_KEYUP) keys[event.key.keysym.sym] = false; + if (event.type == SDL_MOUSEBUTTONDOWN) btns[event.button.button] = true; + if (event.type == SDL_MOUSEBUTTONUP) btns[event.button.button] = false; + if (event.type == SDL_MOUSEMOTION){ mouse[0]= event.motion.x; mouse[1]= event.motion.y;} + } +} + +/*Handle Input*/ +void handleinput() +{ + + //Special input for debugging. Should probably add a console. + //w: adds a walker + //q: adds a jumper + //p: pauses the game + //The rest of the input is handled by the lua scripts. + + if(keys[SDLK_w]){ + if(!dudelock){ + new Actor(mainmap->get_sx()+mouse[0],mainmap->get_sy()+mouse[1],"./sprites/walkersprite.png", "./scripts/npc_walker.lua", mainmap, audio, overwatch, vm); + dudelock = true; + } + } + + if(keys[SDLK_q]){ + if(!dudelock){ + new Actor(mainmap->get_sx()+mouse[0],mainmap->get_sy()+mouse[1],"./sprites/walkersprite.png", "./scripts/npc_jumper.lua", mainmap, audio, overwatch, vm); + dudelock = true; + } + } + + if (dudelock && !keys[SDLK_q] && !keys[SDLK_w]) { + dudelock = false; + } + + if(keys[SDLK_p]){ + if(!pauselock){ + gamepaused = true; + pauselock = true; + Mix_VolumeMusic(10); + } + }else{ + if(pauselock){ + pauselock = false; + } + } + + if(event.type == SDL_QUIT) app = false; +} + +void pauseinput(){ + if(keys[SDLK_p]){ + if(!pauselock){ + gamepaused = false; + pauselock = true; + Mix_VolumeMusic(64); + } + }else{ + if(pauselock){ + pauselock = false; + } + } + + if(event.type == SDL_QUIT) app = false; +} + +/*Free objects, save memory*/ +void freeobjects() +{ + SDL_FreeSurface(text); + TTF_CloseFont(font); + Mix_FreeMusic(music); +} + +/*Render the actual scene... Delegate to the Overwatch draw method when we're in-game*/ +void renderscene() +{ + SDL_FillRect(viewport,NULL,0); + Gfx::drawsurface(bgimage,0,0,viewport); + mainmap->drawmap(viewport); + Gfx::drawsurface(backg,0,0,viewport); + + overwatch->draw(viewport, mainmap); + + Gfx::drawsurface(text,10,10,viewport); + + if(gamepaused){ + Gfx::drawsurface(pauseimage,0,0,viewport); + } + SDL_Flip(viewport); +} \ No newline at end of file -- cgit