summaryrefslogtreecommitdiff
path: root/source/presentation/Screen.h
blob: 16d8ceea49c4bdfd75f4f7ea02ff51d21610aa2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef SCREEN_H
#define SCREEN_H

#include <list>
#include <algorithm>
#include <sstream>
using namespace std;

#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>

#include "../logic/Maze.h"
#include "../logic/GameObject.h"
#include "../logic/PlayerCar.h"
#include "../logic/EnemyCar.h"
#include "../logic/Checkpoint.h"
#include "../logic/Rock.h"
#include "../logic/Smokescreen.h"
#include "../logic/DestroyedObjectPopup.h"
#include "../logic/AllegroWrappers.h"

#include "../presentation/GamePanel.h"
#include "../presentation/InfoPanel.h"

/**
* @brief Exception that is thrown if the Config file requests a resolution that can not work.
*
* @author Justin Wernick
* @author David Schneider
*/
class BadResolution{};

/**
* @brief Class for creating a window on the screen.
*
* Anything involving drawing on the screen is the responsibility of this class. Most of these
* responsibilities are outsourced through creating ScreenPanels with more specialised tasks,
* such as drawing specifically the area where the gameplay takes place (GamePanel) or the
* providing the player with information (InfoPanel).
*
* @author Justin Wernick
* @author David Schneider
*/
class Screen
{
    public:
        /**
        * @brief Creates a Screen with the given width and height in pixels, and the given fullscreen setting.
        *
        * @param [in] screenWidth The width of the display that will be created in pixels.
        * @param [in] screenHeight The height of the display that will be created in pixels.
        * @param [in] fullscreen True if the game should be in fullscreen mode. False otherwise.
        */
        Screen(unsigned int screenWidth, unsigned int screenHeight, bool fullscreen);

        /**
        * @brief Destructor to ensure that the display and any ScreenPanels are destroyed properly.
        */
        ~Screen();

        /**
        * @brief Presents the player with a file dialog, requesting a level file to be selected.
        *
        * If the cancel button is clicked, and empty string is returned and exitClicked() will return
        * true on its next call.
        *
        * @return The path of the level file, or an empty string if no file was selected.
        */
        string getLevel();

        /**
        * @brief Draws the given objects on the screen.
        *
        * The objects are passed to each of the ScreenPanels in turn, and then the buffers
        * are flipped.
        *
        * @param [in] maze The Maze that all of the objects are in.
        * @param [in] players The list of PlayerCars to be drawn.
        * @param [in] enemies The list of EnemyCars to be drawn.
        * @param [in] checkpoints The list of Checkpoints to be drawn.
        * @param [in] rocks The list of Rocks to be drawn.
        * @param [in] smokescreens The list of Smokescreens to be drawn.
        * @param [in] popups The list of DestroyedObjectPopups to be drawn.
        */
        void 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);

        /**
        * @brief Function to find if the player has chosen to exit the game.
        *
        * The game can be exited by clicking the x in the corner of the window, pressing the ESC key
        * during the game, or by clicking 'cancel' on the file selection dialog.
        *
        * @return True if the game should be quit.
        */
        bool exitClicked();

        /**
        * @brief Draws a splash screen when the level has been won.
        */
        void drawWin();
        /**
        * @brief Draws a splash screen when the level has been lost.
        */
        void drawLoss();

    private:
        /**
        * @brief Unimplemented copy constructor, prevents copying of Screen objects.
        *
        * Copying a Screen is unneccesary as there should only be a single Screen object.
        */
        Screen(const Screen& ref);
        /**
        * @brief Unimplemented assignment operator.
        *
        * @see Screen(const Screen& ref)
        */
        Screen& operator=(const Screen& rhs);

        /**
        * @brief Flips the display's buffers, as well as those for all of the ScreenPanels.
        */
        void flip();

        /**
        * @brief Checks the current resolution (_screenWidth and _screenHeight) against the screen's supported resolutions.
        *
        * Used to test if a fullscreen mode selection will launch without issue.
        *
        * @return True if the current resolution is supported.
        */
        bool resolutionSupported();

        AllegroInit _allegro; ///< Ensures that Allegro has been installed, for event queues and creating the display.
        AllegroKeyboardInit _keyboard; ///< Ensures that the keyboard has been installed, for checking for the ESC key.
        AllegroDrawingInit _drawing; ///< Ensures that drawing operations have been installed, for drawing splash screens.

        bool _exitClicked; ///< Set to true when the user chooses to quit the game.

        unsigned int _screenWidth; ///< Horizontal number of pixels per row on the screen.
        unsigned int _screenHeight; ///< Vertical number of pixels per column on the screen.

        unsigned int _gameAreaWidth; ///< Width of the GamePanel created.
        unsigned int _infoPanelWidth; ///< Width of the InfoPanel created. The InfoPanel is placed directly to the right of the GamePanel.

        ALLEGRO_DISPLAY* _display; ///< The window created on the player's monitor to see the game.
        ALLEGRO_EVENT_QUEUE* _windowEvents; ///< Events caught by the screen, checked for an exit command.
        ALLEGRO_FONT* _font; ///< Font used in drawing splash screens.

        /**
        * @brief Polymorphic container used to encapsulate the different types of drawing to the screen.
        *
        * Since the memory of the ScreenPanels in the vector is allocated dynamically, it must be deallocated
        * in the destructor.
        */
        vector<ScreenPanel*> _panels;
};

#endif // SCREEN_H