[sdl] blank windows

This commit is contained in:
Milo Turner 2020-02-24 17:48:54 -05:00
parent 46a3e4552a
commit 8302002cb6
3 changed files with 122 additions and 7 deletions

View File

@ -1,5 +1,6 @@
#include "../backend.h" #include "../backend.h"
#include "../ctxt/theme.h" #include "../ctxt/theme.h"
#include "../ctxt/window.h"
#include "../util.h" #include "../util.h"
#include "../util/region.h" #include "../util/region.h"
#include "../concurrent/msgq.h" #include "../concurrent/msgq.h"
@ -12,6 +13,8 @@
* Backend type * Backend type
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
struct window_list;
const char* ax__backend_impl_name = "SDL"; const char* ax__backend_impl_name = "SDL";
size_t ax__backend_desired_region_size = MEDIUM; size_t ax__backend_desired_region_size = MEDIUM;
@ -19,6 +22,7 @@ struct ax_backend {
struct rgn* rgn; struct rgn* rgn;
struct msgq* inbox; struct msgq* inbox;
bool shutdown; bool shutdown;
struct window_list* windows;
}; };
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
@ -48,6 +52,7 @@ int ax__backend_new(
bac->rgn = init_rgn; bac->rgn = init_rgn;
bac->inbox = msgq_new(init_rgn); bac->inbox = msgq_new(init_rgn);
bac->shutdown = false; bac->shutdown = false;
bac->windows = NULL;
if (out_bac != NULL) { if (out_bac != NULL) {
*out_bac = bac; *out_bac = bac;
@ -82,7 +87,7 @@ bool ax__backend_is_shutdown(struct ax_backend* bac)
void ax__backend_wait_for_event(struct ax_backend* bac) void ax__backend_wait_for_event(struct ax_backend* bac)
{ {
(void) bac; (void) bac;
usleep(1000 * 1000); usleep(1000 * 100);
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
@ -95,21 +100,32 @@ struct font_list {
size_t cat; size_t cat;
}; };
static void load_fonts( struct window_args_list {
struct ax_backend* bac, struct font_list* fonts); struct window_args_list* next;
struct ax_window* win;
const char* title;
uint32_t width, height;
};
static void load_fonts(struct ax_backend* bac, struct font_list* fl);
static void make_windows(struct ax_backend* bac, struct window_args_list* wl);
static void render_windows(struct window_list* wl);
void ax__backend_step(struct ax_backend* bac) void ax__backend_step(struct ax_backend* bac)
{ {
struct rgn tmp_rgn; struct rgn tmp_rgn;
rgn_init(&tmp_rgn, SMALL); rgn_init(&tmp_rgn, SMALL);
struct font_list* fonts = NULL; struct font_list* fonts = NULL;
struct window_args_list* windows = NULL;
size_t nmsg = 0;
msgq_begin_recv(bac->inbox); msgq_begin_recv(bac->inbox);
MSGQ_RECV_ALL(bac->inbox) { MSGQ_RECV_ALL(bac->inbox) {
ON(ax_msg_shutdown, ON(ax_msg_shutdown,
{ {
nmsg++;
(void) m; (void) m;
bac->shutdown = true; bac->shutdown = true;
break; break;
@ -117,6 +133,7 @@ void ax__backend_step(struct ax_backend* bac)
ON(ax_msg_load_font, ON(ax_msg_load_font,
{ {
nmsg++;
struct font_list* l = struct font_list* l =
ralloc_typed(&tmp_rgn, struct font_list, 1); ralloc_typed(&tmp_rgn, struct font_list, 1);
l->next = fonts; l->next = fonts;
@ -126,16 +143,33 @@ void ax__backend_step(struct ax_backend* bac)
break; break;
}); });
ON(ax_msg_make_window,
{
nmsg++;
struct window_args_list* l =
ralloc_typed(&tmp_rgn, struct window_args_list, 1);
l->next = windows;
l->win = m.dst_win;
l->title = rstrdup(&tmp_rgn, m.title);
l->width = m.width;
l->height = m.height;
windows = l;
break;
});
default: default:
nmsg++;
printf("Got a weird message: `%s'\n", ax__msg_name(mqr__type)); printf("Got a weird message: `%s'\n", ax__msg_name(mqr__type));
break; break;
} }
msgq_end_recv(bac->inbox); msgq_end_recv(bac->inbox);
load_fonts(bac, fonts); load_fonts(bac, fonts);
make_windows(bac, windows);
render_windows(bac->windows);
rgn_cleanup(&tmp_rgn); rgn_cleanup(&tmp_rgn);
// printf("ping. %zu message(s)\n", nmsg);
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
@ -180,3 +214,86 @@ static void load_fonts(struct ax_backend* bac, struct font_list* fl)
fail: fail:
ASSERT(rv == 0, "error loading font: %s", err); ASSERT(rv == 0, "error loading font: %s", err);
} }
/* -----------------------------------------------------------------------------
* Window management
* -------------------------------------------------------------------------- */
struct window_list {
struct window_list* next;
struct ax_window* win;
struct window_handle* handle;
};
struct window_handle {
SDL_Window* win;
SDL_Renderer* rnd;
};
static void cleanup_window_handle(struct window_handle*);
static int new_window_handle(
struct rgn* rgn,
struct window_args_list* args,
struct window_handle** out_wh,
const char** out_err)
{
struct window_handle* wh = ralloc_typed(rgn, struct window_handle, 1);
wh->win = NULL;
wh->rnd = NULL;
rgn_pin(rgn, wh, (void*) cleanup_window_handle);
int rv;
if ((rv = SDL_CreateWindowAndRenderer((int) args->width,
(int) args->height,
SDL_WINDOW_SHOWN,
&wh->win, &wh->rnd)) != 0) {
*out_err = SDL_GetError();
return rv;
}
SDL_SetWindowTitle(wh->win, args->title);
*out_wh = wh;
return 0;
}
static void cleanup_window_handle(struct window_handle* wh)
{
SDL_DestroyRenderer(wh->rnd);
SDL_DestroyWindow(wh->win);
}
static void make_windows(struct ax_backend* bac, struct window_args_list* args)
{
int rv = 0;
const char* err;
struct window_handle* wh;
struct window_list* wl;
for (; args != NULL; args = args->next) {
if ((rv = new_window_handle(bac->rgn, args, &wh, &err)) != 0) {
goto fail;
}
wl = ralloc_typed(bac->rgn, struct window_list, 1);
wl->win = args->win;
wl->handle = wh;
wl->next = bac->windows;
bac->windows = wl;
}
fail:
ASSERT(rv == 0, "error: %s", SDL_GetError());
}
static void render_windows(struct window_list* wl)
{
for (; wl != NULL; wl = wl->next) {
// SDL_Window* sdl_win = wl->handle->win;
SDL_Renderer* r = wl->handle->rnd;
SDL_SetRenderDrawColor(r, 255, 255, 255, 255);
SDL_RenderClear(r);
SDL_RenderPresent(r);
}
}

View File

@ -31,7 +31,6 @@ struct ax_window* ax__window_builder_finish(
{ {
struct ax_window* win = struct ax_window* win =
ralloc_typed(dst_rgn, struct ax_window, 1); ralloc_typed(dst_rgn, struct ax_window, 1);
win->handle = NULL;
pthread_mutex_init(&win->mx, NULL); pthread_mutex_init(&win->mx, NULL);
rgn_pin(dst_rgn, &win->mx, (void*) pthread_mutex_destroy); rgn_pin(dst_rgn, &win->mx, (void*) pthread_mutex_destroy);

View File

@ -16,7 +16,6 @@ enum ax_window_flags {
}; };
struct ax_window { struct ax_window {
struct ax_window_h* handle;
pthread_mutex_t mx; pthread_mutex_t mx;
}; };