diff options
| -rw-r--r-- | gui.c | 65 | 
1 files changed, 40 insertions, 25 deletions
| @@ -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); | 
