1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2015 Jussi Lind <jussi.lind@iki.fi>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef GAME_HPP
17 #define GAME_HPP
18 
19 #include <QObject>
20 #include <QTime>
21 #include <QTimer>
22 #include <QTranslator>
23 
24 #include <MCWorld>
25 
26 #include "application.hpp"
27 #include "settings.hpp"
28 
29 class AudioWorker;
30 class Database;
31 class EventHandler;
32 class InputHandler;
33 class Renderer;
34 class Scene;
35 class Startlights;
36 class StartlightsOverlay;
37 class StateMachine;
38 class TimingOverlay;
39 class TrackLoader;
40 class QScreen;
41 
42 //! The main game class.
43 class Game : public QObject
44 {
45     Q_OBJECT
46 
47 public:
48     enum class Mode
49     {
50         OnePlayerRace,
51         TwoPlayerRace,
52         TimeTrial,
53         Duel
54     };
55 
56     enum class SplitType
57     {
58         Horizontal,
59         Vertical
60     };
61 
62     enum class Fps
63     {
64         Fps30,
65         Fps60
66     };
67 
68     //! Constructor
69     Game(int & argc, char ** argv);
70 
71     //! Destructor
72     virtual ~Game();
73 
74     //! Return the game instance.
75     static Game & instance();
76 
77     //! \return The renderer.
78     Renderer & renderer() const;
79 
80     int run();
81 
82     //! Set the game mode.
83     void setMode(Mode mode);
84 
85     //! Get the game mode.
86     Mode mode() const;
87 
88     //! Set the split type on two-player game.
89     void setSplitType(SplitType splitType);
90 
91     //! Get the split type.
92     SplitType splitType() const;
93 
94     void setFps(Fps fps);
95 
96     Fps fps() const;
97 
98     //! Set the lap count.
99     void setLapCount(int lapCount);
100 
101     //! Get the lap count.
102     int lapCount() const;
103 
104     //! \return True if the current mode has two human players.
105     bool hasTwoHumanPlayers() const;
106 
107     //! \return True if the current mode has computer players.
108     bool hasComputerPlayers() const;
109 
110     EventHandler & eventHandler();
111 
112     AudioWorker & audioWorker();
113 
114     DifficultyProfile & difficultyProfile();
115 
116     const std::string & fontName() const;
117 
118     QScreen * screen() const;
119 
120 public slots:
121 
122     void exitGame();
123 
124 private slots:
125 
126     void init();
127 
128     void togglePause();
129 
130 private:
131     void addTrackSearchPaths();
132 
133     void adjustSceneSize(int hRes, int vRes);
134 
135     void createRenderer();
136 
137     void initScene();
138 
139     bool loadTracks();
140 
141     void parseArgs(int argc, char ** argv);
142 
143     void start();
144 
145     void stop();
146 
147     Application m_app;
148 
149     QTranslator m_appTranslator;
150 
151     std::unique_ptr<Database> m_database;
152 
153     bool m_forceNoVSync;
154 
155     Settings m_settings;
156 
157     DifficultyProfile m_difficultyProfile;
158 
159     InputHandler * m_inputHandler;
160 
161     EventHandler * m_eventHandler;
162 
163     StateMachine * m_stateMachine;
164 
165     Renderer * m_renderer;
166 
167     Scene * m_scene;
168 
169     TrackLoader * m_trackLoader;
170 
171     QScreen * m_screen = nullptr;
172 
173     int m_screenIndex = 0;
174 
175     int m_updateFps;
176 
177     int m_updateDelay;
178 
179     float m_timeStep;
180 
181     int m_lapCount;
182 
183     bool m_paused;
184 
185     QTimer m_updateTimer;
186 
187     QTime m_elapsed;
188 
189     int m_renderElapsed;
190 
191     Fps m_fps;
192 
193     Mode m_mode;
194 
195     SplitType m_splitType;
196 
197     AudioWorker * m_audioWorker;
198 
199     QThread * m_audioThread;
200 
201     MCWorld * m_world;
202 
203     static Game * m_instance;
204 };
205 
206 #endif // GAME_HPP
207