Colobot
app.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 #include "common/event.h"
28 #include "common/language.h"
29 #include "common/singleton.h"
30 
31 #include "graphics/core/device.h"
32 
33 #include "level/level_category.h"
34 
35 
36 #include <string>
37 #include <vector>
38 
39 
40 class CEventQueue;
41 class CController;
42 class CSoundInterface;
43 class CInput;
44 class CPathManager;
45 class CConfigFile;
46 class CSystemUtils;
47 struct SystemTimeStamp;
48 
49 namespace Gfx
50 {
51 class CEngine;
52 struct DeviceConfig;
53 }
54 
60 {
62  int index;
64  std::string name;
66  int axisCount;
69 
71  : index(-1), axisCount(0), buttonCount(0) {}
72 };
73 
79 {
80  VIDEO_QUERY_ERROR,
81  VIDEO_QUERY_NONE,
82  VIDEO_QUERY_ALL,
83  VIDEO_QUERY_OK
84 };
85 
91 {
92  PARSE_ARGS_OK = 1,
95 };
96 
102 {
103  MOUSE_SYSTEM,
107 };
108 
114 {
115  PCNT_EVENT_PROCESSING,
116 
121 
129 
131 
133 
135 };
136 
137 enum DebugMode
138 {
139  DEBUG_SYS_EVENTS = 1 << 0,
140  DEBUG_APP_EVENTS = 1 << 1,
141  DEBUG_EVENTS = DEBUG_SYS_EVENTS | DEBUG_APP_EVENTS,
142  DEBUG_MODELS = 1 << 2,
143  DEBUG_ALL = DEBUG_SYS_EVENTS | DEBUG_APP_EVENTS | DEBUG_MODELS
144 };
145 
146 struct ApplicationPrivate;
147 
191 class CApplication : public CSingleton<CApplication>
192 {
193 public:
195  CApplication(CSystemUtils* systemUtils);
197  ~CApplication();
198 
203 
204 public:
206  ParseArgsStatus ParseArguments(int argc, char *argv[]);
208  bool Create();
210  int Run();
212  int GetExitCode() const;
213 
215  const std::string& GetErrorMessage() const;
216 
218  VideoQueryResult GetVideoResolutionList(std::vector<Math::IntPoint> &resolutions,
219  bool fullScreen, bool resizeable) const;
220 
223 
225  bool ChangeVideoConfig(const Gfx::DeviceConfig &newConfig);
226 
228  void SuspendSimulation();
230  void ResumeSimulation();
232  bool GetSimulationSuspended() const;
233 
235  void ResetTimeAfterLoading();
236 
238  void SetSimulationSpeed(float speed);
240  float GetSimulationSpeed() const;
242 
244  float GetAbsTime() const;
246  long long GetExactAbsTime() const;
247 
249  long long GetRealAbsTime() const;
250 
252  float GetRelTime() const;
254  long long GetExactRelTime() const;
255 
257  long long GetRealRelTime() const;
258 
260  std::vector<JoystickDevice> GetJoystickList() const;
261 
263  JoystickDevice GetJoystick() const;
264 
266  bool ChangeJoystick(const JoystickDevice &newJoystick);
267 
269 
270  void SetJoystickEnabled(bool enable);
271  bool GetJoystickEnabled() const;
273 
275  void UpdateJoystick();
276 
278  void UpdateMouse();
279 
281 
282  void SetGrabInput(bool grab);
283  bool GetGrabInput() const;
285 
287 
288  void SetMouseMode(MouseMode mode);
289  MouseMode GetMouseMode() const;
291 
293  void MoveMouse(Math::Point pos);
294 
296 
297  void SetDebugModeActive(DebugMode mode, bool active);
298  bool IsDebugModeActive(DebugMode mode) const;
299  static bool ParseDebugModes(const std::string& str, int& debugModes);
301 
303 
304  Language GetLanguage() const;
305  char GetLanguageChar() const;
306  void SetLanguage(Language language);
307  static bool ParseLanguage(const std::string& str, Language& language);
309 
311 
312  void SetLowCPU(bool low);
313  bool GetLowCPU() const;
315 
317 
319  void StopPerformanceCounter(PerformanceCounter counter);
320  float GetPerformanceCounterData(PerformanceCounter counter) const;
322 
323  bool GetSceneTestMode();
324 
326  void Render();
327 
329  void RenderIfNeeded(int updateRate);
330 
331 protected:
333  bool CreateVideoSurface();
334 
338  Event CreateVirtualEvent(const Event& sourceEvent);
340  TEST_VIRTUAL Event CreateUpdateEvent();
342  void LogEvent(const Event& event);
343 
345  bool OpenJoystick();
347  void CloseJoystick();
348 
351 
356 
357 protected:
361  std::unique_ptr<ApplicationPrivate> m_private;
363  std::unique_ptr<CEventQueue> m_eventQueue;
365  std::unique_ptr<Gfx::CEngine> m_engine;
367  std::unique_ptr<Gfx::CDevice> m_device;
369  std::unique_ptr<CSoundInterface> m_sound;
371  std::unique_ptr<CController> m_controller;
373  std::unique_ptr<CConfigFile> m_configFile;
375  std::unique_ptr<CInput> m_input;
377  std::unique_ptr<CPathManager> m_pathManager;
378 
382  bool m_active;
385 
387  std::string m_errorMessage;
388 
393 
395  std::string m_windowTitle;
396 
398 
400  SystemTimeStamp* m_lastTimeStamp;
401  SystemTimeStamp* m_curTimeStamp;
402 
403  SystemTimeStamp* m_performanceCounters[PCNT_MAX][2];
404  float m_performanceCountersData[PCNT_MAX];
405 
406  long long m_realAbsTimeBase;
407  long long m_realAbsTime;
408  long long m_realRelTime;
409 
410  long long m_absTimeBase;
411  long long m_exactAbsTime;
412  long long m_exactRelTime;
413 
414  float m_absTime;
415  float m_relTime;
416 
417  float m_simulationSpeed;
418  bool m_simulationSuspended;
420 
421  SystemTimeStamp* m_manualFrameLast;
422  SystemTimeStamp* m_manualFrameTime;
423 
425  std::string m_graphics;
426 
429 
435  std::vector<int> m_joyAxeState;
437  std::vector<bool> m_joyButtonState;
438 
440  LevelCategory m_runSceneCategory;
442  int m_runSceneRank;
444 
447 
449  Language m_language;
450 
452  bool m_lowCPU;
453 
456 
459 
461  static char m_languageLocale[50];
462 };
~CApplication()
Destructor.
Definition: app.cpp:166
CSoundInterface * GetSound()
Returns the sound subsystem.
Definition: app.cpp:223
CSingleton base class for singletons.
void Render()
Renders the image in window.
Definition: app.cpp:1302
< frame update in CParticle
Definition: app.h:120
ParseArgsStatus ParseArguments(int argc, char *argv[])
Parses commandline arguments.
Definition: app.cpp:228
Gfx::DeviceConfig GetVideoConfig() const
Returns the current video mode.
Definition: app.cpp:1445
std::unique_ptr< CInput > m_input
Input manager.
Definition: app.h:375
std::string m_windowTitle
Text set as window title.
Definition: app.h:395
ParseArgsStatus
State of parsing commandline arguments.
Definition: app.h:90
static char m_languageLocale[50]
Static buffer for putenv locale.
Definition: app.h:461
void CloseJoystick()
Closes the joystick device.
Definition: app.cpp:781
< swapping buffers and vsync
Definition: app.h:132
Class for managing data/lang/save paths.
Definition: pathman.h:37
const std::string & GetErrorMessage() const
Returns the message of error (set to something if exit code is not 0)
Definition: app.cpp:1036
bool m_sceneTest
Scene test mode.
Definition: app.h:446
void SetJoystickEnabled(bool enable)
Management of joystick enable state.
Definition: app.cpp:1598
long long GetExactAbsTime() const
Returns the exact absolute time counter [nanoseconds].
Definition: app.cpp:1420
< rendering shadow map
Definition: app.h:130
void SetSimulationSpeed(float speed)
Management of simulation speed.
Definition: app.cpp:1361
bool m_resolutionOverride
Screen resoultion overriden by commandline.
Definition: app.h:455
< rendering the water
Definition: app.h:125
CEventQueue * GetEventQueue()
Returns the application's event queue.
Definition: app.cpp:218
std::vector< bool > m_joyButtonState
Current state of joystick buttons; may be updated from another thread.
Definition: app.h:437
void SetMouseMode(MouseMode mode)
Management of mouse mode.
Definition: app.cpp:1555
< all ok
Definition: app.h:93
< all counters together
Definition: app.h:134
CSystemUtils * m_systemUtils
System utils instance.
Definition: app.h:359
std::vector< int > m_joyAxeState
Current state of joystick axes; may be updated from another thread.
Definition: app.h:435
VideoQueryResult GetVideoResolutionList(std::vector< Math::IntPoint > &resolutions, bool fullScreen, bool resizeable) const
Returns a list of possible video modes.
Definition: app.cpp:1450
< invalid syntax
Definition: app.h:94
< the whole frame update process
Definition: app.h:118
Gfx::DeviceConfig m_deviceConfig
Current configuration of OpenGL display device.
Definition: app.h:390
< in-game cursor visible; system cursor hidden
Definition: app.h:105
void ResetTimeAfterLoading()
Resets time counters to account for time spent loading game.
Definition: app.cpp:1341
Definition: singleton.h:30
int index
Device index (-1 = invalid device)
Definition: app.h:62
bool GetSimulationSuspended() const
Returns whether simulation is suspended.
Definition: app.cpp:1356
void InternalResumeSimulation()
Internal procedure to reset time counters.
Definition: app.cpp:1348
std::string m_graphics
Graphics device to use.
Definition: app.h:425
void LogEvent(const Event &event)
Logs debug data for event.
Definition: app.cpp:1172
bool ChangeJoystick(const JoystickDevice &newJoystick)
Change the current joystick device.
Definition: app.cpp:791
LevelCategory m_runSceneCategory
Scene to run on startup.
Definition: app.h:441
long long GetExactRelTime() const
Returns the exact realative time since last update [nanoseconds].
Definition: app.cpp:1435
std::string name
Device name.
Definition: app.h:64
bool CreateVideoSurface()
Creates the window's SDL_Surface.
Definition: app.cpp:652
std::unique_ptr< ApplicationPrivate > m_private
Private (SDL-dependent data)
Definition: app.h:361
Global event queue.
Definition: event.h:840
int axisCount
Number of axes (only available after joystick opened)
Definition: app.h:66
SystemTimeStamp * m_baseTimeStamp
Animation time stamps, etc.
Definition: app.h:399
CApplication(CSystemUtils *systemUtils)
Constructor (can only be called once!)
Definition: app.cpp:105
void SetLowCPU(bool low)
Management of sleep in main loop (lowers CPU usage)
Definition: app.cpp:1803
TEST_VIRTUAL Event CreateUpdateEvent()
Prepares a simulation update event.
Definition: app.cpp:1372
void UpdateJoystick()
Polls the state of joystick axes and buttons.
Definition: app.cpp:820
bool m_lowCPU
Low cpu mode.
Definition: app.h:452
Class for loading config file.
Definition: config_file.h:40
PerformanceCounter
Type of counter testing performance.
Definition: app.h:113
std::unique_ptr< CSoundInterface > m_sound
Sound subsystem.
Definition: app.h:369
General config for graphics device.
Definition: device.h:55
std::unique_ptr< Gfx::CDevice > m_device
Graphics device.
Definition: app.h:367
< rendering 2D interface
Definition: app.h:128
long m_debugModes
Bit array of active debug modes.
Definition: app.h:384
void MoveMouse(Math::Point pos)
Moves (warps) the mouse cursor to the specified position (in interface coords)
Definition: app.cpp:1569
std::unique_ptr< Gfx::CEngine > m_engine
Graphics engine.
Definition: app.h:365
void UpdateMouse()
Updates the mouse position explicitly.
Definition: app.cpp:874
void UpdatePerformanceCountersData()
Updates performance counters from gathered timer data.
Definition: app.cpp:1837
float GetAbsTime() const
Returns the absolute time counter [seconds].
Definition: app.cpp:1415
< rendering the particles in 3D
Definition: app.h:124
< rendering the terrain
Definition: app.h:126
< frame update in CEngine
Definition: app.h:119
2D point
Definition: point.h:50
< both cursors visible (only for debug)
Definition: app.h:106
< the whole rendering process
Definition: app.h:123
bool Create()
Initializes the application.
Definition: app.cpp:429
long long GetRealRelTime() const
Returns the exact relative time since last update disregarding speed setting [nanoseconds].
Definition: app.cpp:1440
Main application.
Definition: app.h:191
std::unique_ptr< CEventQueue > m_eventQueue
Global event queue.
Definition: app.h:363
Language m_language
Application language.
Definition: app.h:449
std::unique_ptr< CPathManager > m_pathManager
Path manager.
Definition: app.h:377
int m_exitCode
Code to return at exit.
Definition: app.h:380
Entry point into CRobotMain.
Definition: controller.h:40
JoystickDevice GetJoystick() const
Returns info about the current joystick.
Definition: app.cpp:1593
float GetRelTime() const
Returns the relative time since last update [seconds].
Definition: app.cpp:1430
JoystickDevice m_joystick
Info about current joystick device.
Definition: app.h:431
Event CreateVirtualEvent(const Event &sourceEvent)
If applicable, creates a virtual event to match the changed state as of new event.
Definition: app.cpp:1254
bool m_joystickEnabled
Whether joystick is enabled.
Definition: app.h:433
std::unique_ptr< CConfigFile > m_configFile
Profile (INI) reader/writer.
Definition: app.h:373
Namespace for (new) graphics code.
Definition: app.h:49
void SetDebugModeActive(DebugMode mode, bool active)
Management of debug modes (printing more info in logger)
Definition: app.cpp:1493
VideoQueryResult
Result of querying for available video resolutions.
Definition: app.h:78
< rendering the 3D objects
Definition: app.h:127
bool m_headless
Headles mode.
Definition: app.h:458
std::vector< JoystickDevice > GetJoystickList() const
Returns a list of available joystick devices.
Definition: app.cpp:1576
The graphics engine.
Definition: engine.h:620
Gfx::DeviceConfig m_lastDeviceConfig
Previous configuration of OpenGL display device.
Definition: app.h:392
Event types, structs and event queue.
MouseMode m_mouseMode
Current mode of mouse.
Definition: app.h:428
bool m_active
Whether application window is active.
Definition: app.h:382
< event processing (except update events)
Definition: app.h:117
std::unique_ptr< CController > m_controller
Game controller - game engine and UI.
Definition: app.h:371
MouseMode
Mode of mouse cursor.
Definition: app.h:101
void SuspendSimulation()
Suspends animation (time will not be updated)
Definition: app.cpp:1327
Platform-specific utils.
Definition: system.h:91
Definition: system_linux.h:30
Abstract graphics device - CDevice class and related structs/enums.
Language GetLanguage() const
Management of language.
Definition: app.cpp:1620
void RenderIfNeeded(int updateRate)
Renders the image in window if needed.
Definition: app.cpp:1314
std::string m_errorMessage
Message to be displayed as error to the user.
Definition: app.h:387
void SetGrabInput(bool grab)
Management of the grab mode for input (keyboard & mouse)
Definition: app.cpp:1544
< system cursor visible; in-game cursor hidden
Definition: app.h:104
long long GetRealAbsTime() const
Returns the exact absolute time counter disregarding speed setting [nanoseconds]. ...
Definition: app.cpp:1425
< frame update in CRobotMain
Definition: app.h:122
Event sent by system, interface or game.
Definition: event.h:709
void ResumeSimulation()
Resumes animation.
Definition: app.cpp:1333
int GetExitCode() const
Returns the code to be returned at main() exit.
Definition: app.cpp:1031
bool OpenJoystick()
Opens the joystick device.
Definition: app.cpp:757
int buttonCount
Number of buttons (only available after joystick opened)
Definition: app.h:68
int Run()
Main event loop.
Definition: app.cpp:881
Event ProcessSystemEvent()
Processes the captured SDL event to Event struct.
Definition: app.cpp:1043
Private data of CApplication class.
Definition: app.cpp:80
bool ChangeVideoConfig(const Gfx::DeviceConfig &newConfig)
Change the video mode to given mode.
Definition: app.cpp:703
Information about a joystick device.
Definition: app.h:59
Sound plugin interface.
Definition: sound.h:57
void ResetPerformanceCounters()
Resets all performance counters to zero.
Definition: app.cpp:1828
void StartPerformanceCounter(PerformanceCounter counter)
Management of performance counters.
Definition: app.cpp:1813
Management of mouse, keyboard and joystick.
Definition: input.h:68