// Main game loop. void GameApp::manageGame() { playBackgroundSound(); while(logic.bAppRunning) { SDL_PollEvent(&sdlEvent); // Important: use only one PollEvent for all the screens!!! if(logic.bShowStartScreen){ startScreen->setScreenSize(logic.flScreenWidth, logic.flScreenHeight);// For resize event. startScreen->doInput(logic, sdlEvent); startScreen->doDrawing(logic); } else if(logic.bShowOptionsScreen){ if(logic.bNewOptionsScreen){ // Initialize buttons if required. optionsScreen->setScreenSize(logic.flScreenWidth, logic.flScreenHeight); logic.bNewOptionsScreen = false; optionsScreen->setupNewScreen(logic); } optionsScreen->doDrawing(logic); optionsScreen->doInput(logic, sdlEvent); } else if(logic.bShowHowtoScreen){ howtoScreen->doInput(logic, sdlEvent); howtoScreen->doDrawing(logic); } else if(logic.bShowPlayScreen){ playScreen->play(logic, sdlEvent); // Play the game. } swapBuffers(); } // End while(logic.bAppRunning). }
// on "init" you need to initialize your instance bool HelloWorld::init() { // super init first if ( !Layer::init() ) return false; visibleSize = Director::getInstance()->getVisibleSize(); origin = Director::getInstance()->getVisibleOrigin(); //add background sound playBackgroundSound(); //add background stars createStarsBackground("Pictures/bigStar.png",20); createStarsBackground("Pictures/smallStar.png",50); //add sprite with spaceCastle in the right side of the screen createSpaceCastle("Pictures/spaceCastle.png"); //add a menu with a button which closes the game exitMenu(); //add label with the title of the game in the top of the screen createTitleLabel("Physica"); //add Points Label createScoreLabel(); //add game menu createGameMenu(); return true; }
void GameApp::notify(Subject* s) // Observer pattern callback hook method. { bBackgroundSound = ((Logic*) s)->bBackgroundSound; playBackgroundSound(); // Play according to the new options. }
void PlayGame() { bool menuToggle = true; Game game; game.setResolution(window_width, window_height); if(window_height > 1080) { //game.gravity = 9; MAX_VELOCITY = 12; INITIAL_VELOCITY = 7; } srand(time(NULL)); clock_gettime(CLOCK_REALTIME, &timePause); clock_gettime(CLOCK_REALTIME, &timeStart); clock_gettime(CLOCK_REALTIME, &start); while(game.run) { game = Game(); //reinitializes upon replay if(TOGGLE_SOUND) //Sound switch so that the background music doesnt get recalled after the initial call { //otherwise they loop over each other and it sounds bad, plus it created a memory leak Buffer = alutCreateBufferFromFile("./Sounds/music.wav"); playBackgroundSound(); TOGGLE_SOUND = false; } gutsToggle = true; bloodToggle = true; //Reset switches to clear the prev game information. This includes the score and blood particles TOGGLE_PAUSE = true; //basically reinitializing anything that didnt get reinitialized in the game = Game(); numblood = 0; SCORE = 0; game.setMissiles = false; while(STATE == MAIN_MENU && game.run) { XEvent menu; while(XPending(dpy)) { XNextEvent(dpy, &menu); check_keys(&menu, &game); //Ahhh STATES, made this far easier to control. check_mouse(&menu, &game); //Each while(STATE == X) loop renders a background and checks for input game.setResolution(window_width, window_height); //and the input functions all have checks to see what STATE is currently running } //and only allows proper input setMenuBackground(); glXSwapBuffers(dpy, win); } while(STATE == HOW_TO && game.run && menuToggle) { XEvent howTo; while(XPending(dpy)) { XNextEvent(dpy, &howTo); check_keys(&howTo, &game); check_mouse(&howTo, &game); game.setResolution(window_width, window_height); } setHowToBackground(); glXSwapBuffers(dpy, win); } if(menuToggle) //No one wants to see the how to menu over and over and over again. menuToggle = false; STATE = RUN_GAME; //gotta have this here, otherwise if the player clicks the green button after one game the STATE will be HOW_TO but it cant access it game.setResolution(window_width, window_height); game.setPos(window_width/2, window_height + game.player.height); //this is when playforms are created and the players position is set to the top game.setGravity(GRAVITY); //if this is called beforehand it wont take proper screen size into consideration makePlatform(5,&game); while(STATE == RUN_GAME && game.run) { // check input XEvent e; while(XPending(dpy)) { if(TOGGLE_PAUSE) { TOGGLE_PAUSE = false; pausegame = false; } XNextEvent(dpy, &e); check_keys(&e, &game); check_resize(&e); game.setResolution(window_width, window_height); } if(game.guts && numblood <= 50) { STATE = DEATH; //changes the game state to the death screen once the person has died and the blood particles are off the screen } clock_gettime(CLOCK_REALTIME, &timeCurrent); timeSpan = timeDiff(&timeStart, &timeCurrent); timeCopy(&timeStart, &timeCurrent); if(!pausegame && numblood < 1) { SCORE++; //iterates the score every loop that the game is not paused and the player is not dead } physicsCountdown += timeSpan; // check for collisions, move player while(physicsCountdown >= physicsRate) { physics(&game); physicsCountdown -= physicsRate; } // used for sprite timing DON'T TOUCH if(frames > 2) frames = 0; frames++; // FPS COUNTER/RESET if(fps > 100) { clock_gettime(CLOCK_REALTIME, &start); fps = 0; } fps++; render(&game); glXSwapBuffers(dpy, win); } while(STATE == DEATH && game.run) { XEvent death; while(XPending(dpy)) { XNextEvent(dpy, &death); check_keys(&death, &game); check_mouse(&death, &game); game.setResolution(window_width, window_height); } clock_gettime(CLOCK_REALTIME, &timeCurrent); timeSpan = timeDiff(&timeStart, &timeCurrent); timeCopy(&timeStart, &timeCurrent); physicsCountdown += timeSpan; while(physicsCountdown >= physicsRate) { physics(&game); physicsCountdown -= physicsRate; //this will keep the game rendering so that the user can see the players full body explosion } render(&game); glXSwapBuffers(dpy, win); } } return; }
void SoundUtil::setIsBackGroundSound(const bool &rIsSound) { _bIsBackGroundSound = rIsSound; _bIsSoundMainBackGroundMusic = !rIsSound; _bIsBackGroundSound ? playBackgroundSound(BACKGROUNDSOUND) : stopBackGroundSound(); }