CUGL 1.1
Cornell University Game Library
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 zlib 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 
147  SDL_Window* _window;
149  SDL_GLContext _glContext;
150 
153 
159  bool _highdpi;
162 
164  float _fps;
167 
170 
171 
172 private:
174  unsigned int _delay;
175 
177  std::deque<float> _fpswindow;
178 
180  Uint32 _start;
182  Uint32 _finish;
183 
185  Uint32 _funcid;
186 
188  std::unordered_map<Uint32, scheduable> _callbacks;
190  std::mutex _queueMutex;
200  void processCallbacks(Uint32 millis);
201 
202 #pragma mark -
203 #pragma mark Constructors
204 public:
211  Application();
212 
217 
224  virtual void dispose();
225 
241  bool init();
242 
251  static Application* get() { return _theapp; }
252 
253 
254 #pragma mark -
255 #pragma mark Virtual Methods
256 
266  virtual void onStartup();
267 
279  virtual void onShutdown();
280 
291  virtual void onLowMemory() { }
292 
304  virtual void onSuspend() { }
305 
316  virtual void onResume() { }
317 
329  virtual void update(float timestep) { }
330 
340  virtual void draw() { }
341 
342 
343 #pragma mark -
344 #pragma mark Application Loop
345 
354  bool getInput();
355 
364  bool step();
365 
377  void quit();
378 
404  Uint32 schedule(std::function<bool()> callback, Uint32 time=0);
405 
432  Uint32 schedule(std::function<bool()> callback, Uint32 time, Uint32 period);
433 
448  void unschedule(Uint32 id);
449 
450 
451 #pragma mark -
452 #pragma mark Initialization Attributes
453 
466  void setSize(int width, int height);
467 
473  int getDisplayWidth() const { return (int)_display.size.width; }
474 
480  int getDisplayHeight() const { return (int)_display.size.height; }
481 
487  const Size& getDisplaySize() const { return _display.size; }
488 
498  const Rect& getDisplayBounds() const { return _display; }
499 
510  void setFullscreen(bool value);
511 
519  bool isFullscreen() const { return _fullscreen; }
520 
539  void setHighDPI(bool highDPI);
540 
555  bool isHighDPI() const { return _highdpi; }
556 
569  void setMultiSampled(bool flag);
570 
583  bool isMultiSampled() const { return _multisamp; }
584 
585 #pragma mark -
586 #pragma mark Runtime Attributes
587 
600  void setName(const char* name);
601 
614  void setName(const std::string& name);
615 
625  const std::string& getName() const { return _name; }
626 
639  void setOrganization(const char* name);
640 
653  void setOrganization(const std::string& name);
654 
664  const std::string& getOrganization() const { return _org; }
665 
681  void setFPS(float fps);
682 
695  float getFPS() const { return _fps; }
696 
706  float getAverageFPS() const;
707 
719  void setClearColor(Color4 color) { _clearColor = color; }
720 
729  Color4 getClearColor() const { return (Color4)_clearColor; }
730 
737  State getState() const { return _state; }
738 
744  const std::string getOpenGLDescription() const;
745 
746 #pragma mark -
747 #pragma mark File Directories
748 
770  std::string getAssetDirectory();
771 
792  std::string getSaveDirectory();
793 
794 #pragma mark -
795 #pragma mark Internal Helpers
796 private:
804  bool prepareOpenGL();
805 
813  bool initOpenGL();
814 };
815 
816 }
817 
818 
819 #endif /* __CU_APPLICATION_H__ */
Definition: CUSize.h:57
bool _highdpi
Definition: CUApplication.h:159
SDL_GLContext _glContext
Definition: CUApplication.h:149
Uint32 period
Definition: CUApplication.h:64
const Size & getDisplaySize() const
Definition: CUApplication.h:487
std::string _savesdir
Definition: CUApplication.h:144
int getDisplayWidth() const
Definition: CUApplication.h:473
bool isMultiSampled() const
Definition: CUApplication.h:583
State getState() const
Definition: CUApplication.h:737
SDL_Window * _window
Definition: CUApplication.h:147
virtual void onSuspend()
Definition: CUApplication.h:304
Rect _display
Definition: CUApplication.h:155
std::string getAssetDirectory()
const std::string getOpenGLDescription() const
const std::string & getName() const
Definition: CUApplication.h:625
virtual void onResume()
Definition: CUApplication.h:316
void setClearColor(Color4 color)
Definition: CUApplication.h:719
bool _fullscreen
Definition: CUApplication.h:157
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
const Rect & getDisplayBounds() const
Definition: CUApplication.h:498
virtual void onShutdown()
float _fps
Definition: CUApplication.h:164
void setFullscreen(bool value)
virtual void update(float timestep)
Definition: CUApplication.h:329
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:480
std::string _name
Definition: CUApplication.h:137
std::function< bool()> callback
Definition: CUApplication.h:62
std::string getSaveDirectory()
Color4 getClearColor() const
Definition: CUApplication.h:729
bool isHighDPI() const
Definition: CUApplication.h:555
Color4f _clearColor
Definition: CUApplication.h:166
Definition: CURect.h:45
float getFPS() const
Definition: CUApplication.h:695
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:152
Definition: CUColor4.h:1084
void setHighDPI(bool highDPI)
virtual void onLowMemory()
Definition: CUApplication.h:291
const std::string & getOrganization() const
Definition: CUApplication.h:664
Definition: CUAction.h:51
bool isFullscreen() const
Definition: CUApplication.h:519
~Application()
Definition: CUApplication.h:216
void setName(const char *name)
bool _multisamp
Definition: CUApplication.h:161
virtual void draw()
Definition: CUApplication.h:340
virtual void dispose()
static Application * _theapp
Definition: CUApplication.h:169
void unschedule(Uint32 id)