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
|
#include "core/application.h"
#include "core/assert.h"
#include "core/log.h"
#include "core/window.h"
#include "core/window_manager.h"
#include "debugger/debugger.h"
#include "ui/ui.h"
#include <glad/gl.h>
#include <algorithm>
#include <charconv>
#include <chrono>
#include <cstdlib>
#include <fstream>
#include <vector>
namespace Hsdbg
{
namespace
{
using Clock = std::chrono::steady_clock;
constexpr float MAX_DELTA_TIME = 0.25f;
constexpr double MINIMIZED_WAIT_TIMEOUT = 0.1;
// dev aid: HSDBG_CAPTURE=<file.ppm> writes the frame numbered
// HSDBG_CAPTURE_FRAME (default 240) as a binary ppm; returns true on
// that frame so the caller can quit
auto capture_frame(uint64_t frame, uint32_t width, uint32_t height) -> bool
{
static const char* path = std::getenv("HSDBG_CAPTURE");
if (path == nullptr)
return false;
static const uint64_t target = [] {
uint64_t value = 240;
if (const char* text = std::getenv("HSDBG_CAPTURE_FRAME"))
std::from_chars(text, text + std::char_traits<char>::length(text), value);
return value;
}();
if (frame != target)
return false;
const size_t stride = static_cast<size_t>(width) * 3;
std::vector<unsigned char> pixels(stride * height);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height),
GL_RGB, GL_UNSIGNED_BYTE, pixels.data());
std::ofstream out(path, std::ios::binary);
out << "P6\n" << width << ' ' << height << "\n255\n";
for (uint32_t y = height; y-- > 0;)
out.write(reinterpret_cast<const char*>(pixels.data() + y * stride),
static_cast<std::streamsize>(stride));
Log::info("capture: wrote '{}' ({}x{})", path, width, height);
return true;
}
}
Application* Application::s_instance = nullptr;
Application::Application(const ApplicationSpec& spec)
: m_spec(spec)
{
HSDBG_ASSERT(s_instance == nullptr, "an Application instance already exists");
s_instance = this;
Log::info("{}: starting up", m_spec.name);
m_window_manager = std::make_unique<WindowManager>();
WindowSpec window_spec;
window_spec.title = m_spec.name;
window_spec.width = m_spec.width;
window_spec.height = m_spec.height;
window_spec.vsync = m_spec.vsync;
m_window = &m_window_manager->create_window(window_spec);
m_debugger = std::make_unique<Debugger>();
m_ui = std::make_unique<Ui>(*m_window);
}
Application::~Application()
{
m_ui.reset();
m_debugger.reset();
m_window = nullptr;
m_window_manager.reset();
Log::info("{}: shutting down after {} frames", m_spec.name, m_frame_count);
s_instance = nullptr;
}
auto Application::get() -> Application&
{
HSDBG_ASSERT(s_instance != nullptr, "no Application instance has been created");
return *s_instance;
}
auto Application::run() -> void
{
m_running = true;
auto last_frame_time = Clock::now();
while (m_running)
{
const auto now = Clock::now();
const float elapsed = std::chrono::duration<float>(now - last_frame_time).count();
last_frame_time = now;
m_delta_time = elapsed < MAX_DELTA_TIME ? elapsed : MAX_DELTA_TIME;
m_time += m_delta_time;
m_frame_count += 1;
m_window_manager->poll_events();
if (m_window->should_close())
{
close();
break;
}
if (m_window->is_minimized())
{
m_window_manager->wait_events(MINIMIZED_WAIT_TIMEOUT);
continue;
}
update();
render();
m_window->swap_buffers();
}
}
auto Application::close() -> void
{
m_running = false;
}
auto Application::update() -> void
{
m_debugger->update();
// dev aid: HSDBG_STEP_AT=<frame>[,<frame>…] steps over a line on those
// frames while stopped, so a capture can show a target that has moved
static const std::vector<uint64_t> step_frames = [] {
std::vector<uint64_t> frames;
const char* text = std::getenv("HSDBG_STEP_AT");
for (const char* at = text; at != nullptr && *at != '\0';)
{
uint64_t value = 0;
const auto [next, error] = std::from_chars(at, at + std::char_traits<char>::length(at), value);
if (error != std::errc{})
break;
frames.push_back(value);
at = *next == ',' ? next + 1 : next;
}
return frames;
}();
if (std::ranges::find(step_frames, m_frame_count) != step_frames.end() && m_debugger->is_stopped())
(void)m_debugger->step_over();
}
auto Application::render() -> void
{
// clear the whole framebuffer at the very start of the frame, before any
// drawing, so nothing from the previous frame can survive underneath
glViewport(0, 0,
static_cast<GLsizei>(m_window->framebuffer_width()),
static_cast<GLsizei>(m_window->framebuffer_height()));
glDisable(GL_SCISSOR_TEST);
glClearColor(0.05f, 0.05f, 0.06f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_ui->begin_frame();
m_ui->draw(*m_debugger);
m_ui->end_frame();
if (capture_frame(m_frame_count, m_window->framebuffer_width(), m_window->framebuffer_height()))
close();
}
}
|