aboutsummaryrefslogtreecommitdiff
path: root/render.c
blob: 578abeff60947a5e5e080d7ac51940618bbdb749 (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
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
#include "render.h"

static struct {
	Colour pixels[renderer_w * renderer_h];
} renderer;

Colour make_colour(unsigned rgb, unsigned char a) {
	Colour r;
	r.r = (unsigned char)(rgb >> 16);
	r.g = (unsigned char)(rgb >> 8);
	r.b = (unsigned char)rgb;
	r.a = a;
	return r;
}

Colour make_black() {
	Colour r = { 0, 0, 0, 255 };
	return r;
}

Colour make_white() {
	return make_colour(0xffffff, 255);
}

void init_bitmap(
	Bitmap* bitmap,
	Colour* pixels,
	int w,
	int h
) {
	bitmap->pixels = pixels;
	bitmap->w = w;
	bitmap->h = h;
}

void init_renderer() {
}

void renderer_begin_frame() {
	int i;

	for (i = 0; i < renderer_w * renderer_h; i++) {
		renderer.pixels[i] = make_black();
	}
}

Colour* get_render_pixels() {
	return renderer.pixels;
}

Colour blend(Colour dst, Colour src) {
	int ima;

	ima = 0xff - src.a;
	dst.r = (unsigned char)(((src.r * src.a) + (dst.r * ima)) >> 8);
	dst.g = (unsigned char)(((src.g * src.a) + (dst.g * ima)) >> 8);
	dst.b = (unsigned char)(((src.b * src.a) + (dst.b * ima)) >> 8);

	return dst;
}

Colour blend_mod(Colour dst, Colour src, Colour mod) {
	int ima;

	src.a = (src.a * mod.a) >> 8;
	ima = 0xff - src.a;
	dst.r = (unsigned char)(((src.r * mod.r * src.a) >> 16) + ((dst.r * ima) >> 8));
	dst.g = (unsigned char)(((src.g * mod.g * src.a) >> 16) + ((dst.g * ima) >> 8));
	dst.b = (unsigned char)(((src.b * mod.b * src.a) >> 16) + ((dst.b * ima) >> 8));

	return dst;
}

void init_font(
	BM_Font* font,
	Bitmap* bmp,
	int char_w,
	int char_h,
	int chars_across
) {
	font->bmp = bmp;
	font->char_w = char_w;
	font->char_h = char_h;
	font->chars_across = chars_across;
}

void rfont_char(int x, int y, char c) {

}

void rfont_char_col(int x, int y, char c, Colour colour) {

}

void rfont_text(
	int x,
	int y,
	const char* text
) {
}

void rfont_text_col(
	int x,
	int y,
	const char* text,
	Colour colour
) {

}

void render_bitmap(
	const Bitmap* bitmap,
	int x,
	int y,
	const Rectangle* rect
) {
	int i, j, stride, sstride, n;
	Colour* dst;
	const Colour* src;
	Rectangle sub;

	sub = *rect;

	if (sub.w <= 0) { return; }
	if (sub.h <= 0) { return; }
	if (sub.w > bitmap->w) { return; }
	if (sub.h > bitmap->h) { return; }

	if ((n = -x) > 0) {
		sub.w -= n;
		sub.x += n;
		x += n;
	}
	
	if ((n = -y) > 0) {
		sub.h -= n;
		sub.y += n;
		y += n;
	}
	
	if ((n = x + sub.w - renderer_w) > 0) {
		sub.w -= n;
	}

	if ((n = y + sub.h - renderer_h) > 0) {
		sub.h -= n;
	}

	if (sub.w <= 0) { return; }
	if (sub.h <= 0) { return; }

	dst = renderer.pixels + (x + y * renderer_w);
	src = bitmap->pixels + (sub.x + sub.y * bitmap->w);

	stride = renderer_w - sub.w;
	sstride = bitmap->w - sub.w;

	for (i = 0; i < sub.h; i++) {
		for (j = 0; j < sub.w; j++) {
			*dst = blend(*dst, *src);
			dst++;
			src++;
		}
		dst += stride;
		src += sstride;
	}
}

void render_bitmap_col(
	const Bitmap* bitmap,
	int x,
	int y,
	const Rectangle* rect,
	Colour colour
) {

}