CUGL
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 
43 namespace cugl {
44 
59 typedef struct {
61  std::function<bool()> callback;
63  Uint32 period;
65  Uint32 timer;
66 } scheduable;
67 
83 class Application {
84 #pragma mark Values
85 public:
92  enum class State : unsigned int {
99  NONE = 0,
107  STARTUP = 1,
114  FOREGROUND = 2,
122  BACKGROUND = 3,
130  SHUTDOWN = 4
131  };
132 
133 
134 protected:
136  std::string _name;
138  std::string _org;
139 
141  std::string _assets;
143  std::string _saves;
144 
146  SDL_Window* _window;
148  SDL_GLContext _glContext;
149 
152 
158  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;
186 
196  void processCallbacks(Uint32 millis);
197 
198 #pragma mark -
199 #pragma mark Constructors
200 public:
207  Application();
208 
213 
220  virtual void dispose();
221 
237  bool init();
238 
247  static Application* get() { return _theapp; }
248 
249 
250 #pragma mark -
251 #pragma mark Virtual Methods
252 
262  virtual void onStartup();
263 
275  virtual void onShutdown();
276 
287  virtual void onLowMemory() { }
288 
300  virtual void onSuspend() { }
301 
312  virtual void onResume() { }
313 
325  virtual void update(float timestep) { }
326 
336  virtual void draw() { }
337 
338 
339 #pragma mark -
340 #pragma mark Application Loop
341 
350  bool getInput();
351 
360  bool step();
361 
373  void quit();
374 
400  Uint32 schedule(std::function<bool()> callback, Uint32 time=0);
401 
428  Uint32 schedule(std::function<bool()> callback, Uint32 time, Uint32 period);
429 
444  void unschedule(Uint32 id);
445 
446 
447 #pragma mark -
448 #pragma mark Initialization Attributes
449 
462  void setSize(int width, int height);
463 
469  int getDisplayWidth() const { return (int)_display.size.width; }
470 
476  int getDisplayHeight() const { return (int)_display.size.height; }
477 
483  const Size& getDisplaySize() const { return _display.size; }
484 
494  const Rect& getDisplayBounds() const { return _display; }
495 
506  void setFullscreen(bool value);
507 
515  bool isFullscreen() const { return _fullscreen; }
516 
535  void setHighDPI(bool highDPI);
536 
551  bool isHighDPI() const { return _highdpi; }
552 
553 
554 #pragma mark -
555 #pragma mark Runtime Attributes
556 
569  void setName(const char* name);
570 
583  void setName(const std::string& name);
584 
594  const std::string& getName() const { return _name; }
595 
608  void setOrganization(const char* name);
609 
622  void setOrganization(const std::string& name);
623 
633  const std::string& getOrganization() const { return _org; }
634 
650  void setFPS(float fps);
651 
664  float getFPS() const { return _fps; }
665 
675  float getAverageFPS() const;
676 
688  void setClearColor(Color4 color) { _clearColor = color; }
689 
698  Color4 getClearColor() const { return (Color4)_clearColor; }
699 
706  State getState() const { return _state; }
707 
713  const std::string getOpenGLDescription() const;
714 
715 #pragma mark -
716 #pragma mark File Directories
717 
739  std::string getAssetDirectory();
740 
761  std::string getSaveDirectory();
762 
763 #pragma mark -
764 #pragma mark Internal Helpers
765 private:
771  bool initOpenGL();
772 
773 };
774 
775 }
776 
777 
778 #endif /* __CU_APPLICATION_H__ */
Definition: CUSize.h:57
bool _highdpi
Definition: CUApplication.h:158
SDL_GLContext _glContext
Definition: CUApplication.h:148
Uint32 period
Definition: CUApplication.h:63
const Size & getDisplaySize() const
Definition: CUApplication.h:483
int getDisplayWidth() const
Definition: CUApplication.h:469
State getState() const
Definition: CUApplication.h:706
SDL_Window * _window
Definition: CUApplication.h:146
virtual void onSuspend()
Definition: CUApplication.h:300
Rect _display
Definition: CUApplication.h:154
std::string _assets
Definition: CUApplication.h:141
std::string getAssetDirectory()
const std::string getOpenGLDescription() const
const std::string & getName() const
Definition: CUApplication.h:594
virtual void onResume()
Definition: CUApplication.h:312
void setClearColor(Color4 color)
Definition: CUApplication.h:688
bool _fullscreen
Definition: CUApplication.h:156
Definition: CUColor4.h:73
State
Definition: CUApplication.h:92
void setFPS(float fps)
float width
Definition: CUSize.h:61
std::string _org
Definition: CUApplication.h:138
const Rect & getDisplayBounds() const
Definition: CUApplication.h:494
virtual void onShutdown()
float _fps
Definition: CUApplication.h:161
void setFullscreen(bool value)
virtual void update(float timestep)
Definition: CUApplication.h:325
Uint32 schedule(std::function< bool()> callback, Uint32 time=0)
float height
Definition: CUSize.h:63
int getDisplayHeight() const
Definition: CUApplication.h:476
std::string _name
Definition: CUApplication.h:136
std::function< bool()> callback
Definition: CUApplication.h:61
std::string getSaveDirectory()
Color4 getClearColor() const
Definition: CUApplication.h:698
bool isHighDPI() const
Definition: CUApplication.h:551
Color4f _clearColor
Definition: CUApplication.h:163
Definition: CURect.h:45
float getFPS() const
Definition: CUApplication.h:664
std::string _saves
Definition: CUApplication.h:143
Definition: CUApplication.h:83
void setOrganization(const char *name)
float getAverageFPS() const
Definition: CUApplication.h:59
Uint32 timer
Definition: CUApplication.h:65
virtual void onStartup()
Size size
Definition: CURect.h:51
void setSize(int width, int height)
State _state
Definition: CUApplication.h:151
Definition: CUColor4.h:1104
void setHighDPI(bool highDPI)
virtual void onLowMemory()
Definition: CUApplication.h:287
const std::string & getOrganization() const
Definition: CUApplication.h:633
Definition: CUAnimationNode.h:52
bool isFullscreen() const
Definition: CUApplication.h:515
~Application()
Definition: CUApplication.h:212
void setName(const char *name)
virtual void draw()
Definition: CUApplication.h:336
virtual void dispose()
static Application * _theapp
Definition: CUApplication.h:166
void unschedule(Uint32 id)