rendered paste body#include "game.hpp"/*** Initiate all variables*/Game::Game() : scr(NULL), data(NULL), ply1(NULL), ply2(NULL), ball(NULL), running(true), dserve(bps * 3), serve(PLY1), served(false), snr(true), _gs(MENU){}Game::~Game(){}/*** Initiates the game*/void Game::init(){ if(allegro_init() != 0) throw Exception("Allegro failed to init!"); if(install_keyboard() != 0) throw Exception("Couldnt install keyboard!"); if(install_mouse() < 0) throw Exception("Couldnt install mouse!"); if(install_timer() != 0) throw Exception("Couldnt install timer!"); set_color_depth(32); if(set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) { // Maybe try some more later? throw Exception("Couldnt setup graphics!"); } // Set cener screen vars CSCREEN_W = SCREEN_W / 2; CSCREEN_H = SCREEN_H / 2; // Initiate the screen scr = new triple_buffer(); scr->init(); // Get the relative path to the executable // and load the apong.dat datafile char datpath[256]; get_executable_name(datpath, sizeof(datpath)); replace_filename(datpath, datpath, "apong.dat", sizeof(datpath)); // Now we can actually load the damn thing data = load_datafile(datpath); if(!data) throw Exception("Couldnt load datafile!"); // Finally, initiate the ball ball = new Ball(5, 5); // Initiate both players, both of them computers ply1 = new Player(); ply2 = new Player(); ply1->init(data, 0); ply2->init(data, 1); ply1->setpos(30, 30); ply2->setpos(SCREEN_W - 30, 30);}/* Cleans everything up */void Game::deinit(){ if(scr) { scr->deinit(); delete scr; scr = NULL; } if(data) { unload_datafile(data); data = NULL; } if(ply1) { ply1->clean(); delete ply1; ply1 = NULL; } if(ply2) { ply2->clean(); delete ply2; ply2 = NULL; } if(ball) { delete ball; ball = NULL; }}void Game::run(){ // Start the timer here start_timer(); while(this->running) { if(check_timer(bps) > 0) { while(check_timer(bps) > 0) { // In what state are we? if(_gs == MENU) { if(key[KEY_P]) _gs = PLAY; } else if(_gs == PLAY) { // Do we need to start a new round? if(snr) { ball->setpos(CSCREEN_H, CSCREEN_H); dserve = bps * 2; snr = false; } // In dserve seconds, we shoot the ball --dserve; if(dserve < 0) { // To which side? if(serve == PLY1) { // PLY1 is supposed to be on the left <<< int speed = ((rand()%2) == 1) ? 0 : 1; ball->setspeed(0, speed); } if(serve == PLY2) { int speed = ((rand()%2) == 1) ? 0 : 1; ball->setspeed(1, speed); } } // Process player and ball ply1->process(); ply2->process(); // Check ball upper/down wall col. if(ball->y + ball->r > SCREEN_H) ball->ys = ball->neg(ball->ys); if(ball->y - ball->r < 0) ball->ys = abs(ball->ys); // Check if ball hit oblivion if(ball->x + ball->r > SCREEN_W) { // Player 2 lost snr = true; serve = PLY1; ply1->addscore(); } if(ball->x - ball->r < 0) { // Player 1 lost snr = true; serve = PLY2; ply2->addscore(); } // Update ball position ball->y += ball->ys; ball->x += ball->xs; } else if(_gs == PAUSE) { } // F9 means a forced exit if(keyr(key[KEY_F9])) running = false; // Decrease timer reset_timer(); } // Clear the buffer clear(scr->get_buffer()); // Check for states if(_gs == MENU) { } else if(_gs == PLAY) { // Draw player ply1->draw(scr->get_buffer()); ply2->draw(scr->get_buffer()); ball->draw(scr->get_buffer()); } else if(_gs == PAUSE) { } // Blit the buffer to screen blit(scr->get_buffer(), screen, 0, 0, 0, 0, scr->get_buffer()->w, scr->get_buffer()->h); } else { // Give some time to the system rest(1); } }}/*** Returns true if key is released*/bool Game::keyr(int key){ static bool keydown = false; if(key) keydown = true; if(!key && keydown) { return true; keydown = false; } return false;}