CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUApplication.h
1 //
2 // CUApplication.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class provides the core application class. In initializes both the
6 // SDL and CUGL settings, and creates the core loop. You should inherit from
7 // this class to make your root game class.
8 //
9 // This class is always intended to be used on the stack of the main function.
10 // Thererfore, this class has no allocators.
11 //
12 // CUGL MIT License:
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 7/1/16
33 //
34 #ifndef __CU_APPLICATION_H__
35 #define __CU_APPLICATION_H__
36 #include <cugl/util/CUTimestamp.h>
37 #include <cugl/math/CUColor4.h>
38 #include <cugl/math/CURect.h>
39 #include <unordered_map>
40 #include <functional>
41 #include <deque>
42 #include <mutex>
43 
44 namespace cugl {
45 
60 typedef struct {
62  std::function<bool()> callback;
64  Uint32 period;
66  Uint32 timer;
67 } scheduable;
68 
84 class Application {
85 #pragma mark Values
86 public:
93  enum class State : unsigned int {
100  NONE = 0,
108  STARTUP = 1,
115  FOREGROUND = 2,
123  BACKGROUND = 3,
131  SHUTDOWN = 4
132  };
133 
134 
135 protected:
137  std::string _name;
139  std::string _org;
140 
142  std::string _assetdir;
144  std::string _savesdir;
145 
148 
156  bool _highdpi;
159 
161  float _fps;
164 
167 
168 
169 private:
171  unsigned int _delay;
172 
174  std::deque<float> _fpswindow;
175 
177  Uint32 _start;
179  Uint32 _finish;
180 
182  Uint32 _funcid;
183 
185  std::unordered_map<Uint32, scheduable> _callbacks;
187  std::mutex _queueMutex;
197  void processCallbacks(Uint32 millis);
198 
199 #pragma mark -
200 #pragma mark Constructors
201 public:
208  Application();
209 
214 
221  virtual void dispose();
222 
238  bool init();
239 
248  static Application* get() { return _theapp; }
249 
250 
251 #pragma mark -
252 #pragma mark Virtual Methods
253 
263  virtual void onStartup();
264 
276  virtual void onShutdown();
277 
288  virtual void onLowMemory() { }
289 
301  virtual void onSuspend() { }
302 
313  virtual void onResume() { }
314 
326  virtual void update(float timestep) { }
327 
337  virtual void draw() { }
338 
339 
340 #pragma mark -
341 #pragma mark Application Loop
342 
351  bool getInput();
352 
361  bool step();
362 
374  void quit();
375 
401  Uint32 schedule(std::function<bool()> callback, Uint32 time=0);
402 
429  Uint32 schedule(std::function<bool()> callback, Uint32 time, Uint32 period);
430 
445  void unschedule(Uint32 id);
446 
447 
448 #pragma mark -
449 #pragma mark Initialization Attributes
450  // TODO: This is naming clash that should be deprecated and refactored
464  void setSize(int width, int height);
465 
476  int getDisplayWidth() const { return (int)_display.size.width; }
477 
488  int getDisplayHeight() const { return (int)_display.size.height; }
489 
500  Size getDisplaySize() const { return _display.size; }
501 
516  Rect getDisplayBounds() const { return _display; }
517 
532  Rect getSafeArea() const { return _safearea; }
533 
547  void setFullscreen(bool value);
548 
559  bool isFullscreen() const { return _fullscreen; }
560 
579  void setHighDPI(bool highDPI);
580 
595  bool isHighDPI() const { return _highdpi; }
596 
609  void setMultiSampled(bool flag);
610 
623  bool isMultiSampled() const { return _multisamp; }
624 
625 #pragma mark -
626 #pragma mark Runtime Attributes
627 
640  void setName(const char* name);
641 
654  void setName(const std::string& name);
655 
665  const std::string& getName() const { return _name; }
666 
679  void setOrganization(const char* name);
680 
693  void setOrganization(const std::string& name);
694 
704  const std::string& getOrganization() const { return _org; }
705 
721  void setFPS(float fps);
722 
735  float getFPS() const { return _fps; }
736 
746  float getAverageFPS() const;
747 
759  void setClearColor(Color4 color) { _clearColor = color; }
760 
769  Color4 getClearColor() const { return (Color4)_clearColor; }
770 
777  State getState() const { return _state; }
778 
784  const std::string getOpenGLDescription() const;
785 
786 #pragma mark -
787 #pragma mark File Directories
788 
810  std::string getAssetDirectory();
811 
832  std::string getSaveDirectory();
833 };
834 
835 }
836 
837 
838 #endif /* __CU_APPLICATION_H__ */
Definition: CUSize.h:57
bool _highdpi
Definition: CUApplication.h:156
Uint32 period
Definition: CUApplication.h:64
std::string _savesdir
Definition: CUApplication.h:144
int getDisplayWidth() const
Definition: CUApplication.h:476
bool isMultiSampled() const
Definition: CUApplication.h:623
Rect getSafeArea() const
Definition: CUApplication.h:532
State getState() const
Definition: CUApplication.h:777
virtual void onSuspend()
Definition: CUApplication.h:301
Rect getDisplayBounds() const
Definition: CUApplication.h:516
Rect _display
Definition: CUApplication.h:150
std::string getAssetDirectory()
const std::string getOpenGLDescription() const
const std::string & getName() const
Definition: CUApplication.h:665
Size getDisplaySize() const
Definition: CUApplication.h:500
virtual void onResume()
Definition: CUApplication.h:313
void setClearColor(Color4 color)
Definition: CUApplication.h:759
bool _fullscreen
Definition: CUApplication.h:154
Definition: CUColor4.h:73
State
Definition: CUApplication.h:93
void setFPS(float fps)
float width
Definition: CUSize.h:61
std::string _org
Definition: CUApplication.h:139
Rect _safearea
Definition: CUApplication.h:152
virtual void onShutdown()
float _fps
Definition: CUApplication.h:161
void setFullscreen(bool value)
virtual void update(float timestep)
Definition: CUApplication.h:326
std::string _assetdir
Definition: CUApplication.h:142
Uint32 schedule(std::function< bool()> callback, Uint32 time=0)
float height
Definition: CUSize.h:63
int getDisplayHeight() const
Definition: CUApplication.h:488
std::string _name
Definition: CUApplication.h:137
std::function< bool()> callback
Definition: CUApplication.h:62
std::string getSaveDirectory()
Color4 getClearColor() const
Definition: CUApplication.h:769
bool isHighDPI() const
Definition: CUApplication.h:595
Color4f _clearColor
Definition: CUApplication.h:163
Definition: CURect.h:45
float getFPS() const
Definition: CUApplication.h:735
Definition: CUApplication.h:84
void setOrganization(const char *name)
float getAverageFPS() const
Definition: CUApplication.h:60
Uint32 timer
Definition: CUApplication.h:66
virtual void onStartup()
void setMultiSampled(bool flag)
Size size
Definition: CURect.h:51
void setSize(int width, int height)
State _state
Definition: CUApplication.h:147
Definition: CUColor4.h:1084
void setHighDPI(bool highDPI)
virtual void onLowMemory()
Definition: CUApplication.h:288
const std::string & getOrganization() const
Definition: CUApplication.h:704
Definition: CUAction.h:51
bool isFullscreen() const
Definition: CUApplication.h:559
~Application()
Definition: CUApplication.h:213
void setName(const char *name)
bool _multisamp
Definition: CUApplication.h:158
virtual void draw()
Definition: CUApplication.h:337
virtual void dispose()
static Application * _theapp
Definition: CUApplication.h:166
void unschedule(Uint32 id)