#include "maths.h" #include "plat.h" #include "render.h" #include static int font_w = 960; static unsigned font_data[] = { 0x00000000, 0x00003000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0cc0c000, 0x8000301b, 0x60601803, 0x00000000, 0x20000000, 0x0780c078, 0x83f0e01e, 0xe0783f07, 0x60000001, 0x1e018000, 0x07e0c078, 0xe3f87e1f, 0xe0cc1e0f, 0x1e638f01, 0x1f0c6318, 0x07e1f07e, 0xc330fc1e, 0x30cc318c, 0x021e0fc3, 0x000101e0, 0x00e00000, 0x0000e000, 0xc00e0007, 0x3c038c00, 0x00000000, 0x00000000, 0x00001000, 0x00000000, 0x30380000, 0x0000c070, 0x0cc1e000, 0xc630f81b, 0xc0301806, 0x00000cc0, 0x30000000, 0x0cc0f0cc, 0xc030f033, 0x30cc330c, 0x30000003, 0x33030000, 0x8cc1e0cc, 0xc230cc33, 0xc0cc3308, 0x0c630600, 0x318ce3b8, 0x0cc318cc, 0xc330b433, 0x30cc318c, 0x06060cc3, 0x00038180, 0x00c00018, 0x8000c000, 0x000c000d, 0x30030000, 0x00000000, 0x00000000, 0x00001800, 0x00000000, 0x300c0000, 0x0409e0c0, 0x8481e000, 0xc3300c3f, 0x80180c06, 0x000c0781, 0x18000000, 0x0c00c0ec, 0xc1f0d830, 0x30cc1800, 0x180c0303, 0x30060000, 0x8cc330ec, 0xc0318c01, 0xc0cc3180, 0x0c330600, 0x318de3f8, 0x0cc318cc, 0xc3303003, 0x3078318c, 0x0c060643, 0x0006c180, 0x07c0f018, 0x81e0f81e, 0xf06c3701, 0x30330f00, 0x1e07c1f8, 0x0ee37076, 0xc330fc3e, 0x30c6318c, 0x300c0fc3, 0x0e0f20c0, 0x0001e000, 0x8180781b, 0x80180013, 0x000c1fe1, 0x0c000000, 0x0600c0dc, 0xc300cc1c, 0xe0780c07, 0x0c0c0303, 0x180c03f0, 0x87c330ec, 0xc1f18c01, 0xc0fc0187, 0x0c1f0600, 0x318fe358, 0x07c3187c, 0xc330300e, 0xe030358c, 0x18060301, 0x000c6180, 0x0cc18030, 0xc330cc33, 0xc0dc1987, 0x301b0c00, 0x330cc358, 0x0dc198cc, 0xc3301803, 0x306c358c, 0x00060643, 0x1b060180, 0x0000c000, 0xc0c0c01b, 0x8018001e, 0x003f0781, 0x060003f0, 0x0300c0cc, 0xc301fc30, 0x00cc0c0c, 0x0c000003, 0x0c0c0000, 0x8cc3f00c, 0xc0318c01, 0xc0cc3980, 0x0c330660, 0x318f6318, 0x06c3980c, 0xc3303018, 0xc078358c, 0x30060180, 0x00000180, 0x0cc1f000, 0x83f0cc03, 0xc0cc1981, 0x301f0c00, 0x330cc358, 0x00c198cc, 0xc330181e, 0x3038358c, 0x300c0303, 0x318000c0, 0x80000000, 0xc6607c3f, 0xc030000c, 0x300c0cc0, 0x03030000, 0x0180c0cc, 0xc330c033, 0x30cc0c0c, 0x180c0303, 0x000603f0, 0x8cc3308c, 0xc230cc33, 0xc0cc3300, 0x8c630660, 0x318e6318, 0x0cc1f00c, 0x83303033, 0xc0cc1f07, 0x600609c0, 0x00000180, 0x0cc19800, 0x8030cc33, 0xc0cc1f01, 0x30330cc0, 0x330cc358, 0x00c198cc, 0x8330d830, 0xe06c1f07, 0x300c0981, 0x318000c0, 0x0000c000, 0x8630301b, 0x6060001b, 0x300c0000, 0x01830000, 0x0fc3f078, 0x81e0c01e, 0xe0780c07, 0x300c0301, 0x0c030000, 0x07e33078, 0xe3f87e1f, 0xe0cc3e01, 0xfe6383c1, 0x1f0c6318, 0x0ce3c01e, 0x01e0781e, 0xe0cc1b03, 0xc01e0fc1, 0x000001e0, 0x07e3f000, 0xc1e0f81e, 0xf0ce1803, 0xfc338783, 0x1e0cc318, 0x01e1f07c, 0x06e0701f, 0x80c61b03, 0x30380fc1, 0x3f800070, 0x0000c000, 0x0000301b, 0x00000000, 0x18000000, 0x00800000, 0x00000000, 0x00000000, 0x00000000, 0x60060000, 0x00018000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x80000000, 0x7f800000, 0x00000000, 0x00000000, 0x00000f00, 0x00000000, 0x00000000, 0x0001800c, 0x00000000, 0xf0000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0003c01e, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; 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_red(void) { return make_colour(0xff0000, 0xff); } Colour make_green(void) { return make_colour(0x00ff00, 0xff); } Colour make_blue(void) { return make_colour(0x0000ff, 0xff); } Colour make_cyan(void) { return make_colour(0x00ffff, 0xff); } Colour make_pink(void) { return make_colour(0xff00ff, 0xff); } Colour make_yellow(void) { return make_colour(0xffff00, 0xff); } Colour make_aliceblue(void) { return make_colour(0xf0f8ff, 0xff); } 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; } Colour col_mul(Colour a, Colour b) { Colour r; r.r = (a.r * b.r) >> 8; r.g = (a.g * b.g) >> 8; r.b = (a.b * b.b) >> 8; r.a = (a.a * b.a) >> 8; return r; } void ren_begin(Renderer* r, Colour* t, int* d, int w, int h) { r->t = t; r->w = w; r->h = h; r->d = d; r->clip.x = 0; r->clip.y = 0; r->clip.w = w; r->clip.h = h; } void ren_end(Renderer* r) { (void)r; } void ren_clear(Renderer* r) { Colour* d = r->t, b = { 0 }; int i = 0, e = r->w * r->h; for (i = 0; i < e; i++, d++) *d = b; } void ren_clearc(Renderer* r, Colour c) { Colour* d = r->t; int i = 0, e = r->w * r->h; for (i = 0; i < e; i++, d++) *d = c; } void ren_cleard(Renderer* r, int depth) { int* d = r->d; int i = 0, e = r->w * r->h; for (i = 0; i < e; i++, d++) *d = depth; } void ren_clip(Renderer* r, const Rect* c) { r->clip = *c; } void ren_point(Renderer* r, Colour c, int x, int y) { if (x < 0) return; if (y < 0) return; if (x >= r->w) return; if (y >= r->h) return; r->t[x + y * r->w] = c; } void ren_char( Renderer* r, Colour c, int x, int y, char ch ) { int i, j, k, l, ex, ey, s; Colour* dst; Rect re = { 0, 0, 10, 10 }; Rect sub = { 0, 0, 10, 10 }; re.x = x; re.y = y; sub.x = (ch - ' ') * 10; rect_clips(&re, &sub, &r->clip); ex = re.x + re.w; ey = re.y + re.h; dst = r->t + (re.x + re.y * r->w); s = r->w - re.w; for (j = re.y, l = sub.y; j < ey; j++, l++) { for (i = re.x, k = sub.x; i < ex; i++, k++) { int si = k + l * font_w; unsigned bits = font_data[si >> 5]; int bit = bits & 1 << (si & 0x1f); if (bit) *dst = blend(*dst, c); dst++; } dst += s; } } void ren_text( Renderer* r, Colour c, int x, int y, const char* t ) { const char* s; for (s = t; *s; s++, x += 10) ren_char(r, c, x, y, *s); } void swap_vert(int* a, int* b, int ec) { int t, i; for (i = 0; i < ec; i++) { t = a[i]; a[i] = b[i]; b[i] = t; } } int tri_hand( const int* v0, const int* v1, const int* v2 ) { return (v1[0] - v0[0]) * (v2[1] - v0[1]) - (v2[0] - v0[0]) * (v1[1] - v0[1]) > 0; } Colour sample_tex( const Bitmap* t, int u, int v ) { int x = (u * t->w) >> fbits; int y = (v * t->h) >> fbits; x &= t->w - 1; y &= t->h - 1; return t->p[x + y * t->w]; } typedef struct { int x, y; int dx, dy; int sx, sy; int e; } Line; void init_line( Line* l, const int* f, const int* t ) { const int dx = abs(t[0] - f[0]); const int dy = -abs(t[1] - f[1]); l->e = dx + dy; l->dx = dx; l->dy = dy; l->sx = f[0] < t[0]? 1: -1; l->sy = f[1] < t[1]? 1: -1; l->x = f[0]; l->y = f[1]; } void init_lerp( Line* l, int a, int b, int x, int y ) { int f[2], t[2]; f[0] = a; f[1] = x; t[0] = b; t[1] = y; init_line(l, f, t); } void step_line( Line* l ) { const int e2 = l->e * 2; const int dx = l->dx; const int dy = l->dy; if (e2 > dy) { l->e += dy; l->x += l->sx; } if (e2 <= dx) { l->e += dx; l->y += l->sy; } } void ren_tri( Renderer* r, const int* iv0, const int* iv1, const int* iv2, const Bitmap* tex ) { #define ic 6 #define ec 8 int ha, i; int v0[ec], v1[ec], v2[ec]; Line ne, fe; /* near and far edges */ Line ni[ic], fi[ic]; /* near and far interpolants */ Line xi[ic]; /* scanline interpolant */ vec_cpy(v0, iv0, ec); vec_cpy(v1, iv1, ec); vec_cpy(v2, iv2, ec); if (v0[1] > v1[1]) swap_vert(v0, v1, ec); if (v1[1] > v2[1]) swap_vert(v1, v2, ec); if (v0[1] > v1[1]) swap_vert(v0, v1, ec); ha = tri_hand(v0, v1, v2); if (ha) { init_line(&ne, v0, v2); init_line(&fe, v0, v1); for (i = 0; i < ic; i++) { init_lerp( &ni[i], v0[2 + i], v2[2 + i], v0[1], v2[1] ); init_lerp( &fi[i], v0[2 + i], v1[2 + i], v0[1], v1[1] ); } } else { init_line(&ne, v0, v1); init_line(&fe, v0, v2); for (i = 0; i < ic; i++) { init_lerp( &ni[i], v0[2 + i], v1[2 + i], v0[1], v1[1] ); init_lerp( &fi[i], v0[2 + i], v2[2 + i], v0[1], v2[1] ); } } #define scanline\ int x;\ for (i = 0; i < ic; i++)\ init_lerp(\ &xi[i],\ ni[i].x, fi[i].x,\ ne.x, fe.x\ );\ for (x = ne.x; x < fe.x; x++) {\ Colour col;\ col.r = (xi[3].x * 255) >> fbits;\ col.g = (xi[4].x * 255) >> fbits;\ col.b = (xi[5].x * 255) >> fbits;\ col.a = 0xff;\ if (xi[0].x < r->d[x + fe.y * r->w]) { \ r->t[x + fe.y * r->w] = col_mul(\ sample_tex(tex, xi[1].x, xi[2].x),\ col\ );\ r->d[x + fe.y * r->w] = xi[0].x; \ } \ for (i = 0; i < ic; i++)\ while (xi[i].y != x)\ step_line(&xi[i]);\ }\ step_line(&fe);\ for (i = 0; i < ic; i++)\ while (fi[i].y != fe.y)\ step_line(&fi[i]);\ while (ne.y != fe.y)\ step_line(&ne);\ for (i = 0; i < ic; i++)\ while (ni[i].y != ne.y)\ step_line(&ni[i]); while (fe.y < v1[1]) { scanline } if (ha) { init_line(&fe, v1, v2); for (i = 0; i < ic; i++) init_lerp( &fi[i], v1[2 + i], v2[2 + i], v1[1], v2[1] ); } else { init_line(&ne, v1, v2); for (i = 0; i < ic; i++) init_lerp( &ni[i], v1[2 + i], v2[2 + i], v1[1], v2[1] ); } while (fe.y < v2[1]) { scanline } #undef scanline #undef ic #undef ec }