Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

cgame.cpp

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 //#include <ruby.h>
00012 
00013 CGame *CGame::unique_instance = 0;
00014 
00015 /* Returns true if the game quit flag has been activated. */
00016 int CGame::is_game_quit()
00017 {
00018   return quit_flag;
00019 }
00020 
00021 /* Quit the game.  This should be called by a game state when it wants the game
00022    to quit, so that everything is properly shut down upon exit.  This is done by
00023    setting the game quit flag to TRUE; the game loop checks the value of this flag
00024    each time around, and if it has been activated, the game deinitializes. */
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 /* Draw the current frame. */
00042 void CGame::draw_frame()
00043 {
00044   CScreen &screen = CVideoSystemManager::get_instance().get_screen();
00045 
00046   /* Remove all consecutive rectangles on our update list. */
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         /* If the game state's controller doesn't handle the input,
00062                         we'll deal with it ourselves. */
00063   switch (event.get_type()) {
00064                         /* If the user presses "f", show framerate info. */
00065       case CEvent::TYPE_KEY_DOWN:
00066         switch (event.get_key_ascii_value()) {
00067                                         case 'f':
00068                                                 /* If the user pressed CTRL-F, toggle fullscreen mode,
00069                                                                 otherwise just toggle display of the game's
00070                                                                 framerate statistics. */
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                         /* If the user uses the OS or GUI to stop the program,
00081                                         e.g. by clicking the game window's close box, then quit. */
00082       case CEvent::TYPE_APPLICATION_EXIT:
00083                                 quit();
00084         return 1;
00085                 }
00086     return 0;
00087 }
00088 
00089 /* Run the game.  This is the main "game loop". */
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                 /* Process input using a basic "chain of responsibility" pattern. */
00098                 if (event) {
00099       handle_event(*event);
00100       delete event;
00101     }
00102 
00103                 /* Process the model of the current game state. */
00104     for (std::list<ITickable *>::iterator i = tickables.begin(); i != tickables.end(); i++) {
00105       (*i)->tick(timer.get_time_passed());
00106     }
00107 
00108                 /* Draw the current frame. */
00109                 draw_frame();
00110 
00111                 /* Update framerate and other "passage of time"-related information. */
00112     timer.update();
00113         }
00114 }
00115 
00116 /* Initialize the game. */
00117 CGame::CGame(CSystemFactorySet *driver) : timer(TempGameConstants::GAME_MAX_FRAME_TIME)
00118 {
00119   using namespace TempGameConstants;
00120   //ruby_init();
00121   //ruby_script("embedded");
00122 
00123   CVideoSystemManager::init(driver->videoFactory);
00124   CEventSystemManager::init(driver->eventFactory);
00125   CTimerSystemManager::init(driver->timerFactory);
00126 
00127   /* \todo figure out a way to encapsulate this into the cscreen object so that
00128      when the screen size changes, this is automatically changed too... maybe
00129      make cscreen a descendant of CModel so we have a pub/sub system? */
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 /* Shut down the game. */
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 }

Generated on Wed Aug 27 11:58:41 2003 for GFW by doxygen 1.3.3