Compare commits

...

5 Commits

10 changed files with 122 additions and 23 deletions

View File

@ -12,6 +12,7 @@ enum font
};
int font_puts(enum font, short x, short y, const char *s);
int font_dim(enum font f, const char *str, short *x, short *y);
extern struct sprite font_sprite;

View File

@ -5,7 +5,9 @@
struct sprite font_sprite;
static int renderstr(const enum font f, const short x, short y, const char *str)
static int renderstr(const enum font f, const short x, short y,
const char *str, const bool render, short *const max_x,
short *const max_y)
{
static const struct cfg
{
@ -26,46 +28,73 @@ static int renderstr(const enum font f, const short x, short y, const char *str)
char c;
short rx = x;
if (max_x)
*max_x = rx;
if (max_y)
*max_y = y + cfg->fh;
while ((c = *str++))
{
if (c == ' ')
{
rx += cfg->fs;
if (max_x && rx >= *max_x)
*max_x = rx;
continue;
}
else if (c == '\n' || c == '\r')
{
rx = x;
y += cfg->fh;
if (max_y)
*max_y = y;
continue;
}
sprite_get_or_ret(s, -1);
if (render)
{
sprite_get_or_ret(s, -1);
if (sprite_clone(cfg->s, s))
return -1;
if (sprite_clone(cfg->s, s))
return -1;
s->w = cfg->fw;
s->h = cfg->fh;
s->w = cfg->fw;
s->h = cfg->fh;
/* Substract non-printable characters (NUL to SP). */
const char ch = c - '!';
const short u = (cfg->fw * ch) % cfg->s->w;
const short v = cfg->fh * ((cfg->fw * ch) / cfg->s->w);
/* Substract non-printable characters (NUL to SP). */
const char ch = c - '!';
const short u = (cfg->fw * ch) % cfg->s->w;
const short v = cfg->fh * ((cfg->fw * ch) / cfg->s->w);
s->u += u;
s->v += v;
s->x = rx;
s->y = y;
sprite_sort(s);
}
s->u += u;
s->v += v;
s->x = rx;
s->y = y;
sprite_sort(s);
rx += cfg->fs;
if (max_x && rx >= *max_x)
*max_x = rx;
}
return 0;
}
int font_dim(const enum font f, const char *const str, short *const x,
short *const y)
{
return renderstr(f, 0, 0, str, false, x, y);
}
int font_puts(const enum font f, const short x, const short y,
const char *const str)
{
return renderstr(f, x, y, str) ? EOF : 0;
return renderstr(f, x, y, str, true, NULL, NULL) ? EOF : 0;
}

View File

@ -15,8 +15,9 @@ struct gui_common
int (*update)(struct gui_common *, const union peripheral *,
const struct camera *);
int (*render)(const struct gui_common *);
void (*get_dim)(const struct gui_common *, short *w, short *h);
short x, y;
bool centered;
bool hcentered, vcentered;
struct gui_common *parent, *child, *sibling;
};

View File

@ -66,6 +66,8 @@ static int render_mid(const struct gui_button *const b,
*x += m->w;
}
}
else
return -1;
return 0;
}
@ -151,12 +153,22 @@ 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)
{
*b = (const struct gui_button)
{
.common =
{
.get_dim = get_dim,
.update = update,
.render = render
}

View File

@ -1,5 +1,33 @@
#include <gui.h>
static void get_centered(const struct gui_common *const g,
short *const x, short *const y)
{
if (g->get_dim)
{
short w, h, pw = 0, ph = 0;
if (g->parent)
{
if (g->parent->get_dim)
g->parent->get_dim(g->parent, &pw, &ph);
}
else
{
pw = screen_w;
ph = screen_h;
}
g->get_dim(g, &w, &h);
if (g->hcentered)
*x += (pw - w) / 2;
if (g->vcentered)
*y += (ph - h) / 2;
}
}
void gui_coords(const struct gui_common *const g, short *const x,
short *const y)
{
@ -8,9 +36,17 @@ void gui_coords(const struct gui_common *const g, short *const x,
for (const struct gui_common *p = g->parent; p; p = p->parent)
{
*x += p->x;
*y += p->y;
short px = p->x, py = p->y;
if (p->hcentered || p->vcentered)
gui_coords(p, &px, &py);
*x += px;
*y += py;
}
if (g->hcentered || g->vcentered)
get_centered(g, x, y);
}
void gui_add_sibling(struct gui_common *const g,

View File

@ -13,12 +13,21 @@ static int render(const struct gui_common *const g)
return l->text ? font_puts(l->font, x, y, l->text) : -1;
}
static void get_dim(const struct gui_common *const g,
short *const w, short *const h)
{
const struct gui_label *const l = (const struct gui_label *)g;
font_dim(l->font, l->text, w, h);
}
void gui_label_init(struct gui_label *const l)
{
*l = (const struct gui_label)
{
.common =
{
.get_dim = get_dim,
.render = render
}
};

View File

@ -212,12 +212,23 @@ static int render(const 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_rounded_rect *const r =
(const struct gui_rounded_rect *)g;
*w = r->w;
*h = r->h;
}
void gui_rounded_rect_init(struct gui_rounded_rect *const r)
{
*r = (const struct gui_rounded_rect)
{
.common =
{
.get_dim = get_dim,
.render = render
}
};

View File

@ -27,6 +27,8 @@ int menu(void)
play.on_pressed = on_pressed;
play.arg = &start;
play.w = 140;
play.common.hcentered = true;
play.common.vcentered = true;
play.label.text = "Play";
{
@ -44,8 +46,6 @@ int menu(void)
peripheral_update(&p);
camera_update(&cam, &p);
play.common.x = screen_w / 2 - play.w / 2;
play.common.y = screen_h / 2 - 20;
play.label.common.x = play.w / 2 - 20;
play.label.common.y = 4;

View File

@ -128,5 +128,5 @@ int terrain_render(const struct terrain_map *const map,
void terrain_init(struct terrain_map *const map)
{
memset(map, 0, sizeof *map);
*map = (const struct terrain_map){0};
}

View File

@ -133,7 +133,7 @@ static void unit_stop(struct unit *const u)
static void target_reset(struct unit *const u)
{
memset(&u->target, 0, sizeof u->target);
u->target = (const struct unit_target){0};
u->state = UNIT_STATE_IDLE_MOVING;
unit_stop(u);
}