From becf716c4de03d58113017cd135b3c90fd77bcac Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 23 Jan 2023 21:26:42 +0100 Subject: gui.c: Create separate create_window() This splits the GUI code into smaller functions. --- gui.c | 62 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/gui.c b/gui.c index 1b3606e..12f8f47 100644 --- a/gui.c +++ b/gui.c @@ -45,6 +45,8 @@ struct GuiContext *init() { ctx->pixels = malloc(sizeof(uint32_t) * 640 * 480); memset(ctx->pixels, 255, 640 * 480 * sizeof(uint32_t)); + SDL_Init(SDL_INIT_VIDEO); + return ctx; } @@ -60,50 +62,54 @@ void deinit(struct GuiContext *ctx) { free(ctx); } -void gui_run(void) { - - struct GuiContext *ctx = init(); +bool create_window(struct GuiContext *ctx) { - SDL_Init(SDL_INIT_VIDEO); ctx->window = SDL_CreateWindow("LimoX", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE ); - fprintf(stderr, "SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); - ctx->renderer = SDL_CreateRenderer(ctx->window, -1, 0); ctx->texture = SDL_CreateTexture(ctx->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 640, 480); - if (ctx->window == NULL) { + return ctx->window != NULL; +} + +void gui_run(void) { + + struct GuiContext *ctx = init(); + + fprintf(stderr, "SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); + + if (!create_window(ctx)) { fprintf(stderr, "Failed to create SDL2 window!\n"); return; - } else { - while (!ctx->quit) { - SDL_UpdateTexture(ctx->texture, NULL, ctx->pixels, 640 * sizeof(uint32_t)); - SDL_WaitEvent(&ctx->event); - switch (ctx->event.type) { - case SDL_QUIT: - ctx->quit = true; - break; - case SDL_MOUSEBUTTONDOWN: - if (ctx->xmpp_fd == -1) { - ctx->xmpp_fd = xmpp_connect(); - } else { - close(ctx->xmpp_fd); - ctx->xmpp_fd = -1; - printf("Closed XMPP connection.\n"); - } - break; + } + + while (!ctx->quit) { + SDL_UpdateTexture(ctx->texture, NULL, ctx->pixels, 640 * sizeof(uint32_t)); + SDL_WaitEvent(&ctx->event); + switch (ctx->event.type) { + case SDL_QUIT: + ctx->quit = true; + break; + case SDL_MOUSEBUTTONDOWN: + if (ctx->xmpp_fd == -1) { + ctx->xmpp_fd = xmpp_connect(); + } else { + close(ctx->xmpp_fd); + ctx->xmpp_fd = -1; + printf("Closed XMPP connection.\n"); } - SDL_RenderClear(ctx->renderer); - SDL_RenderCopy(ctx->renderer, ctx->texture, NULL, NULL); - SDL_RenderPresent(ctx->renderer); + break; } - SDL_DestroyWindow(ctx->window); + SDL_RenderClear(ctx->renderer); + SDL_RenderCopy(ctx->renderer, ctx->texture, NULL, NULL); + SDL_RenderPresent(ctx->renderer); } + SDL_DestroyWindow(ctx->window); deinit(ctx); } -- cgit v1.2.3-70-g09d2