aboutsummaryrefslogtreecommitdiff
path: root/src/saffron_window.c
blob: 022c5e41ed772177de1ad6b5d503e5b2096154c4 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#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>
#include <EGL/egl.h>

EGLDisplay saffron_window_get_egl_display(SaffronWindow* window) {
    if (!window || !window->gl_context) {
        return EGL_NO_DISPLAY;
    }
    
    EGLDisplay display = eglGetCurrentDisplay();
    
    if (display == EGL_NO_DISPLAY) {
        display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    }
    
    return display;
}

EGLConfig saffron_window_get_egl_config(SaffronWindow* window) {
    if (!window || !window->gl_context) {
        return NULL;
    }
    
    EGLDisplay display = saffron_window_get_egl_display(window);
    if (display == EGL_NO_DISPLAY) {
        return NULL;
    }
    
    EGLConfig config = NULL;
    EGLint config_id;
    
    if (eglQueryContext(display, eglGetCurrentContext(), EGL_CONFIG_ID, &config_id)) {
        EGLint num_configs;
        EGLConfig configs[32];
        EGLint attribs[] = {
            EGL_CONFIG_ID, config_id,
            EGL_NONE
        };
        
        if (eglChooseConfig(display, attribs, configs, 32, &num_configs) && num_configs > 0) {
            config = configs[0];
        }
    }
    
    if (!config) {
        EGLint config_attribs[] = {
            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
            EGL_RED_SIZE, 8,
            EGL_GREEN_SIZE, 8,
            EGL_BLUE_SIZE, 8,
            EGL_ALPHA_SIZE, 8,
            EGL_DEPTH_SIZE, 24,
            EGL_STENCIL_SIZE, 8,
            EGL_NONE
        };
        
        EGLint num_configs;
        if (eglChooseConfig(display, config_attribs, &config, 1, &num_configs) && num_configs > 0) {
            return config;
        }
        return NULL;
    }
    
    return config;
}

EGLSurface saffron_window_get_egl_surface(SaffronWindow* window) {
    if (!window || !window->gl_context) {
        return EGL_NO_SURFACE;
    }
    
    EGLSurface surface = eglGetCurrentSurface(EGL_DRAW);
    
    if (surface == EGL_NO_SURFACE) {
        EGLDisplay display = saffron_window_get_egl_display(window);
        EGLConfig config = saffron_window_get_egl_config(window);
        
        if (display != EGL_NO_DISPLAY && config) {
            surface = eglCreateWindowSurface(display, config, 
                                            (EGLNativeWindowType)window->sdl_window, NULL);
        }
    }
    
    return surface;
}

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