GlistEngine
Loading...
Searching...
No Matches
gAppManager.h
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (c) 2014 Nitra Games Ltd., Istanbul, Turkey *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice should not be *
13 * deleted from the source form of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28
29/****************************************************************************
30 * Author: Noyan Culum, 2014-on *
31 ****************************************************************************/
32
33/*
34 * gAppManager.h
35 *
36 * Created on: May 6, 2014
37 * Author: noyan
38 */
39
40#ifndef GAPPMANAGER_H
41#define GAPPMANAGER_H
42
43#include "gBaseWindow.h"
44#include "gEvent.h"
45#include "gWindowEvents.h"
46#include "gFont.h"
47#include "gObject.h"
48class gGUIAppThread;
49
50#include <atomic>
51#include <chrono>
52#include <iostream>
53#include <mutex>
54#include <thread>
55#include "gGUIManager.h"
56#include "gRenderObject.h"
57
58class gCanvasManager;
59class gBaseCanvas;
60class gBaseApp;
61class gAppManager;
62class gGUIFrame;
63class gInputManager;
64class gImage;
65
96void gStartEngine(gBaseApp* baseApp, const std::string& appName, int windowMode, int width, int height, bool isResizable = true, int renderEngine = G_RENDERER_GL);
97
141void gStartEngine(gBaseApp* baseApp, const std::string& appName, int windowMode, int unitWidth, int unitHeight, int screenScaling, int width, int height, bool isResizable = true, int renderEngine = G_RENDERER_GL);
142
154void gStartEngine(gBaseApp* baseApp, const std::string& appName, int loopMode);
155
156extern gAppManager* appmanager;
157
158class gAppManager : public gObject {
159public:
160 static const int STEPMODE_FRAME = 0;
161 static const int STEPMODE_TIME = 1;
162 gAppManager(const std::string& appName, gBaseApp *baseApp, int width, int height, int windowMode, int unitWidth, int unitHeight, int screenScaling, bool isResizable, int loopMode);
164
165 // Main application
166 void runApp();
167
169 void setup();
170 void loop();
171#ifdef EMSCRIPTEN
172 void emscriptenLoop();
173#endif
174 void stop();
175
182 void submitToMainThread(std::function<void()> fn);
183
189 std::string getAppName();
190
197
204
212
221
228 bool callEvent(gEvent& event);
229
236
237 void setStepMode(int stepMode);
239
240 void setUpdateTargetRate(int rate);
242
246 void setTargetFramerate(int framerate);
247
252
257
262
267
272
279 void setClipboardString(const std::string& clipboard);
280
284 std::string getClipboardString();
285
296 int getWindowMode() const { return windowmode; }
297
298 gBaseWindow* getWindow() const { return window; }
299
306 void setRenderEngine(int renderEngine) { renderengine = renderEngine; }
307 int getRenderEngine() const { return renderengine; }
308
309 void setWindowSize(int width, int height);
310 void setWindowResizable(bool isResizable);
311 void setWindowSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeight);
312
319 void setScreenSize(int width, int height);
320
327
330
347 void setMultiSampling(int samples);
348 int getMultiSampling() const;
349
356
358
359 void setCursor(int cursorId);
360 void setCursorMode(gCursorMode cursorMode);
362
363 void setCustomCursor(gImage& image, int hotspotX, int hotspotY);
364
365 void setWindowIcon(std::string pngFullpath);
366 void setWindowIcon(unsigned char* imageData, int w, int h);
367
368 void setWindowTitle(const std::string& windowTitle);
369
370 bool isJoystickConnected(int joystickId);
371 int getJoystickAxesCount(int joystickId);
372 const float* getJoystickAxes(int joystickId);
373 bool isGamepadEnabled() { return isjoystickenabled; }
374 bool isGamepadButtonPressed(int joystickId, int buttonId);
375 int getMaxJoystickNum() { return maxjoysticknum; }
376 int getMaxJoystickButtonNum() { return maxjoystickbuttonnum; }
377
378 std::string getANSILocale();
379
380#ifdef ANDROID
381 DeviceOrientation getDeviceOrientation() { return deviceorientation; }
382 void setDeviceOrientation(DeviceOrientation orientation);
383#endif
384
385#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
386 void iosLoop();
387#endif // TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
388
389private:
390 static const int maxjoysticknum = 16;
391 static const int maxjoystickbuttonnum = 15;
392 static const int maxmousebuttonnum = 3;
393
394 using AppClock = std::chrono::steady_clock;
395 using AppClockDuration = AppClock::duration;
396 using AppClockTimePoint = AppClock::time_point;
397
398 gCanvasManager* canvasmanager;
399 gGUIManager* guimanager;
400
401 EventHandlerFn eventhandler;
402 gBaseWindow* window;
403 bool usewindow;
404 bool isrunning;
405 bool setupcomplete;
406 std::string appname;
407 gBaseApp* app;
408 int width;
409 int height;
410 int windowmode;
411 int unitwidth;
412 int unitheight;
413 int screenscaling;
414 bool isresizable;
415 int loopmode;
416 int stepmode;
417 int renderengine = G_RENDERER_GL;
418 int requestedmultisampling = 1;
419 bool initialized;
420 bool initializedbefore;
421 bool iswindowfocused;
422 std::string ansilocale;
423
424 bool ismouseentered;
425 bool mousebuttonpressed[maxmousebuttonnum];
426 int mousebuttonstate;
427
428 AppClockTimePoint starttime, endtime;
429 AppClockDuration deltatime;
430 AppClockDuration targettimestep;
431 uint64_t totaltime;
432
433 int framerate;
434 int totalupdates;
435 int totaldraws;
436 int targetframerate;
437 int updatetargetrate;
438 bool iscanvasset;
439
440 std::thread updatethread;
441 std::atomic<std::thread::id> updatethreadid{};
442 bool isupdatethreadrunning;
443 AppClockDuration updatetargettimestep;
444 AppClockDuration updateelapsedtime = AppClockDuration(0);
445 std::recursive_mutex gamestatemutex;
446 std::atomic<int> updaterate{0};
447 int totalupdatesforups;
448 uint64_t updatetotaltime;
449
450 bool isjoystickenabled;
451 bool joystickconnected[maxjoysticknum];
452 bool joystickbuttonstate[maxjoysticknum][maxjoystickbuttonnum];
453 int joystickaxecount;
454
455#if GLIST_ANDROID || GLIST_IOS || GLIST_WEB
456 DeviceOrientation deviceorientation;
457 DeviceOrientation olddeviceorientation;
458 bool delayedresize;
459#endif
460
461 std::vector<std::function<void()>> mainthreadqueue;
462 std::mutex mainthreadqueuemutex;
463
464private:
465
466 void tick();
467 void stepUpdate();
468 void startUpdateThread();
469 void stopUpdateThread();
470 void updateThreadFunction();
471 void onEvent(gEvent& event);
472
473 bool onWindowResizedEvent(gWindowResizeEvent&);
474 bool onWindowScaleChangedEvent(gWindowScaleChangedEvent&);
475 bool onCharTypedEvent(gCharTypedEvent&);
476 bool onKeyPressedEvent(gKeyPressedEvent&);
477 bool onKeyReleasedEvent(gKeyReleasedEvent&);
478 bool onMouseMovedEvent(gMouseMovedEvent&);
479 bool onMouseButtonPressedEvent(gMouseButtonPressedEvent&);
480 bool onMouseButtonReleasedEvent(gMouseButtonReleasedEvent&);
481 bool onWindowMouseEnterEvent(gWindowMouseEnterEvent&);
482 bool onWindowMouseExitEvent(gWindowMouseExitEvent&);
483 bool onMouseScrolledEvent(gMouseScrolledEvent&);
484 bool onWindowFocusEvent(gWindowFocusEvent&);
485 bool onWindowLoseFocusEvent(gWindowLoseFocusEvent&);
486 bool onJoystickConnectEvent(gJoystickConnectEvent&);
487 bool onJoystickDisconnectEvent(gJoystickDisconnectEvent&);
488 bool onAppPauseEvent(gAppPauseEvent&);
489 bool onAppResumeEvent(gAppResumeEvent&);
490#if GLIST_ANDROID || GLIST_IOS || GLIST_WEB
491 bool onDeviceOrientationChangedEvent(gDeviceOrientationChangedEvent&);
492 bool onTouchEvent(gTouchEvent&);
493#endif
494
495 void updateTime();
496 void executeQueue();
497
498 EventHandlerFn getEventHandler() { return eventhandler; }
499
500 static void preciseSleep(double seconds);
501
502 gGUIAppThread* guiappthread;
503 bool isguiapp;
504};
505
506#endif //GAPPMANAGER_H
Definition gAppManager.h:158
gGUIManager * getGUIManager()
void runApp()
int getMaxJoystickNum()
Definition gAppManager.h:375
void setMultiSampling(int samples)
int getStepMode()
bool callEvent(gEvent &event)
std::string getANSILocale()
const float * getJoystickAxes(int joystickId)
gCursorMode getCursorMode()
void setClipboardString(const std::string &clipboard)
static const int STEPMODE_TIME
Definition gAppManager.h:161
static const int STEPMODE_FRAME
Definition gAppManager.h:160
void setWindowSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeight)
void setWindowSize(int width, int height)
bool isGamepadEnabled()
Definition gAppManager.h:373
void setRenderEngine(int renderEngine)
Definition gAppManager.h:306
gBaseCanvas * getCurrentCanvas()
void setStepMode(int stepMode)
void enableVsync()
int getUpdateFramerate()
void setUpdateTargetRate(int rate)
int getMaxJoystickButtonNum()
Definition gAppManager.h:376
gCanvasManager * getCanvasManager()
void setWindowResizable(bool isResizable)
void setWindowIcon(unsigned char *imageData, int w, int h)
int getTargetFramerate()
int getRenderEngine() const
Definition gAppManager.h:307
void setCustomCursor(gImage &image, int hotspotX, int hotspotY)
void initialize()
void submitToMainThread(std::function< void()> fn)
std::string getAppName()
double getElapsedTime()
bool isJoystickConnected(int joystickId)
bool isWindowFocused()
int getJoystickAxesCount(int joystickId)
gGUIFrame * getCurrentGUIFrame()
void setTargetFramerate(int framerate)
void setCursorMode(gCursorMode cursorMode)
std::string getClipboardString()
int getWindowMode() const
Definition gAppManager.h:296
void hideKeyboard()
void setCurrentCanvas(gBaseCanvas *canvas)
gGUIAppThread * getGUIAppThread()
void setScreenSize(int width, int height)
void setWindowIcon(std::string pngFullpath)
int getMultiSampling() const
void setCurrentGUIFrame(gGUIFrame *guiFrame)
int getUpdateTargetRate()
gBaseWindow * getWindow() const
Definition gAppManager.h:298
int getLoopMode()
void setWindowTitle(const std::string &windowTitle)
void setCursor(int cursorId)
gAppManager(const std::string &appName, gBaseApp *baseApp, int width, int height, int windowMode, int unitWidth, int unitHeight, int screenScaling, bool isResizable, int loopMode)
void setup()
bool isGamepadButtonPressed(int joystickId, int buttonId)
int getFramerate()
void showKeyboard()
void disableVsync()
Definition gBaseApp.h:16
Definition gBaseCanvas.h:16
Definition gBaseWindow.h:29
Definition gCanvasManager.h:13
Definition gEvent.h:32
Definition gGUIAppThread.h:15
Definition gGUIFrame.h:14
Definition gGUIManager.h:37
Definition gImage.h:34
Definition gInputManager.h:42
int width
Definition gTexture.h:171
int height
Definition gTexture.h:171
gAppManager * appmanager
void gStartEngine(gBaseApp *baseApp, const std::string &appName, int windowMode, int width, int height, bool isResizable=true, int renderEngine=G_RENDERER_GL)
#define G_RENDERER_GL
Definition gConstants.h:32
std::function< void(gEvent &)> EventHandlerFn
Definition gEvent.h:47
gCursorMode
Definition gWindowEvents.h:14
DeviceOrientation
Definition gWindowEvents.h:270