165 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			165 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include "aliases/aliases.h"
 | |
| #include "nodes.h"
 | |
| #include "window.h"
 | |
| #include <SDL2/SDL.h>
 | |
| #include <SDL2/SDL_events.h>
 | |
| #include <SDL2/SDL_render.h>
 | |
| #include <SDL2/SDL_video.h>
 | |
| #include <stdbool.h>
 | |
| #include <stdlib.h>
 | |
| 
 | |
| #define MAX_WINDOWS 2
 | |
| 
 | |
| #define WINDOW_WIDTH 1280
 | |
| #define WINDOW_HEIGHT 720
 | |
| 
 | |
| typedef struct compositor compositor_t;
 | |
| struct compositor {
 | |
|   window_t windows[MAX_WINDOWS];
 | |
|   SDL_Event event;
 | |
|   bool running;
 | |
|   u64 mouse_x;
 | |
|   u64 mouse_y;
 | |
|   i64 node_hovered;
 | |
|   u64 count;
 | |
|   node_t *nodes;
 | |
|   bool move_node;
 | |
| };
 | |
| 
 | |
| void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
 | |
|               i32 y);
 | |
| 
 | |
| i32 run_main_loop(void) {
 | |
|   if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
 | |
|     return EXIT_FAILURE;
 | |
|   }
 | |
| 
 | |
|   compositor_t comp = {0};
 | |
|   comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES);
 | |
| 
 | |
|   window_t *main_window = &(comp.windows[0]);
 | |
|   window_t *toolbox = &(comp.windows[1]);
 | |
| 
 | |
|   if (!init_window(main_window, "Compositor", WINDOW_WIDTH, WINDOW_HEIGHT, -1,
 | |
|                    -1)) {
 | |
|     SDL_Quit();
 | |
| 
 | |
|     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;
 | |
| 
 | |
|   SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
 | |
| 
 | |
|   colour_t bg_colour = {.abgr = 0xffffffff};
 | |
| 
 | |
|   while (comp.running) {
 | |
|     while (SDL_PollEvent(&(comp.event))) {
 | |
|       switch (comp.event.type) {
 | |
|       case SDL_QUIT:
 | |
|         comp.running = false;
 | |
|         break;
 | |
|       case SDL_WINDOWEVENT:
 | |
|         switch (comp.event.window.event) {
 | |
|         case SDL_WINDOWEVENT_CLOSE:
 | |
|           comp.running = false;
 | |
|           break;
 | |
|         }
 | |
| 
 | |
|         break;
 | |
|       case SDL_MOUSEBUTTONDOWN:
 | |
|         if (comp.event.button.windowID == main_window->id) {
 | |
|           if (comp.node_hovered != -1) {
 | |
|             comp.move_node = true;
 | |
|           }
 | |
| 
 | |
|           break;
 | |
|         }
 | |
|       case SDL_MOUSEBUTTONUP:
 | |
|         comp.move_node = false;
 | |
| 
 | |
|         break;
 | |
|       case SDL_MOUSEMOTION:
 | |
|         if (comp.event.motion.windowID == main_window->id) {
 | |
|           comp.mouse_x = comp.event.motion.x;
 | |
|           comp.mouse_y = comp.event.motion.y;
 | |
| 
 | |
|           if (comp.move_node) {
 | |
|             i32 dx = comp.event.motion.xrel;
 | |
|             i32 dy = comp.event.motion.yrel;
 | |
| 
 | |
|             node_t *node = &(comp.nodes[comp.node_hovered]);
 | |
| 
 | |
|             node->rect.topleft.x += dx;
 | |
|             node->rect.topleft.y += dy;
 | |
|           } else {
 | |
|             comp.node_hovered = -1;
 | |
| 
 | |
|             for (u64 i = comp.count - 1; i >= 0; --i) {
 | |
|               node_t *node = &(comp.nodes[i]);
 | |
| 
 | |
|               if (aabb(node, comp.mouse_x, comp.mouse_y)) {
 | |
|                 comp.node_hovered = i;
 | |
|                 break;
 | |
|               }
 | |
|             }
 | |
|           }
 | |
| 
 | |
|           break;
 | |
|         }
 | |
|       case SDL_DROPFILE:
 | |
|         if (comp.event.drop.windowID == main_window->id) {
 | |
|           node_data_t data = (node_data_t){.path = comp.event.drop.file};
 | |
| 
 | |
|           add_node(&comp, NODE_TYPE_IO, data, comp.mouse_x, comp.mouse_y);
 | |
| 
 | |
|           break;
 | |
|         }
 | |
|       }
 | |
|     }
 | |
| 
 | |
|     for (u64 i = 0; i < MAX_WINDOWS; ++i) {
 | |
|       clear_window(&(comp.windows[i]), bg_colour);
 | |
|     }
 | |
| 
 | |
|     for (u64 i = 0; i < comp.count; ++i) {
 | |
|       node_t *node = &(comp.nodes[i]);
 | |
|       draw_node(main_window, node);
 | |
|     }
 | |
| 
 | |
|     for (u64 i = 0; i < MAX_WINDOWS; ++i) {
 | |
|       swap_buffers(&(comp.windows[i]));
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   cleanup_window(toolbox);
 | |
|   cleanup_window(main_window);
 | |
| 
 | |
|   SDL_Quit();
 | |
| 
 | |
|   return EXIT_SUCCESS;
 | |
| }
 | |
| 
 | |
| void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
 | |
|               i32 y) {
 | |
|   if (comp->count + 1 >= MAX_NODES) {
 | |
|     return;
 | |
|   }
 | |
| 
 | |
|   comp->nodes[(comp->count)++] = (node_t){
 | |
|       .rect =
 | |
|           (rect_t){
 | |
|               .topleft.x = x,
 | |
|               .topleft.y = y,
 | |
|               .w = NODE_WIDTH,
 | |
|               .h = NODE_HEIGHT,
 | |
|           },
 | |
|       .type = type,
 | |
|       .data.path = data.path,
 | |
|   };
 | |
| }
 |