Support selecting a menu and terminate execution if so

This commit is contained in:
Jaby 2024-01-03 16:31:43 -06:00
parent 7674d4b55f
commit 59c974ab17
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?! // Do we want Paco to be HERE?!
static object::Paco paco; static object::Paco paco;
static Menu::SimpleMenu menu; static Menu::SimpleMenu menu;
static bool enter_loading = false;
static void setup() { static void setup() {
Assets::Main::load(); Assets::Main::load();
FontWriter::setup(); FontWriter::setup();
paco.setup(); paco.setup();
menu.setup(MenuEntries); menu.setup([](uint32_t selection) {
enter_loading = true;
},MenuEntries);
} }
static void update() { namespace NormalScene {
static const char Title[] = "Pool Box"; static void update() {
static constexpr auto TitleLength = (sizeof(Title) - 1)*DefaultFont::Info.font_size.width; static const char Title[] = "Pool Box";
static constexpr auto TitleLength = (sizeof(Title) - 1)*DefaultFont::Info.font_size.width;
Periphery::query_controller(); Periphery::query_controller();
auto cursor = FontWriter::update(JabyEngine::Make::PositionI16((GPU::Display::Width-TitleLength)/2, 16)); auto cursor = FontWriter::update(JabyEngine::Make::PositionI16((GPU::Display::Width-TitleLength)/2, 16));
paco.update(); paco.update();
FontWriter::new_font_writer.write(cursor, Title, GPU::Color24::Yellow(), &FontWriter::wiggle); FontWriter::new_font_writer.write(cursor, Title, GPU::Color24::Yellow(), &FontWriter::wiggle);
menu.update(FontWriter::bios_font_writer, cursor, Make::PositionI16(8, 64)); 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() { namespace LoadingScene {
FontWriter::new_font_writer.render(); static void update() {
FontWriter::bios_font_writer.render(); static const char Title[] = "Loading...";
paco.render(); 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() { void main() {
setup(); setup();
while(true) { while(true) {
update(); if(enter_loading) {
GPU::swap_buffers_vsync(1); LoadingScene::run();
render(); }
else {
NormalScene::run();
}
} }
} }

View File

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

View File

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