aboutsummaryrefslogtreecommitdiff
path: root/src/saffron_window.c
blob: 085ee20dadb7a0d7d48d03027acad8d2e414b564 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "saffron_api.h"
#include "saffron_theme.h"
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
#include <stdio.h>
#include <SDL3/SDL_events.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <saffron.h>
#include <SDL3/SDL_egl.h>
#include <SDL3/SDL_opengles2.h>

SaffronWindow* saffron_window_new(const char* title, int w, int h) {
	SaffronWindow* window = malloc(sizeof(SaffronWindow));
	window->root = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_VERTICAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 10, 10, 0, w, h);
	window->title = title;
	window->w = w;
	window->h = h;
	memset(window->hooks, 0, sizeof(window->hooks));
	window->hook_count = 0;

	Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_OPENGL;
	window->sdl_window = SDL_CreateWindow(title, w, h, flags);

	SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
	window->renderer = SDL_CreateRenderer(window->sdl_window, NULL);

	window->gl_context = SDL_GL_CreateContext(window->sdl_window);
	SDL_GL_MakeCurrent(window->sdl_window, window->gl_context);

	window->root->theme = SF_MACRO_DEFAULT_THEME;

	return window;
}

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

SDL_GLContext saffron_window_get_gl_context(SaffronWindow* window) {
	return window->gl_context;
}

static void handle_mouse_down(SDL_Event* event, SaffronWindow* window) {
	if (event->type != SDL_EVENT_MOUSE_BUTTON_DOWN) return;

	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);
	}
	// TODO: handle right click
}

static void handle_window_resized(SDL_Event* event, SaffronWindow* window) {
	if (event->type != SDL_EVENT_WINDOW_RESIZED) return;

	printf("[Saffron] window resized!\n");

	int new_w, new_h;
	SDL_GetWindowSize(window->sdl_window, &new_w, &new_h);
	window->w = window->root->w = new_w;
	window->h = window->root->h = new_h;
	printf("[Saffron] new dimensions: %dx%d\n", new_w, new_h);

	printf("[Saffron] recalculating layout on window->root\n");
	saffron_box_layout((SaffronBox*)window->root);
}

void saffron_window_detach_gl_context(SaffronWindow *window) {
	SDL_GL_MakeCurrent(window->sdl_window, NULL);
}

void saffron_window_reattach_gl_context(SaffronWindow *window) {
	SDL_GL_MakeCurrent(window->sdl_window, window->gl_context);
}

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

	bool running = true;
	printf("[Saffron] window starting!\n");
	SDL_Event event;

	printf("[Saffron] calculating layout on window->root\n");
	saffron_box_layout((SaffronBox*)window->root);

	printf("[Saffron] starting window mainloop\n");
	while (running) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_EVENT_QUIT) {
				running = false;
			}
			if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
				handle_mouse_down(&event, window);
			}
			if (event.type == SDL_EVENT_WINDOW_RESIZED) {
				handle_window_resized(&event, window);
			}
			for (int i = 0; i < window->hook_count; i++) {
				bool consumed = window->hooks[i].callback(&event);
				if (consumed) break;
			}
		}

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