aboutsummaryrefslogtreecommitdiff
path: root/src/saffron_window.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/saffron_window.c')
-rw-r--r--src/saffron_window.c50
1 files changed, 41 insertions, 9 deletions
diff --git a/src/saffron_window.c b/src/saffron_window.c
index c595c08..74742fa 100644
--- a/src/saffron_window.c
+++ b/src/saffron_window.c
@@ -1,3 +1,5 @@
+#include <SDL3/SDL_render.h>
+#include <SDL3/SDL_video.h>
#include <stdio.h>
#include <SDL3/SDL_events.h>
#include <stdlib.h>
@@ -7,12 +9,15 @@
SaffronWindow* saffron_window_new(const char* title, int w, int h) {
SaffronWindow* window = malloc(sizeof(SaffronWindow));
- window->root = saffron_widget_new();
+ window->root = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_VERTICAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 10, 10, 0);
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);
+
+ Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY;
+ window->sdl_window = SDL_CreateWindow(title, w, h, flags);
window->renderer = SDL_CreateRenderer(window->sdl_window, NULL);
+
return window;
}
@@ -23,10 +28,40 @@ void saffron_window_free(SaffronWindow* window) {
free(window);
}
+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_main(SaffronWindow *window) {
if (!window) return;
bool running = true;
+ printf("[Saffron] window running!\n");
SDL_Event event;
while (running) {
@@ -35,13 +70,10 @@ void saffron_window_main(SaffronWindow *window) {
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);
- }
+ handle_mouse_down(&event, window);
+ }
+ if (event.type == SDL_EVENT_WINDOW_RESIZED) {
+ handle_window_resized(&event, window);
}
}