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
|
#include "main-impl.hpp"
#include "src/tile-constants.hpp"
#include "floormat/app.hpp"
#include "src/camera-offset.hpp"
#include "src/anim-atlas.hpp"
#include "main/clickable.hpp"
#include "src/nanosecond.inl"
#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Containers/ArrayView.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/Math/Color.h>
namespace floormat {
namespace {
size_t bad_frame_counter = 0; // NOLINT
constexpr auto clear_color = 0x222222ff_rgbaf;
} // namespace
void main_impl::do_update(const Ns& dtʹ)
{
constexpr auto eps = 1e-5f;
constexpr auto max_dt = Milliseconds*100;
const auto dt = dtʹ > max_dt ? max_dt : dtʹ;
if (float secs{Time::to_seconds(dt)}; secs > eps)
{
#if 1
constexpr float RC = 60;
constexpr float alpha = 1 / (1 + RC);
float& value = _frame_timings.smoothed_frame_time;
value = (value*(1 - alpha) + alpha * secs);
#else
value = secs;
#endif
if (secs > 35e-3f /* && !dt_expected.do_sleep */) [[likely]]
fm_debug("%zu frame took %.2f milliseconds", bad_frame_counter++, (double)secs*1e3);
}
else
swapBuffers();
app.update(dt);
}
void main_impl::clear_framebuffer()
{
#ifdef FM_USE_DEPTH32
framebuffer.fb.clearColor(0, clear_color);
#else
GL::defaultFramebuffer.clearColor(clear_color);
#endif
}
void main_impl::cache_draw_on_startup()
{
_shader.set_tint({1, 1, 1, 1});
clear_framebuffer();
for (int i = 0; i < 3; i++)
{
do_update(Ns{1});
draw_world();
}
clear_framebuffer();
(void)timeline.update();
swapBuffers();
redraw();
}
void main_impl::drawEvent()
{
if (_first_frame) [[unlikely]]
{
_first_frame = false;
cache_draw_on_startup();
}
_shader.set_tint({1, 1, 1, 1});
clear_framebuffer();
draw_world();
app.draw();
GL::Renderer::flush();
do_update(timeline.update());
#ifdef FM_USE_DEPTH32
GL::Framebuffer::blit(framebuffer.fb, GL::defaultFramebuffer, framebuffer.fb.viewport(), GL::FramebufferBlit::Color);
#endif
swapBuffers();
redraw();
}
void main_impl::draw_world() noexcept
{
const auto [z_min, z_max, z_cur, only] = app.get_z_bounds();
const auto [minx, maxx, miny, maxy] = get_draw_bounds();
const auto sz = window_size();
fm_debug_assert(1 + maxx - minx <= 8);
fm_debug_assert(1 + maxy - miny <= 8);
arrayResize(_clickable_scenery, 0);
#ifdef FM_USE_DEPTH32
framebuffer.fb.clearDepth(0);
#else
GL::defaultFramebuffer.clearDepth(0);
#endif
GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
GL::Renderer::setDepthMask(true);
for (int8_t z = z_max; z >= z_min; z--)
{
if (only && z != z_cur)
_shader.set_tint({1, 1, 1, 0.75});
else
_shader.set_tint({1, 1, 1, 1});
for (int16_t y = maxy; y >= miny; y--)
for (int16_t x = maxx; x >= minx; x--)
{
const chunk_coords_ ch{x, y, z};
auto* cʹ = _world.at(ch);
if (!cʹ)
continue;
auto& c = *cʹ;
bind();
const with_shifted_camera_offset o{_shader, ch, {minx, miny}, {maxx, maxy}};
if (check_chunk_visible(_shader.camera_offset(), sz))
{
_ground_mesh.draw(_shader, c);
_wall_mesh.draw(_shader, c);
}
}
}
GL::Renderer::setDepthMask(false);
for (int8_t z = z_min; z <= z_max; z++)
for (int16_t y = miny; y <= maxy; y++)
for (int16_t x = minx; x <= maxx; x++)
{
if (only && z != z_cur)
_shader.set_tint({1, 1, 1, 0.75});
else
_shader.set_tint({1, 1, 1, 1});
const chunk_coords_ pos{x, y, z};
auto* cʹ = _world.at(pos);
if (!cʹ)
continue;
auto& c = *cʹ;
const with_shifted_camera_offset o{_shader, pos, {minx, miny}, {maxx, maxy}};
if (check_chunk_visible(_shader.camera_offset(), sz))
_anim_mesh.draw(_shader, sz, c, _clickable_scenery, _do_render_vobjs);
}
_shader.set_tint({1, 1, 1, 1});
GL::Renderer::setDepthMask(true);
GL::Renderer::disable(GL::Renderer::Feature::DepthTest);
}
void main_impl::bind() noexcept
{
framebuffer.fb.bind();
}
ArrayView<const clickable> main_impl::clickable_scenery() const noexcept
{
return { _clickable_scenery.data(), _clickable_scenery.size() };
}
ArrayView<clickable> main_impl::clickable_scenery() noexcept
{
return { _clickable_scenery.data(), _clickable_scenery.size() };
}
} // namespace floormat
|