graphics.h (4854B)
1 /* 2 * Copyright (c) 2021-2024 lumidify <nobody@lumidify.org> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef LTK_GRAPHICS_H 18 #define LTK_GRAPHICS_H 19 20 typedef struct ltk_renderdata ltk_renderdata; 21 typedef struct ltk_renderwindow ltk_renderwindow; 22 23 #include <stddef.h> 24 25 #include "rect.h" 26 #include "color.h" 27 28 typedef enum { 29 LTK_BORDER_NONE = 0, 30 LTK_BORDER_TOP = 1, 31 LTK_BORDER_RIGHT = 2, 32 LTK_BORDER_BOTTOM = 4, 33 LTK_BORDER_LEFT = 8, 34 LTK_BORDER_ALL = 0xF 35 } ltk_border_sides; 36 37 typedef struct { 38 int val; 39 enum { 40 LTK_UNIT_PX, 41 LTK_UNIT_PT, 42 LTK_UNIT_MM, 43 } unit; 44 } ltk_size; 45 46 typedef struct ltk_surface ltk_surface; 47 48 /* FIXME: graphics context */ 49 ltk_surface *ltk_surface_create(ltk_renderwindow *window, int w, int h); 50 void ltk_surface_destroy(ltk_surface *s); 51 /* returns 0 if successful, 1 if not resizable */ 52 int ltk_surface_resize(ltk_surface *s, int w, int h); 53 /* FIXME: kind of hacky */ 54 void ltk_surface_update_size(ltk_surface *s, int w, int h); 55 ltk_surface *ltk_surface_from_window(ltk_renderwindow *window, int w, int h); 56 void ltk_surface_get_size(ltk_surface *s, int *w, int *h); 57 void ltk_surface_copy(ltk_surface *src, ltk_surface *dst, ltk_rect src_rect, int dst_x, int dst_y); 58 void ltk_surface_draw_rect(ltk_surface *s, ltk_color *c, ltk_rect rect, int line_width); 59 void ltk_surface_fill_rect(ltk_surface *s, ltk_color *c, ltk_rect rect); 60 /* FIXME: document properly, especially difference to draw_rect with offsets and line_width */ 61 void ltk_surface_draw_border(ltk_surface *s, ltk_color *c, ltk_rect rect, int line_width, ltk_border_sides border_sides); 62 void ltk_surface_draw_border_clipped(ltk_surface *s, ltk_color *c, ltk_rect rect, int line_width, ltk_border_sides border_sides, ltk_rect clip); 63 void ltk_surface_fill_polygon(ltk_surface *s, ltk_color *c, ltk_point *points, size_t npoints); 64 void ltk_surface_fill_polygon_clipped(ltk_surface *s, ltk_color *c, ltk_point *points, size_t npoints, ltk_rect clip); 65 void ltk_surface_fill_ellipse(ltk_surface *s, ltk_color *c, ltk_rect rect); 66 void ltk_surface_fill_ellipse_clipped(ltk_surface *s, ltk_color *c, ltk_rect rect, ltk_rect clip); 67 68 /* dpi is actually 5 * the real dpi! */ 69 /* sz.val is scaled by 100! */ 70 /* FIXME: overflow prevention */ 71 /* -> maybe add max value for dpi and numbers that is checked beforehand */ 72 /* It might make sense to just use double for everything */ 73 static inline unsigned int 74 ltk_size_to_pixel(ltk_size sz, unsigned int dpi) { 75 if (sz.val < 0) { 76 switch (sz.unit) { 77 case LTK_UNIT_PT: 78 return (sz.val * dpi - 18000) / 36000; 79 break; 80 case LTK_UNIT_MM: 81 return (sz.val * dpi - 6300) / 12700; 82 break; 83 default: 84 return (sz.val - 50) / 100; 85 } 86 } else { 87 switch (sz.unit) { 88 case LTK_UNIT_PT: 89 return (sz.val * dpi + 18000) / 36000; 90 break; 91 case LTK_UNIT_MM: 92 return (sz.val * dpi + 6300) / 12700; 93 break; 94 default: 95 return (sz.val + 50) / 100; 96 } 97 } 98 } 99 100 /* TODO */ 101 /* 102 void ltk_surface_draw_arc(ltk_surface *s, ltk_color *c, int x, int y, int w, int h, int angle1, int angle2, int line_width); 103 void ltk_surface_fill_arc(ltk_surface *s, ltk_color *c, int x, int y, int w, int h, int angle1, int angle2); 104 void ltk_surface_draw_circle(ltk_surface *s, ltk_color *c, int xc, int yc, int r, int line_width); 105 void ltk_surface_fill_circle(ltk_surface *s, ltk_color *c, int xc, int yc, int r); 106 */ 107 108 /* FIXME: rename some of these functions */ 109 void ltk_renderer_set_imspot(ltk_renderwindow *window, int x, int y); 110 ltk_renderdata *ltk_renderer_create(void); 111 ltk_renderwindow *ltk_renderer_create_window(ltk_renderdata *data, const char *title, int x, int y, unsigned int w, unsigned int h, unsigned int initial_dpi); 112 void ltk_renderer_destroy_window(ltk_renderwindow *window); 113 void ltk_renderer_destroy(ltk_renderdata *data); 114 void ltk_renderer_set_window_properties(ltk_renderwindow *window, ltk_color *bg); 115 unsigned int ltk_renderer_get_window_dpi(ltk_renderwindow *window); 116 /* FIXME: this is kind of out of place */ 117 void ltk_renderer_swap_buffers(ltk_renderwindow *window); 118 /* FIXME: this is just for the socket name in ltkd and is a bit weird */ 119 unsigned long ltk_renderer_get_window_id(ltk_renderwindow *window); 120 121 #endif /* LTK_GRAPHICS_H */