diff options
Diffstat (limited to 'source/presentation/InfoPanel.cpp')
-rw-r--r-- | source/presentation/InfoPanel.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/source/presentation/InfoPanel.cpp b/source/presentation/InfoPanel.cpp new file mode 100644 index 0000000..b4d33fa --- /dev/null +++ b/source/presentation/InfoPanel.cpp @@ -0,0 +1,111 @@ +#include "InfoPanel.h" + +InfoPanel::InfoPanel(ALLEGRO_BITMAP* back, ALLEGRO_BITMAP* front, int x, int y, int width, int height) + :ScreenPanel(back, front, x, y, width, height), + _petrolHeadingY(_width/10), + _petrolGuageY(_petrolHeadingY + _width/10), + _petrolGuageHeight(_width/10), + _checkpointHeadingY(_petrolGuageY + _petrolGuageHeight + _width/10), + _checkpointValueY(_checkpointHeadingY + _width/10), + _miniMazeY(_checkpointValueY + _width/5), + _miniMazeHeight(_height - _miniMazeY), + _miniMazeblockWidth(0) +{ + _panelFont = al_load_font("junction 02.ttf", _width/10, 0); + if (_panelFont == NULL) + { + 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); + throw InstallFailure(); + } +} + +InfoPanel::~InfoPanel() +{ + al_destroy_font(_panelFont); +} + +void InfoPanel::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) +{ + ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap(); + al_set_target_bitmap(_back); + + double petrol = 0; + if (!players.empty()) + { + petrol = players.front().petrol(); + } + + al_clear_to_color(_colourStore.getColour(BitmapStore::MAZE_FLOOR)); + + //gets a mazeblock width the fits the current maze + _miniMazeblockWidth = min(static_cast<float>(_width)/maze.width(), static_cast<float>(_miniMazeHeight)/maze.height()); + + //draws petrol heading and bar + al_draw_text(_panelFont, al_map_rgb(255,255,255), 1, _petrolHeadingY, ALLEGRO_ALIGN_LEFT , "Petrol"); + al_draw_filled_rectangle(0,_petrolGuageY,_width*petrol, _petrolGuageY+_petrolGuageHeight, al_map_rgb(255,128,0)); + + //draws checkpoints remaining heading and value + al_draw_text(_panelFont, al_map_rgb(255,255,255), 1, _checkpointHeadingY, ALLEGRO_ALIGN_LEFT , "Checkpoints"); + stringstream checkpointCountString; + checkpointCountString << Checkpoint::checkpointCount(); + al_draw_text(_panelFont, al_map_rgb(255,255,255), 1, _checkpointValueY, ALLEGRO_ALIGN_LEFT , checkpointCountString.str().c_str()); + + draw(maze); + for (list<PlayerCar>::const_iterator iter = players.begin(); iter != players.end(); ++iter) + { + draw(*iter); + } + for (list<EnemyCar>::const_iterator iter = enemies.begin(); iter != enemies.end(); ++iter) + { + draw(*iter); + } + for (list<Checkpoint>::const_iterator iter = checkpoints.begin(); iter != checkpoints.end(); ++iter) + { + draw(*iter); + } + + //restore draw target + al_set_target_bitmap(prev_draw); +} + +void InfoPanel::draw(const Maze& maze) +{ + ALLEGRO_COLOR wallColour = _colourStore.getColour(BitmapStore::MAZE_WALL); + ALLEGRO_COLOR floorColour = _colourStore.getColour(BitmapStore::MAZE_FLOOR); + + for (int x=0; x<maze.width(); ++x) + { + for (int y=0; y<maze.height(); ++y) + { + float x1 = getPanelX(x); + float x2 = x1 + _miniMazeblockWidth; + float y1 = getPanelY(y) + _miniMazeY; + float y2 = y1 + _miniMazeblockWidth; + if (maze.getSolid(x,y)) + { + al_draw_filled_rectangle(x1, y1, x2, y2, wallColour); + } + else + { + al_draw_filled_rectangle(x1, y1, x2, y2, floorColour); + } + } + } +} + +void InfoPanel::draw(const GameObject& object) +{ + float r = _miniMazeblockWidth/2; + float cx = getPanelX(object.x()) + r; + float cy = getPanelY(object.y()) + _miniMazeY + r; + al_draw_filled_circle(cx, cy, r, _colourStore.getColour(object.image())); +} + +float InfoPanel::getPanelX(const double& x) const +{ + return static_cast<float>(x*_miniMazeblockWidth); +} +float InfoPanel::getPanelY(const double& y) const +{ + return static_cast<float>(y*_miniMazeblockWidth); +} |