CUGL
Cornell University Game Library
CUOrthographicCamera.h
1 //
2 // CUOrthographicCamera.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides an Orthographic camera class. As cugl's primary support
6 // is for 2d (mobile) gameplay it is our primary camera class.
7 //
8 // This module is based on the original file OrthographicCamera.java by
9 // Mario Zechner, written for LibGDX ( https://libgdx.badlogicgames.com ). It
10 // has been modified to support the CUGL framework.
11 //
12 // This class uses our standard shared-pointer architecture.
13 //
14 // 1. The constructor does not perform any initialization; it just sets all
15 // attributes to their defaults.
16 //
17 // 2. All initialization takes place via init methods, which can fail if an
18 // object is initialized more than once.
19 //
20 // 3. All allocation takes place via static constructors which return a shared
21 // pointer.
22 //
23 // CUGL zlib License:
24 // This software is provided 'as-is', without any express or implied
25 // warranty. In no event will the authors be held liable for any damages
26 // arising from the use of this software.
27 //
28 // Permission is granted to anyone to use this software for any purpose,
29 // including commercial applications, and to alter it and redistribute it
30 // freely, subject to the following restrictions:
31 //
32 // 1. The origin of this software must not be misrepresented; you must not
33 // claim that you wrote the original software. If you use this software
34 // in a product, an acknowledgment in the product documentation would be
35 // appreciated but is not required.
36 //
37 // 2. Altered source versions must be plainly marked as such, and must not
38 // be misrepresented as being the original software.
39 //
40 // 3. This notice may not be removed or altered from any source distribution.
41 //
42 // Author: Walker White
43 // Version: 6/24/16
44 
45 #ifndef __CU_ORTHOGRAPHIC_CAMERA_H__
46 #define __CU_ORTHOGRAPHIC_CAMERA_H__
47 
48 #include "CUCamera.h"
49 
50 namespace cugl {
51 
57 class OrthographicCamera : public Camera {
58 #pragma mark Values
59 protected:
61  float _zoom;
62 
65 
66 #pragma mark -
67 #pragma mark Constructors
68 public:
75  _zoom = 1; _near = 0; _initialized = false;
76  }
77 
82 
86  void dispose() override;
87 
102  bool init(const Size& size, bool yDown=false) {
103  return initOffset(0,0,size.width,size.height,yDown);
104  }
105 
121  bool init(float width, float height, bool yDown=false) {
122  return initOffset(0,0,width,height,yDown);
123  }
124 
142  bool initOffset(const Rect& rect, bool yDown=false) {
143  return initOffset(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height,yDown);
144  }
145 
164  bool initOffset(const Vec2& origin, const Size& size, bool yDown=false) {
165  return initOffset(origin.x,origin.y,size.width,size.height,yDown);
166  }
167 
188  bool initOffset(float x, float y, float width, float height, bool yDown=false);
189 
190 #pragma mark -
191 #pragma mark Static Constructors
192 
206  static std::shared_ptr<OrthographicCamera> alloc(const Size& size, bool yDown=false) {
207  std::shared_ptr<OrthographicCamera> result = std::make_shared<OrthographicCamera>();
208  return(result->init(size,yDown) ? result : nullptr);
209  }
210 
226  static std::shared_ptr<OrthographicCamera> alloc(float width, float height, bool yDown=false) {
227  std::shared_ptr<OrthographicCamera> result = std::make_shared<OrthographicCamera>();
228  return (result->init(width,height,yDown) ? result : nullptr);
229  }
230 
248  static std::shared_ptr<OrthographicCamera> allocOffset(const Rect& rect, bool yDown=false) {
249  std::shared_ptr<OrthographicCamera> result = std::make_shared<OrthographicCamera>();
250  return (result->initOffset(rect,yDown) ? result : nullptr);
251  }
252 
271  static std::shared_ptr<OrthographicCamera> allocOffset(const Vec2& origin, const Size& size,
272  bool yDown=false) {
273  std::shared_ptr<OrthographicCamera> result = std::make_shared<OrthographicCamera>();
274  return (result->initOffset(origin,size,yDown) ? result : nullptr);
275  }
276 
297  static std::shared_ptr<OrthographicCamera> allocOffset(float x, float y, float width, float height,
298  bool yDown=false) {
299  std::shared_ptr<OrthographicCamera> result = std::make_shared<OrthographicCamera>();
300  return (result->initOffset(x,y,width,height,yDown) ? result : nullptr);
301  }
302 
303 #pragma mark -
304 #pragma mark Setters
305 
317  void set(const Size& size, bool yDown=false) {
318  set(0,0,size.width,size.height,yDown);
319  }
320 
334  void set(float width, float height, bool yDown=false) {
335  set(0,0,width,height,yDown);
336  }
337 
353  void set(const Rect& rect, bool yDown=false) {
354  set(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height,yDown);
355  }
356 
373  void set(const Vec2& origin, const Size& size, bool yDown=false) {
374  set(origin.x,origin.y,size.width,size.height,yDown);
375  }
376 
395  void set(float x, float y, float width, float height, bool yDown=false);
396 
402  void update() override;
403 
404 
405 #pragma mark -
406 #pragma mark Attributes
407 
415  float getZoom() const { return _zoom; }
416 
428  void setZoom(float zoom);
429 
430 };
431 
432 }
433 
434 #endif /* __CU_ORTHOGRAPHIC_CAMERA_H__ */
Definition: CUSize.h:57
void dispose() override
float getZoom() const
Definition: CUOrthographicCamera.h:415
float x
Definition: CUVec2.h:66
float y
Definition: CUVec2.h:68
static std::shared_ptr< OrthographicCamera > allocOffset(const Vec2 &origin, const Size &size, bool yDown=false)
Definition: CUOrthographicCamera.h:271
float _near
Definition: CUCamera.h:76
Definition: CUVec2.h:61
bool _initialized
Definition: CUOrthographicCamera.h:64
static std::shared_ptr< OrthographicCamera > alloc(const Size &size, bool yDown=false)
Definition: CUOrthographicCamera.h:206
bool initOffset(const Rect &rect, bool yDown=false)
Definition: CUOrthographicCamera.h:142
bool initOffset(const Vec2 &origin, const Size &size, bool yDown=false)
Definition: CUOrthographicCamera.h:164
void setZoom(float zoom)
void set(const Vec2 &origin, const Size &size, bool yDown=false)
Definition: CUOrthographicCamera.h:373
float _zoom
Definition: CUOrthographicCamera.h:61
float width
Definition: CUSize.h:61
static std::shared_ptr< OrthographicCamera > allocOffset(float x, float y, float width, float height, bool yDown=false)
Definition: CUOrthographicCamera.h:297
bool init(const Size &size, bool yDown=false)
Definition: CUOrthographicCamera.h:102
float height
Definition: CUSize.h:63
bool init(float width, float height, bool yDown=false)
Definition: CUOrthographicCamera.h:121
~OrthographicCamera()
Definition: CUOrthographicCamera.h:81
void set(const Size &size, bool yDown=false)
Definition: CUOrthographicCamera.h:317
Definition: CURect.h:45
Vec2 origin
Definition: CURect.h:49
void set(const Rect &rect, bool yDown=false)
Definition: CUOrthographicCamera.h:353
void set(float width, float height, bool yDown=false)
Definition: CUOrthographicCamera.h:334
Size size
Definition: CURect.h:51
static std::shared_ptr< OrthographicCamera > allocOffset(const Rect &rect, bool yDown=false)
Definition: CUOrthographicCamera.h:248
Definition: CUOrthographicCamera.h:57
Definition: CUCamera.h:56
void update() override
OrthographicCamera()
Definition: CUOrthographicCamera.h:74
static std::shared_ptr< OrthographicCamera > alloc(float width, float height, bool yDown=false)
Definition: CUOrthographicCamera.h:226
Definition: CUAnimationNode.h:52