2b5b6aafd6cb40f04585307d2a47018afb5c7470
[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_start(void)
38 {
39 }
40
41 void dna_update(void)
42 {
43         static int x = 160;
44         static int y = 120;
45         static int sz = 24;
46
47         if(bnstate & PAD_UP) {
48                 if(y > 0) y--;
49         } else if(bnstate & PAD_DOWN) {
50                 if(y < 239) y++;
51         }
52         if(bnstate & PAD_LEFT) {
53                 if(y > 0) x--;
54         } else if(bnstate & PAD_RIGHT) {
55                 if(y < 319) x++;
56         }
57         if(bndiff & bnstate & PAD_A) {
58                 if(sz > 0) sz--;
59         } else if(bndiff & bnstate & PAD_B) {
60                 if(sz < 32) sz++;
61         }
62
63         particle(x, y, sz);
64 }
65
66 static void particle(int x, int y, int pixsz)
67 {
68         int tile, offs = 4, invoffs, szlevel;
69         static int offstab[] = {
70                 4, 4, 4, 4, 4, 4, 4, 4,
71                 4, 8, 8, 8, 8, 8, 8, 8,
72
73                 8, 14, 14, 15, 15, 15, 16, 16,
74                 16, 14, 14, 15, 15, 15, 16, 16,
75                 16
76         };
77
78         dbgval[0] = pixsz;
79
80         if(pixsz <= 2) {
81                 szlevel = 0;
82                 pixsz = 2;
83                 tile = VDP_ADDR2TILE(SPRITE_BASE);
84         } else if(pixsz <= 16) {
85                 szlevel = (pixsz - 1) >> 1;
86                 tile = VDP_ADDR2TILE(SPRITE_BASE) + szlevel;
87         } else if(pixsz <= 24) {
88                 szlevel = 8;
89                 tile = VDP_ADDR2TILE(SPRITE_BASE) + 8;
90         } else if(pixsz <= 32) {
91                 szlevel = 9;
92                 tile = VDP_ADDR2TILE(SPRITE_BASE) + 12;
93         } else {
94                 szlevel = 9;
95                 tile = VDP_ADDR2TILE(SPRITE_BASE) + 12;
96                 pixsz = 32;
97         }
98
99         dbgval[1] = szlevel;
100         dbgval[2] = tile;
101
102         offs = offstab[pixsz];
103
104         switch(szlevel) {
105         case 0:
106         case 1:
107         case 2:
108         case 3:
109                 spr_add(x - offs, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG), SPR_SIZE(1, 1));
110                 break;
111
112         case 4: /* 10x10 */
113         case 5: /* 12x12 */
114         case 6: /* 14x14 */
115         case 7: /* 16x16 */
116                 spr_add(x - offs, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG), SPR_SIZE(1, 1));
117                 spr_add(x, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_HFLIP), SPR_SIZE(1, 1));
118                 spr_add(x - offs, y, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_VFLIP), SPR_SIZE(1, 1));
119                 spr_add(x, y, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_HVFLIP), SPR_SIZE(1, 1));
120                 break;
121
122         case 8: /* 17x17 - 24x24 */
123         default: /* 25x25 - 32x32 */
124                 invoffs = 16 - offs;
125                 spr_add(x - offs, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG), SPR_SIZE(2, 2));
126                 spr_add(x - invoffs, y - offs, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_HFLIP), SPR_SIZE(2, 2));
127                 spr_add(x - offs, y - invoffs, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_VFLIP), SPR_SIZE(2, 2));
128                 spr_add(x - invoffs, y - invoffs, VDP_TILENAME(tile, 1, VDP_TILE_FG | VDP_TILE_HVFLIP), SPR_SIZE(2, 2));
129                 break;
130         }
131
132         dbgval[3] = offs;
133 }