Skip to content

Instantly share code, notes, and snippets.

@adammw
Created August 22, 2012 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammw/3422226 to your computer and use it in GitHub Desktop.
Save adammw/3422226 to your computer and use it in GitHub Desktop.
/*
* 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_ */
/*
* 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();
}
/*
* 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_ */
Initial commit.
#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;
}
/*
* mainmenu.cpp
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "mainmenu.h"
main_menu::main_menu() {
_title = "Main Menu";
_items.push_back(new menu_item("New Game"));
_items.push_back(new menu_item("Quit"));
}
main_menu::~main_menu() {
// TODO Auto-generated destructor stub
}
/*
* mainmenu.h
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "menu.h"
#include "menuitem.h"
#ifndef MAINMENU_H_
#define MAINMENU_H_
class main_menu: public menu {
public:
main_menu();
virtual ~main_menu();
};
#endif /* MAINMENU_H_ */
/*
* menu.cpp
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "menu.h"
using namespace std;
/**
* Get the menu title
* @return
*/
string menu::get_title() {
return _title;
}
/**
* Set the menu title
* @param title
*/
void menu::set_title(std::string title) {
_title = title;
}
/**
* Get the array of menu items
* @return
*/
std::vector<menu_item*> menu::get_items() {
return _items;
}
/*
* menu.h
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include <string>
#include <vector>
#include "menuitem.h"
#ifndef MENU_H_
#define MENU_H_
/**
* Abstract menu model
*
* Menus have many items and have properties such as a title
*/
class menu {
protected:
std::string _title;
std::vector<menu_item*> _items;
public:
std::string get_title();
void set_title(std::string);
std::vector<menu_item*> get_items();
};
#endif /* MENU_H_ */
/*
* menucontroller.cpp
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "menucontroller.h"
menu_controller::menu_controller(menu* menu) {
_menu = menu;
}
void menu_controller::update() {
}
/*
* menucontroller.h
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "controller.h"
#include "menu.h"
#ifndef MENUCONTROLLER_H_
#define MENUCONTROLLER_H_
/**
* Responsible for updating the menu model in response to user input
*/
class menu_controller: public controller {
private:
menu* _menu;
public:
menu_controller(menu*);
virtual void update();
};
#endif /* MENUCONTROLLER_H_ */
/*
* menuitem.cpp
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "menuitem.h"
using namespace std;
menu_item::menu_item(std::string title) {
_title = title;
}
menu_item::~menu_item() {
// TODO Auto-generated destructor stub
}
string menu_item::get_title() {
return _title;
}
bool menu_item::is_selected() {
return _selected;
}
void menu_item::set_selected(bool selected) {
_selected = selected;
}
/*
* menuitem.h
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include <string>
#ifndef MENUITEM_H_
#define MENUITEM_H_
/**
* Menu item model, stores all data related to a menu item
*/
class menu_item {
protected:
std::string _title;
bool _selected;
public:
menu_item(std::string title);
virtual ~menu_item();
std::string get_title();
bool is_selected();
void set_selected(bool selected);
};
#endif /* MENUITEM_H_ */
/*
* menuitemcontroller.cpp
*
* Created on: 22/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "menuitemcontroller.h"
menu_item_controller::menu_item_controller(menu_item* item) {
_item = item;
}
menu_item_controller::~menu_item_controller() {
// TODO Auto-generated destructor stub
}
void menu_item_controller::update() {
// TODO
// if the mouse is over the menu item
// _item->set_selected(true);
// else
// _item->set_selected(false);
}
/*
* menuitemcontroller.h
*
* Created on: 22/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "controller.h"
#include "menuitem.h"
#include "SwinGame.h"
#ifndef MENUITEMCONTROLLER_H_
#define MENUITEMCONTROLLER_H_
/**
* Responsible for handling user input and affecting the menu_item model
*/
class menu_item_controller: public controller {
private:
menu_item* _item;
public:
menu_item_controller(menu_item* item);
virtual ~menu_item_controller();
virtual void update();
};
#endif /* MENUITEMCONTROLLER_H_ */
/*
* menuitemrenderer.cpp
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include "menuitemrenderer.h"
font menu_item_renderer::_title_font = NULL;
menu_item_renderer::menu_item_renderer(menu_item* item, unsigned order) {
_item = item;
_order = order;
if (!_title_font)
_title_font = ::load_font("maven_pro_regular.ttf",24);
}
menu_item_renderer::~menu_item_renderer() {
// TODO Auto-generated destructor stub
}
void menu_item_renderer::render() {
const char* title = _item->get_title().c_str();
int title_width = ::text_width(_title_font, title);
unsigned y = 44 * _order + 20;
if (_item->is_selected()) {
::fill_rectangle(color_green, 20, y, title_width + 40, 34);
} else {
::fill_rectangle(color_red, 20, y, title_width + 40, 34);
}
::draw_text(title, color_white, _title_font, 30, y + 5);
}
/*
* menuitemrenderer.h
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#ifndef MENUITEMRENDERER_H_
#define MENUITEMRENDERER_H_
#include "SwinGame.h"
#include "menuitem.h"
#include "renderer.h"
/**
* Responsible for rendering the menu item
*/
class menu_item_renderer: public renderer {
private:
unsigned _order;
menu_item* _item;
static font _title_font;
public:
menu_item_renderer(menu_item*, unsigned);
virtual ~menu_item_renderer();
virtual void render();
};
#endif /* MENUITEMRENDERER_H_ */
/*
* menurenderer.cpp
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#include <vector>
#include "menurenderer.h"
#include "menuitem.h"
font menu_renderer::_title_font = NULL;
menu_renderer::menu_renderer(menu* menu) {
_menu = menu;
_background_bitmap = ::load_bitmap("blackjack_menu_bg.jpg");
if (!_title_font)
_title_font = ::load_font("maven_pro_regular.ttf",48);
}
void menu_renderer::render() {
::draw_bitmap(_background_bitmap, 0, 0);
/* Draw the menu title */
const char* title = _menu->get_title().c_str();
int title_width = ::text_width(_title_font, title);
::draw_text(title, color_white, _title_font, screen_width() - title_width - 20, 20);
/* Render each menu item */
std::vector<menu_item*> items = _menu->get_items();
for (std::vector<menu_item*>::iterator it = items.begin(); it != items.end(); ++it) {
menu_item* item = *it;
if (!_item_renderers.count(item))
_item_renderers[item] = new menu_item_renderer(item, it - items.begin());
_item_renderers[item]->render();
}
}
/*
* menurenderer.h
*
* Created on: 21/08/2012
* Author: Adam Malcontenti-Wilson
*/
#ifndef MENURENDERER_H_
#define MENURENDERER_H_
#include <map>
#include "SwinGame.h"
#include "menu.h"
#include "renderer.h"
#include "menuitemrenderer.h"
/**
* Responsible for rendering a menu on screen
*/
class menu_renderer: public renderer {
private:
menu* _menu;
bitmap _background_bitmap;
static font _title_font;
std::map<menu_item*, menu_item_renderer*> _item_renderers;
public:
menu_renderer(menu*);
virtual void render();
};
#endif /* MENURENDERER_H_ */
/*
* 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