Compare commits

...

5 Commits

15 changed files with 555 additions and 172 deletions

View File

@ -1,6 +1,7 @@
add_library(gui
"src/bar.c"
"src/button.c"
"src/button_type1.c"
"src/container.c"
"src/gui.c"
"src/label.c"

View File

@ -15,8 +15,27 @@ extern "C"
struct gui_button
{
struct gui_common common;
struct gui_label label;
short w;
enum gui_button_type
{
GUI_BUTTON_TYPE_1,
GUI_BUTTON_TYPE_SPRITE
} type;
union
{
struct
{
short w;
struct gui_label label;
} type1;
struct
{
const struct sprite *s;
} sprite;
} u;
void *arg;
void (*on_pressed)(void *);
};
@ -24,7 +43,7 @@ struct gui_button
UTIL_STATIC_ASSERT(!offsetof(struct gui_button, common),
"unexpected offset for struct gui_button");
void gui_button_init(struct gui_button *b);
void gui_button_init(struct gui_button *b, enum gui_button_type t);
enum
{

View File

@ -20,7 +20,7 @@ struct gui_rounded_rect
UTIL_STATIC_ASSERT(!offsetof(struct gui_rounded_rect, common),
"unexpected offset for struct gui_rounded_rect");
void gui_rounded_rect_init(struct gui_rounded_rect *l);
void gui_rounded_rect_init(struct gui_rounded_rect *r);
enum
{

View File

@ -0,0 +1,22 @@
#ifndef GUI_BUTTON_PRIVATE_H
#define GUI_BUTTON_PRIVATE_H
#include <gui/button.h>
#ifdef __cplusplus
extern "C"
{
#endif
void gui_button_init_type1(struct gui_button *b);
void gui_button_init_sprite(struct gui_button *b);
int gui_button_render_type1(const struct gui_button *b);
int gui_button_render_sprite(const struct gui_button *b);
void gui_button_get_dim_type1(const struct gui_button *b, short *w, short *h);
void gui_button_get_dim_sprite(const struct gui_button *b, short *w, short *h);
#ifdef __cplusplus
}
#endif
#endif /* GUI_BUTTON_PRIVATE_H */

View File

@ -1,5 +1,6 @@
#include <gui.h>
#include <gui/button.h>
#include <gui_button_private.h>
#include <gui_private.h>
#include <camera.h>
#include <gfx.h>
@ -7,98 +8,31 @@
#include <pad.h>
#include <peripheral.h>
struct sprite gui_button_sprites[MAX_GUI_BUTTON_SPRITES];
/* Alias for readability. */
static const struct sprite *const refs = gui_button_sprites;
static int render_left(const struct gui_button *const b,
short *const x, const short y)
{
sprite_get_or_ret(s, -1);
if (sprite_clone(&refs[GUI_BUTTON_LEFT], s))
return -1;
s->x = *x;
s->y = y;
sprite_sort(s);
*x = s->x + s->w;
return 0;
}
static int render_mid(const struct gui_button *const b,
short *const x, const short y)
{
const short mid_w = refs[GUI_BUTTON_MID].w;
const short lw = refs[GUI_BUTTON_LEFT].w;
const short rw = refs[GUI_BUTTON_RIGHT].w;
const short w = b->w - lw - rw;
if (w > 0)
{
const short rem_mid = w > 0 ? w % mid_w : 0;
const short whole_mid = w / mid_w;
const short n_mid = rem_mid ? whole_mid + 1 : whole_mid;
for (struct
{
size_t i;
short x;
} a = {.x = lw};
a.i < n_mid;
a.i++, a.x += mid_w)
{
sprite_get_or_ret(m, -1);
if (sprite_clone(&refs[GUI_BUTTON_MID], m))
return -1;
m->x = *x;
m->y = y;
if (rem_mid && a.i + 1 == n_mid)
m->w = rem_mid;
else
m->w = mid_w;
sprite_sort(m);
*x += m->w;
}
}
else
return -1;
return 0;
}
static int render_right(const struct gui_button *const b,
const short x, const short y)
{
sprite_get_or_ret(s, -1);
if (sprite_clone(&refs[GUI_BUTTON_RIGHT], s))
return -1;
s->x = x;
s->y = y;
sprite_sort(s);
return 0;
}
static int render(const struct gui_common *const g)
{
static int (*const f[])(const struct gui_button *) =
{
[GUI_BUTTON_TYPE_1] = gui_button_render_type1,
/* [GUI_BUTTON_TYPE_SPRITE] = gui_button_render_sprite */
};
const struct gui_button *const b = (const struct gui_button *)g;
short x, y;
gui_coords(&b->common, &x, &y);
return f[b->type](b);
}
if (render_left(b, &x, y)
|| render_mid(b, &x, y)
|| render_right(b, x, y))
return -1;
static void get_dim(const struct gui_common *const g,
short *const w, short *const h)
{
static void (*const f[])(const struct gui_button *, short *, short *) =
{
[GUI_BUTTON_TYPE_1] = gui_button_get_dim_type1,
/* [GUI_BUTTON_TYPE_SPRITE] = gui_button_get_dim_sprite */
};
return 0;
const struct gui_button *const b = (const struct gui_button *)g;
f[b->type](b, w, h);
}
static bool pressed(const struct gui_button *const b,
@ -110,7 +44,7 @@ static bool pressed(const struct gui_button *const b,
switch (p->common.type)
{
case PERIPHERAL_TYPE_PAD:
check = pad_pressed(&p->pad.pad, PAD_KEY_A);
check = pad_justpressed(&p->pad.pad, PAD_KEY_A);
break;
case PERIPHERAL_TYPE_KEYBOARD_MOUSE:
@ -125,16 +59,12 @@ static bool pressed(const struct gui_button *const b,
if (check)
{
short x, y;
struct util_rect d;
gui_coords(&b->common, &x, &y);
const struct util_rect d =
{
.x = x,
.y = y,
.w = b->w,
.h = refs[GUI_BUTTON_LEFT].h
};
get_dim(&b->common, &d.w, &d.h);
d.x = x;
d.y = y;
return cursor_collision(cam, &d);
}
@ -153,16 +83,7 @@ static int update(struct gui_common *const g,
return 0;
}
static void get_dim(const struct gui_common *const g,
short *const w, short *const h)
{
const struct gui_button *const b = (const struct gui_button *)g;
*w = b->w;
*h = refs[GUI_BUTTON_MID].h;
}
void gui_button_init(struct gui_button *const b)
void gui_button_init(struct gui_button *const b, const enum gui_button_type t)
{
static const struct gui_common_cb cb =
{
@ -176,11 +97,16 @@ void gui_button_init(struct gui_button *const b)
.common =
{
.cb = &cb
}
},
.type = t
};
gui_label_init(&b->label);
b->label.common.hcentered = true;
b->label.common.vcentered = true;
gui_add_child(&b->common, &b->label.common);
static void (*const f[])(struct gui_button *) =
{
[GUI_BUTTON_TYPE_1] = gui_button_init_type1,
/* [GUI_BUTTON_TYPE_SPRITE] = gui_button_get_dim_sprite */
};
f[b->type](b);
}

114
src/gui/src/button_type1.c Normal file
View File

@ -0,0 +1,114 @@
#include <gui.h>
#include <gui/button.h>
#include <gui_private.h>
#include <gui_button_private.h>
struct sprite gui_button_sprites[MAX_GUI_BUTTON_SPRITES];
/* Alias for readability. */
static const struct sprite *const refs = gui_button_sprites;
static int render_left(const struct gui_button *const b,
short *const x, const short y)
{
sprite_get_or_ret(s, -1);
if (sprite_clone(&refs[GUI_BUTTON_LEFT], s))
return -1;
s->x = *x;
s->y = y;
sprite_sort(s);
*x = s->x + s->w;
return 0;
}
static int render_mid(const struct gui_button *const b,
short *const x, const short y)
{
const short mid_w = refs[GUI_BUTTON_MID].w,
lw = refs[GUI_BUTTON_LEFT].w,
rw = refs[GUI_BUTTON_RIGHT].w,
w = b->u.type1.w - lw - rw;
if (w > 0)
{
const short rem_mid = w > 0 ? w % mid_w : 0,
whole_mid = w / mid_w,
n_mid = rem_mid ? whole_mid + 1 : whole_mid;
for (struct
{
size_t i;
short x;
} a = {.x = lw};
a.i < n_mid;
a.i++, a.x += mid_w)
{
sprite_get_or_ret(m, -1);
if (sprite_clone(&refs[GUI_BUTTON_MID], m))
return -1;
m->x = *x;
m->y = y;
if (rem_mid && a.i + 1 == n_mid)
m->w = rem_mid;
else
m->w = mid_w;
sprite_sort(m);
*x += m->w;
}
}
else
return -1;
return 0;
}
static int render_right(const struct gui_button *const b,
const short x, const short y)
{
sprite_get_or_ret(s, -1);
if (sprite_clone(&refs[GUI_BUTTON_RIGHT], s))
return -1;
s->x = x;
s->y = y;
sprite_sort(s);
return 0;
}
int gui_button_render_type1(const struct gui_button *const b)
{
short x, y;
gui_coords(&b->common, &x, &y);
if (render_left(b, &x, y)
|| render_mid(b, &x, y)
|| render_right(b, x, y))
return -1;
return 0;
}
void gui_button_get_dim_type1(const struct gui_button *const b,
short *const w, short *const h)
{
*w = b->u.type1.w;
*h = refs[GUI_BUTTON_MID].h;
}
void gui_button_init_type1(struct gui_button *const b)
{
struct gui_label *const l = &b->u.type1.label;
gui_label_init(l);
l->common.hcentered = true;
l->common.vcentered = true;
gui_add_child(&b->common, &l->common);
}

View File

@ -1,5 +1,8 @@
add_library(menu
"src/gamecfg_menu.c"
"src/hostjoin_menu.c"
"src/menu.c"
"src/main_menu.c"
)
target_include_directories(menu PUBLIC "inc")
target_include_directories(menu PUBLIC "inc" PRIVATE "privinc")
target_link_libraries(menu PRIVATE camera game gfx gui system)

View File

@ -0,0 +1,32 @@
#ifndef MENU_PRIVATE_H
#define MENU_PRIVATE_H
#include <camera.h>
#include <peripheral.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct menu_common
{
struct camera cam;
union peripheral p;
};
int menu_update(struct menu_common *c,
int (*update)(struct menu_common *, void *),
int (*render)(const struct menu_common *, void *),
void *arg);
int menu_main(struct menu_common *c);
int menu_hostjoin(struct menu_common *c, bool *back);
int menu_gamecfg(struct menu_common *c);
void menu_on_pressed(void *arg);
#ifdef __cplusplus
}
#endif
#endif /* MENU_PRIVATE_H */

View File

@ -0,0 +1,88 @@
#include <menu.h>
#include <menu_private.h>
#include <game.h>
#include <gui.h>
#include <gui/button.h>
#include <gui/container.h>
#include <gui/rounded_rect.h>
#include <stdbool.h>
#include <stddef.h>
struct gamecfg_menu
{
struct gui_container cnt, bcnt;
struct gui_rounded_rect r;
struct gui_button start, back;
};
static int update(struct menu_common *const c, void *const arg)
{
struct gamecfg_menu *const m = arg;
m->bcnt.common.y = screen_h - 40;
if (gui_update(&m->bcnt.common, &c->p, &c->cam))
return -1;
return 0;
}
static int render(const struct menu_common *const c, void *const arg)
{
const struct gamecfg_menu *const m = arg;
if (gui_render(&m->cnt.common)
|| gui_render(&m->bcnt.common))
return -1;
return 0;
}
int menu_gamecfg(struct menu_common *const c)
{
struct gamecfg_menu m;
bool start = false, back = false;
gui_container_init(&m.cnt);
m.cnt.common.hcentered = true;
m.cnt.common.vcentered = true;
gui_rounded_rect_init(&m.r);
m.r.w = screen_w / 2;
m.r.h = screen_h / 2;
gui_add_child(&m.cnt.common, &m.r.common);
gui_container_init(&m.bcnt);
m.bcnt.common.hcentered = true;
m.bcnt.mode = GUI_CONTAINER_MODE_H;
m.bcnt.spacing = 8;
gui_button_init(&m.start, GUI_BUTTON_TYPE_1);
m.start.u.type1.label.text = "Start";
m.start.u.type1.w = 100;
m.start.common.vcentered = true;
m.start.on_pressed = menu_on_pressed;
m.start.arg = &start;
m.back.common.vcentered = true;
gui_add_child(&m.bcnt.common, &m.start.common);
gui_button_init(&m.back, GUI_BUTTON_TYPE_1);
m.back.u.type1.label.text = "Back";
m.back.u.type1.w = 100;
m.back.common.vcentered = true;
m.back.on_pressed = menu_on_pressed;
m.back.arg = &back;
m.back.common.vcentered = true;
gui_add_child(&m.bcnt.common, &m.back.common);
while (!back && !c->p.common.exit && !start)
{
if (menu_update(c, update, render, &m))
return -1;
}
if (start)
return game(NULL);
return 0;
}

View File

@ -0,0 +1,85 @@
#include <menu.h>
#include <menu_private.h>
#include <game.h>
#include <gui.h>
#include <gui/button.h>
#include <gui/container.h>
#include <stdbool.h>
struct menu_hostjoin
{
struct gui_container cnt;
struct gui_button host, join, back;
};
static int update(struct menu_common *const c, void *const arg)
{
struct menu_hostjoin *const m = arg;
if (gui_update(&m->cnt.common, &c->p, &c->cam))
return -1;
return 0;
}
static int render(const struct menu_common *const c, void *const arg)
{
const struct menu_hostjoin *const m = arg;
if (gui_render(&m->cnt.common))
return -1;
return 0;
}
int menu_hostjoin(struct menu_common *const c, bool *const back)
{
do
{
struct menu_hostjoin m;
bool host = false, join = false;
gui_container_init(&m.cnt);
m.cnt.common.hcentered = true;
m.cnt.common.vcentered = true;
m.cnt.spacing = 4;
m.cnt.mode = GUI_CONTAINER_MODE_V;
gui_button_init(&m.host, GUI_BUTTON_TYPE_1);
m.host.u.type1.label.text = "Host game";
m.host.common.hcentered = true;
m.host.u.type1.w = 140;
m.host.arg = &host;
m.host.on_pressed = menu_on_pressed;
gui_add_child(&m.cnt.common, &m.host.common);
gui_button_init(&m.join, GUI_BUTTON_TYPE_1);
m.join.u.type1.label.text = "Join game";
m.join.common.hcentered = true;
m.join.u.type1.w = 140;
m.join.arg = &join;
m.join.on_pressed = menu_on_pressed;
gui_add_child(&m.cnt.common, &m.join.common);
gui_button_init(&m.back, GUI_BUTTON_TYPE_1);
m.back.u.type1.label.text = "Back";
m.back.common.hcentered = true;
m.back.u.type1.w = 140;
m.back.arg = back;
m.back.on_pressed = menu_on_pressed;
gui_add_child(&m.cnt.common, &m.back.common);
while (!*back && !c->p.common.exit && !host && !join)
{
if (menu_update(c, update, render, &m))
return -1;
}
if (host || join)
if (menu_gamecfg(c))
return -1;
} while (!*back && !c->p.common.exit);
return 0;
}

87
src/menu/src/main_menu.c Normal file
View File

@ -0,0 +1,87 @@
#include <menu.h>
#include <menu_private.h>
#include <gui.h>
#include <gui/button.h>
#include <gui/container.h>
#include <gui/rounded_rect.h>
#include <system.h>
#include <stdbool.h>
struct main_menu
{
bool start, exit;
struct gui_button play, exit_btn;
struct gui_container cnt;
};
static int update(struct menu_common *const c, void *const arg)
{
struct main_menu *const m = arg;
if (gui_update(&m->play.common, &c->p, &c->cam))
return -1;
return 0;
}
static int render(const struct menu_common *const c, void *const arg)
{
const struct main_menu *const m = arg;
if (gui_render(&m->cnt.common))
return -1;
return 0;
}
int menu_main(struct menu_common *const c)
{
bool back;
do
{
struct main_menu m = {0};
back = false;
gui_container_init(&m.cnt);
m.cnt.mode = GUI_CONTAINER_MODE_V;
m.cnt.common.hcentered = true;
m.cnt.common.vcentered = true;
m.cnt.spacing = 4;
gui_button_init(&m.play, GUI_BUTTON_TYPE_1);
m.play.on_pressed = menu_on_pressed;
m.play.arg = &m.start;
m.play.u.type1.w = 140;
m.play.common.hcentered = true;
m.play.u.type1.label.text = "Play";
gui_add_child(&m.cnt.common, &m.play.common);
if (system_can_exit())
{
gui_button_init(&m.exit_btn, GUI_BUTTON_TYPE_1);
m.exit_btn.arg = &m.exit;
m.exit_btn.u.type1.w = 140;
m.exit_btn.common.hcentered = true;
m.exit_btn.u.type1.label.text = "Exit";
m.exit_btn.on_pressed = menu_on_pressed;
gui_add_child(&m.cnt.common, &m.exit_btn.common);
}
while (!m.start)
{
if (menu_update(c, update, render, &m))
return -1;
if (m.exit)
return 0;
}
if (menu_hostjoin(c, &back))
return -1;
} while (back && !c->p.common.exit);
return 0;
}

View File

@ -1,82 +1,75 @@
#include <menu.h>
#include <menu_private.h>
#include <camera.h>
#include <game.h>
#include <gfx.h>
#include <gui.h>
#include <gui/button.h>
#include <gui/container.h>
#include <peripheral.h>
#include <system.h>
#include <stdbool.h>
static void on_pressed(void *const arg)
void menu_on_pressed(void *const arg)
{
*(bool *)arg = true;
}
int menu(void)
int menu_update(struct menu_common *const c,
int (*update)(struct menu_common *, void *),
int (*render)(const struct menu_common *, void *),
void *const arg)
{
struct camera cam = {0};
struct gui_button play, exit_btn;
struct gui_container cnt;
union peripheral p;
bool start = false, exit = false;
system_loop();
peripheral_update(&c->p);
camera_update(&c->cam, &c->p);
if (game_resinit())
if (update && update(c, arg))
return -1;
cursor_init(&cam.cursor);
gui_container_init(&cnt);
cnt.mode = GUI_CONTAINER_MODE_V;
cnt.common.hcentered = true;
cnt.common.vcentered = true;
gui_button_init(&play);
gui_button_init(&exit_btn);
play.on_pressed = on_pressed;
play.arg = &start;
play.w = 140;
play.common.hcentered = true;
play.label.text = "Play";
rect_get_or_ret(r, -1);
rect_init(r);
r->w = screen_w;
r->h = screen_h;
rect_sort(r);
exit_btn.arg = &exit;
exit_btn.w = 140;
exit_btn.common.hcentered = true;
exit_btn.label.text = "Exit";
exit_btn.on_pressed = on_pressed;
gui_add_child(&cnt.common, &play.common);
gui_add_child(&cnt.common, &exit_btn.common);
if (render && render(c, arg))
return -1;
switch (c->p.common.type)
{
const struct peripheral_cfg cfg =
{
.type = PERIPHERAL_TYPE_KEYBOARD_MOUSE
};
case PERIPHERAL_TYPE_PAD:
/* Fall through. */
case PERIPHERAL_TYPE_KEYBOARD_MOUSE:
if (cursor_render(&c->cam.cursor))
return -1;
peripheral_init(&cfg, &p);
}
break;
while (!start)
{
system_loop();
peripheral_update(&p);
camera_update(&cam, &p);
case PERIPHERAL_TYPE_TOUCH:
break;
if (gui_update(&play.common, &p, &cam))
return -1;
rect_get_or_ret(r, -1);
rect_init(r);
r->w = screen_w;
r->h = screen_h;
rect_sort(r);
if (p.common.exit || exit)
return 0;
if (gui_render(&cnt.common)
|| cursor_render(&cam.cursor)
|| gfx_draw())
default:
return -1;
}
return game(NULL);
if (gfx_draw())
return -1;
return 0;
}
int menu(void)
{
const struct peripheral_cfg cfg =
{
.type = PERIPHERAL_TYPE_KEYBOARD_MOUSE
};
struct menu_common c = {0};
if (game_resinit())
return -1;
cursor_init(&c.cam.cursor);
peripheral_init(&cfg, &c.p);
return menu_main(&c);
}

View File

@ -1,6 +1,8 @@
#ifndef SYSTEM_H
#define SYSTEM_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
@ -9,6 +11,7 @@ extern "C"
int system_init(void);
void system_deinit(void);
void system_loop(void);
bool system_can_exit(void);
#ifdef __cplusplus
}

View File

@ -6,6 +6,11 @@
volatile bool vblank_set;
bool system_can_exit(void)
{
return false;
}
static void vblank(void *const arg)
{
vblank_set = true;

View File

@ -5,6 +5,11 @@
#include <stdio.h>
#include <stdlib.h>
bool system_can_exit(void)
{
return true;
}
void system_loop(void)
{
SDL_PumpEvents();