summaryrefslogtreecommitdiff
path: root/main.c
blob: 2c789e64a4d3749bd8ceb937316fcf12eab21318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <SDL2/SDL.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_image.h>
#include "microui.h"
#include "ui.h"
#include "font.h"
#include "text_size.h"
#include "render.h"

SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
mu_Context *mu_ctx = NULL;

static int init(void);
static void deinit(void);

int main(int argc, char **argv)
{
	if (init()) {
		deinit();
		return 1;
	}
	
	for (;;) {
		SDL_Event e;
		while (SDL_PollEvent(&e)) switch (e.type) {
		case SDL_QUIT:
			return 0;
		default:
			break;
		}

		mu_begin(mu_ctx);
		if (mu_begin_window(mu_ctx, "lol", mu_rect(0, 0, 256, 256))) {
			mu_button(mu_ctx, "kekw");
			mu_end_window(mu_ctx);
		}
		mu_end(mu_ctx);

		r_draw_set_color(0, 0, 0, 1);
		SDL_RenderClear(renderer);
		ui_draw();
		SDL_RenderPresent(renderer);
	}
	return 0;
}

static int init(void)
{
	if (SDL_Init(SDL_INIT_VIDEO)) {
		fprintf(stderr, "%s\n", SDL_GetError());
		return 1;
	}
	if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
		fprintf(stderr, "%s\n", IMG_GetError());
		return 1;
	}
	if (SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_RESIZABLE,
	                                &window, &renderer)) {
		fprintf(stderr, "%s\n", SDL_GetError());
		return 1;
	}
	if (font_init("res/font.png"))
		return 1;
	if ((mu_ctx = malloc(sizeof(mu_Context))) == NULL) {
		perror("init");
		return 1;
	}
	mu_init(mu_ctx);
	mu_ctx->text_width = text_width;
	mu_ctx->text_height = text_height;
	if (atexit(deinit)) {
		perror("init");
		return 1;
	}
	return 0;
}

static void deinit(void)
{
	if (mu_ctx != NULL) {
		free(mu_ctx);
		mu_ctx = NULL;
	}
	font_deinit();
	if (renderer != NULL) {
		SDL_DestroyRenderer(renderer);
		renderer = NULL;
	}
	if (window != NULL) {
		SDL_DestroyWindow(window);
		window = NULL;
	}
	IMG_Quit();
	SDL_Quit();
}