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

SaffronWindow* saffron_window_new(const char* title, int w, int h) {
	SaffronWindow* window = malloc(sizeof(SaffronWindow));
	window->root = saffron_widget_new();
	window->title = title;
	window->w = window->root->w = w;
	window->h = window->root->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;
			}
			if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
				printf("[saffron] mouse down!\n");
				if (event.button.button == SDL_BUTTON_LEFT) {
					printf("[saffron] left click at %f, %f!\n", event.button.x, event.button.y);
					printf("[saffron] calling hit test at coordinates\n");
					SaffronWidget* hit = saffron_widget_hit_test(window->root, (int)event.button.x, (int)event.button.y);
					if (hit && hit->on_click) hit->on_click(hit);
				}
			}
		}

		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
	}
}