[backend] design a little different

This commit is contained in:
Milo Turner 2020-02-19 12:15:46 -05:00
parent 174e57e51d
commit 932e7fc708
2 changed files with 66 additions and 32 deletions

View File

@ -2,9 +2,12 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
struct rgn;
struct msgq;
struct ax_backend;
struct ax_theme;
extern const char* ax__backend_impl_name;
@ -13,7 +16,20 @@ int ax__backend_new(
struct ax_backend** out_bac,
const char** out_err);
void ax__backend_enqueue_font(
struct ax_backend* bac,
const char* font_path,
size_t font_size);
enum {
ax_msg_shutdown_TAG,
ax_msg_load_font_TAG,
};
struct ax_msg_shutdown {};
struct ax_msg_load_font {
struct ax_theme* theme;
int category;
};
struct msgq* ax__backend_msgq(struct ax_backend*);
void ax__backend_wait_for_event(struct ax_backend*);
void ax__backend_step(struct ax_backend*, bool* out_shutdown);

View File

@ -1,32 +1,24 @@
#include "../backend.h"
#include "../util/region.h"
#include "../concurrent/msgq.h"
#include "../ctxt/theme.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
/* --- Backend data type --- */
/* -----------------------------------------------------------------------------
* Backend type
* -------------------------------------------------------------------------- */
const char* ax__backend_impl_name = "SDL";
struct ax_backend {
struct msgq* mq;
bool shutdown;
};
/* --- Async messages --- */
enum {
msg_close_TAG,
msg_load_font_TAG,
};
struct msg_close {};
struct msg_load_font {
const char* path;
size_t size;
};
/* --- Functions --- */
/* -----------------------------------------------------------------------------
* Backend functions
* -------------------------------------------------------------------------- */
static void backend_cleanup(struct ax_backend* bac);
@ -49,6 +41,7 @@ int ax__backend_new(
rgn_pin(init_rgn, bac, (void*) backend_cleanup);
bac->mq = msgq_new(init_rgn);
bac->shutdown = false;
if (out_bac != NULL) {
*out_bac = bac;
@ -64,21 +57,46 @@ fail:
static void backend_cleanup(struct ax_backend* bac)
{
msgq_begin_send_typed(bac->mq, msg_close);
msgq_end_send(bac->mq);
// printf("bye, from SDL\n");
TTF_Quit();
SDL_Quit();
}
void ax__backend_enqueue_font(
struct ax_backend* bac,
const char* font_path,
size_t font_size)
struct msgq* ax__backend_msgq(struct ax_backend* bac)
{
struct msg_load_font* m = msgq_begin_send_typed(bac->mq, msg_load_font);
m->path = rstrdup(&bac->mq->rgn, font_path);
m->size = font_size;
msgq_end_send(bac->mq);
return bac->mq;
}
void ax__backend_wait_for_event(struct ax_backend* bac)
{
(void) bac;
}
void ax__backend_step(struct ax_backend* bac, bool* out_shutdown)
{
int type;
void* data;
msgq_begin_recv(bac->mq);
while ((data = msgq_recv1(bac->mq, &type)) != NULL) {
switch (type) {
case ax_msg_shutdown_TAG:
bac->shutdown = true;
break;
case ax_msg_load_font_TAG: {
struct ax_msg_load_font* m = data;
printf("TODO: load font `%s' (size %zu)\n",
m->theme->font_path[m->category],
m->theme->font_size[m->category]);
break;
}
default: break;
}
}
msgq_end_recv(bac->mq);
if (out_shutdown != NULL) {
*out_shutdown = bac->shutdown;
}
}