aboutsummaryrefslogtreecommitdiff
path: root/src/saffron_window.c
blob: 4bdae76a8919b639713265628cdb48a3473a45c4 (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
#include <SDL3/SDL_events.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <saffron_api.h>
#include <saffron_widget.h>
#include <saffron_window.h>

SaffronWindow* saffron_window_new(const char* title, int w, int h) {
	SaffronWindow* window = malloc(sizeof(SaffronWindow));
	window->root = saffron_widget_new(); // frick, need to implement this.
	window->title = title;
	window->w = w;
	window->h = h;
	window->sdl_window = SDL_CreateWindow(title, w, h, SDL_WINDOW_RESIZABLE);
	window->renderer = SDL_CreateRenderer(window->sdl_window, NULL);
	return window;
}

void saffron_window_free(SaffronWindow* window) {
	SDL_DestroyRenderer(window->renderer);
	SDL_DestroyWindow(window->sdl_window);
	saffron_widget_free(window->root);
	free(window);
}

void saffron_window_main(SaffronWindow *window) {
	if (!window) return;

	bool running = true;
	SDL_Event event;

	while (running) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_EVENT_QUIT) {
				running = false;
			}
			// TODO: send events to widgets and stuff
		}

		SDL_SetRenderDrawColor(window->renderer, 0, 0, 0, 255);
		SDL_RenderClear(window->renderer);
		saffron_widget_draw(window->root, window->renderer);
		SDL_RenderPresent(window->renderer);

		SDL_Delay(16); // around 60fps or so
	}
}