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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#include <raylib.h>
#include "log.h"
#include "editor.h"
#include "mem.h"
#include <string.h>
#define MIN(a, b) ((a) < (b))? (a) : (b)
struct redr_ctx {
/* window */
float width;
float height;
const char *title;
/* font */
float font_size;
float font_spacing;
};
static void CustomLogCallback(int logLevel, const char *text, va_list args) {
enum log_level red_level = RED_LOG_ALL;
switch (logLevel) {
case LOG_ALL:
red_level = RED_LOG_ALL;
break;
case LOG_TRACE:
case LOG_DEBUG:
red_level = RED_LOG_DEBUG;
break;
case LOG_INFO:
red_level = RED_LOG_INFO;
break;
case LOG_WARNING:
red_level = RED_LOG_WARNING;
break;
case LOG_ERROR:
case LOG_FATAL:
red_level = RED_LOG_ERROR;
break;
case LOG_NONE:
red_level = RED_LOG_NONE;
break;
}
log_printf(red_level, text, args);
log_printf(red_level, "\n");
}
static inline void draw_text_slice(int x, int y, int size, Color c,
const struct slice sl) {
char line[4096];
size_t len;
if ((len = sl.len) > sizeof (line) - 1)
len = sizeof(line) - 1;
memcpy(line, sl.ptr, len);
line[len] = '\0';
DrawText(line, x, y, size, c);
}
void redr_init(struct redr_ctx **ctxp, float width, float height,
float font_size, float font_spacing, const char *title) {
SetTraceLogCallback(CustomLogCallback);
struct redr_ctx *ctx = mem_malloc(sizeof(struct redr_ctx));
ctx->height = height;
ctx->width = width;
ctx->title = title;
ctx->font_size = font_size;
ctx->font_spacing = font_spacing;
*ctxp = ctx;
InitWindow(width, height, title);
SetWindowState(FLAG_WINDOW_RESIZABLE);
EnableEventWaiting();
}
bool redr_should_close(struct redr_ctx *ctx) {
return WindowShouldClose();
}
void redr_resize(struct redr_ctx *ctx) {
if (IsWindowResized()) {
ctx->width = GetScreenWidth();
ctx->height = GetScreenHeight();
}
}
void redr_draw(struct redr_ctx *ctx, const struct ed_buf *eb) {
float font_size = ctx->font_size;
float spacing = ctx->font_spacing;
float font_height = MeasureTextEx(GetFontDefault(), "a", font_size, spacing).y;
BeginDrawing();
ClearBackground(RAYWHITE);
const size_t linenum_to_draw = MIN(eb_get_line_num(eb), ctx->height / font_height);
#ifdef CONFIG_DEBUG
log_printf(RED_LOG_DEBUG,
"[draw]: %lu lines ------\n", linenum_to_draw);
log_printf(RED_LOG_DEBUG,
"[draw]: cursor: (%lu, %lu) ------\n",
eb_get_cur_col(eb), eb_get_cur_row(eb));
#endif
for (size_t i = 0; i <= linenum_to_draw; i++) {
struct slice sl = {};
eb_get_line_slice(eb, i, &sl);
#ifdef CONFIG_DEBUG
log_printf(RED_LOG_DEBUG,
"[draw]: line %lu, ptr=%p, len=%lu\n",
i, sl.ptr, sl.len);
#endif
if (i != eb_get_cur_row(eb)) {
draw_text_slice(10, 10 + font_size * i,
font_size, BLACK, sl);
} else { /* draw cursor */
#define BUFSIZE 4096
char buf[BUFSIZE];
size_t cur_col = eb_get_cur_col(eb);
strncpy(buf, sl.ptr == NULL? "" : sl.ptr, MIN(sl.len, BUFSIZE));
if (sl.len < 4096)
buf[sl.len] = '\0';
if (buf[cur_col] == '\0')
buf[cur_col + 1] = '\0';
buf[cur_col] = '_';
DrawText(buf, 10, 10 + font_size * i,
font_size, BLACK);
}
}
EndDrawing();
}
void redr_free(struct redr_ctx *ctx) {
CloseWindow();
mem_free(ctx);
}
|