new sprite system first successfull test
[retrocrawl] / src / game.c
1 #include <string.h>
2 #include "game.h"
3 #include "gfx.h"
4 #include "copper.h"
5 #include "data.h"
6 #include "sprite.h"
7
8 #include "hwregs.h"     /* XXX */
9
10 #define TILE_W  64
11 #define TILE_H  32
12
13 void draw_tile(int tid, int x, int y, int light);
14
15 static uint16_t *sprdata[NUM_HWSPRITES];
16
17 /* hardcoded test sprite */
18 static struct sprite test_sprite;
19
20 int game_init(void)
21 {
22         int i;
23
24         REG_COLOR0 = 0x221;
25         REG_COLOR1 = 0x222;
26         REG_COLOR2 = 0x332;
27         REG_COLOR3 = 0x433;
28         REG_COLOR4 = 0x543;
29         REG_COLOR5 = 0x554;
30         REG_COLOR6 = 0x654;
31         REG_COLOR7 = 0x765;
32
33         for(i=0; i<16; i++) {
34                 REG_COLOR_PTR[i + 16] = sprpal[i];
35         }
36
37         sprdata[0] = spr0a;
38         sprdata[1] = spr0b;
39         sprdata[2] = spr1a;
40         sprdata[3] = spr1b;
41         sprdata[4] = spr2a;
42         sprdata[5] = spr2b;
43
44         test_sprite.width = test_sprite.height = 48;
45         test_sprite.origx = 24;
46         test_sprite.origy = 24;
47         test_sprite.img = test_sprite.mask = 0;
48         test_sprite.hwslices = 3;
49         for(i=0; i<8; i++) {
50                 test_sprite.hwspr[i] = i < 6 ? sprdata[i] : 0;
51         }
52
53         return 0;
54 }
55
56 #define XTILES  5
57 #define YTILES  13
58
59 void game_draw(void)
60 {
61         int i, j, xoffs, yoffs, ntiles;
62
63         yoffs = 0;
64         for(i=0; i<YTILES; i++) {
65                 xoffs = i & 1 ? TILE_W / 2 : 0;
66                 ntiles = i & 1 ? XTILES - 1 : XTILES;
67                 for(j=0; j<ntiles; j++) {
68                         draw_tile(0, xoffs, yoffs, 0);
69                         xoffs += TILE_W;
70                 }
71                 yoffs += TILE_H / 2;
72         }
73
74         begin_sprites();
75         draw_sprite(&test_sprite, 160, 100);
76         end_sprites();
77 }
78
79
80 void draw_tile(int tid, int x, int y, int light)
81 {
82         unsigned char *dest = bplptr[0] + (y * SCANSZ * NBPL) + x / 8;
83         unsigned char *src = test_tile;
84
85         wait_blit();
86
87         REG32_BLTCON = BLTCON_USEA | BLTCON_USEB | BLTCON_USEC | BLTCON_USED |
88                 BLTCON_LF(0xca);
89         REG32_BLTAFLWM = 0xffffffff;
90         REG_BLTAMOD = 0;
91         REG_BLTBMOD = 0;
92         REG_BLTCMOD = SCANSZ - TILE_W / 8;
93         REG_BLTDMOD = SCANSZ - TILE_W / 8;
94         REG32_BLTAPT = (intptr_t)test_tile_mask;
95         REG32_BLTBPT = (intptr_t)src;
96         REG32_BLTCPT = (intptr_t)dest;
97         REG32_BLTDPT = (intptr_t)dest;
98         REG_BLTSIZE = BLTSIZE(TILE_W, TILE_H * NBPL);
99 }