added enemy sprites, tanked the framerate... need to investigate
[gbajam22] / src / voxscape.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <math.h>
6 #include <assert.h>
7 #include "voxscape.h"
8 #include "debug.h"
9
10 /* hardcoded dimensions for the GBA */
11 #define FBWIDTH         240
12 #define FBHEIGHT        160
13 #define FBPITCH         240
14 /* map size */
15 #define XSZ                     512
16 #define YSZ                     512
17 #define XSHIFT          9
18 #define XMASK           0x1ff
19 #define YMASK           0x1ff
20 #define HSCALE          40
21
22
23 #define NO_LERP
24
25 #define XLERP(a, b, t, fp) \
26         ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
27
28 enum {
29         SLICELEN        = 1
30 };
31
32 struct voxscape {
33         int xsz, ysz;
34         unsigned char *height;
35         unsigned char *color;
36         int xshift, xmask, ymask;
37
38         int hfilt, cfilt;
39
40         /* framebuffer */
41         uint16_t *fb;
42         int fbwidth, fbheight;
43         int *coltop;
44         int horizon;
45
46         /* view */
47         int32_t x, y, angle;
48         int vheight;
49
50         /* projection */
51         int fov, znear, zfar;
52         int nslices;
53         int32_t *slicelen;
54         int proj_dist;
55
56         int zfog;       /* fog start Z (0: no fog) */
57         uint8_t fogcolor;
58
59         unsigned int valid;
60
61         struct vox_object *obj;
62         int num_obj, obj_stride;
63 };
64
65 int vox_quality = 1;
66 int *projlut;
67
68 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
69 {
70         struct voxscape *vox;
71
72         assert(xsz == XSZ && ysz == YSZ);
73
74         if(!(vox = calloc(1, sizeof *vox))) {
75                 return 0;
76         }
77         vox->height = himg;
78         vox->color = cimg;
79         vox->xsz = xsz;
80         vox->ysz = ysz;
81
82         vox->xmask = vox->xsz - 1;
83         vox->ymask = vox->ysz - 1;
84
85         vox->xshift = -1;
86         while(xsz) {
87                 xsz >>= 1;
88                 vox->xshift++;
89         }
90
91         vox->vheight = 80;
92         vox->proj_dist = 4;     /* TODO */
93
94         return vox;
95 }
96
97 void vox_free(struct voxscape *vox)
98 {
99         if(!vox) return;
100
101         free(vox->color);
102         free(vox->height);
103         free(vox->coltop);
104         free(vox->slicelen);
105         free(vox);
106 }
107
108 uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
109 {
110         if(data) {
111                 memcpy(vox->color, data, vox->xsz * vox->ysz);
112         }
113         return vox->color;
114 }
115
116 uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
117 {
118         if(data) {
119                 memcpy(vox->height, data, vox->xsz * vox->ysz);
120         }
121         return vox->height;
122 }
123
124 void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
125 {
126         vox->zfog = zstart;
127         vox->fogcolor = color;
128 }
129
130 #define H(x, y) \
131         vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
132 #define C(x, y) \
133         vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
134
135 #ifdef NO_LERP
136 #define vox_height(vox, x, y)   H(x, y)
137 #define vox_color(vox, x, y)    C(x, y)
138
139 #else
140
141 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
142 {
143         int32_t u, v;
144         int h00, h01, h10, h11, h0, h1;
145
146         if(!vox->hfilt) {
147                 return H(x, y);
148         }
149
150         h00 = H(x, y);
151         h01 = H(x, y + 0x10000);
152         h10 = H(x + 0x10000, y);
153         h11 = H(x + 0x10000, y + 0x10000);
154
155         u = x & 0xffff;
156         v = y & 0xffff;
157
158         h0 = XLERP(h00, h01, v, 16);
159         h1 = XLERP(h10, h11, v, 16);
160         return XLERP(h0, h1, u, 16);
161 }
162
163 int vox_color(struct voxscape *vox, int32_t x, int32_t y)
164 {
165         int32_t u, v;
166         int c00, c01, c10, c11, c0, c1;
167
168         if(!vox->cfilt) {
169                 return C(x, y);
170         }
171
172         c00 = C(x, y);
173         c01 = C(x, y + 0x10000);
174         c10 = C(x + 0x10000, y);
175         c11 = C(x + 0x10000, y + 0x10000);
176
177         u = x & 0xffff;
178         v = y & 0xffff;
179
180         c0 = XLERP(c00, c01, v, 16);
181         c1 = XLERP(c10, c11, v, 16);
182         return XLERP(c0, c1, u, 16);
183 }
184 #endif  /* !NO_LERP */
185
186
187 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
188 {
189         vox->hfilt = hfilt;
190         vox->cfilt = cfilt;
191 }
192
193 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
194 {
195         if(xres != vox->fbwidth) {
196                 if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
197                         panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
198                 }
199         }
200         vox->fb = fb;
201         vox->fbwidth = xres;
202         vox->fbheight = yres;
203         vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
204 }
205
206 void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
207 {
208         if(h < 0) {
209                 h = vox_height(vox, x, y) - h;
210         }
211
212         vox->x = x;
213         vox->y = y;
214         vox->vheight = h;
215         vox->angle = angle;
216
217         vox->valid &= ~SLICELEN;
218 }
219
220 void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
221 {
222         vox->fov = fov;
223         vox->znear = znear;
224         vox->zfar = zfar;
225
226         vox->nslices = vox->zfar - vox->znear;
227         if(!vox->slicelen) {
228                 if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
229                         panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
230                 }
231                 if(!(projlut = iwram_sbrk(vox->nslices * sizeof *projlut))) {
232                         panic(get_pc(), "vox_framebuf: failed to allocate projection table (%d)\n", vox->nslices);
233                 }
234         }
235
236         vox->valid &= ~SLICELEN;
237 }
238
239 /* algorithm:
240  * calculate extents of horizontal equidistant line from the viewer based on fov
241  * for each column step along this line and compute height for each pixel
242  * fill the visible (top) part of each column
243  */
244 ARM_IWRAM
245 void vox_render(struct voxscape *vox)
246 {
247         int i;
248
249         vox_begin(vox);
250
251         if(vox_quality) {
252                 for(i=0; i<vox->nslices; i++) {
253                         vox_render_slice(vox, i);
254                 }
255         } else {
256                 for(i=0; i<vox->nslices; i++) {
257                         if(i >= 10 && (i & 1) == 0) {
258                                 continue;
259                         }
260                         vox_render_slice(vox, i);
261                 }
262         }
263 }
264
265 ARM_IWRAM
266 void vox_begin(struct voxscape *vox)
267 {
268         int i;
269
270         memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
271
272         if(!(vox->valid & SLICELEN)) {
273                 float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
274                 for(i=0; i<vox->nslices; i++) {
275                         vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
276                         projlut[i] = (HSCALE << 8) / (vox->znear + i);
277                 }
278                 vox->valid |= SLICELEN;
279         }
280 }
281
282 ARM_IWRAM
283 void vox_render_slice(struct voxscape *vox, int n)
284 {
285         int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
286         int32_t x, y, len, xstep, ystep;
287         uint8_t color, last_col;
288         uint16_t *fbptr;
289         int proj;
290         struct vox_object *obj;
291
292         z = vox->znear + n;
293
294         len = vox->slicelen[n] >> 8;
295         xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
296         ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
297
298         x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
299         y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
300
301         /*proj = (HSCALE << 8) / (vox->znear + n);*/
302
303         for(i=0; i<FBWIDTH/2; i++) {
304                 col = i << 1;
305                 offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
306                 if(offs == last_offs) {
307                         hval = last_hval;
308                         color = last_col;
309                 } else {
310                         hval = vox->height[offs] - vox->vheight;
311                         hval = ((hval * projlut[n]) >> 8) + vox->horizon;
312                         if(hval > FBHEIGHT) hval = FBHEIGHT;
313                         color = vox->color[offs];
314                         last_offs = offs;
315                         last_hval = hval;
316                         last_col = color;
317                 }
318                 if(hval > vox->coltop[col]) {
319                         colstart = FBHEIGHT - hval;
320                         colheight = hval - vox->coltop[col];
321                         fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
322
323                         for(j=0; j<colheight; j++) {
324                                 *fbptr = color | ((uint16_t)color << 8);
325                                 fbptr += FBPITCH / 2;
326                         }
327                         vox->coltop[col] = hval;
328
329                         /* check to see if there's an object here */
330                         obj = vox->obj;
331                         for(j=0; j<vox->num_obj; j++) {
332                                 if(obj->offs == offs) {
333                                         obj->px = col;
334                                         obj->py = colstart;
335                                         obj->scale = projlut[n];
336                                 }
337                                 obj = (struct vox_object*)((char*)obj + vox->obj_stride);
338                         }
339                 }
340                 x += xstep;
341                 y += ystep;
342         }
343 }
344
345 ARM_IWRAM
346 void vox_sky_solid(struct voxscape *vox, uint8_t color)
347 {
348         int i, j, colheight;
349         uint16_t *fbptr;
350
351         for(i=0; i<FBWIDTH / 2; i++) {
352                 fbptr = vox->fb + i;
353                 colheight = FBHEIGHT - vox->coltop[i << 1];
354
355                 for(j=0; j<colheight; j++) {
356                         *fbptr = color | ((uint16_t)color << 8);
357                         fbptr += FBPITCH / 2;
358                 }
359         }
360 }
361
362 ARM_IWRAM
363 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
364 {
365         int i, j, colheight, t;
366         int d = FBHEIGHT - vox->horizon;
367         uint8_t grad[FBHEIGHT];
368         uint16_t *fbptr;
369
370         for(i=0; i<d; i++) {
371                 t = (i << 16) / d;
372                 grad[i] = XLERP(ctop, chor, t, 16);
373         }
374         for(i=d; i<FBHEIGHT; i++) {
375                 grad[i] = chor;
376         }
377
378         for(i=0; i<FBWIDTH / 2; i++) {
379                 fbptr = vox->fb + i;
380                 colheight = FBHEIGHT - vox->coltop[i << 1];
381
382                 for(j=0; j<colheight; j++) {
383                         *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
384                         fbptr += FBPITCH / 2;
385                 }
386         }
387 }
388
389 void vox_objects(struct voxscape *vox, struct vox_object *ptr, int count, int stride)
390 {
391         int i;
392         struct vox_object *obj;
393
394         vox->obj = ptr;
395         vox->num_obj = count;
396         vox->obj_stride = stride;
397
398         obj = ptr;
399         for(i=0; i<count; i++) {
400                 obj->offs = obj->y * XSZ + obj->x;
401                 emuprint("%d,%d -> %d", obj->x, obj->y, obj->offs);
402                 obj = (struct vox_object*)((char*)obj + stride);
403         }
404 }