summaryrefslogtreecommitdiffstats
path: root/src/camera.cpp
blob: 59e99a00539322135fe1039c6803d87e2ce7e09a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "camera.hpp"
#include "shape.hpp"
#include <cmath>
#include <algorithm>
#include <variant>
#include <typeinfo>
#include <any>

void 
Camera::updateFrustum(){
    if(m_dirty) {
        *m_viewport << straw::clear(' ');
        m_frustum = shapes::rectangle<long>(0, 0, (long)m_viewport->getwidth(), (long)m_viewport->getheight());
        m_frustum.position -= (m_frustum.bounds / 2);
        m_frustum.position += m_position + m_origin;
        m_dirty = false;
    }
}

void
RenderBatchEntry::translate(Camera *camera)
{
    std::visit([camera](auto &shapeval) {
        shapeval.translate(-camera->m_frustum.position);
        shapeval.translate((camera->m_frustum.bounds * camera->m_scale) / 2);
        shapeval.position = vex::vec2<long>(
                (long)std::floor((double)shapeval.position[0] / (double)camera->m_scale),
                (long)std::floor((double)shapeval.position[1] / (double)camera->m_scale));
        shapeval.scale(camera->m_scale);
        }, shape);
}

void 
Camera::draw()
{
    if(!m_dirty) return;
    updateFrustum();
    shapes::rectangle<long> ssfrustum{vex::vec2<long>(0, 0), vex::vec2<long>(m_viewport->getwidth(), m_viewport->getheight())};

    //Remove all batches not intersecting the screenspace frustum
    m_shapeBatch.erase(
            std::remove_if(
                m_shapeBatch.begin(), 
                m_shapeBatch.end(),
                [ssfrustum, this](RenderBatchEntry &entry){
                    entry.translate(this);
                    return std::visit([ssfrustum](auto &&shape) {
                        return shapes::intersects(shape, ssfrustum) == 0;
                    }, entry.shape);
                }),
            m_shapeBatch.end());
    for(RenderBatchEntry &entry : m_shapeBatch) entry.plot(this);
    m_shapeBatch.clear();
}

void
RenderBatchEntry::plotPoint(shapes::point<long> point, Camera *camera) {
    long ploty = camera->m_viewport->getheight() - point.position[1];
    if(point.position[0] < 0 || point.position[0] >= camera->m_viewport->getwidth() ||
            ploty < 0 || ploty >= camera->m_viewport->getheight()) return;
    *camera->m_viewport << straw::setcolor(fg, bg) << straw::plot(point.position[0], ploty, c);
}

void
RenderBatchEntry::plotLine(shapes::line<long> line, Camera *camera) {
    if(line.end == line.position) {
        plotPoint(shapes::point<long>(line.position), camera);
    }
    vex::vec2<long> vpdim(camera->m_viewport->getwidth(),
                          camera->m_viewport->getheight());
    vex::vec2<long> d(line.end - line.position);
    long adx = std::abs(d[0]);
    long ady = std::abs(d[1]);

    long offX= d[0] > 0 ? 1 : -1;
    long offY = d[1] > 0 ? 1 : -1;

    *camera->m_viewport << straw::setcolor(fg, bg);
    if(adx < ady) {
        long err = ady / 2;
        long x = line.position[0];
        long y = line.position[1];
        for(long i = 0; i < ady; i++) {
            if(x > 0 && x < vpdim[0] && y > 0 && y < vpdim[1]) {
                long ploty = vpdim[1] - y;
                *camera->m_viewport << straw::plot(x, ploty, c);
            }
            if(err >= ady) {
                x += offX;
                y += offY;
                err += adx - ady;
            }else {
                y += offY;
                err += adx;
            }
        }

    }else{
        long err = adx / 2;
        long x = line.position[0];
        long y = line.position[1];
        for(long i = 0; i < adx; i++) {
            if(x > 0 && x < vpdim[0] && y > 0 && y < vpdim[1]) {
                long ploty = vpdim[1] - y;
                *camera->m_viewport << straw::plot(x, ploty, c);
            }
            if(err >= adx) {
                x += offX;
                y += offY;
                err += ady - adx;
            }else {
                x += offX;
                err += ady;
            }
        }
    }
}

void
RenderBatchEntry::plotCircle(shapes::circle<long> circle, Camera *camera) {
    if(circle.radius == 1) {
        plotPoint(shapes::point<long>(circle.position), camera);
        return;
    }
    long sy = std::max<long>(1, circle.position[1] - circle.radius);
    long ey = std::min<long>(camera->m_viewport->getheight(), circle.position[1] + circle.radius);
    *camera->m_viewport << straw::setcolor(fg, bg);
    for(long y = sy; y <= ey; y++) {
        long ploty = camera->m_viewport->getheight() - y;
        long r2 = circle.radius * circle.radius;
        long dy = circle.position[1] - y;
        long dx = (long)std::sqrt(r2 - (dy * dy));
        long sx = std::max<long>(0, circle.position[0] - dx + 1);
        long ex = std::min<long>(camera->m_viewport->getwidth(), circle.position[0] + dx);
        for(unsigned x = (unsigned)sx; x < ex; x++) {
            *camera->m_viewport << straw::plot(x, ploty, c);
        }
    }
}

void
RenderBatchEntry::plotRectangle(shapes::rectangle<long> rectangle, Camera *camera)
{
    if(rectangle.bounds[1] == 1) {
        plotPoint(shapes::point<long>(rectangle.position), camera);
        return;
    }
    long sy = std::max<long>(1, rectangle.position[1]);
    long ey = std::min<long>(camera->m_viewport->getheight(), rectangle.position[1] + rectangle.bounds[1]);
    *camera->m_viewport << straw::setcolor(fg, bg);
    for(long y = sy; y < ey; y++) {
        long ploty = camera->m_viewport->getheight() - y;
        long sx = std::max<long>(0, rectangle.position[1]);
        long ex = std::min<long>(camera->m_viewport->getwidth(), rectangle.position[1] + rectangle.bounds[1]);
        for(unsigned x = (unsigned)sx; x < ex; x++) {
            *camera->m_viewport << straw::plot(x, ploty, c);
        }
    }
}

void
RenderBatchEntry::plotEllipse(shapes::ellipse<long> shapeval, Camera *camera) {
    if(shapeval.a == 1) {
        plotPoint(shapes::point<long>(shapeval.position), camera);
        return;
    }
    long sy = std::max<long>(1, shapeval.position[1] - shapeval.b);
    long ey = std::min<long>(camera->m_viewport->getheight(), shapeval.position[1] + shapeval.b);
    *camera->m_viewport << straw::setcolor(fg, bg);
    for(long y = sy; y <= ey; y++) {
        long ploty = camera->m_viewport->getheight() - y;
        long dy = shapeval.position[1] - y;
        long dy2 = dy * dy;
        long a2 = shapeval.a * shapeval.a;
        long b2 = shapeval.b * shapeval.b;
        long dx = (long)(((2.0 * (double)shapeval.a) /
                    (double)shapeval.b) * std::sqrt(b2 - dy2) / 2.0);
        long sx = std::max<long>(0, shapeval.position[0] - dx + 1);
        long ex = std::min<long>(camera->m_viewport->getwidth(), shapeval.position[0] + dx);

        for(unsigned x = (unsigned)sx; x < ex; x++) {
            *camera->m_viewport << straw::plot(x, ploty, c);
        }       
    }
}
void
RenderBatchEntry::plot(Camera *camera) 
{
    std::visit([this, camera](auto &shapeval) -> void{
        using T = std::decay_t<decltype(shapeval)>;
        if constexpr(std::is_same_v<T, shapes::point<long>>) plotPoint(shapeval, camera);
        if constexpr(std::is_same_v<T, shapes::line<long>>) plotLine(shapeval, camera);
        if constexpr(std::is_same_v<T, shapes::ellipse<long>>) plotEllipse(shapeval, camera);
        if constexpr(std::is_same_v<T, shapes::circle<long>>) plotCircle(shapeval, camera);
        if constexpr(std::is_same_v<T, shapes::rectangle<long>>) plotRectangle(shapeval, camera);
    }, shape);
}