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
|
#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"
namespace nes::app
{
auto application::display_proxy::switch_buffers() -> void
{
auto r = renderer{ base };
if (screen)
{
screen->render(r);
}
if (popup)
{
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_counter.get_fps());
}
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_error_{ keyboard }
, screen_confirm_quit_{ keyboard }
{
display_.screen = &screen_title_;
}
auto application::frame(u32 const elapsed_time_us) -> void
{
display_.fps_counter.frame(elapsed_time_us);
if (display_.popup)
{
auto const a = display_.popup->process_events();
handle_action(a);
}
else if (display_.screen)
{
auto const a = display_.screen->process_events();
handle_action(a);
}
if (display_.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_);
screen_confirm_quit_.set_confirm(false);
display_.screen = &screen_freeze_;
display_.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_);
display_.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:
{
// TODO
break;
}
case action::type::close_popup:
{
display_.popup = nullptr;
break;
}
case action::type::cancel_quit:
{
display_.popup = nullptr;
display_.screen = nullptr;
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;
}
console_.emplace(display_, span{ buffer, length });
if (console_->get_status() != status::success)
{
show_error("Unable to load cartridge", console_->get_status());
console_.clear();
break;
}
display_.screen = nullptr;
display_.popup = nullptr;
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_.popup = &screen_error_;
}
auto application::go_to_screen(screen* screen) -> void
{
console_.clear();
display_.screen = screen;
display_.popup = nullptr;
}
} // namespace nes::app
|