-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
117 lines (93 loc) · 2.48 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* See LICENSE for copyright details */
#include <dlfcn.h>
#include <fcntl.h>
#include <raylib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "util.c"
static const char *fontpath = "/home/rnp/.local/share/fonts/Aozora Mincho Medium.ttf";
#ifdef _DEBUG
typedef struct timespec Filetime;
static const char *libname = "./libcolourpicker.so";
static void *libhandle;
typedef void (do_colour_picker_fn)(ColourPickerCtx *);
static do_colour_picker_fn *do_colour_picker;
static Filetime
get_filetime(const char *name)
{
struct stat sb;
if (stat(name, &sb) < 0)
return (Filetime){0};
return sb.st_mtim;
}
static i32
compare_filetime(Filetime a, Filetime b)
{
return (a.tv_sec - b.tv_sec) + (a.tv_nsec - b.tv_nsec);
}
static void
load_library(const char *lib)
{
/* NOTE: glibc is buggy gnuware so we need to check this */
if (libhandle)
dlclose(libhandle);
libhandle = dlopen(lib, RTLD_NOW|RTLD_LOCAL);
if (!libhandle)
fprintf(stderr, "do_debug: dlopen: %s\n", dlerror());
do_colour_picker = dlsym(libhandle, "do_colour_picker");
if (!do_colour_picker)
fprintf(stderr, "do_debug: dlsym: %s\n", dlerror());
}
static void
do_debug(void)
{
static Filetime updated_time;
Filetime test_time = get_filetime(libname);
if (compare_filetime(test_time, updated_time)) {
sync();
load_library(libname);
updated_time = test_time;
}
}
#else
static void do_debug(void) { }
#include "colourpicker.c"
#endif /* _DEBUG */
int
main(void)
{
ColourPickerCtx ctx = {
.window_size = { .w = 720, .h = 960 },
.mode = CPM_HSV,
.flags = CPF_REFILL_TEXTURE,
.colour = { .r = 0.53, .g = 0.82, .b = 0.59, .a = 1.0 },
.bg = { .r = 0x26, .g = 0x1e, .b = 0x22, .a = 0xff },
.fg = { .r = 0xea, .g = 0xe1, .b = 0xb4, .a = 0xff },
.colour_stack = {
.fade_param = 1.0f,
.scales = { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
.items = {
{ .r = 0.04, .g = 0.04, .b = 0.04, .a = 1.00 },
{ .r = 0.92, .g = 0.88, .b = 0.78, .a = 1.00 },
{ .r = 0.59, .g = 0.11, .b = 0.25, .a = 1.00 },
{ .r = 0.11, .g = 0.59, .b = 0.36, .a = 1.00 },
{ .r = 0.14, .g = 0.29, .b = 0.72, .a = 1.00 },
},
},
};
#ifndef _DEBUG
SetTraceLogLevel(LOG_ERROR);
#endif
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(ctx.window_size.w, ctx.window_size.h, "Colour Picker");
ctx.font = LoadFontEx(fontpath, 128, 0, 0);
ctx.font_size = 44;
ctx.hsv_texture = LoadRenderTexture(360, 360);
while(!WindowShouldClose()) {
do_debug();
BeginDrawing();
do_colour_picker(&ctx);
EndDrawing();
}
}