summaryrefslogtreecommitdiff
path: root/lib/nes/app/application.cc
blob: 6b7253e3ce88fc6461ebb9cdb6b8a8ed3002d953 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#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 <iostream>

namespace nes::app
{
	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, file_browser& file_browser)
		: display_{ display }
		, input_manager_{ keyboard }
		, file_browser_{ file_browser }
		, screen_title_{ keyboard }
		, screen_browser_{ keyboard, file_browser }
	{
		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() != 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.get_type())
		{
			case action::type::none:
			{
				break;
			}
			case action::type::go_to_title:
			{
				console_.clear();
				display_.screen = &screen_title_;
				break;
			}
			case action::type::go_to_browser:
			{
				console_.clear();
				display_.screen = &screen_browser_;
				break;
			}
			case action::type::go_to_settings:
			{
				// TODO
				break;
			}
			case action::type::go_to_help:
			{
				// TODO
				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)
				{
					std::cerr << "Unable to open cartridge file: " << to_string(console_->get_status()) << std::endl;
					std::abort();
				}

				console_.emplace(display_, span{ buffer, length });
				if (console_->get_status() != status::success)
				{
					std::cerr << "Unable to load cartridge: " << to_string(console_->get_status()) << std::endl;
					std::abort();
				}

				display_.screen = nullptr;
				break;
			}
			case action::type::show_error:
			{
				// TODO
				break;
			}
		}
	}
} // namespace nes::app