00001 #include "globals.h"
00002
00003 #include "cevent.h"
00004 #include "cgame.h"
00005 #include "cdebug.h"
00006 #include "ctimer.h"
00007 #include "cvideo.h"
00008
00009 #include <assert.h>
00010
00011
00012
00013 CGame *CGame::unique_instance = 0;
00014
00015
00016 int CGame::is_game_quit()
00017 {
00018 return quit_flag;
00019 }
00020
00021
00022
00023
00024
00025 void CGame::quit()
00026 {
00027 quit_flag = 1;
00028 }
00029
00037 void CGame::do_draw(CSurface &surface, CRect &rect) {
00038 surface.fill_rect(rect, 0);
00039 }
00040
00041
00042 void CGame::draw_frame()
00043 {
00044 CScreen &screen = CVideoSystemManager::get_instance().get_screen();
00045
00046
00047 dirty_rects.unique();
00048
00049 for (CRectList::iterator i = dirty_rects.begin(); i != dirty_rects.end(); i++) {
00050 screen.set_clipping_rect(*i);
00051 draw(screen, *i);
00052 }
00053
00054 screen.update_rects(dirty_rects);
00055
00056 dirty_rects.clear();
00057 }
00058
00059 int CGame::do_handle_event(CEvent &event)
00060 {
00061
00062
00063 switch (event.get_type()) {
00064
00065 case CEvent::TYPE_KEY_DOWN:
00066 switch (event.get_key_ascii_value()) {
00067 case 'f':
00068
00069
00070
00071 if (event.is_ctrl_pressed()) {
00072 CScreen &s = CVideoSystemManager::get_instance().get_screen();
00073
00074 s.set_fullscreen_mode(!s.get_fullscreen_mode());
00075 return 1;
00076 }
00077 break;
00078 }
00079 break;
00080
00081
00082 case CEvent::TYPE_APPLICATION_EXIT:
00083 quit();
00084 return 1;
00085 }
00086 return 0;
00087 }
00088
00089
00090 void CGame::run()
00091 {
00092 timer.restart();
00093
00094 while ( !is_game_quit() ) {
00095 CEvent *event = CEventSystemManager::get_instance().get_next_event();
00096
00097
00098 if (event) {
00099 handle_event(*event);
00100 delete event;
00101 }
00102
00103
00104 for (std::list<ITickable *>::iterator i = tickables.begin(); i != tickables.end(); i++) {
00105 (*i)->tick(timer.get_time_passed());
00106 }
00107
00108
00109 draw_frame();
00110
00111
00112 timer.update();
00113 }
00114 }
00115
00116
00117 CGame::CGame(CSystemFactorySet *driver) : timer(TempGameConstants::GAME_MAX_FRAME_TIME)
00118 {
00119 using namespace TempGameConstants;
00120
00121
00122
00123 CVideoSystemManager::init(driver->videoFactory);
00124 CEventSystemManager::init(driver->eventFactory);
00125 CTimerSystemManager::init(driver->timerFactory);
00126
00127
00128
00129
00130 CScreen &screen = CVideoSystemManager::get_instance().get_screen();
00131 CRect screen_size = CRect(0, 0, screen.get_width(), screen.get_height());
00132 this->set_bounding_rect(screen_size);
00133
00134 quit_flag = 0;
00135
00136 font_large = new CFont(GAME_FONT_BIG_FILENAME, GAME_FONT_BIG_CHAR_WIDTH,
00137 GAME_FONT_BIG_CHAR_HEIGHT, GAME_FONT_BIG_CHARS_PER_LINE);
00138
00139 font_small = new CFont(GAME_FONT_SMALL_FILENAME, GAME_FONT_SMALL_CHAR_WIDTH,
00140 GAME_FONT_SMALL_CHAR_HEIGHT, GAME_FONT_SMALL_CHARS_PER_LINE);
00141 }
00142
00143
00144 CGame::~CGame()
00145 {
00146 delete font_large;
00147 delete font_small;
00148
00149 CTimerSystemManager::deinit();
00150 CEventSystemManager::deinit();
00151
00152 CVideoSystemManager::deinit();
00153 }
00154
00155 void CGame::init(CSystemFactorySet *driver)
00156 {
00157 unique_instance = new CGame(driver);
00158 }
00159
00160 void CGame::deinit()
00161 {
00162 delete unique_instance;
00163 }