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 <chrono>
51#include <iostream>
52#include <mutex>
53#include "gGUIManager.h"
54
55class gCanvasManager;
56class gBaseCanvas;
57class gBaseApp;
58class gAppManager;
59class gGUIFrame;
60class gInputManager;
61
92void gStartEngine(gBaseApp* baseApp, const std::string& appName, int windowMode, int width, int height, bool isResizable = true);
93
137void gStartEngine(gBaseApp* baseApp, const std::string& appName, int windowMode, int unitWidth, int unitHeight, int screenScaling, int width, int height, bool isResizable = true);
138
150void gStartEngine(gBaseApp* baseApp, const std::string& appName, int loopMode);
151
152extern gAppManager* appmanager;
153
154class gAppManager : public gObject {
155public:
156 gAppManager(const std::string& appName, gBaseApp *baseApp, int width, int height, int windowMode, int unitWidth, int unitHeight, int screenScaling, bool isResizable, int loopMode);
158
159 // Main application
160 void runApp();
161
163 void setup();
164 void loop();
165#ifdef EMSCRIPTEN
167#endif
168 void stop();
169
176 void submitToMainThread(std::function<void()> fn);
177
183 std::string getAppName();
184
191
198
206
213
220 bool callEvent(gEvent& event);
221
228
232 void setTargetFramerate(int framerate);
233
238
243
248
253
260 void setClipboardString(const std::string& clipboard);
261
265 std::string getClipboardString();
266
277 int getWindowMode() const { return windowmode; }
278
279 gBaseWindow* getWindow() const { return window; }
280
281 void setWindowSize(int width, int height);
282 void setWindowResizable(bool isResizable);
283 void setWindowSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeight);
284
291 void setScreenSize(int width, int height);
292
299
302
309
311
312 void setCursor(int cursorId);
313 void setCursorMode(gCursorMode cursorMode);
315
316 void setWindowIcon(std::string pngFullpath);
317 void setWindowIcon(unsigned char* imageData, int w, int h);
318
319 void setWindowTitle(const std::string& windowTitle);
320
321 bool isJoystickConnected(int joystickId);
322 int getJoystickAxesCount(int joystickId);
323 const float* getJoystickAxes(int joystickId);
324 bool isGamepadEnabled() { return isjoystickenabled; }
325 bool isGamepadButtonPressed(int joystickId, int buttonId);
326 int getMaxJoystickNum() { return maxjoysticknum; }
327 int getMaxJoystickButtonNum() { return maxjoystickbuttonnum; }
328
329 std::string getANSILocale();
330
331#ifdef ANDROID
332 DeviceOrientation getDeviceOrientation() { return deviceorientation; }
333 void setDeviceOrientation(DeviceOrientation orientation);
334#endif
335
336#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
337 void iosLoop();
338#endif // TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
339
340private:
341 static const int maxjoysticknum = 16;
342 static const int maxjoystickbuttonnum = 15;
343 static const int maxmousebuttonnum = 3;
344
345 using AppClock = std::chrono::steady_clock;
346 using AppClockDuration = AppClock::duration;
347 using AppClockTimePoint = AppClock::time_point;
348
349 gCanvasManager* canvasmanager;
350 gGUIManager* guimanager;
351
352 EventHandlerFn eventhandler;
353 gBaseWindow* window;
354 bool usewindow;
355 bool isrunning;
356 bool setupcomplete;
357 std::string appname;
358 gBaseApp* app;
359 int width;
360 int height;
361 int windowmode;
362 int unitwidth;
363 int unitheight;
364 int screenscaling;
365 bool isresizable;
366 int loopmode;
367 bool initialized;
368 bool initializedbefore;
369 bool iswindowfocused;
370 std::string ansilocale;
371
372 bool ismouseentered;
373 bool mousebuttonpressed[maxmousebuttonnum];
374 int mousebuttonstate;
375
376 AppClockTimePoint starttime, endtime;
377 AppClockDuration deltatime;
378 AppClockDuration targettimestep;
379 uint64_t totaltime;
380
381 int framerate;
382 int totalupdates;
383 int totaldraws;
384 int targetframerate;
385 bool iscanvasset;
386
387 bool isjoystickenabled;
388 bool joystickconnected[maxjoysticknum];
389 bool joystickbuttonstate[maxjoysticknum][maxjoystickbuttonnum];
390 int joystickaxecount;
391
392#if GLIST_ANDROID || GLIST_IOS
393 DeviceOrientation deviceorientation;
394 DeviceOrientation olddeviceorientation;
395 bool delayedresize;
396#endif
397
398 std::vector<std::function<void()>> mainthreadqueue;
399 std::mutex mainthreadqueuemutex;
400
401private:
402
403 void tick();
404 void onEvent(gEvent& event);
405
406 bool onWindowResizedEvent(gWindowResizeEvent&);
407 bool onWindowScaleChangedEvent(gWindowScaleChangedEvent&);
408 bool onCharTypedEvent(gCharTypedEvent&);
409 bool onKeyPressedEvent(gKeyPressedEvent&);
410 bool onKeyReleasedEvent(gKeyReleasedEvent&);
411 bool onMouseMovedEvent(gMouseMovedEvent&);
412 bool onMouseButtonPressedEvent(gMouseButtonPressedEvent&);
413 bool onMouseButtonReleasedEvent(gMouseButtonReleasedEvent&);
414 bool onWindowMouseEnterEvent(gWindowMouseEnterEvent&);
415 bool onWindowMouseExitEvent(gWindowMouseExitEvent&);
416 bool onMouseScrolledEvent(gMouseScrolledEvent&);
417 bool onWindowFocusEvent(gWindowFocusEvent&);
418 bool onWindowLoseFocusEvent(gWindowLoseFocusEvent&);
419 bool onJoystickConnectEvent(gJoystickConnectEvent&);
420 bool onJoystickDisconnectEvent(gJoystickDisconnectEvent&);
421 bool onAppPauseEvent(gAppPauseEvent&);
422 bool onAppResumeEvent(gAppResumeEvent&);
423#if GLIST_ANDROID || GLIST_IOS
424 bool onDeviceOrientationChangedEvent(gDeviceOrientationChangedEvent&);
425 bool onTouchEvent(gTouchEvent&);
426#endif
427
428 void updateTime();
429 void executeQueue();
430
431 EventHandlerFn getEventHandler() { return eventhandler; }
432
433 static void preciseSleep(double seconds);
434
435 gGUIAppThread* guiappthread;
436 bool isguiapp;
437};
438
439#endif //GAPPMANAGER_H
Definition gAppManager.h:154
gGUIManager * getGUIManager()
void runApp()
int getMaxJoystickNum()
Definition gAppManager.h:326
bool callEvent(gEvent &event)
std::string getANSILocale()
const float * getJoystickAxes(int joystickId)
gCursorMode getCursorMode()
void setClipboardString(const std::string &clipboard)
void setWindowSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeight)
void setWindowSize(int width, int height)
bool isGamepadEnabled()
Definition gAppManager.h:324
gBaseCanvas * getCurrentCanvas()
void enableVsync()
int getMaxJoystickButtonNum()
Definition gAppManager.h:327
gCanvasManager * getCanvasManager()
void setWindowResizable(bool isResizable)
void setWindowIcon(unsigned char *imageData, int w, int h)
void iosLoop()
int getTargetFramerate()
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:277
DeviceOrientation getDeviceOrientation()
Definition gAppManager.h:332
void setCurrentCanvas(gBaseCanvas *canvas)
gGUIAppThread * getGUIAppThread()
void setScreenSize(int width, int height)
void setWindowIcon(std::string pngFullpath)
void setDeviceOrientation(DeviceOrientation orientation)
void emscriptenLoop()
void setCurrentGUIFrame(gGUIFrame *guiFrame)
gBaseWindow * getWindow() const
Definition gAppManager.h:279
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 disableVsync()
Definition gWindowEvents.h:293
Definition gWindowEvents.h:302
Definition gBaseApp.h:16
Definition gBaseCanvas.h:16
Definition gBaseWindow.h:27
Definition gCanvasManager.h:13
Definition gWindowEvents.h:33
Definition gEvent.h:32
Definition gGUIAppThread.h:15
Definition gGUIFrame.h:14
Definition gGUIManager.h:37
Definition gInputManager.h:42
Definition gWindowEvents.h:196
Definition gWindowEvents.h:211
Definition gWindowEvents.h:46
Definition gWindowEvents.h:59
Definition gWindowEvents.h:117
Definition gWindowEvents.h:125
Definition gWindowEvents.h:68
Definition gWindowEvents.h:86
Definition gObject.h:33
Definition gWindowEvents.h:164
Definition gWindowEvents.h:172
Definition gWindowEvents.h:180
Definition gWindowEvents.h:188
Definition gWindowEvents.h:133
Definition gWindowEvents.h:147
void gStartEngine(gBaseApp *baseApp, const std::string &appName, int windowMode, int width, int height, bool isResizable=true)
gAppManager * appmanager
std::function< void(gEvent &)> EventHandlerFn
Definition gEvent.h:47
gCursorMode
Definition gWindowEvents.h:14