Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
app.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
3 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
4 // *
5 // * This program is free software: you can redistribute it and/or modify
6 // * it under the terms of the GNU General Public License as published by
7 // * the Free Software Foundation, either version 3 of the License, or
8 // * (at your option) any later version.
9 // *
10 // * This program is distributed in the hope that it will be useful,
11 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // * GNU General Public License for more details.
14 // *
15 // * You should have received a copy of the GNU General Public License
16 // * along with this program. If not, see http://www.gnu.org/licenses/.
17 
23 #pragma once
24 
25 
26 #include "common/global.h"
27 #include "common/singleton.h"
28 #include "common/profile.h"
29 
30 #include "graphics/core/device.h"
31 #include "graphics/engine/engine.h"
33 
34 #include "object/objman.h"
35 
36 
37 #include <string>
38 #include <vector>
39 
40 
41 class CInstanceManager;
42 class CEventQueue;
43 class CRobotMain;
44 class CSoundInterface;
45 
46 namespace Gfx {
47 class CModelManager;
48 }
49 
55 {
57  int index;
59  std::string name;
61  int axisCount;
64 
66  : index(-1), axisCount(0), buttonCount(0) {}
67 };
68 
74 {
75  VIDEO_QUERY_ERROR,
76  VIDEO_QUERY_NONE,
77  VIDEO_QUERY_ALL,
78  VIDEO_QUERY_OK
79 };
80 
81 
87 {
88  TRKEY_NUM_UP = (1<<0),
89  TRKEY_NUM_DOWN = (1<<1),
90  TRKEY_NUM_LEFT = (1<<2),
91  TRKEY_NUM_RIGHT = (1<<3),
92  TRKEY_NUM_PLUS = (1<<4),
93  TRKEY_NUM_MINUS = (1<<5),
94  TRKEY_PAGE_UP = (1<<6),
95  TRKEY_PAGE_DOWN = (1<<7)
96 };
97 
103 {
104  PARSE_ARGS_OK = 1,
107 };
108 
114 {
115  MOUSE_SYSTEM,
119 };
120 
126 {
127  PCNT_EVENT_PROCESSING,
128 
133 
140 
142 
144 };
145 
146 enum DebugMode
147 {
148  DEBUG_SYS_EVENTS = 1 << 0,
149  DEBUG_APP_EVENTS = 1 << 1,
150  DEBUG_EVENTS = DEBUG_SYS_EVENTS | DEBUG_APP_EVENTS,
151  DEBUG_MODELS = 1 << 2,
152  DEBUG_ALL = DEBUG_SYS_EVENTS | DEBUG_APP_EVENTS | DEBUG_MODELS
153 };
154 
155 struct ApplicationPrivate;
156 
200 class CApplication : public CSingleton<CApplication>
201 {
202 public:
204  CApplication();
206  ~CApplication();
207 
212 
213 public:
215  ParseArgsStatus ParseArguments(int argc, char *argv[]);
217  bool Create();
219  int Run();
221  int GetExitCode() const;
222 
224  const std::string& GetErrorMessage() const;
225 
227  void Destroy();
228 
230  VideoQueryResult GetVideoResolutionList(std::vector<Math::IntPoint> &resolutions,
231  bool fullScreen, bool resizeable) const;
232 
235 
237  bool ChangeVideoConfig(const Gfx::GLDeviceConfig &newConfig);
238 
240  void SuspendSimulation();
242  void ResumeSimulation();
244  bool GetSimulationSuspended() const;
245 
247  void ResetTimeAfterLoading();
248 
250  void SetSimulationSpeed(float speed);
252  float GetSimulationSpeed() const;
254 
256  float GetAbsTime() const;
258  long long GetExactAbsTime() const;
259 
261  long long GetRealAbsTime() const;
262 
264  float GetRelTime() const;
266  long long GetExactRelTime() const;
267 
269  long long GetRealRelTime() const;
270 
272  std::vector<JoystickDevice> GetJoystickList() const;
273 
275  JoystickDevice GetJoystick() const;
276 
278  bool ChangeJoystick(const JoystickDevice &newJoystick);
279 
281 
282  void SetJoystickEnabled(bool enable);
283  bool GetJoystickEnabled() const;
285 
287  void UpdateJoystick();
288 
290  void UpdateMouse();
291 
293  int GetKmods() const;
295  bool GetKmodState(int kmod) const;
296 
298  bool GetTrackedKeyState(TrackedKey key) const;
299 
301  bool GetMouseButtonState(int index) const;
302 
304  void ResetKeyStates();
305 
307 
308  void SetGrabInput(bool grab);
309  bool GetGrabInput() const;
311 
313 
314  void SetMouseMode(MouseMode mode);
315  MouseMode GetMouseMode() const;
317 
319  Math::Point GetMousePos() const;
320 
322  void MoveMouse(Math::Point pos);
323 
325 
326  void SetDebugModeActive(DebugMode mode, bool active);
327  bool IsDebugModeActive(DebugMode mode) const;
328  static bool ParseDebugModes(const std::string& str, int& debugModes);
330 
332  std::string GetDataDirPath() const;
333 
335  std::string GetDataSubdirPath(DataDir stdDir) const;
336 
338  std::string GetDataFilePath(DataDir stdDir, const std::string &subpath) const;
339 
341  std::string GetTexPackFilePath(const std::string& textureName) const;
342 
344 
345  Language GetLanguage() const;
346  char GetLanguageChar() const;
347  void SetLanguage(Language language);
348  static bool ParseLanguage(const std::string& str, Language& language);
350 
352 
353  void SetLowCPU(bool low);
354  bool GetLowCPU() const;
356 
358 
360  void StopPerformanceCounter(PerformanceCounter counter);
361  float GetPerformanceCounterData(PerformanceCounter counter) const;
363 
364  bool GetProtoMode() const;
365 
366 protected:
368  bool CreateVideoSurface();
369 
373  Event CreateVirtualEvent(const Event& sourceEvent);
375  TEST_VIRTUAL Event CreateUpdateEvent();
377  void LogEvent(const Event& event);
379  void Render();
380 
382  bool OpenJoystick();
384  void CloseJoystick();
385 
388 
393 
394 protected:
398  // TODO: to be removed
416 
420  bool m_active;
423 
425  std::string m_errorMessage;
426 
431 
433  std::string m_windowTitle;
434 
436 
438  SystemTimeStamp* m_lastTimeStamp;
439  SystemTimeStamp* m_curTimeStamp;
440 
441  SystemTimeStamp* m_performanceCounters[PCNT_MAX][2];
442  float m_performanceCountersData[PCNT_MAX];
443 
444  long long m_realAbsTimeBase;
445  long long m_realAbsTime;
446  long long m_realRelTime;
447 
448  long long m_absTimeBase;
449  long long m_exactAbsTime;
450  long long m_exactRelTime;
451 
452  float m_absTime;
453  float m_relTime;
454 
455  float m_simulationSpeed;
456  bool m_simulationSuspended;
458 
460  unsigned int m_kmodState;
462  unsigned int m_trackedKeys;
463 
469  unsigned int m_mouseButtonsState;
470 
476  std::vector<int> m_joyAxeState;
478  std::vector<bool> m_joyButtonState;
479 
481  std::string m_dataPath;
482 
485 
487  std::string m_langPath;
488 
490  std::string m_texPackPath;
491 
493  std::string m_runSceneName;
495  int m_runSceneRank;
497 
498  const char* m_standardDataDirs[DIR_MAX];
499 
502 
504  bool m_lowCPU;
505 
508 };
509 
~CApplication()
Destructor.
Definition: app.cpp:177
CSoundInterface * GetSound()
Returns the sound subsystem.
Definition: app.cpp:210
CEventQueue * m_eventQueue
Global event queue.
Definition: app.h:403
Additional config with OpenGL-specific settings.
Definition: gldevice.h:50
CSingleton base class for singletons.
std::string m_runSceneName
Scene to run on startup.
Definition: app.h:494
void Render()
Renders the image in window.
Definition: app.cpp:1273
< frame update in CParticle
Definition: app.h:132
ParseArgsStatus ParseArguments(int argc, char *argv[])
Parses commandline arguments.
Definition: app.cpp:221
std::string m_windowTitle
Text set as window title.
Definition: app.h:433
ParseArgsStatus
State of parsing commandline arguments.
Definition: app.h:102
Main graphics engine - CEngine class.
std::string m_texPackPath
Path to directory with user texture pack.
Definition: app.h:490
void CloseJoystick()
Closes the joystick device.
Definition: app.cpp:760
< rendering 2D interface
Definition: app.h:141
bool GetKmodState(int kmod) const
Returns whether the given kmod is active.
Definition: app.cpp:1506
const std::string & GetErrorMessage() const
Returns the message of error (set to something if exit code is not 0)
Definition: app.cpp:1008
void SetJoystickEnabled(bool enable)
Management of joystick enable state.
Definition: app.cpp:1589
long long GetExactAbsTime() const
Returns the exact absolute time counter [nanoseconds].
Definition: app.cpp:1377
< textures
Definition: global.h:196
void SetSimulationSpeed(float speed)
Management of simulation speed.
Definition: app.cpp:1315
std::string m_langPath
Path to directory with language files.
Definition: app.h:487
< rendering the water
Definition: app.h:137
CEventQueue * GetEventQueue()
Returns the application's event queue.
Definition: app.cpp:205
std::vector< bool > m_joyButtonState
Current state of joystick buttons; may be updated from another thread.
Definition: app.h:478
void SetMouseMode(MouseMode mode)
Management of mouse mode.
Definition: app.cpp:1540
< all ok
Definition: app.h:105
< all counters together
Definition: app.h:143
ApplicationPrivate * m_private
Private (SDL-dependent data)
Definition: app.h:396
std::string m_dataPath
Path to directory with data files.
Definition: app.h:481
std::vector< int > m_joyAxeState
Current state of joystick axes; may be updated from another thread.
Definition: app.h:476
VideoQueryResult GetVideoResolutionList(std::vector< Math::IntPoint > &resolutions, bool fullScreen, bool resizeable) const
Returns a list of possible video modes.
Definition: app.cpp:1407
< invalid syntax
Definition: app.h:106
< the whole frame update process
Definition: app.h:130
< in-game cursor visible; system cursor hidden
Definition: app.h:117
void ResetTimeAfterLoading()
Resets time counters to account for time spent loading game.
Definition: app.cpp:1295
Definition: singleton.h:27
int index
Device index (-1 = invalid device)
Definition: app.h:57
bool GetSimulationSuspended() const
Returns whether simulation is suspended.
Definition: app.cpp:1310
void InternalResumeSimulation()
Internal procedure to reset time counters.
Definition: app.cpp:1302
Some common, global definitions.
void LogEvent(const Event &event)
Logs debug data for event.
Definition: app.cpp:1167
bool ChangeJoystick(const JoystickDevice &newJoystick)
Change the current joystick device.
Definition: app.cpp:768
Gfx::CEngine * m_engine
Graphics engine.
Definition: app.h:405
CProfile * m_profile
Profile (INI) reader/writer.
Definition: app.h:415
long long GetExactRelTime() const
Returns the exact realative time since last update [nanoseconds].
Definition: app.cpp:1392
std::string name
Device name.
Definition: app.h:59
bool CreateVideoSurface()
Creates the window's SDL_Surface.
Definition: app.cpp:578
Global event queue.
Definition: event.h:765
Gfx::CDevice * m_device
Graphics device.
Definition: app.h:407
int axisCount
Number of axes (only available after joystick opened)
Definition: app.h:61
Gfx::CModelManager * m_modelManager
3D models manager
Definition: app.h:409
SystemTimeStamp * m_baseTimeStamp
Animation time stamps, etc.
Definition: app.h:437
Math::Point GetMousePos() const
Returns the position of mouse cursor (in interface coords)
Definition: app.cpp:1554
void SetLowCPU(bool low)
Management of sleep in main loop (lowers CPU usage)
Definition: app.cpp:1822
int GetKmods() const
Returns the current key modifiers.
Definition: app.cpp:1501
TEST_VIRTUAL Event CreateUpdateEvent()
Prepares a simulation update event.
Definition: app.cpp:1326
void UpdateJoystick()
Polls the state of joystick axes and buttons.
Definition: app.cpp:792
CSoundInterface * m_sound
Sound subsystem.
Definition: app.h:411
bool m_lowCPU
Low cpu mode.
Definition: app.h:504
Definition: robotmain.h:191
PerformanceCounter
Type of counter testing performance.
Definition: app.h:125
bool ChangeVideoConfig(const Gfx::GLDeviceConfig &newConfig)
Change the video mode to given mode.
Definition: app.cpp:684
long m_debugModes
Bit array of active debug modes.
Definition: app.h:422
void MoveMouse(Math::Point pos)
Moves (warps) the mouse cursor to the specified position (in interface coords)
Definition: app.cpp:1559
Math::Point m_mousePos
Current position of mouse cursor.
Definition: app.h:467
void UpdateMouse()
Updates the mouse position explicitly.
Definition: app.cpp:846
void UpdatePerformanceCountersData()
Updates performance counters from gathered timer data.
Definition: app.cpp:1856
float GetAbsTime() const
Returns the absolute time counter [seconds].
Definition: app.cpp:1372
CApplication()
Constructor (can only be called once!)
Definition: app.cpp:96
< rendering the particles in 3D
Definition: app.h:136
< rendering the terrain
Definition: app.h:138
unsigned int m_kmodState
Current state of key modifiers (bitmask of SDLMod)
Definition: app.h:460
unsigned int m_trackedKeys
Current state of some tracked keys (bitmask of TrackedKey enum values)
Definition: app.h:462
bool GetTrackedKeyState(TrackedKey key) const
Returns whether the tracked key is pressed.
Definition: app.cpp:1511
CObjectManager * m_objMan
Object manager.
Definition: app.h:401
Gfx::GLDeviceConfig m_lastDeviceConfig
Previous configuration of OpenGL display device.
Definition: app.h:430
< frame update in CEngine
Definition: app.h:131
void ResetKeyStates()
Resets tracked key states and modifiers.
Definition: app.cpp:1521
unsigned int m_mouseButtonsState
Current state of mouse buttons (bitmask of MouseButton enum values)
Definition: app.h:469
2D point
Definition: point.h:46
< both cursors visible (only for debug)
Definition: app.h:118
< the whole rendering process
Definition: app.h:135
bool Create()
Initializes the application.
Definition: app.cpp:395
long long GetRealRelTime() const
Returns the exact relative time since last update disregarding speed setting [nanoseconds].
Definition: app.cpp:1397
Instance manager for objects.
Main application.
Definition: app.h:200
Language m_language
Application language.
Definition: app.h:501
Gfx::GLDeviceConfig GetVideoConfig() const
Returns the current video mode.
Definition: app.cpp:1402
std::string GetDataSubdirPath(DataDir stdDir) const
Returns the full path to a standard dir in data directory.
Definition: app.cpp:1616
CRobotMain * m_robotMain
Main class of the proper game engine.
Definition: app.h:413
int m_exitCode
Code to return at exit.
Definition: app.h:418
bool m_customDataPath
True if datadir was passed in command line.
Definition: app.h:484
DataDir
Directories in data directory.
Definition: global.h:184
JoystickDevice GetJoystick() const
Returns info about the current joystick.
Definition: app.cpp:1584
OpenGL implementation - CGLDevice class.
float GetRelTime() const
Returns the relative time since last update [seconds].
Definition: app.cpp:1387
JoystickDevice m_joystick
Info about current joystick device.
Definition: app.h:472
Event CreateVirtualEvent(const Event &sourceEvent)
If applicable, creates a virtual event to match the changed state as of new event.
Definition: app.cpp:1232
bool m_joystickEnabled
Whether joystick is enabled.
Definition: app.h:474
Class for loading profile (currently for loading ini config file)
Definition: profile.h:42
void SetDebugModeActive(DebugMode mode, bool active)
Management of debug modes (printing more info in logger)
Definition: app.cpp:1450
VideoQueryResult
Result of querying for available video resolutions.
Definition: app.h:73
< rendering the 3D objects
Definition: app.h:139
std::vector< JoystickDevice > GetJoystickList() const
Returns a list of available joystick devices.
Definition: app.cpp:1567
Manager for static models.
Definition: modelmanager.h:35
The graphics engine.
Definition: engine.h:681
MouseMode m_mouseMode
Current mode of mouse.
Definition: app.h:465
bool m_active
Whether application window is active.
Definition: app.h:420
< event processing (except update events)
Definition: app.h:129
Gfx::GLDeviceConfig m_deviceConfig
Current configuration of OpenGL display device.
Definition: app.h:428
Class for loading profile (currently for loading ini config file)
MouseMode
Mode of mouse cursor.
Definition: app.h:113
void SuspendSimulation()
Suspends animation (time will not be updated)
Definition: app.cpp:1281
Definition: system_linux.h:28
bool m_protoMode
Show prototype levels.
Definition: app.h:507
Abstract graphics device - CDevice class and related structs/enums.
Language GetLanguage() const
Management of language.
Definition: app.cpp:1664
std::string GetTexPackFilePath(const std::string &textureName) const
Returns the full path to a file in texture pack directory.
Definition: app.cpp:1645
CInstanceManager * m_iMan
Instance manager.
Definition: app.h:399
std::string m_errorMessage
Message to be displayed as error to the user.
Definition: app.h:425
void SetGrabInput(bool grab)
Management of the grab mode for input (keyboard & mouse)
Definition: app.cpp:1529
std::string GetDataDirPath() const
Returns the full path to data directory.
Definition: app.cpp:1611
< system cursor visible; in-game cursor hidden
Definition: app.h:116
long long GetRealAbsTime() const
Returns the exact absolute time counter disregarding speed setting [nanoseconds]. ...
Definition: app.cpp:1382
Definition: objman.h:34
< frame update in CRobotMain
Definition: app.h:134
Event sent by system, interface or game.
Definition: event.h:687
void ResumeSimulation()
Resumes animation.
Definition: app.cpp:1287
int GetExitCode() const
Returns the code to be returned at main() exit.
Definition: app.cpp:1003
bool OpenJoystick()
Opens the joystick device.
Definition: app.cpp:738
int buttonCount
Number of buttons (only available after joystick opened)
Definition: app.h:63
Language
Application language.
Definition: global.h:170
int Run()
Main event loop.
Definition: app.cpp:853
Manager for instances of certain classes.
Definition: iman.h:71
Event ProcessSystemEvent()
Processes the captured SDL event to Event struct.
Definition: app.cpp:1015
Private data of CApplication class.
Definition: app.cpp:71
Information about a joystick device.
Definition: app.h:54
void Destroy()
Cleans up before exit.
Definition: app.cpp:629
Sound plugin interface.
Definition: sound.h:149
Abstract interface of graphics device.
Definition: device.h:235
TrackedKey
Additional keys whose state (pressed/released) is tracked by CApplication.
Definition: app.h:86
void ResetPerformanceCounters()
Resets all performance counters to zero.
Definition: app.cpp:1847
bool GetMouseButtonState(int index) const
Returns whether the mouse button is pressed.
Definition: app.cpp:1516
void StartPerformanceCounter(PerformanceCounter counter)
Management of performance counters.
Definition: app.cpp:1832
std::string GetDataFilePath(DataDir stdDir, const std::string &subpath) const
Returns the full path to a file in data directory given standard dir and subpath. ...
Definition: app.cpp:1627