#include "saffron_theme.h" #include #include #include #include #include #include #include #include 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; window->sdl_window = SDL_CreateWindow(title, w, h, flags); window->renderer = SDL_CreateRenderer(window->sdl_window, NULL); window->root->theme = SF_MACRO_DEFAULT_THEME; return window; } void saffron_window_free(SaffronWindow* window) { SDL_DestroyRenderer(window->renderer); SDL_DestroyWindow(window->sdl_window); saffron_widget_free(window->root); 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 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 } }