finished the tile deduplication and tilemap generation in pngdump
[mdlife] / src / part_dna.c
1 #include <stdio.h>
2 #include "sprite.h"
3 #include "vdp.h"
4 #include "demo.h"
5 #include "pad.h"
6 #include "parts.h"
7 #include "debug.h"
8
9 static void particle(int x, int y, int sz);
10
11 #define SPRITE_BASE             0x8000
12 extern uint16_t cellspr_data[], cellspr_data_end[];
13 extern unsigned char cellspr_cmap[], cellspr_cmap_end[];
14
15
16 void dna_init(void)
17 {
18         int i;
19         uint16_t *src;
20         unsigned char *cptr;
21
22         cptr = cellspr_cmap;
23         for(i=0; i<16; i++) {
24                 vdp_setcolor(0, i, i, i, i);
25                 vdp_setcolor(1, i, cptr[0] >> 4, cptr[1] >> 4, cptr[2] >> 4);
26                 cptr += 3;
27         }
28
29         /* upload sprite tiles */
30         src = cellspr_data;
31         vdp_setup_addr(VDP_VRAM, SPRITE_BASE);
32         while(src < cellspr_data_end) {
33                 VDP_DATA = *src++;
34         }
35 }
36
37 void dna_update(void)
38 {
39         static int x = 160;
40         static int y = 120;
41         static int sz = 24;
42
43         if(bnstate & PAD_UP) {
44                 if(y > 0) y--;
45         } else if(bnstate & PAD_DOWN) {
46                 if(y < 239) y++;
47         }
48         if(bnstate & PAD_LEFT) {
49                 if(y > 0) x--;
50         } else if(bnstate & PAD_RIGHT) {
51                 if(y < 319) x++;
52         }
53         if(bndiff & bnstate & PAD_A) {
54                 if(sz > 0) sz--;
55         } else if(bndiff & bnstate & PAD_B) {
56                 if(sz < 32) sz++;
57         }
58
59         particle(x, y, sz);
60 }
61
62 static void particle(int x, int y, int pixsz)
63 {
64         int tile, offs = 4, invoffs, szlevel;
65         static int offstab[] = {
66                 4, 4, 4, 4, 4, 4, 4, 4,
67                 4, 8, 8, 8, 8, 8, 8, 8,
68
69                 8, 14, 14, 15, 15, 15, 16, 16,
70                 16, 14, 14, 15, 15, 15, 16, 16,
71                 16
72         };
73
74         dbgval[0] = pixsz;
75
76         if(pixsz <= 2) {
77                 szlevel = 0;
78                 pixsz = 2;
79                 tile = VDP_ADDR2TILE(SPRITE_BASE);
80         } else if(pixsz <= 16) {
81                 szlevel = (pixsz - 1) >> 1;
82                 tile = VDP_ADDR2TILE(SPRITE_BASE) + szlevel;
83         } else if(pixsz <= 24) {
84                 szlevel = 8;
85                 tile = VDP_ADDR2TILE(SPRITE_BASE) + 8;
86         } else if(pixsz <= 32) {
87                 szlevel = 9;
88                 tile = VDP_ADDR2TILE(SPRITE_BASE) + 12;
89         } else {
90                 szlevel = 9;
91                 tile = VDP_ADDR2TILE(SPRITE_BASE) + 12;
92                 pixsz = 32;
93         }
94
95         dbgval[1] = szlevel;
96         dbgval[2] = tile;
97
98         offs = offstab[pixsz];
99
100         switch(szlevel) {
101         case 0:
102         case 1:
103         case 2:
104         case 3:
105                 spr_add(x - offs, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG), SPR_SIZE(1, 1));
106                 break;
107
108         case 4: /* 10x10 */
109         case 5: /* 12x12 */
110         case 6: /* 14x14 */
111         case 7: /* 16x16 */
112                 spr_add(x - offs, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG), SPR_SIZE(1, 1));
113                 spr_add(x, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_HFLIP), SPR_SIZE(1, 1));
114                 spr_add(x - offs, y, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_VFLIP), SPR_SIZE(1, 1));
115                 spr_add(x, y, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_HVFLIP), SPR_SIZE(1, 1));
116                 break;
117
118         case 8: /* 17x17 - 24x24 */
119         default: /* 25x25 - 32x32 */
120                 invoffs = 16 - offs;
121                 spr_add(x - offs, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG), SPR_SIZE(2, 2));
122                 spr_add(x - invoffs, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_HFLIP), SPR_SIZE(2, 2));
123                 spr_add(x - offs, y - invoffs, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_VFLIP), SPR_SIZE(2, 2));
124                 spr_add(x - invoffs, y - invoffs, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_HVFLIP), SPR_SIZE(2, 2));
125                 break;
126         }
127
128         dbgval[3] = offs;
129 }