Support selecting a menu and terminate execution if so

This commit is contained in:
jaby 2024-01-03 16:31:43 -06:00
parent f024cba8c2
commit f2183b3a99
3 changed files with 71 additions and 23 deletions

View File

@ -21,38 +21,78 @@ static const Menu::SimpleMenu::Entry MenuEntries[] = {
// Do we want Paco to be HERE?!
static object::Paco paco;
static Menu::SimpleMenu menu;
static bool enter_loading = false;
static void setup() {
Assets::Main::load();
FontWriter::setup();
paco.setup();
menu.setup(MenuEntries);
menu.setup([](uint32_t selection) {
enter_loading = true;
},MenuEntries);
}
static void update() {
static const char Title[] = "Pool Box";
static constexpr auto TitleLength = (sizeof(Title) - 1)*DefaultFont::Info.font_size.width;
namespace NormalScene {
static void update() {
static const char Title[] = "Pool Box";
static constexpr auto TitleLength = (sizeof(Title) - 1)*DefaultFont::Info.font_size.width;
Periphery::query_controller();
auto cursor = FontWriter::update(JabyEngine::Make::PositionI16((GPU::Display::Width-TitleLength)/2, 16));
paco.update();
Periphery::query_controller();
auto cursor = FontWriter::update(JabyEngine::Make::PositionI16((GPU::Display::Width-TitleLength)/2, 16));
paco.update();
FontWriter::new_font_writer.write(cursor, Title, GPU::Color24::Yellow(), &FontWriter::wiggle);
menu.update(FontWriter::bios_font_writer, cursor, Make::PositionI16(8, 64));
FontWriter::new_font_writer.write(cursor, Title, GPU::Color24::Yellow(), &FontWriter::wiggle);
menu.update(FontWriter::bios_font_writer, cursor, Make::PositionI16(8, 64));
}
static void render() {
FontWriter::new_font_writer.render();
FontWriter::bios_font_writer.render();
paco.render();
}
static void run() {
update();
GPU::swap_buffers_vsync(1);
render();
}
}
static void render() {
FontWriter::new_font_writer.render();
FontWriter::bios_font_writer.render();
paco.render();
namespace LoadingScene {
static void update() {
static const char Title[] = "Loading...";
static constexpr auto TitleLength = (sizeof(Title) - 1)*DefaultFont::Info.font_size.width;
auto cursor = FontWriter::update(JabyEngine::Make::PositionI16((GPU::Display::Width-TitleLength)/2, (GPU::Display::Height-16)/2));
FontWriter::new_font_writer.write(cursor, Title, GPU::Color24::Blue());
}
static void render() {
FontWriter::new_font_writer.render();
}
static void run() {
enter_loading = false;
update();
GPU::swap_buffers_vsync(1);
render();
GPU::swap_buffers_vsync(1);
printf("End of execution!\n");
while(true);
}
}
void main() {
setup();
while(true) {
update();
GPU::swap_buffers_vsync(1);
render();
if(enter_loading) {
LoadingScene::run();
}
else {
NormalScene::run();
}
}
}

View File

@ -11,17 +11,20 @@ namespace Menu {
const char* name;
};
typedef void (*Callback)(uint32_t selection);
private:
Callback selection_callback;
const Entry* entries;
size_t size;
uint8_t cur_selection;
public:
void setup(const Entry* entries, size_t size);
void setup(Callback callback, const Entry* entries, size_t size);
template<size_t N>
void setup(const Entry (&entries)[N]) {
SimpleMenu::setup(entries, N);
void setup(Callback callback, const Entry (&entries)[N]) {
SimpleMenu::setup(callback, entries, N);
}
void update(JabyEngine::FontWriter& font_writer, State& cursor, const GPU::PositionI16& start);

View File

@ -2,10 +2,11 @@
#include <PSX/Periphery/periphery.hpp>
namespace Menu {
void SimpleMenu :: setup(const Entry* entries, size_t size) {
this->entries = entries;
this->size = size;
this->cur_selection = 0;
void SimpleMenu :: setup(Callback callback, const Entry* entries, size_t size) {
this->selection_callback = callback;
this->entries = entries;
this->size = size;
this->cur_selection = 0;
}
void SimpleMenu :: update(JabyEngine::FontWriter& font_writer, State& cursor, const GPU::PositionI16& start) {
@ -20,6 +21,10 @@ namespace Menu {
this->cur_selection += 1;
}
if(controller.button.went_down(DigitalButton::Cross)) {
this->selection_callback(this->cur_selection);
}
cursor.pos = Make::PositionI16(8, 64);
for(size_t n = 0; n < this->size; n++) {
const auto& cur_entry = this->entries[n];