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