aboutsummaryrefslogtreecommitdiff
path: root/menu.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2023-05-06 09:02:04 +1000
committerquou <quou@disroot.org>2023-05-06 09:02:04 +1000
commit2ab411c4b8855d11d48454a93262e8eae3ba7fc7 (patch)
treee608ebd0bea71570be0a3619f9848f975669e5ef /menu.c
parentfb104368dd33b66e0575dcc0327cbae7046a4e1e (diff)
Menus, game over, dying, etc.
Diffstat (limited to 'menu.c')
-rw-r--r--menu.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/menu.c b/menu.c
new file mode 100644
index 0000000..e4e82f5
--- /dev/null
+++ b/menu.c
@@ -0,0 +1,86 @@
+#include "asset.h"
+#include "input.h"
+#include "menu.h"
+
+#ifdef DEBUG
+#include "error.h"
+#include "platform.h"
+#endif
+
+void init_menu(Menu* menu, void* ptr) {
+ menu->el_count = 0;
+ menu->selected = 0;
+ menu->y = renderer_h / 2;
+ menu->h = 0;
+ menu->ptr = ptr;
+}
+
+void menu_add(
+ Menu* menu,
+ const char* text,
+ Menu_Event on_select,
+ const BM_Font* font
+) {
+ Menu_Item* it;
+ int i;
+
+#ifdef DEBUG
+ if (menu->el_count >= menu_max_elements) {
+ platform_err("Too many menu elements.\n");
+ platform_abort(error_gameplay_error);
+ }
+#endif
+
+ it = &menu->els[menu->el_count++];
+ it->on_select = on_select;
+ it->text_len = 0;
+ for (i = 0; text[i]; i++) {
+ it->text[i] = text[i];
+ it->text_len++;
+ } it->text[i] = 0;
+
+ menu->h = font->char_h * menu->el_count;
+ menu->y = renderer_h / 2 - (menu->h) / 2;
+}
+
+void update_menu(Menu* menu) {
+ if (button_just_pressed(btn_dpad_down)) {
+ menu->selected++;
+ if (menu->selected >= menu->el_count) {
+ menu->selected = 0;
+ }
+ }
+
+ if (button_just_pressed(btn_dpad_up)) {
+ menu->selected--;
+ if (menu->selected < 0) {
+ menu->selected = menu->el_count - 1;
+ }
+ }
+
+ if (
+ button_just_pressed(btn_jump) ||
+ button_just_pressed(btn_shoot)
+ ) {
+ menu->els[menu->selected].on_select(menu);
+ }
+}
+
+void render_menu(Menu* menu, const BM_Font* font) {
+ int i, x, y, cw, ch;
+ const Menu_Item* el;
+
+ y = menu->y;
+ cw = font->char_w;
+ ch = font->char_h;
+
+ for (i = 0; i < menu->el_count; i++) {
+ el = &menu->els[i];
+ x = renderer_w / 2 - (cw * el->text_len) / 2;
+ rfont_text(font, x, y, el->text);
+ if (i == menu->selected) {
+ rfont_char(font, x - cw, y, '>');
+ }
+ y += ch;
+ }
+}