blob: b18e6d662336966118d87ff0a386fdc56af06b23 (
plain)
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
|
#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 "ui/action.hh"
#include <fstream>
#include <iostream>
namespace nes::app
{
namespace
{
constexpr auto file = std::string_view{ "/Users/hannes/Documents/temp/supermario.nes" };
} // namespace
auto application::display_proxy::switch_buffers() -> void
{
if (screen)
{
auto r = renderer{ base };
screen->render(r);
}
base.switch_buffers();
}
application::application(display& display, input_device_keyboard& keyboard)
: display_{ display }
, input_manager_{ keyboard }
, screen_title_{ input_manager_ }
{
display_.screen = &screen_title_;
}
auto application::frame(std::chrono::microseconds const elapsed_time) -> void
{
if (display_.screen)
{
auto const a = display_.screen->process_events();
handle_action(a);
}
if (console_)
{
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(elapsed_time);
if (console_->get_status() != sys::status::success)
{
std::cerr << "Invalid state: " << to_string(console_->get_status()) << std::endl;
std::abort();
}
}
else
{
// Directly let the scene render itself without rendering any gameplay (the scene is responsible for
// clearing the buffer).
display_.switch_buffers();
}
}
auto application::handle_action(action const a) -> void
{
switch (a)
{
case action::none:
{
break;
}
case action::load_game:
{
auto stream = std::ifstream{ file, std::ios::binary };
if (!stream.good())
{
std::cerr << "Unable to open cartridge file!" << std::endl;
std::abort();
}
stream.seekg(0, std::ios::end);
auto const length = stream.tellg();
stream.seekg(0, std::ios::beg);
auto data = std::vector<u8>(static_cast<u32>(length));
stream.read(reinterpret_cast<char*>(data.data()), length);
console_.emplace(display_, data.data(), static_cast<u32>(length));
if (console_->get_status() != sys::status::success)
{
std::cerr << "Unable to load cartridge: " << to_string(console_->get_status()) << std::endl;
std::abort();
}
display_.screen = nullptr;
break;
}
}
}
} // namespace nes::app
|