Explorar el Código

New camera features

Eiyeron Fulmincendii hace 9 años
padre
commit
7d1b587143
Se han modificado 3 ficheros con 34 adiciones y 9 borrados
  1. 2 1
      src/map/StateMap.cpp
  2. 21 7
      src/render/Camera.cpp
  3. 11 1
      src/render/Camera.h

+ 2 - 1
src/map/StateMap.cpp

@@ -19,8 +19,9 @@ namespace
     }
 }
 
-StateMap::StateMap(int x, int y, Map &map) : camera(x, y), map(map)
+StateMap::StateMap(int x, int y, Map &map) : camera(x, y, CameraCenterType::CENTER), map(map)
 {
+    camera.set_x(0);
 }
 
 void StateMap::update(unsigned dt)

+ 21 - 7
src/render/Camera.cpp

@@ -3,15 +3,15 @@
 #include "input/Input.h"
 
 using WalrusRPG::Camera;
+using WalrusRPG::CameraCenterType;
 using namespace WalrusRPG;
 using WalrusRPG::Input::Key;
 
-Camera::Camera(signed x, signed y)
+Camera::Camera(signed x, signed y, CameraCenterType center_type)
+: x(x), y(y), render_area_width(320), render_area_height(240), center_type(center_type)
 {
-    this->x = x;
-    this->y = y;
-    render_area_width = 320;
-    render_area_height = 240;
+    set_x(x);
+    set_y(y);
 }
 
 Camera::~Camera()
@@ -43,21 +43,25 @@ void Camera::update(unsigned dt)
 void Camera::set_x(signed x)
 {
     this->x = x;
+    if(center_type == CENTER)
+        this->x -= render_area_width/2;
 }
 
 signed Camera::get_x() const
 {
-    return this->x;
+    return center_type == CENTER? this->x + render_area_width/2 : this->x;
 }
 
 void Camera::set_y(signed y)
 {
     this->y = y;
+    if(this->center_type == CENTER)
+        this->y -= render_area_height/2;
 }
 
 signed Camera::get_y() const
 {
-    return this->y;
+    return center_type == CENTER? this->y + render_area_height/2 : this->y;
 }
 
 bool Camera::is_visible(const WalrusRPG::Utils::Rect &object) const
@@ -76,3 +80,13 @@ bool Camera::is_visible(const WalrusRPG::Utils::Rect &object) const
         return false;
     }
 }
+
+void Camera::set_center_type(CameraCenterType center_type)
+{
+    this->center_type = center_type;
+}
+
+CameraCenterType Camera::get_center_type()
+{
+    return this->center_type;
+}

+ 11 - 1
src/render/Camera.h

@@ -5,6 +5,8 @@
 
 namespace WalrusRPG
 {
+    enum CameraCenterType{TOP_LEFT_CORNER, CENTER};
+
     class Camera
     {
       protected:
@@ -15,6 +17,8 @@ namespace WalrusRPG
                                     // of the screen?
         unsigned render_area_height;
 
+        CameraCenterType center_type;
+
         // Do you want to use classes for coordinates and allow them to have vector-based
         // mathematics? With operator overriding that could be doable to have
         // Vector2 a(3, 2); Vector2 b(1, 2); Vector3 c = a+b;
@@ -23,7 +27,8 @@ namespace WalrusRPG
         // Vector2 acceleration;
 
       public:
-        Camera(signed x, signed y);
+        Camera(signed x, signed y, CameraCenterType center_type = TOP_LEFT_CORNER);
+
         ~Camera();
         // This doesn't need any render as it's the utility which helps rendering. Unless
         // you want to show debnug things.
@@ -37,6 +42,11 @@ namespace WalrusRPG
 
         // Can you see me, senpai ? >.<
         bool is_visible(const WalrusRPG::Utils::Rect &object) const;
+
+        void set_center_type(CameraCenterType center_type);
+
+        CameraCenterType get_center_type();
+
     };
 }