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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "nes/app/application.hh"
#include "nes/app/input/input-device-keyboard.hh"
#include "nes/app/input/input-device-controller.hh"
#include "nes/app/ui/screen.hh"
#include "nes/app/graphics/renderer.hh"
#include "nes/app/action.hh"
#include "nes/app/crypto/aes256.hh"
#include "nes/app/crypto/sha256.hh"
namespace nes::app
{
auto application::display_proxy::get_front() const -> span<rgb, display::width * display::height>
{
return buffer_front_;
}
auto application::display_proxy::set(u32 const x, u32 const y, rgb const value) -> void
{
buffer_back_[y * display::width + x] = value;
base.set(x, y, value);
}
auto application::display_proxy::switch_buffers() -> void
{
auto r = renderer{ base };
if (visible_screen)
{
visible_screen->render(r);
}
if (visible_popup)
{
visible_popup->render(r);
}
if (preferences_.get_fps_counter())
{
r.render_rect(30, 29, 2, 1, color::fixed_black);
auto const attrs = text_attributes{}
.set_max_width(2)
.set_alignment(text_alignment::right);
r.render_text_format(32, 29, color::fixed_white, attrs, "{}", fps.get_fps());
}
swap(buffer_front_, buffer_back_);
base.switch_buffers();
}
application::application(display& display, input_device_keyboard& keyboard, file_browser& file_browser)
: input_manager_{ keyboard }
, file_browser_{ file_browser }
, display_{ preferences_, display }
, screen_title_{ keyboard }
, screen_browser_{ keyboard, file_browser }
, screen_settings_{ input_manager_, preferences_ }
, screen_help_{ input_manager_ }
, screen_error_{ keyboard }
, screen_confirm_quit_{ keyboard }
, screen_prompt_key_{ keyboard }
, screen_file_viewer_{ keyboard, file_browser }
{
display_.visible_screen = &screen_title_;
}
auto application::frame(u32 const elapsed_time_us) -> void
{
display_.fps.frame(elapsed_time_us);
if (display_.visible_popup)
{
auto const a = display_.visible_popup->process_events();
handle_action(a);
}
else if (display_.visible_screen)
{
auto const a = display_.visible_screen->process_events();
handle_action(a);
}
if (display_.visible_screen)
{
// Directly let the scene render itself without rendering any gameplay (the scene is responsible for
// clearing the buffer).
display_.switch_buffers();
}
else if (console_)
{
// Handle escape key.
while (auto const event = input_manager_.get_keyboard().poll_event())
{
if (event == input_event::key_down(key::escape))
{
screen_freeze_.freeze(display_.get_front());
screen_confirm_quit_.set_confirm(false);
display_.visible_screen = &screen_freeze_;
display_.visible_popup = &screen_confirm_quit_;
display_.switch_buffers();
return;
}
}
// Forward current input state to the NES.
console_->ref_controller_1().set_pressed(input_manager_.get_input_1().read_buttons());
console_->ref_controller_2().set_pressed(input_manager_.get_input_2().read_buttons());
// The scene is rendered by the console after the PPU requests a new frame (thus calling
// display_proxy::switch_buffers).
console_->step(sys::cycle_count::from_microseconds(elapsed_time_us));
if (console_->get_status() != status::success)
{
screen_freeze_.freeze(display_.get_front());
display_.visible_screen = &screen_freeze_;
show_error("Runtime error", console_->get_status(), action::go_to_browser());
console_.clear();
}
}
}
auto application::handle_action(action const& a) -> void
{
switch (a.get_type())
{
case action::type::none:
{
break;
}
case action::type::go_to_title:
{
go_to_screen(&screen_title_);
break;
}
case action::type::go_to_browser:
case action::type::confirm_quit:
{
go_to_screen(&screen_browser_);
break;
}
case action::type::go_to_settings:
{
go_to_screen(&screen_settings_);
break;
}
case action::type::go_to_help:
{
go_to_screen(&screen_help_);
break;
}
case action::type::close_popup:
{
display_.visible_popup = nullptr;
break;
}
case action::type::cancel_quit:
{
display_.visible_popup = nullptr;
display_.visible_screen = nullptr;
break;
}
case action::type::prompt_key:
{
screen_prompt_key_.reset(a.get_file_name());
display_.visible_popup = &screen_prompt_key_;
break;
}
case action::type::launch_game:
{
u8 buffer[sys::cartridge::max_file_size];
auto length = u32{ 0 };
if (auto const s = file_browser_.load(a.get_file_name(), buffer, &length); s != status::success)
{
show_error("Unable to open file", s);
break;
}
if (!a.get_key().is_empty())
{
auto const key = span{ reinterpret_cast<u8 const*>(a.get_key().get_data()), a.get_key().get_length() };
auto const key_hash = sha256::hash(key);
aes256::decrypt(span{ buffer, length }, key_hash.get_data());
}
console_.emplace(display_, span<u8 const>{ buffer, length });
if (console_->get_status() != status::success)
{
show_error("Unable to load cartridge", console_->get_status());
console_.clear();
break;
}
display_.visible_screen = nullptr;
display_.visible_popup = nullptr;
break;
}
case action::type::view_file:
{
if (auto const s = screen_file_viewer_.load(a.get_file_name()); s != status::success)
{
show_error("Unable to open file", s);
}
else
{
display_.visible_popup = &screen_file_viewer_;
}
break;
}
case action::type::show_error:
{
show_error(a.get_message(), a.get_error());
break;
}
}
}
auto application::show_error(string_view const message, status const error, action const& action) -> void
{
screen_error_.set_message(message);
screen_error_.set_error(error);
screen_error_.set_action(action);
display_.visible_popup = &screen_error_;
}
auto application::go_to_screen(screen* screen) -> void
{
console_.clear();
display_.visible_screen = screen;
display_.visible_popup = nullptr;
}
} // namespace nes::app
|