aboutsummaryrefslogtreecommitdiff
path: root/render.c
blob: 07af8cf3ae33bff722c9878096fa5de5599f1b0a (plain) (blame)
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
#include "rect.h"
#include "render.h"

static int smol_font_w = 384;
static unsigned smol_font_data[] = {
	0x2296a520, 0x40000524, 0x77757767, 0x60002077,
	0x6773637e, 0x69b15475, 0x95576767, 0x02616755,
	0x66640102, 0x00021421, 0x00020000, 0xf0326000,
	0x2543e520, 0x20002242, 0x41152645, 0x92740277,
	0x1135175d, 0x9bf13427, 0x95523595, 0x05422272,
	0x72d66364, 0x73f25003, 0x95576677, 0xfa6233a9,
	0x06267000, 0x20727542, 0x27674145, 0x44022045,
	0x53151571, 0x9d915525, 0xf55243d7, 0x00422122,
	0x47351550, 0x55d23425, 0xb5522277, 0xf5222266,
	0x0b935020, 0x12022024, 0x277437e7, 0x42742277,
	0x61736756, 0x69975775, 0xb27235e1, 0x70646725,
	0x32666370, 0x75945625, 0xf2763241, 0xf0326639
};

void ren_begin(Renderer* r, unsigned* t, int w, int h) {
	r->t = t;
	r->w = w;
	r->h = h;
	r->clip[0] = 0;
	r->clip[1] = 0;
	r->clip[2] = w;
	r->clip[3] = h;
}

void ren_end(Renderer* r) {
	(void)r;
}

void ren_clip(Renderer* r, const Rect* re) {
	r->clip[0] = re->x;
	r->clip[1] = re->y;
	r->clip[2] = re->x + re->w;
	r->clip[3] = re->y + re->h;
}

void ren_rclip(Renderer* r) {
	r->clip[0] = 0;
	r->clip[1] = 0;
	r->clip[2] = r->w;
	r->clip[3] = r->h;
}

void ren_clear(Renderer* r) {
	int i, j;
	for (j = r->clip[1]; j < r->clip[3]; j++)
		for (i = r->clip[0]; i < r->clip[2]; i++) {
			int idx = i + j * r->w;
			int bit = 1 << (idx & 0x1f);
			bit = ~bit;
			r->t[idx >> 5] &= bit;
		}
}

void ren_char(Renderer* r, int x, int y, char ch) {
	Rect re, su;
	int i, j, sx, sy, ex, ey;
	re.x = x;
	re.y = y;
	re.w = 4;
	re.h = 4;
	su.x = (ch - ' ') * 4;
	su.y = 0;
	su.w = 4;
	su.h = 4;
	rect_clipsr(&re, &su, r->clip);
	ex = re.x + re.w;
	ey = re.y + re.h;
	for (j = re.y, sy = su.y; j < ey; j++, sy++) {
		for (i = re.x, sx = su.x; i < ex; i++, sx++) {
			int di = (i + j * r->w);
			int si = (sx + sy * smol_font_w);
			int bit = (1 << (si & 0x1f));
			bit &= smol_font_data[si >> 5];
			r->t[di >> 5] ^= (1 << (di & 0x1f)) * (bit != 0);
		}
	}
}

void ren_text(Renderer* r, int x, int y, const char* t) {
	for (; *t; t++, x += 4)
		ren_char(r, x, y, *t);
}

void ren_map(
	Renderer* r,
	int x,
	int y,
	const Rect* re,
	const Bitmap* bm
) {
	Rect rect, su = *re;
	const unsigned* data = (const unsigned*)&bm[1];
	int i, j, sx, sy, ex, ey;
	rect.x = x;
	rect.y = y;
	rect.w = re->w;
	rect.h = re->h;
	rect_clipsr(&rect, &su, r->clip);
	ex = rect.x + rect.w;
	ey = rect.y + rect.h;
	for (j = rect.y, sy = su.y; j < ey; j++, sy++) {
		for (i = rect.x, sx = su.x; i < ex; i++, sx++) {
			int di = (i + j * r->w);
			int si = (sx + sy * bm->w);
			int bit = (1 << (si & 0x1f));
			bit &= data[si >> 5];
			r->t[di >> 5] ^= (1 << (di & 0x1f)) * (bit != 0);
		}
	}
}

void ren_rect(
	Renderer* r,
	const struct Rect* re
) {
	Rect rect = *re;
	int i, j;
	int ex, ey;
	rect_clipr(&rect, r->clip);
	ex = rect.x + rect.w;
	ey = rect.y + rect.h;
	for (j = rect.y; j < ey; j++)
		for (i = rect.x; i < ex; i++) {
			int idx = i + j * r->w;
			int bit = 1 << (idx & 0x1f);
			r->t[idx >> 5] ^= bit;
		}
}