TDME2  1.9.200
Application.h
Go to the documentation of this file.
1 #pragma once
2 
3 #if defined(_MSC_VER)
4  // this suppresses a warning redefinition of APIENTRY macro
5  #define NOMINMAX
6  #include <windows.h>
7 #endif
8 #define GLFW_INCLUDE_NONE
9 #include <GLFW/glfw3.h>
10 
11 #define MOUSE_CURSOR_DISABLED 0
12 #define MOUSE_CURSOR_ENABLED 1
13 #define MOUSE_CURSOR_NORMAL 1
14 #define MOUSE_CURSOR_HAND 2
15 
16 #include <array>
17 #include <cstdlib>
18 #include <string>
19 #include <unordered_set>
20 #include <vector>
21 
22 #include <tdme/tdme.h>
25 #include <tdme/engine/fwd-tdme.h>
27 
28 using std::array;
29 using std::string;
30 using std::unordered_set;
31 using std::vector;
32 
35 
36 /**
37  * Application base class, please make sure to allocate application on heap to have correct application shutdown working
38  * @author Andreas Drewke
39  */
41 {
42  friend class InputEventHandler;
44 
45 public:
46  // forbid class copy
48 
49  static constexpr int WINDOW_HINT_NONE { 0 };
50  static constexpr int WINDOW_HINT_NOTRESIZEABLE { 1 };
51  static constexpr int WINDOW_HINT_NOTDECORATED { 2 };
52  static constexpr int WINDOW_HINT_INVISIBLE { 4 };
53  static constexpr int WINDOW_HINT_MAXIMIZED { 8 };
54 
55  static constexpr int FPS { 60 };
56 
57  static constexpr int EXITCODE_SUCCESS { EXIT_SUCCESS };
58  static constexpr int EXITCODE_FAILURE { EXIT_FAILURE };
59 
60  static constexpr int64_t JOYSTICK_BUTTON_TIME_REPEAT { 150LL };
61  /**
62  * @return renderer
63  */
64  inline static Renderer* getRenderer() {
65  return renderer;
66  }
67 
68  /**
69  * @return if having a GL/Vulkan window and context
70  */
71  inline static bool hasApplication() {
72  return application != nullptr;
73  }
74 
75  /**
76  * @return application
77  */
78  inline static Application* getApplication() {
79  return application;
80  }
81 
82  /**
83  * @return if FPS should be limited to 60 frames per seconds
84  */
85  inline static bool isLimitFPS() {
86  return limitFPS;
87  }
88 
89  /**
90  * Set frames per seconds limit
91  * @param limitFPS set up if FPS should be limited to 60 frames per seconds
92  */
93  inline static void setLimitFPS(bool limitFPS) {
95  }
96 
97  /**
98  * Set vsync enabled
99  * @param vSync vertical sync
100  */
101  static void setVSyncEnabled(bool vSync);
102 
103  /**
104  * @return Operating system the application is running on
105  */
106  static string getOSName();
107 
108  /**
109  * @return CPU the application is running on
110  */
111  static string getCPUName();
112 
113  /**
114  * Set locale
115  * @param locale locale
116  */
117  static void setLocale(const string& locale);
118 
119  /**
120  * Public constructor
121  */
122  Application();
123 
124  /**
125  * Destructor
126  */
127  virtual ~Application();
128 
129  /**
130  * @return title
131  */
132  inline const string& getTitle() {
133  return title;
134  }
135 
136  /**
137  * @return executable file name
138  */
139  inline const string& getExecutableFileName() {
140  return executableFileName;
141  }
142 
143  /**
144  * @return debugging enabled
145  */
146  inline bool isDebuggingEnabled() {
147  return debuggingEnabled;
148  }
149 
150  /**
151  * Set input event handler
152  * @param inputEventHandler input event handler
153  */
155 
156  /**
157  * Execute a command and wait until it finished running
158  * @param command command to execute
159  * @throws std::runtime_error
160  * @return application output
161  */
162  static string execute(const string& command);
163 
164  /**
165  * Execute a command in background
166  * @param command command to execute
167  */
168  static void executeBackground(const string& command);
169 
170  /**
171  * Open browser with given url
172  * @param url url
173  */
174  static void openBrowser(const string& url);
175 
176  /**
177  * Cancels a users requested exit (ALT-F4 or X button)
178  */
179  static void cancelExit();
180 
181  /**
182  * Exits this application with given exit code
183  * @param exitCode exit code
184  */
185  static void exit(int exitCode);
186 
187  /**
188  * @return If window is active on Win32, on other platforms it currently always return true
189  */
190  static bool isActive();
191 
192  /**
193  * @return window X position
194  */
195  int getWindowXPosition();
196 
197  /**
198  * Set window X position when initializing
199  * @param windowXPosition window X position
200  */
202 
203  /**
204  * @return window Y position
205  */
206  int getWindowYPosition();
207 
208  /**
209  * Set window Y position when initializing
210  * @param windowYPosition window Y position
211  */
213 
214  /**
215  * @return window width
216  */
217  int getWindowWidth() const;
218 
219  /**
220  * Set window width
221  * @param windowWidth window width
222  */
223  void setWindowWidth(int windowWidth);
224 
225  /**
226  * @return window height
227  */
228  int getWindowHeight() const;
229 
230  /**
231  * Set window height
232  * @param windowHeight window height
233  */
234  void setWindowHeight(int windowHeight);
235 
236  /**
237  * @return is full screen
238  */
239  bool isFullScreen() const;
240 
241  /**
242  * Set full screen mode
243  * @param fullScreen full screen
244  */
245  void setFullScreen(bool fullScreen);
246 
247  /**
248  * Windows only: Install exception handler that will print a stack trace if crashing
249  */
250  static void installExceptionHandler();
251 
252  /**
253  * @return mouse cursor
254  */
255  inline static int getMouseCursor() {
256  return mouseCursor;
257  }
258 
259  /**
260  * Set mouse cursor
261  * @param mouseCursor mouse cursor, see MOUSE_CURSOR_*
262  */
263  static void setMouseCursor(int mouseCursor);
264 
265  /**
266  * @return get mouse X position
267  */
268  static int getMousePositionX();
269 
270  /**
271  * @return get mouse Y position
272  */
273  static int getMousePositionY();
274 
275  /**
276  * Set mouse position
277  * @param x x
278  * @param y y
279  */
280  static void setMousePosition(int x, int y);
281 
282  /**
283  * Swap rendering buffers
284  */
285  static void swapBuffers();
286 
287  /**
288  * @return clipboard content as utf8 string
289  */
290  string getClipboardContent();
291 
292  /**
293  * Set clipboard content
294  * @param content content
295  */
296  void setClipboardContent(const string& content);
297 
298  /**
299  * Run this application
300  * @param argc argument count
301  * @param argv argument values
302  * @param title title
303  * @param inputEventHandler application input event handler
304  * @param windowHints window hints
305  * @return exit code
306  */
307  int run(int argc, char** argv, const string& title, InputEventHandler* inputEventHandler = nullptr, int windowHints = WINDOW_HINT_NONE);
308 
309  /**
310  * Init
311  */
312  virtual void initialize() = 0;
313 
314  /**
315  * Resize
316  * @param width width
317  * @param height height
318  */
319  virtual void reshape(int width, int height) = 0;
320 
321  /**
322  * Display
323  */
324  virtual void display() = 0;
325 
326  /**
327  * Disposes
328  */
329  virtual void dispose() = 0;
330 
331  /**
332  * On close
333  */
334  virtual void onClose();
335 
336  /**
337  * On drop
338  * @param paths paths of items that were dropped
339  */
340  virtual void onDrop(const vector<string>& paths);
341 
342 private:
348  bool debuggingEnabled { false };
349  bool initialized { false };
350  int windowWidth { 1024 };
351  int windowHeight { 768 };
352  int windowXPosition { -1 };
353  int windowYPosition { -1 };
354  bool fullScreen { false };
355  STATIC_DLL_IMPEXT static int64_t timeLast;
357  string title;
359 
360  STATIC_DLL_IMPEXT static GLFWwindow* glfwWindow;
361  STATIC_DLL_IMPEXT static array<unsigned int, 10> glfwMouseButtonDownFrames;
364  STATIC_DLL_IMPEXT static GLFWcursor* glfwHandCursor;
365 
367 
368  unordered_set<int> connectedJoysticks;
369  unordered_set<int> connectedGamepads;
370  array<array<int64_t, 16>, 16> joystickButtons;
371 
372  /**
373  * Set application icon
374  */
375  void setIcon();
376 
377  /**
378  * Update joystick input for given joystick index
379  * @param joystickIdx joystick index
380  */
381  void updateJoystickInput(int joystickIdx);
382 
383  /**
384  * Update gamepad input for given gamepad index
385  * @param gamepadIdx gamepad index
386  */
387  void updateGamepadInput(int gamepadIdx);
388 
389  /**
390  * Display function
391  */
392  static void displayInternal();
393 
394  /**
395  * Reshape function
396  * @param width width
397  * @param height height
398  */
399  static void reshapeInternal(int width, int height);
400 
401  /**
402  * GLFW on char
403  * @param window window
404  * @param key key
405  */
406  static void glfwOnChar(GLFWwindow* window, unsigned int key);
407 
408  /**
409  * GLFW on key
410  * @param window window
411  * @param key key
412  * @param scanCode scan code
413  * @param action action
414  * @param mods modifier keys
415  */
416  static void glfwOnKey(GLFWwindow* window, int key, int scanCode, int action, int mods);
417 
418  /**
419  * GLFW on mouse moved
420  * @param window window
421  * @param x x
422  * @param y y
423  */
424  static void glfwOnMouseMoved(GLFWwindow* window, double x, double y);
425 
426  /**
427  * GLFW on key
428  * @param window window
429  * @param button button
430  * @param action action
431  * @param mods modifier keys
432  */
433  static void glfwOnMouseButton(GLFWwindow* window, int button, int action, int mods);
434 
435  /**
436  * GLFW on key
437  * @param window window
438  * @param x x
439  * @param y y
440  */
441  static void glfwOnMouseWheel(GLFWwindow* window, double x, double y);
442 
443  /**
444  * GLFW on window resize
445  * @param window window
446  * @param width width
447  * @param height height
448  */
449  static void glfwOnWindowResize(GLFWwindow* window, int width, int height);
450 
451  /**
452  * GLFW on close
453  */
454  static void glfwOnClose(GLFWwindow* window);
455 
456  /**
457  * GLFW on drop
458  * @param window window
459  * @param count count
460  * @param paths paths
461  */
462  static void glfwOnDrop(GLFWwindow* window, int count, const char** paths);
463 
464  /**
465  * GLFW on joystick connect/disconnect
466  * @param joystickIdx joystick index
467  * @param event event
468  */
469  static void glfwOnJoystickConnect(int joystickIdx, int event);
470 
471 };
Application base class, please make sure to allocate application on heap to have correct application ...
Definition: Application.h:41
static void setMousePosition(int x, int y)
Set mouse position.
static STATIC_DLL_IMPEXT Renderer * renderer
Definition: Application.h:343
static STATIC_DLL_IMPEXT int glfwMouseButtonLast
Definition: Application.h:362
static void glfwOnMouseWheel(GLFWwindow *window, double x, double y)
GLFW on key.
static constexpr int EXITCODE_FAILURE
Definition: Application.h:58
static void executeBackground(const string &command)
Execute a command in background.
static STATIC_DLL_IMPEXT Application * application
Definition: Application.h:344
static void swapBuffers()
Swap rendering buffers.
array< array< int64_t, 16 >, 16 > joystickButtons
Definition: Application.h:370
void setIcon()
Set application icon.
virtual void reshape(int width, int height)=0
Resize.
static void glfwOnMouseMoved(GLFWwindow *window, double x, double y)
GLFW on mouse moved.
int run(int argc, char **argv, const string &title, InputEventHandler *inputEventHandler=nullptr, int windowHints=WINDOW_HINT_NONE)
Run this application.
void setWindowXPosition(int windowXPosition)
Set window X position when initializing.
static void glfwOnClose(GLFWwindow *window)
GLFW on close.
static void glfwOnWindowResize(GLFWwindow *window, int width, int height)
GLFW on window resize.
static STATIC_DLL_IMPEXT array< unsigned int, 10 > glfwMouseButtonDownFrames
Definition: Application.h:361
void setWindowYPosition(int windowYPosition)
Set window Y position when initializing.
static string execute(const string &command)
Execute a command and wait until it finished running.
Definition: Application.cpp:98
void setClipboardContent(const string &content)
Set clipboard content.
static void glfwOnDrop(GLFWwindow *window, int count, const char **paths)
GLFW on drop.
static void displayInternal()
Display function.
static constexpr int64_t JOYSTICK_BUTTON_TIME_REPEAT
Definition: Application.h:60
static void glfwOnChar(GLFWwindow *window, unsigned int key)
GLFW on char.
static void reshapeInternal(int width, int height)
Reshape function.
void setWindowWidth(int windowWidth)
Set window width.
static STATIC_DLL_IMPEXT bool limitFPS
Definition: Application.h:356
static constexpr int EXITCODE_SUCCESS
Definition: Application.h:57
void updateJoystickInput(int joystickIdx)
Update joystick input for given joystick index.
virtual ~Application()
Destructor.
static void cancelExit()
Cancels a users requested exit (ALT-F4 or X button)
static Renderer * getRenderer()
Definition: Application.h:64
virtual void onClose()
On close.
static void setLimitFPS(bool limitFPS)
Set frames per seconds limit.
Definition: Application.h:93
static STATIC_DLL_IMPEXT bool glfwCapsLockEnabled
Definition: Application.h:363
static void installExceptionHandler()
Windows only: Install exception handler that will print a stack trace if crashing.
virtual void display()=0
Display.
static Application * getApplication()
Definition: Application.h:78
static STATIC_DLL_IMPEXT int64_t timeLast
Definition: Application.h:355
void setFullScreen(bool fullScreen)
Set full screen mode.
static constexpr int WINDOW_HINT_MAXIMIZED
Definition: Application.h:53
static constexpr int FPS
Definition: Application.h:55
static STATIC_DLL_IMPEXT InputEventHandler * inputEventHandler
Definition: Application.h:345
virtual void initialize()=0
Init.
static STATIC_DLL_IMPEXT int mouseCursor
Definition: Application.h:366
static constexpr int WINDOW_HINT_NOTDECORATED
Definition: Application.h:51
static constexpr int WINDOW_HINT_INVISIBLE
Definition: Application.h:52
static void exit(int exitCode)
Exits this application with given exit code.
void updateGamepadInput(int gamepadIdx)
Update gamepad input for given gamepad index.
unordered_set< int > connectedGamepads
Definition: Application.h:369
static STATIC_DLL_IMPEXT GLFWwindow * glfwWindow
Definition: Application.h:360
static constexpr int WINDOW_HINT_NOTRESIZEABLE
Definition: Application.h:50
unordered_set< int > connectedJoysticks
Definition: Application.h:368
static void setVSyncEnabled(bool vSync)
Set vsync enabled.
void setWindowHeight(int windowHeight)
Set window height.
virtual void dispose()=0
Disposes.
static void glfwOnJoystickConnect(int joystickIdx, int event)
GLFW on joystick connect/disconnect.
static void glfwOnKey(GLFWwindow *window, int key, int scanCode, int action, int mods)
GLFW on key.
static void openBrowser(const string &url)
Open browser with given url.
void setInputEventHandler(InputEventHandler *inputEventHandler)
Set input event handler.
const string & getExecutableFileName()
Definition: Application.h:139
static void setLocale(const string &locale)
Set locale.
static constexpr int WINDOW_HINT_NONE
Definition: Application.h:49
Application()
Public constructor.
static void glfwOnMouseButton(GLFWwindow *window, int button, int action, int mods)
GLFW on key.
static void setMouseCursor(int mouseCursor)
Set mouse cursor.
static STATIC_DLL_IMPEXT GLFWcursor * glfwHandCursor
Definition: Application.h:364
virtual void onDrop(const vector< string > &paths)
On drop.
Application input event handler interface.
#define STATIC_DLL_IMPEXT
Definition: tdme.h:15
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6