]> git.localhorst.tv Git - space.git/commitdiff
dynamic zoom
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 20 Dec 2013 06:25:45 +0000 (07:25 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 20 Dec 2013 06:26:21 +0000 (07:26 +0100)
butt ugly

src/app/Application.cpp
src/graphics/Camera.cpp
src/graphics/Camera.h
src/graphics/primitive.cpp
src/graphics/primitive.h

index 9ebc2613d7d24a93be4fca69ddc66c200d16a13d..d18c757eca9141e62b18ea112844bc49eb049cc4 100644 (file)
@@ -88,10 +88,10 @@ void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
                        control.x += 1;
                        break;
                case SDLK_z:
-                       cam.DoubleZoom();
+                       cam.StartZoom();
                        break;
                case SDLK_x:
-                       cam.HalfZoom();
+                       cam.StartShrink();
                        break;
                default:
                        break;
@@ -124,6 +124,12 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
                case SDLK_d:
                        control.x -= 1;
                        break;
+               case SDLK_z:
+                       cam.StopZoom();
+                       break;
+               case SDLK_x:
+                       cam.StopShrink();
+                       break;
                default:
                        break;
        }
@@ -149,11 +155,11 @@ void Application::Render() {
        SDL_Surface *dst = screen.Screen();
        const Vector<int> begin = cam.ToScreen(Vector<float>(0, 0));
        const Vector<int> end =
-               cam.ToScreen((univ.size * univ.secSize * univ.areaSize)) + Vector<int>(1, 1);;
+               cam.ToScreen((univ.size * univ.secSize * univ.areaSize))
+                       + Vector<int>(1, 1);
 
        Fill(dst, background);
-       Grid(dst, begin, end, cam.ToScale(univ.areaSize), secGrid);
-       Grid(dst, begin, end, cam.ToScale(univ.secSize * univ.areaSize), univGrid);
+       Grid2(dst, begin, end, cam.ToScale(univ.areaSize), univ.secSize, secGrid, univGrid);
        Cross(dst, cam.ToScreen(focus.Pos()), 15, focusColor);
 
        for (const Entity &e : univ.Entities()) {
index a72157a9ccc15c2e02d54929812b876a257fa249..553b74f77f0f9afbd36e1f6373d1b7b41bda50b5 100644 (file)
@@ -6,7 +6,7 @@ namespace space {
 Camera::Camera(int w, int h, const Vector<float> &t)
 : target(&t)
 , offset(w/2, h/2)
-, zoom(0) {
+, zoom(1) {
 
 }
 
@@ -16,15 +16,24 @@ void Camera::Resize(int w, int h) {
 }
 
 void Camera::Update(float delta) {
+       zoom += zoom * zoomAcc * delta;
+       if (zoom < 0.0001) zoom = 0.0001;
+}
+
+void Camera::StartZoom() {
+       zoomAcc += 1;
+}
 
+void Camera::StopZoom() {
+       zoomAcc -= 1;
 }
 
-void Camera::DoubleZoom() {
-       zoom += 1;
+void Camera::StartShrink() {
+       zoomAcc -= 1;
 }
 
-void Camera::HalfZoom() {
-       zoom -= 1;
+void Camera::StopShrink() {
+       zoomAcc += 1;
 }
 
 }
index e40b051bc64d78c18479fdbcac6f276049a0e830..ad1a997ef6383cad9283ff884321b46133822e33 100644 (file)
@@ -18,8 +18,10 @@ public:
        void Resize(int w, int h);
        void Update(float deltaT);
 
-       void DoubleZoom();
-       void HalfZoom();
+       void StartZoom();
+       void StopZoom();
+       void StartShrink();
+       void StopShrink();
 
        Vector<int> ToScreen(Vector<float> v) const {
                return Vector<int>(OffsetOf(v));
@@ -28,20 +30,15 @@ public:
                return ToScale(v - *target) + offset;
        }
        Vector<float> ToScale(Vector<float> v) const {
-               if (zoom == 0) {
-                       return v;
-               } else if (zoom > 0) {
-                       return v * float(1 << zoom);
-               } else {
-                       return v / float(1 << -zoom);
-               }
+               return v * zoom;
        }
 
 private:
        const Vector<float> *target;
        Vector<float> offset;
 
-       int zoom;
+       float zoom;
+       int zoomAcc;
 
 };
 
index 1a68d45a9a8a16acfcd5843af60fce1242f35119..f8b5863c68db7ffe12941cdfb9ea254ee2dd8f8a 100644 (file)
@@ -164,4 +164,86 @@ void Grid(
        }
 }
 
+void Grid2(
+               SDL_Surface *dst,
+               Vector<int> first,
+               Vector<int> second,
+               Vector<int> size,
+               Vector<int> n,
+               Color color1,
+               Color color2) {
+       Uint32 c1 = color1.MapRGBA(dst);
+       Uint32 c2 = color2.MapRGBA(dst);
+
+       Vector<int> from = min(first, second);
+       Vector<int> to = max(first, second);
+
+       if (size.x <= 1 || size.y <= 1) {
+               FillRect(dst, from, to - from, c1);
+               Grid(dst, from, to, size * n, color2);
+               return;
+       }
+
+       if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) {
+               return;
+       }
+
+       Vector<int> i(0, 0);
+       while (from.x < -size.x) { from.x += size.x; ++i.x; }
+       while (from.y < -size.y) { from.y += size.y; ++i.y; }
+       while (to.x > dst->w + size.x) to.x -= size.x;
+       while (to.y > dst->h + size.y) to.y -= size.y;
+
+       int width = to.x - from.x;
+       int height = to.y - from.y;
+
+       for (Vector<int> pos(from); pos.x <= to.x; pos.x += size.x) {
+               VLine(dst, pos, height, (i.x++ % n.x) ? c1 : c2);
+       }
+       for (Vector<int> pos(from); pos.y <= to.y; pos.y += size.y) {
+               HLine(dst, pos, width, (i.y++ % n.y) ? c1 : c2);
+       }
+}
+
+void Grid2(
+               SDL_Surface *dst,
+               Vector<int> first,
+               Vector<int> second,
+               Vector<float> size,
+               Vector<int> n,
+               Color color1,
+               Color color2) {
+       Uint32 c1 = color1.MapRGBA(dst);
+       Uint32 c2 = color2.MapRGBA(dst);
+
+       Vector<int> from = min(first, second);
+       Vector<int> to = max(first, second);
+
+       if (size.x <= 1 || size.y <= 1) {
+               FillRect(dst, from, to - from, c1);
+               Grid(dst, from, to, size * Vector<float>(n), color2);
+               return;
+       }
+
+       if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) {
+               return;
+       }
+
+       Vector<int> i(0, 0);
+       while (from.x < -size.x) { from.x += size.x; ++i.x; }
+       while (from.y < -size.y) { from.y += size.y; ++i.y; }
+       while (to.x > dst->w + size.x) to.x -= size.x;
+       while (to.y > dst->h + size.y) to.y -= size.y;
+
+       float width = to.x - from.x;
+       float height = to.y - from.y;
+
+       for (Vector<float> pos(from); pos.x <= to.x; pos.x += size.x) {
+               VLine(dst, pos, height, (i.x++ % n.x) ? c1 : c2);
+       }
+       for (Vector<float> pos(from); pos.y <= to.y; pos.y += size.y) {
+               HLine(dst, pos, width, (i.y++ % n.y) ? c1 : c2);
+       }
+}
+
 }
index 77267008db520334ffd0e11048c881c2e06a0101..6dcc6fe6b21f9d8cfb6b21bc24d4ffe648a04ddc 100644 (file)
@@ -41,6 +41,23 @@ void Grid(
        Vector<float> size,
        Color);
 
+void Grid2(
+       SDL_Surface *,
+       Vector<int> from,
+       Vector<int> to,
+       Vector<int> size,
+       Vector<int> n,
+       Color,
+       Color);
+void Grid2(
+       SDL_Surface *,
+       Vector<int> from,
+       Vector<int> to,
+       Vector<float> size,
+       Vector<int> n,
+       Color,
+       Color);
+
 }
 
 #endif