[backend,theme] load fonts and store font handles in theme object

This commit is contained in:
Milo Turner 2020-02-20 13:17:47 -05:00
parent 1dd6d858c2
commit c3d7804279
4 changed files with 76 additions and 21 deletions

View File

@ -9,6 +9,8 @@ struct msgq;
struct ax_backend;
struct ax_theme;
struct ax_font_h;
extern const char* ax__backend_impl_name;
extern size_t ax__backend_desired_region_size;

View File

@ -12,10 +12,11 @@
* -------------------------------------------------------------------------- */
const char* ax__backend_impl_name = "SDL";
size_t ax__backend_desired_region_size = SMALL;
size_t ax__backend_desired_region_size = MEDIUM;
struct ax_backend {
struct msgq* mq;
struct rgn* rgn;
struct msgq* inbox;
bool shutdown;
};
@ -43,7 +44,8 @@ int ax__backend_new(
struct ax_backend* bac = ralloc_typed(init_rgn, struct ax_backend, 1);
rgn_pin(init_rgn, bac, (void*) backend_cleanup);
bac->mq = msgq_new(init_rgn);
bac->rgn = init_rgn;
bac->inbox = msgq_new(init_rgn);
bac->shutdown = false;
if (out_bac != NULL) {
@ -68,7 +70,7 @@ static void backend_cleanup(struct ax_backend* bac)
struct msgq* ax__backend_msgq(struct ax_backend* bac)
{
return bac->mq;
return bac->inbox;
}
bool ax__backend_is_shutdown(struct ax_backend* bac)
@ -82,11 +84,29 @@ void ax__backend_wait_for_event(struct ax_backend* bac)
usleep(1000 * 1000);
}
/* -----------------------------------------------------------------------------
* Event handling
* -------------------------------------------------------------------------- */
struct font_list {
struct font_list* next;
struct ax_theme* thm;
size_t cat;
struct msgq* cb_mq;
};
static void load_fonts(
struct ax_backend* bac, struct font_list* fonts);
void ax__backend_step(struct ax_backend* bac)
{
msgq_begin_recv(bac->mq);
struct rgn tmp_rgn;
rgn_init(&tmp_rgn, SMALL);
struct font_list* fonts = NULL;
MSGQ_RECV_ALL(bac->mq) {
msgq_begin_recv(bac->inbox);
MSGQ_RECV_ALL(bac->inbox) {
ON(ax_msg_shutdown,
{
@ -97,22 +117,53 @@ void ax__backend_step(struct ax_backend* bac)
ON(ax_msg_load_font,
{
printf("TODO: load font `%s' (size %zu)\n",
m.theme->font_path[m.category],
m.theme->font_size[m.category]);
m.theme->font_loaded[m.category] = true;
if (ax__theme_is_loaded(m.theme)) {
struct ax_msg_theme_loaded* res =
msgq_begin_send_typed(m.callback_mq, ax_msg_theme_loaded);
res->theme = m.theme;
msgq_end_send(m.callback_mq);
}
struct font_list* l =
ralloc_typed(&tmp_rgn, struct font_list, 1);
l->next = fonts;
l->thm = m.theme;
l->cat = m.category;
l->cb_mq = m.callback_mq;
fonts = l;
break;
});
default: break;
}
msgq_end_recv(bac->mq);
msgq_end_recv(bac->inbox);
load_fonts(bac, fonts);
rgn_cleanup(&tmp_rgn);
}
/* -----------------------------------------------------------------------------
* Fonts
* -------------------------------------------------------------------------- */
struct ax_font_h {
TTF_Font* font;
};
static void load_fonts(
struct ax_backend* bac,
struct font_list* fl)
{
for (; fl != NULL; fl = fl->next) {
struct ax_theme* thm = fl->thm;
struct msgq* cb_mq = fl->cb_mq;
const char* path = thm->font_path[fl->cat];
size_t size = thm->font_size[fl->cat];
struct ax_font_h* handle = ralloc_typed(bac->rgn, struct ax_font_h, 1);
handle->font = TTF_OpenFont(path, size);
thm->font_handle[fl->cat] = handle;
if (ax__theme_is_loaded(thm)) {
struct ax_msg_theme_loaded* m =
msgq_begin_send_typed(cb_mq, ax_msg_theme_loaded);
m->theme = thm;
msgq_end_send(cb_mq);
}
}
}

View File

@ -29,7 +29,7 @@ static void theme_init(struct ax_theme* thm)
for (size_t i = 0; i < AX_FONT__COUNT; i++) {
thm->font_path[i] = NULL;
thm->font_loaded[i] = false;
thm->font_handle[i] = NULL;
}
thm->font_size[AX_FONT_H1] = 96;

View File

@ -34,12 +34,14 @@ enum ax_font_cat {
AX_FONT__DEFAULT,
};
struct ax_font_h; // defined by backend
struct ax_theme {
const char* fatal_err;
int64_t colors[AX_COLOR__COUNT];
const char* font_path[AX_FONT__COUNT];
size_t font_size[AX_FONT__COUNT];
bool font_loaded[AX_FONT__COUNT];
struct ax_font_h* font_handle[AX_FONT__COUNT];
};
struct ax_theme_builder {
@ -82,7 +84,7 @@ static inline bool ax__theme_is_loaded(
struct ax_theme* thm)
{
for (size_t i = 0; i < AX_FONT__COUNT; i++) {
if (!thm->font_loaded[i]) {
if (thm->font_handle[i] == NULL) {
return false;
}
}