Rally X
ELEN3009 Project by Justin Wernick and David Schneider
|
00001 #include "Screen.h" 00002 00003 Screen::Screen(unsigned int screenWidth, unsigned int screenHeight, bool fullscreen) 00004 :_exitClicked(false), 00005 _screenWidth(screenWidth), 00006 _screenHeight(screenHeight), 00007 _gameAreaWidth(_screenWidth*0.75), 00008 _infoPanelWidth(_screenWidth - _gameAreaWidth) 00009 { 00010 if (fullscreen) 00011 { 00012 al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); 00013 if (!resolutionSupported()) 00014 { 00015 al_show_native_message_box(NULL, "Fatal error", "Fatal error", "The fullscreen resolution specified in config.txt is not supported by your system. Please open config.txt and change the resolution to a supported resolution, or change fullscreen to false.", NULL, ALLEGRO_MESSAGEBOX_ERROR); 00016 throw BadResolution(); 00017 } 00018 } 00019 else 00020 { 00021 al_set_new_display_flags(ALLEGRO_WINDOWED); 00022 //need to add error checking for windows that are way too big 00023 } 00024 00025 al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); 00026 00027 _display = al_create_display(_screenWidth, _screenHeight); 00028 00029 al_hide_mouse_cursor(_display); 00030 _windowEvents = al_create_event_queue(); 00031 al_register_event_source(_windowEvents, al_get_display_event_source(_display)); 00032 //used so that ESC can be pressed to exit. 00033 al_register_event_source(_windowEvents, al_get_keyboard_event_source()); 00034 00035 _font = al_load_font("junction 02.ttf", _screenWidth/10, 0); 00036 if (_font == NULL) 00037 { 00038 al_show_native_message_box(NULL, "Fatal error", "Fatal error", "The file 'junction 02.ttf' was not found. Ensure that it is located in the working directory.", NULL, ALLEGRO_MESSAGEBOX_ERROR); 00039 al_destroy_event_queue(_windowEvents); 00040 al_destroy_display(_display); 00041 throw InstallFailure(); 00042 } 00043 00044 ALLEGRO_BITMAP* front = al_get_backbuffer(_display); 00045 al_flip_display(); 00046 ALLEGRO_BITMAP* back = al_get_backbuffer(_display); 00047 00048 _panels.push_back(new GamePanel(back, front, 0, 0, _gameAreaWidth, _screenHeight)); 00049 _panels.push_back(new InfoPanel(back, front, _gameAreaWidth, 0, _infoPanelWidth, _screenHeight)); 00050 } 00051 00052 Screen::~Screen() 00053 { 00054 for (vector<ScreenPanel*>::iterator iter = _panels.begin(); iter!=_panels.end(); ++iter) 00055 { 00056 delete (*iter); 00057 } 00058 _panels.clear(); 00059 00060 al_destroy_font(_font); 00061 al_destroy_event_queue(_windowEvents); 00062 al_destroy_display(_display); 00063 } 00064 00065 string Screen::getLevel() 00066 { 00067 string result(""); 00068 ALLEGRO_FILECHOOSER* filechooser = al_create_native_file_dialog(".", "Choose your level", "*.lvl",ALLEGRO_FILECHOOSER_FILE_MUST_EXIST); 00069 al_show_native_file_dialog(_display, filechooser); 00070 if (al_get_native_file_dialog_count(filechooser)==0) 00071 { 00072 _exitClicked = true; 00073 } 00074 else 00075 { 00076 result = al_get_native_file_dialog_path(filechooser, 0); 00077 } 00078 00079 al_destroy_native_file_dialog(filechooser); 00080 return result; 00081 } 00082 00083 void Screen::draw(const Maze& maze, const list<PlayerCar>& players, const list<EnemyCar>& enemies, const list<Checkpoint>& checkpoints, const list<Rock>& rocks, const list<Smokescreen>& smokescreens, const list<DestroyedObjectPopup>& popups) 00084 { 00085 for (vector<ScreenPanel*>::iterator iter = _panels.begin(); iter!=_panels.end(); ++iter) 00086 { 00087 (*iter)->draw(maze, players, enemies, checkpoints, rocks, smokescreens, popups); 00088 } 00089 flip(); 00090 } 00091 00092 void Screen::flip() 00093 { 00094 al_flip_display(); 00095 for (vector<ScreenPanel*>::iterator iter = _panels.begin(); iter!=_panels.end(); ++iter) 00096 { 00097 (*iter)->flip(); 00098 } 00099 } 00100 00101 void Screen::drawWin() 00102 { 00103 flip(); 00104 00105 ALLEGRO_COLOR transBlack = al_map_rgba(0,0,0,200); 00106 al_draw_filled_rectangle(0,0,_screenWidth,_screenHeight, transBlack); 00107 00108 ALLEGRO_COLOR textColour = al_map_rgb(255,255,255); 00109 al_draw_text(_font, textColour, _screenWidth/2, _screenHeight/2, ALLEGRO_ALIGN_CENTRE , "You win!"); 00110 00111 flip(); 00112 } 00113 00114 void Screen::drawLoss() 00115 { 00116 flip(); 00117 00118 ALLEGRO_COLOR transBlack = al_map_rgba(0,0,0,200); 00119 al_draw_filled_rectangle(0,0,_screenWidth,_screenHeight, transBlack); 00120 00121 ALLEGRO_COLOR textColour = al_map_rgb(255,255,255); 00122 al_draw_text(_font, textColour, _screenWidth/2, _screenHeight/2, ALLEGRO_ALIGN_CENTRE , "You lose!"); 00123 00124 flip(); 00125 } 00126 00127 bool Screen::exitClicked() 00128 { 00129 if (_exitClicked) return true; 00130 00131 ALLEGRO_EVENT event; 00132 while (al_get_next_event(_windowEvents, &event)) 00133 { 00134 if (event.type==ALLEGRO_EVENT_DISPLAY_CLOSE || (event.type==ALLEGRO_EVENT_KEY_CHAR && event.keyboard.keycode==ALLEGRO_KEY_ESCAPE)) 00135 { 00136 al_flush_event_queue(_windowEvents); 00137 _exitClicked = true; 00138 return true; 00139 } 00140 } 00141 00142 return false; 00143 } 00144 00145 bool Screen::resolutionSupported() 00146 { 00147 ALLEGRO_DISPLAY_MODE mode; 00148 for (int i=0; i<al_get_num_display_modes(); ++i) 00149 { 00150 al_get_display_mode(i, &mode); 00151 00152 if (static_cast<unsigned int>(mode.width)==_screenWidth && static_cast<unsigned int>(mode.height)==_screenHeight) 00153 { 00154 return true; 00155 } 00156 } 00157 00158 return false; 00159 }