Created
August 22, 2012 04:11
-
-
Save adammw/3422226 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* controller.h | |
* | |
* Created on: 21/08/2012 | |
* Author: Adam Malcontenti-Wilson | |
*/ | |
#ifndef CONTROLLER_H_ | |
#define CONTROLLER_H_ | |
/** | |
* This is an abstract class which all controllers inherit from | |
*/ | |
class controller { | |
public: | |
virtual void update() = 0; | |
}; | |
#endif /* CONTROLLER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* gameengine.cpp | |
* | |
* Created on: 21/08/2012 | |
* Author: Adam Malcontenti-Wilson | |
*/ | |
#include "gameengine.h" | |
#include "mainmenu.h" | |
#include "menu.h" | |
#include "menurenderer.h" | |
#include "menucontroller.h" | |
//TODO: switch to more a stack based model (push/pop) of state | |
game_engine::game_engine() { | |
/* Initialise private fields */ | |
_state = MAIN_MENU; | |
/* Initialise SwinGame Graphics */ | |
::open_audio(); | |
::open_graphics_window("Blackjack", 800, 600); | |
::load_default_colors(); | |
/* Create Main Menu */ | |
menu* main_menu_model = new main_menu(); | |
_renderers[MAIN_MENU] = new menu_renderer(main_menu_model); | |
_controllers[MAIN_MENU] = new menu_controller(main_menu_model); | |
} | |
game_engine::~game_engine() { | |
/* Release SwinGame Objects */ | |
::close_audio(); | |
::release_all_resources(); | |
} | |
void game_engine::process_events() { | |
::process_events(); | |
} | |
void game_engine::update() { | |
_controllers[_state]->update(); | |
} | |
void game_engine::render() { | |
/* clear the screen each frame */ | |
::clear_screen(color_white); | |
/* ask the renderer to render the frame */ | |
_renderers[_state]->render(); | |
/* draw the framerate counter */ | |
::draw_framerate(0,0); | |
/* limit to 60 FPS */ | |
::refresh_screen(60); | |
} | |
bool game_engine::running() { | |
return !::window_close_requested(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* gameengine.h | |
* | |
* Created on: 21/08/2012 | |
* Author: Adam Malcontenti-Wilson | |
*/ | |
#include "SwinGame.h" | |
#include "renderer.h" | |
#include "controller.h" | |
#ifndef GAMEENGINE_H_ | |
#define GAMEENGINE_H_ | |
/** | |
* The Game Engine class processes the user input, updates the model state and | |
* renders the game on-screen. The main game loop delegates to this class. | |
*/ | |
class game_engine { | |
public: | |
game_engine(); | |
~game_engine(); | |
void process_events(); | |
void update(); | |
void render(); | |
bool running(); | |
enum game_state { | |
MAIN_MENU, | |
GAME_IN_PROGRESS, | |
GAME_OVER, | |
GAME_STATE_MAX | |
}; | |
private: | |
game_state _state; | |
renderer* _renderers[GAME_STATE_MAX]; | |
controller* _controllers[GAME_STATE_MAX]; | |
}; | |
#endif /* GAMEENGINE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Initial commit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#include "gameengine.h" | |
int main() | |
{ | |
/* create a game engine object */ | |
game_engine* engine = new game_engine; | |
/* loop until the window is closed */ | |
do | |
{ | |
engine->process_events(); | |
engine->update(); | |
engine->render(); | |
} while ( engine->running() ); | |
/* delete the card deck on exit */ | |
delete engine; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* renderer.h | |
* | |
* Created on: 21/08/2012 | |
* Author: Adam Malcontenti-Wilson | |
*/ | |
#ifndef RENDERER_H_ | |
#define RENDERER_H_ | |
/** | |
* This is an abstract class which all renderers inherit from | |
*/ | |
class renderer { | |
public: | |
virtual void render() = 0; | |
}; | |
#endif /* RENDERER_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment