diff options
| author | xengineering <me@xengineering.eu> | 2023-01-23 21:26:42 +0100 | 
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2023-01-23 21:26:42 +0100 | 
| commit | becf716c4de03d58113017cd135b3c90fd77bcac (patch) | |
| tree | 42a59199b11d9ad1c96b51c52cb55a4278586328 | |
| parent | 7f2af8d7a28cb48b5b4dde919a5775506478b975 (diff) | |
| download | limox-becf716c4de03d58113017cd135b3c90fd77bcac.tar limox-becf716c4de03d58113017cd135b3c90fd77bcac.tar.zst limox-becf716c4de03d58113017cd135b3c90fd77bcac.zip | |
gui.c: Create separate create_window()legacy/sdl2
This splits the GUI code into smaller functions.
| -rw-r--r-- | gui.c | 62 | 
1 files changed, 34 insertions, 28 deletions
| @@ -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);  } | 
