summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-01-23 20:45:37 +0100
committerxengineering <me@xengineering.eu>2023-01-23 20:45:37 +0100
commit5d57490507ea04c8739ee2a51639c86d1eda3cd2 (patch)
treef24d877e85e16d685f1638786c3db0eec1dc2d2d
parent59eff22a223c64fc894d00d2216a7c701c34c5b8 (diff)
downloadlimox-5d57490507ea04c8739ee2a51639c86d1eda3cd2.tar
limox-5d57490507ea04c8739ee2a51639c86d1eda3cd2.tar.zst
limox-5d57490507ea04c8739ee2a51639c86d1eda3cd2.zip
Bundle GUI variables in context struct
This allows to pass all variables easily between small functions.
-rw-r--r--gui.c65
1 files changed, 40 insertions, 25 deletions
diff --git a/gui.c b/gui.c
index 39e2b9a..cb26990 100644
--- a/gui.c
+++ b/gui.c
@@ -19,24 +19,39 @@
#include <SDL2/SDL.h>
#include <stdbool.h>
+#include <stdlib.h>
#include <unistd.h>
#include "xmpp.h"
-void gui_run(void) {
-
- bool quit = false;
+struct GuiContext {
+ bool quit;
SDL_Event event;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* texture;
uint32_t* pixels;
- int xmpp_fd = -1;
+ int xmpp_fd;
+};
+
+
+struct GuiContext *init() {
+ struct GuiContext *ctx = malloc(sizeof(struct GuiContext));
+
+ ctx->quit = false;
+ ctx->xmpp_fd = -1;
+
+ return ctx;
+}
+
+void gui_run(void) {
+
+ struct GuiContext *ctx = init();
// init SDL2 and create window
SDL_Init(SDL_INIT_VIDEO);
- window = SDL_CreateWindow("LimoX",
+ ctx->window = SDL_CreateWindow("LimoX",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,
SDL_WINDOW_RESIZABLE
);
@@ -45,42 +60,42 @@ void gui_run(void) {
fprintf(stderr, "SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver());
// create and initialize renderer, texture and pixel buffer
- renderer = SDL_CreateRenderer(window, -1, 0);
- texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
- SDL_TEXTUREACCESS_STATIC, 640, 480);
- pixels = malloc(sizeof(uint32_t) * 640 * 480);
- memset(pixels, 255, 640 * 480 * sizeof(uint32_t));
+ ctx->renderer = SDL_CreateRenderer(ctx->window, -1, 0);
+ ctx->texture = SDL_CreateTexture(ctx->renderer, SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STATIC, 640, 480);
+ ctx->pixels = malloc(sizeof(uint32_t) * 640 * 480);
+ memset(ctx->pixels, 255, 640 * 480 * sizeof(uint32_t));
// handle failed window creation
- if (window == NULL) {
+ if (ctx->window == NULL) {
fprintf(stderr, "Failed to create SDL2 window!\n");
return;
} else {
- while (!quit) {
- SDL_UpdateTexture(texture, NULL, pixels, 640 * sizeof(uint32_t));
- SDL_WaitEvent(&event);
- switch (event.type) {
+ 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:
- quit = true;
+ ctx->quit = true;
break;
case SDL_MOUSEBUTTONDOWN:
- if (xmpp_fd == -1) {
- xmpp_fd = xmpp_connect();
+ if (ctx->xmpp_fd == -1) {
+ ctx->xmpp_fd = xmpp_connect();
} else {
- close(xmpp_fd);
- xmpp_fd = -1;
+ close(ctx->xmpp_fd);
+ ctx->xmpp_fd = -1;
printf("Closed XMPP connection.\n");
}
break;
}
- SDL_RenderClear(renderer);
- SDL_RenderCopy(renderer, texture, NULL, NULL);
- SDL_RenderPresent(renderer);
+ SDL_RenderClear(ctx->renderer);
+ SDL_RenderCopy(ctx->renderer, ctx->texture, NULL, NULL);
+ SDL_RenderPresent(ctx->renderer);
}
- SDL_DestroyWindow(window);
+ SDL_DestroyWindow(ctx->window);
}
- free(pixels);
+ free(ctx->pixels);
// TODO this seems to end in memory access errors but ... why?
//SDL_DestroyTexture(texture);