void OpenSpace::initialize() { maxProgress(200); setAllEdges(false); world().SetGravity(b2Vec2(0, 10)); upper = makePlatform(10, -25); addProgress(25); middle = makePlatform(50, -25); addProgress(25); auto pd = physicsDimensions(); for(int i = 0; i < 150; ++i) { polygonDef def; def.bodyDef.position.Set(randuniform(pd.x, pd.x + 30), randuniform(-30, pd.y-30)); def.bodyDef.angle = to_radians(randuniform(0, 360)); def.bodyDef.type = b2_dynamicBody; def.shape.SetAsBox(1, 5); def.fixtureDef.restitution = 0.5; def.fixtureDef.density = 4; bodies.push_back(make_shape(world(), def)); addProgress(1); } }
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; }