-
Notifications
You must be signed in to change notification settings - Fork 0
/
DGLDRAW.C
executable file
·427 lines (363 loc) · 11.1 KB
/
DGLDRAW.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "dgldraw.h"
#include "dglgfx.h"
#include "dglrect.h"
#include "dglutil.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
static char printf_buffer[1024];
void draw_hline_f(SURFACE *surface, int x1, int x2, int y, uint8 color) {
uint8 *p = surface_pointer(surface, x1, y);
mem_fill(p, color, (x2 - x1 + 1));
}
extern void _draw_vline(uint8 *dest, uint8 color, int line_inc, int lines_left);
#pragma aux _draw_vline = \
" test ecx, ecx" \
" jz done" \
"draw:" \
" mov [edx], al" \
" add edx, ebx" \
" dec ecx" \
" jnz draw" \
"done:" \
parm [edx] [al] [ebx] [ecx];
void draw_vline_f(SURFACE *surface, int x, int y1, int y2, uint8 color) {
uint8 *p = surface_pointer(surface, x, y1);
int line_inc = surface->width;
int lines_left = y2 - y1 + 1;
_draw_vline(p, color, line_inc, lines_left);
}
void draw_line(SURFACE *surface, int x1, int y1, int x2, int y2, uint8 color) {
int delta_x, delta_y;
int delta_x_abs, delta_y_abs;
int delta_x_sign, delta_y_sign;
int x, y;
uint8 *p;
int p_x_inc;
int p_y_inc;
int i;
int dx = x1;
int dy = y1;
int clip_x1 = surface->clip_region.x;
int clip_y1 = surface->clip_region.y;
int clip_x2 = rect_right(&surface->clip_region);
int clip_y2 = rect_bottom(&surface->clip_region);
delta_x = x2 - x1;
delta_y = y2 - y1;
delta_x_abs = abs(delta_x);
delta_y_abs = abs(delta_y);
delta_x_sign = SIGN(delta_x);
delta_y_sign = SIGN(delta_y);
x = delta_y_abs / 2;
y = delta_x_abs / 2;
p = surface_pointer(surface, x1, y1);
p_x_inc = delta_x_sign;
p_y_inc = delta_y_sign * surface->width;
if (dx >= clip_x1 &&
dy >= clip_y1 &&
dx <= clip_x2 &&
dy <= clip_y2)
*p = color;
if (delta_x_abs >= delta_y_abs) {
for (i = 0; i < delta_x_abs; ++i) {
y += delta_y_abs;
if (y >= delta_x_abs) {
y -= delta_x_abs;
p += p_y_inc;
dy += delta_y_sign;
}
p += p_x_inc;
dx += delta_x_sign;
if (dx >= clip_x1 &&
dy >= clip_y1 &&
dx <= clip_x2 &&
dy <= clip_y2)
*p = color;
}
} else {
for (i = 0; i < delta_y_abs; ++i) {
x += delta_x_abs;
if (x >= delta_y_abs) {
x -= delta_y_abs;
p += p_x_inc;
dx += delta_x_sign;
}
p += p_y_inc;
dy += delta_y_sign;
if (dx >= clip_x1 &&
dy >= clip_y1 &&
dx <= clip_x2 &&
dy <= clip_y2)
*p = color;
}
}
}
void draw_line_f(SURFACE *surface, int x1, int y1, int x2, int y2, uint8 color) {
int delta_x, delta_y;
int delta_x_abs, delta_y_abs;
int delta_x_sign, delta_y_sign;
int x, y;
uint8 *p;
int p_x_inc;
int p_y_inc;
int i;
delta_x = x2 - x1;
delta_y = y2 - y1;
delta_x_abs = abs(delta_x);
delta_y_abs = abs(delta_y);
delta_x_sign = SIGN(delta_x);
delta_y_sign = SIGN(delta_y);
x = delta_y_abs / 2;
y = delta_x_abs / 2;
p = surface_pointer(surface, x1, y1);
p_x_inc = delta_x_sign;
p_y_inc = delta_y_sign * surface->width;
*p = color;
if (delta_x_abs >= delta_y_abs) {
for (i = 0; i < delta_x_abs; ++i) {
y += delta_y_abs;
if (y >= delta_x_abs) {
y -= delta_x_abs;
p += p_y_inc;
}
p += p_x_inc;
*p = color;
}
} else {
for (i = 0; i < delta_y_abs; ++i) {
x += delta_x_abs;
if (x >= delta_y_abs) {
x -= delta_y_abs;
p += p_x_inc;
}
p += p_y_inc;
*p = color;
}
}
}
extern void _draw_both_vert_lines(
uint8 *left,
uint8 *right,
uint8 color,
int y_inc,
int height
);
#pragma aux _draw_both_vert_lines = \
" inc ecx" \
"draw:" \
" mov [edi], al" \
" mov [esi], al" \
" add edi, edx" \
" add esi, edx" \
" dec ecx" \
" jnz draw" \
"done:" \
parm [edi] [esi] [al] [edx] [ecx];
extern void _draw_vert_line(
uint8 *dest,
uint8 color,
int y_inc,
int height
);
#pragma aux _draw_vert_line = \
" inc ecx" \
"draw:" \
" mov [edi], al" \
" add edi, edx" \
" dec ecx" \
" jnz draw" \
"done:" \
parm [edi] [al] [edx] [ecx];
void draw_rect(SURFACE *surface, int x1, int y1, int x2, int y2, uint8 color) {
RECT clipped;
int clipped_x2, clipped_y2;
if (x2 < x1)
SWAP(int, x1, x2);
if (y2 < y1)
SWAP(int, y1, y2);
clipped.x = x1;
clipped.y = y1;
clipped.width = x2 - x1 + 1;
clipped.height = y2 - y1 + 1;
if (!clip_to_region(&surface->clip_region, &clipped))
return;
clipped_x2 = clipped.x + clipped.width - 1;
clipped_y2 = clipped.y + clipped.height - 1;
// top line, only if y1 was within bounds
if (y1 == clipped.y) {
uint8 *p = surface_pointer(surface, clipped.x, clipped.y);
mem_fill(p, color, clipped.width);
}
// bottom line, only if y2 was within bounds
if (y2 == clipped_y2) {
uint8 *p = surface_pointer(surface, clipped.x, clipped_y2);
mem_fill(p, color, clipped.width);
}
// draw both left and right lines if neither x1 nor x2 were clipped
if (x1 == clipped.x && x2 == clipped_x2) {
uint8 *p1 = surface_pointer(surface, clipped.x, clipped.y);
uint8 *p2 = surface_pointer(surface, clipped_x2, clipped.y);
_draw_both_vert_lines(p1, p2, color, surface->width, clipped.height - 1);
// draw left line if x1 was not clipped
} else if (x1 == clipped.x) {
uint8 *p = surface_pointer(surface, clipped.x, clipped.y);
_draw_vert_line(p, color, surface->width, clipped.height - 1);
// draw right line if x2 was not clipped
} else if (x2 == clipped_x2) {
uint8 *p = surface_pointer(surface, clipped_x2, clipped.y);
_draw_vert_line(p, color, surface->width, clipped.height - 1);
}
}
void draw_rect_f(SURFACE *surface, int x1, int y1, int x2, int y2, uint8 color) {
uint8 *p;
uint8 *p1;
uint8 *p2;
int width = x2 - x1 + 1;
int lines_left = y2 - y1;
int y_inc = surface->width;
p = surface_pointer(surface, x1, y1);
p1 = p;
p2 = p + width - 1;
lowlevel_rect_f(color, width, y_inc, lines_left, p1, p2);
}
void draw_filled_rect(
SURFACE *surface,
int x1,
int y1,
int x2,
int y2,
uint8 color
) {
if (!clamp_to_region(&surface->clip_region, &x1, &y1, &x2, &y2))
return;
if (x2 < x1)
SWAP(int, x1, x2);
if (y2 < y1)
SWAP(int, y1, y2);
draw_filled_rect_f(surface, x1, y1, x2, y2, color);
}
void draw_filled_rect_f(
SURFACE *surface,
int x1,
int y1,
int x2,
int y2,
uint8 color
) {
uint8 *p;
int width = x2 - x1 + 1;
int y_inc = surface->width;
int lines_left = y2 - y1 + 1;
p = surface_pointer(surface, x1, y1);
lowlevel_filled_rect_f(color, y_inc, width, lines_left, p);
}
#define CHAR_WIDTH 8
#define CHAR_HEIGHT 8
#define CHAR_LINE_BITMASK(x) (1 << ((CHAR_WIDTH - 1) - (x)))
#define IS_CHAR_PIXEL(x, line) ((line) & CHAR_LINE_BITMASK(x))
#define BIOS_FONT_ADDR 0xffa6e
// dest_x, dest_y - original unclipped render x,y coords
// dest_clipped - clipped destination render region
// TODO: rewrite this. this function is kinda sloppy...
static void print_char(
SURFACE *surface,
int dest_x,
int dest_y,
const RECT *dest_clipped,
uint8 color,
char c
) {
uint8 *p;
uint8 *rom_char;
uint8 char_line_bits;
int cx, cy;
int offset_x, offset_y;
int width, height;
int y_inc = surface->width;
p = surface_pointer(surface, dest_clipped->x, dest_clipped->y);
rom_char = ((uint8*)BIOS_FONT_ADDR) + (c * CHAR_HEIGHT);
// get offset x,y to start rendering char from (will be in range 0-7)
offset_x = dest_clipped->x - dest_x;
offset_y = dest_clipped->y - dest_y;
width = dest_clipped->width + offset_x;
height = dest_clipped->height + offset_y;
// cx,cy are always in "char coordinate space" (that is, 0-7)
for (cy = offset_y; cy < height; ++cy) {
char_line_bits = rom_char[cy];
for (cx = offset_x; cx < width; ++cx) {
if (IS_CHAR_PIXEL(cx, char_line_bits))
p[cx - offset_x] = color;
}
p += y_inc;
}
}
static void print_text(
SURFACE *surface,
int x,
int y,
uint8 color,
bool clip,
const char *text
) {
const char *c;
RECT r;
RECT draw_r;
r = rect(x, y, CHAR_WIDTH, CHAR_HEIGHT);
for (c = text; *c; ++c) {
switch (*c) {
case '\n':
r.x = x;
r.y += r.height;
break;
case ' ':
r.x += r.width;
break;
case '\r':
break;
default:
if (clip) {
draw_r = r;
if (clip_to_region(&surface->clip_region, &draw_r))
print_char(surface, r.x, r.y, &draw_r, color, *c);
} else {
print_char(surface, r.x, r.y, &r, color, *c);
}
r.x += r.width;
}
}
}
void draw_text(SURFACE *surface, int x, int y, uint8 color, const char *text) {
print_text(surface, x, y, color, true, text);
}
void draw_text_f(SURFACE *surface, int x, int y, uint8 color, const char *text) {
print_text(surface, x, y, color, false, text);
}
void draw_printf(
SURFACE *surface,
int x,
int y,
uint8 color,
const char *format,
...
) {
va_list args;
va_start(args, format);
vsprintf(printf_buffer, format, args);
va_end(args);
print_text(surface, x, y, color, true, printf_buffer);
}
void draw_printf_f(
SURFACE *surface,
int x,
int y,
uint8 color,
const char *format,
...
) {
va_list args;
va_start(args, format);
vsprintf(printf_buffer, format, args);
va_end(args);
print_text(surface, x, y, color, false, printf_buffer);
}