Compare commits

...

2 Commits

Author SHA1 Message Date
d114cfce99 Create toolbox window 2024-01-15 20:48:36 +00:00
9806a5c708 Add window position information 2024-01-15 20:48:21 +00:00
3 changed files with 48 additions and 9 deletions

View File

@ -37,6 +37,8 @@ struct rect {
struct window { struct window {
u32 id; u32 id;
u64 x;
u64 y;
u64 width; u64 width;
u64 height; u64 height;
const char *title; const char *title;
@ -52,7 +54,8 @@ struct colour {
}; };
}; };
bool init_window(window_t *wnd, const char *title, u64 width, u64 height); bool init_window(window_t *wnd, const char *title, u32 width, u32 height, i32 x,
i32 y);
void cleanup_window(window_t *wnd); void cleanup_window(window_t *wnd);
void clear_window(const window_t *wnd, colour_t colour); void clear_window(const window_t *wnd, colour_t colour);
void swap_buffers(const window_t *wnd); void swap_buffers(const window_t *wnd);

View File

@ -38,13 +38,19 @@ i32 run_main_loop(void) {
comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES); comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES);
window_t *main_window = &(comp.windows[0]); window_t *main_window = &(comp.windows[0]);
window_t *toolbox = &(comp.windows[1]);
if (!init_window(main_window, "Compositor", WINDOW_WIDTH, WINDOW_HEIGHT)) { if (!init_window(main_window, "Compositor", WINDOW_WIDTH, WINDOW_HEIGHT, -1,
-1)) {
SDL_Quit(); SDL_Quit();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
u32 toolbox_window_width = WINDOW_WIDTH / 7;
init_window(toolbox, "Toolbox", toolbox_window_width, WINDOW_HEIGHT,
main_window->x - toolbox_window_width, -1);
comp.running = true; comp.running = true;
SDL_EventState(SDL_DROPFILE, SDL_ENABLE); SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
@ -56,6 +62,14 @@ i32 run_main_loop(void) {
switch (comp.event.type) { switch (comp.event.type) {
case SDL_QUIT: case SDL_QUIT:
comp.running = false; comp.running = false;
break;
case SDL_WINDOWEVENT:
switch (comp.event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
comp.running = false;
break;
}
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
if (comp.event.button.windowID == main_window->id) { if (comp.event.button.windowID == main_window->id) {
@ -108,15 +122,22 @@ i32 run_main_loop(void) {
} }
} }
clear_window(main_window, bg_colour); for (u64 i = 0; i < MAX_WINDOWS; ++i) {
clear_window(&(comp.windows[i]), bg_colour);
}
for (u64 i = 0; i < comp.count; ++i) { for (u64 i = 0; i < comp.count; ++i) {
node_t *node = &(comp.nodes[i]); node_t *node = &(comp.nodes[i]);
draw_node(main_window, node); draw_node(main_window, node);
} }
swap_buffers(main_window); for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i]));
} }
}
cleanup_window(toolbox);
cleanup_window(main_window);
SDL_Quit(); SDL_Quit();

View File

@ -5,10 +5,13 @@
#include <SDL2/SDL_video.h> #include <SDL2/SDL_video.h>
#include <stdbool.h> #include <stdbool.h>
bool init_window(window_t *wnd, const char *title, u64 width, u64 height) { bool init_window(window_t *wnd, const char *title, u32 width, u32 height, i32 x,
i32 y) {
i32 pos_x = x >= 0 ? x : SDL_WINDOWPOS_CENTERED;
i32 pos_y = y >= 0 ? y : SDL_WINDOWPOS_CENTERED;
wnd->window = wnd->window =
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_CreateWindow(title, pos_x, pos_y, width, height, SDL_WINDOW_SHOWN);
width, height, SDL_WINDOW_SHOWN);
if (!(wnd->window)) { if (!(wnd->window)) {
return false; return false;
} }
@ -22,8 +25,20 @@ bool init_window(window_t *wnd, const char *title, u64 width, u64 height) {
wnd->id = SDL_GetWindowID(wnd->window); wnd->id = SDL_GetWindowID(wnd->window);
wnd->title = title; wnd->title = title;
wnd->width = width;
wnd->height = height; i32 x_tmp = -1;
i32 y_tmp = -1;
SDL_GetWindowPosition(wnd->window, &x_tmp, &y_tmp);
wnd->x = x_tmp;
wnd->y = y_tmp;
i32 w_tmp = -1;
i32 h_tmp = -1;
SDL_GetWindowSize(wnd->window, &w_tmp, &h_tmp);
wnd->width = w_tmp;
wnd->height = h_tmp;
return true; return true;
} }