[sdl] sdl backend stub (runs SDL_Init and friends)

This commit is contained in:
Milo Turner 2020-02-18 16:19:32 -05:00
parent 344d5ca059
commit ea96f494ca
5 changed files with 51 additions and 29 deletions

View File

@ -1,5 +1,7 @@
cc = clang
sdl_ldflags = $(shell pkg-config SDL2_ttf sdl2 --libs)
cflags = -g -O0 -pthread \
-Wall \
-Wshadow \
@ -25,6 +27,6 @@ gen:
racket scripts/enum.rkt src _build
${test_bin}: ${srcs} ${hdrs}
${cc} ${cflags} ${srcs} -o ${test_bin}
${cc} ${cflags} ${srcs} ${sdl_ldflags} -o ${test_bin}
.PHONY: all clean gen

View File

@ -10,8 +10,10 @@ struct ax_font_h;
extern const char* ax__backend_impl_name;
struct ax_backend* ax__backend_new(
struct rgn* init_rgn);
int ax__backend_new(
struct rgn* init_rgn,
struct ax_backend** out_bac,
const char** out_err);
const char* ax__backend_get_error(
struct ax_backend* bac);

View File

@ -1,20 +1,53 @@
#include "../backend.h"
#include "../util/region.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
struct ax_backend {
};
const char* ax__backend_impl_name = "SDL";
struct ax_backend* ax__backend_new(
struct rgn* init_rgn)
static void shutdown_backend(struct ax_backend* bac);
int ax__backend_new(
struct rgn* init_rgn,
struct ax_backend** out_bac,
const char** out_err)
{
const char* err;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
err = SDL_GetError();
goto fail;
}
if (TTF_Init() != 0) {
err = TTF_GetError();
goto fail;
}
struct ax_backend* bac = ralloc_typed(init_rgn, struct ax_backend, 1);
return bac;
rgn_pin(init_rgn, bac, (void*) shutdown_backend);
if (out_bac != NULL) {
*out_bac = bac;
}
return 0;
fail:
if (out_err != NULL) {
*out_err = err;
}
return 1;
}
const char* ax__backend_get_error(
struct ax_backend* bac)
static void shutdown_backend(struct ax_backend* bac)
{
// printf("bye, from SDL\n");
TTF_Quit();
SDL_Quit();
}
const char* ax__backend_get_error(struct ax_backend* bac)
{
(void) bac;
return NULL;

View File

@ -43,5 +43,3 @@ static inline void ax__set_error(struct ax_ctxt* ax, const char* text)
{
strcpy(ax__make_error_buf(ax, strlen(text) + 1), text);
}
struct ax_backend* ax__backend(struct ax_ctxt* ax);

View File

@ -49,7 +49,12 @@ void ax__ctxt_init(struct ax_ctxt* ax, struct rgn* init_rgn)
ax->thmb = NULL;
ax->sel_theme = NULL;
ax->bac = NULL;
const char* err;
if (ax__backend_new(init_rgn, &ax->bac, &err) != 0) {
ASSERT(0, "initializing %s backend failed: %s",
ax__backend_impl_name,
err);
}
}
void ax__ctxt_cleanup(struct ax_ctxt* ax)
@ -58,20 +63,6 @@ void ax__ctxt_cleanup(struct ax_ctxt* ax)
rgn_cleanup(&ax->err_rgn);
}
struct ax_backend* ax__backend(struct ax_ctxt* ax)
{
if (ax->bac == NULL) {
struct rgn msg_rgn;
rgn_init(&msg_rgn, SMALL);
ax_log(ax, rsprintf(&msg_rgn, "initializing `%s' backend",
ax__backend_impl_name));
rgn_cleanup(&msg_rgn);
ax->bac = ax__backend_new(ax->init_rgn);
}
return ax->bac;
}
/* -----------------------------------------------------------------------------
* API functions :: logging
* -------------------------------------------------------------------------- */
@ -106,13 +97,9 @@ void ax_begin_theme(struct ax_ctxt* ax)
void ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm)
{
ASSERT(ax->thmb != NULL, "`ax_end_theme' called while not building a theme");
struct ax_theme* thm;
ax__theme_builder_finish(ax->thmb, ax->init_rgn, &thm);
rgn_clear(&ax->thmb_rgn);
(void) ax__backend(ax); // TODO: tell backend to load this theme
if (out_thm != NULL) {
*out_thm = thm;
}