Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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-2016, 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  PARSE_ARGS_OK = 1,
83 };
84 
90 {
91  MOUSE_SYSTEM,
95 };
96 
97 enum DebugMode
98 {
99  DEBUG_SYS_EVENTS = 1 << 0,
100  DEBUG_UPDATE_EVENTS = 1 << 1,
101  DEBUG_APP_EVENTS = 1 << 2,
102  DEBUG_EVENTS = DEBUG_SYS_EVENTS | DEBUG_UPDATE_EVENTS | DEBUG_APP_EVENTS,
103  DEBUG_MODELS = 1 << 3,
104  DEBUG_ALL = DEBUG_SYS_EVENTS | DEBUG_APP_EVENTS | DEBUG_MODELS
105 };
106 
107 struct ApplicationPrivate;
108 
152 class CApplication : public CSingleton<CApplication>
153 {
154 public:
156  CApplication(CSystemUtils* systemUtils);
158  ~CApplication();
159 
164 
165 public:
167  ParseArgsStatus ParseArguments(int argc, char *argv[]);
169  bool Create();
171  int Run();
173  int GetExitCode() const;
174 
176  const std::string& GetErrorMessage() const;
177 
179  void GetVideoResolutionList(std::vector<Math::IntPoint> &resolutions, int display = 0) const;
180 
183 
185  bool ChangeVideoConfig(const Gfx::DeviceConfig &newConfig);
186 
188  void SuspendSimulation();
190  void ResumeSimulation();
192  bool GetSimulationSuspended() const;
193 
195  void ResetTimeAfterLoading();
196 
198  void SetSimulationSpeed(float speed);
200  float GetSimulationSpeed() const;
202 
204  float GetAbsTime() const;
206  long long GetExactAbsTime() const;
207 
209  long long GetRealAbsTime() const;
210 
212  float GetRelTime() const;
214  long long GetExactRelTime() const;
215 
217  long long GetRealRelTime() const;
218 
220  std::vector<JoystickDevice> GetJoystickList() const;
221 
223  JoystickDevice GetJoystick() const;
224 
226  bool ChangeJoystick(const JoystickDevice &newJoystick);
227 
229 
230  void SetJoystickEnabled(bool enable);
231  bool GetJoystickEnabled() const;
233 
235  void UpdateJoystick();
236 
238  void UpdateMouse();
239 
241 
242  void SetMouseMode(MouseMode mode);
243  MouseMode GetMouseMode() const;
245 
247 
248  void SetTextInput(bool textInputEnabled);
249 
251  void MoveMouse(Math::Point pos);
252 
254 
255  void SetDebugModeActive(DebugMode mode, bool active);
256  bool IsDebugModeActive(DebugMode mode) const;
257  static bool ParseDebugModes(const std::string& str, int& debugModes);
259 
261 
262  Language GetLanguage() const;
263  char GetLanguageChar() const;
264  void SetLanguage(Language language);
266 
267  bool GetSceneTestMode();
268 
270  void Render();
271 
273  void RenderIfNeeded(int updateRate);
274 
276  void PlayForceFeedbackEffect(float strength = 1.0f, int length = 999999);
279 
280 protected:
282  bool CreateVideoSurface();
283 
287  Event CreateVirtualEvent(const Event& sourceEvent);
289  TEST_VIRTUAL Event CreateUpdateEvent();
291  void LogEvent(const Event& event);
292 
294  bool OpenJoystick();
296  void CloseJoystick();
297 
300 
301 protected:
305  std::unique_ptr<ApplicationPrivate> m_private;
307  std::unique_ptr<CEventQueue> m_eventQueue;
309  std::unique_ptr<Gfx::CEngine> m_engine;
311  std::unique_ptr<Gfx::CDevice> m_device;
313  std::unique_ptr<CSoundInterface> m_sound;
315  std::unique_ptr<CController> m_controller;
317  std::unique_ptr<CConfigFile> m_configFile;
319  std::unique_ptr<CInput> m_input;
321  std::unique_ptr<CPathManager> m_pathManager;
322 
326  bool m_active;
329 
331  std::string m_errorMessage;
332 
335 
337  std::string m_windowTitle;
338 
340 
342  SystemTimeStamp* m_lastTimeStamp;
343  SystemTimeStamp* m_curTimeStamp;
344 
345  long long m_realAbsTimeBase;
346  long long m_realAbsTime;
347  long long m_realRelTime;
348 
349  long long m_absTimeBase;
350  long long m_exactAbsTime;
351  long long m_exactRelTime;
352 
353  float m_absTime;
354  float m_relTime;
355 
356  float m_simulationSpeed;
357  bool m_simulationSuspended;
359 
360  SystemTimeStamp* m_manualFrameLast;
361  SystemTimeStamp* m_manualFrameTime;
362 
364  bool m_graphicsOverride = false;
365  std::string m_graphics = "default";
367  bool m_glVersionOverride = false;
368  int m_glMajor = -1;
369  int m_glMinor = -1;
371  bool m_glProfileOverride = false;
372  int m_glProfile = 0;
373 
376 
382  std::vector<int> m_joyAxeState;
384  std::vector<bool> m_joyButtonState;
385 
387  LevelCategory m_runSceneCategory;
389  int m_runSceneRank;
391 
394 
396  Language m_language;
397 
400 
403 
405  static char m_languageLocale[50];
406 };
~CApplication()
Destructor.
Definition: app.cpp:164
CSoundInterface * GetSound()
Returns the sound subsystem.
Definition: app.cpp:221
CSingleton base class for singletons.
void Render()
Renders the image in window.
Definition: app.cpp:1404
ParseArgsStatus ParseArguments(int argc, char *argv[])
Parses commandline arguments.
Definition: app.cpp:226
Gfx::DeviceConfig GetVideoConfig() const
Returns the current video mode.
Definition: app.cpp:1547
std::unique_ptr< CInput > m_input
Input manager.
Definition: app.h:319
std::string m_windowTitle
Text set as window title.
Definition: app.h:337
ParseArgsStatus
State of parsing commandline arguments.
Definition: app.h:78
static char m_languageLocale[50]
Static buffer for putenv locale.
Definition: app.h:405
void CloseJoystick()
Closes the joystick device.
Definition: app.cpp:883
Class for managing data/lang/save paths.
Definition: pathman.h:37
void SetTextInput(bool textInputEnabled)
Enable/disable text input, this toggles the on-screen keyboard on some platforms. ...
Definition: app.cpp:1855
const std::string & GetErrorMessage() const
Returns the message of error (set to something if exit code is not 0)
Definition: app.cpp:1134
bool m_sceneTest
Scene test mode.
Definition: app.h:393
void SetJoystickEnabled(bool enable)
Management of joystick enable state.
Definition: app.cpp:1665
long long GetExactAbsTime() const
Returns the exact absolute time counter [nanoseconds].
Definition: app.cpp:1522
void SetSimulationSpeed(float speed)
Management of simulation speed.
Definition: app.cpp:1463
bool m_resolutionOverride
Screen resoultion overriden by commandline.
Definition: app.h:399
CEventQueue * GetEventQueue()
Returns the application's event queue.
Definition: app.cpp:216
std::vector< bool > m_joyButtonState
Current state of joystick buttons; may be updated from another thread.
Definition: app.h:384
void SetMouseMode(MouseMode mode)
Management of mouse mode.
Definition: app.cpp:1622
< all ok
Definition: app.h:81
CSystemUtils * m_systemUtils
System utils instance.
Definition: app.h:303
std::vector< int > m_joyAxeState
Current state of joystick axes; may be updated from another thread.
Definition: app.h:382
< invalid syntax
Definition: app.h:82
Gfx::DeviceConfig m_deviceConfig
Current configuration of OpenGL display device.
Definition: app.h:334
bool m_glVersionOverride
OpenGL version to use.
Definition: app.h:367
< in-game cursor visible; system cursor hidden
Definition: app.h:93
void ResetTimeAfterLoading()
Resets time counters to account for time spent loading game.
Definition: app.cpp:1443
Definition: singleton.h:30
bool m_graphicsOverride
Graphics device to use.
Definition: app.h:364
int index
Device index (-1 = invalid device)
Definition: app.h:62
bool GetSimulationSuspended() const
Returns whether simulation is suspended.
Definition: app.cpp:1458
void InternalResumeSimulation()
Internal procedure to reset time counters.
Definition: app.cpp:1450
void LogEvent(const Event &event)
Logs debug data for event.
Definition: app.cpp:1270
bool ChangeJoystick(const JoystickDevice &newJoystick)
Change the current joystick device.
Definition: app.cpp:898
LevelCategory m_runSceneCategory
Scene to run on startup.
Definition: app.h:388
long long GetExactRelTime() const
Returns the exact realative time since last update [nanoseconds].
Definition: app.cpp:1537
std::string name
Device name.
Definition: app.h:64
bool CreateVideoSurface()
Creates the window's SDL_Surface.
Definition: app.cpp:703
std::unique_ptr< ApplicationPrivate > m_private
Private (SDL-dependent data)
Definition: app.h:305
Global event queue.
Definition: event.h:866
int axisCount
Number of axes (only available after joystick opened)
Definition: app.h:66
SystemTimeStamp * m_baseTimeStamp
Animation time stamps, etc.
Definition: app.h:341
CApplication(CSystemUtils *systemUtils)
Constructor (can only be called once!)
Definition: app.cpp:114
TEST_VIRTUAL Event CreateUpdateEvent()
Prepares a simulation update event.
Definition: app.cpp:1474
void UpdateJoystick()
Polls the state of joystick axes and buttons.
Definition: app.cpp:927
Class for loading config file.
Definition: config_file.h:40
void PlayForceFeedbackEffect(float strength=1.0f, int length=999999)
Starts a force feedback effect on the joystick.
Definition: app.cpp:1867
std::unique_ptr< CSoundInterface > m_sound
Sound subsystem.
Definition: app.h:313
General config for graphics device.
Definition: device.h:64
std::unique_ptr< Gfx::CDevice > m_device
Graphics device.
Definition: app.h:311
long m_debugModes
Bit array of active debug modes.
Definition: app.h:328
void MoveMouse(Math::Point pos)
Moves (warps) the mouse cursor to the specified position (in interface coords)
Definition: app.cpp:1636
std::unique_ptr< Gfx::CEngine > m_engine
Graphics engine.
Definition: app.h:309
void UpdateMouse()
Updates the mouse position explicitly.
Definition: app.cpp:981
float GetAbsTime() const
Returns the absolute time counter [seconds].
Definition: app.cpp:1517
void GetVideoResolutionList(std::vector< Math::IntPoint > &resolutions, int display=0) const
Returns a list of possible video modes.
Definition: app.cpp:1552
2D point
Definition: point.h:50
< both cursors visible (only for debug)
Definition: app.h:94
bool Create()
Initializes the application.
Definition: app.cpp:473
long long GetRealRelTime() const
Returns the exact relative time since last update disregarding speed setting [nanoseconds].
Definition: app.cpp:1542
Main application.
Definition: app.h:152
std::unique_ptr< CEventQueue > m_eventQueue
Global event queue.
Definition: app.h:307
Language m_language
Application language.
Definition: app.h:396
std::unique_ptr< CPathManager > m_pathManager
Path manager.
Definition: app.h:321
int m_exitCode
Code to return at exit.
Definition: app.h:324
Entry point into CRobotMain.
Definition: controller.h:39
JoystickDevice GetJoystick() const
Returns info about the current joystick.
Definition: app.cpp:1660
float GetRelTime() const
Returns the relative time since last update [seconds].
Definition: app.cpp:1532
JoystickDevice m_joystick
Info about current joystick device.
Definition: app.h:378
Event CreateVirtualEvent(const Event &sourceEvent)
If applicable, creates a virtual event to match the changed state as of new event.
Definition: app.cpp:1357
bool m_joystickEnabled
Whether joystick is enabled.
Definition: app.h:380
std::unique_ptr< CConfigFile > m_configFile
Profile (INI) reader/writer.
Definition: app.h:317
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:1567
bool m_headless
Headles mode.
Definition: app.h:402
std::vector< JoystickDevice > GetJoystickList() const
Returns a list of available joystick devices.
Definition: app.cpp:1643
bool m_glProfileOverride
OpenGL profile.
Definition: app.h:371
The graphics engine.
Definition: engine.h:585
Event types, structs and event queue.
MouseMode m_mouseMode
Current mode of mouse.
Definition: app.h:375
bool m_active
Whether application window is active.
Definition: app.h:326
std::unique_ptr< CController > m_controller
Game controller - game engine and UI.
Definition: app.h:315
void StopForceFeedbackEffect()
Stops a force feedback effect on the joystick.
Definition: app.cpp:1878
MouseMode
Mode of mouse cursor.
Definition: app.h:89
void SuspendSimulation()
Suspends animation (time will not be updated)
Definition: app.cpp:1429
Platform-specific utils.
Definition: system.h:91
Definition: system_linux.h:31
Abstract graphics device - CDevice class and related structs/enums.
Language GetLanguage() const
Management of language.
Definition: app.cpp:1687
void RenderIfNeeded(int updateRate)
Renders the image in window if needed.
Definition: app.cpp:1416
std::string m_errorMessage
Message to be displayed as error to the user.
Definition: app.h:331
< system cursor visible; in-game cursor hidden
Definition: app.h:92
long long GetRealAbsTime() const
Returns the exact absolute time counter disregarding speed setting [nanoseconds]. ...
Definition: app.cpp:1527
Event sent by system, interface or game.
Definition: event.h:735
void ResumeSimulation()
Resumes animation.
Definition: app.cpp:1435
int GetExitCode() const
Returns the code to be returned at main() exit.
Definition: app.cpp:1129
bool OpenJoystick()
Opens the joystick device.
Definition: app.cpp:844
int buttonCount
Number of buttons (only available after joystick opened)
Definition: app.h:68
int Run()
Main event loop.
Definition: app.cpp:988
Event ProcessSystemEvent()
Processes the captured SDL event to Event struct.
Definition: app.cpp:1141
Private data of CApplication class.
Definition: app.cpp:83
bool ChangeVideoConfig(const Gfx::DeviceConfig &newConfig)
Change the video mode to given mode.
Definition: app.cpp:829
Information about a joystick device.
Definition: app.h:59
Sound plugin interface.
Definition: sound.h:60
Management of mouse, keyboard and joystick.
Definition: input.h:71