sprites in progress
[retrocrawl] / src / sprite.c
1 #include <string.h>
2 #include "sprite.h"
3
4 #define MAX_SPRITES     32
5
6 static int scount;
7 static sprite *spr[MAX_SPRITES];
8 static sprite *hwspr[4];        /* 4 channels because we support only attached sprites */
9
10 void begin_sprites(void)
11 {
12         scount = 0;
13         memset(hwspr_used, 0, sizeof hwspr_used);
14 }
15
16 void end_sprites(void)
17 {
18         int i, j;
19
20         /* sort sprites by increasing start position */
21         for(i=0; i<scount; i++) {
22                 for(j=i+1; j<scount; j++) {
23                         if(spr[j]->y < spr[i]->y) {
24                                 struct sprite *tmp = spr[i];
25                                 spr[i] = spr[j];
26                                 spr[j] = tmp;
27                         }
28                 }
29         }
30
31         /* set sprite positions and populate copperlist to draw
32          * hardware sprites
33          */
34         for(i=0; i<scount; i++) {
35                 struct sprite *s = spr[i];
36                 uint16_t sx = 0x80 + s->x;
37                 uint16_t sy = 0x2c + s->y;
38                 uint16_t vstop = sy + s->height;
39
40                 int chan = -1;
41                 for(j=0; j<4; j++) {
42                         if(!hwspr[j] || hwspr[j]->y + hwspr[j]->height < s->y) {
43                                 chan = j;
44                                 hwspr[j] = s;
45                         }
46                 }
47                 if(chan == -1) continue;
48
49                 for(j=0; j<s->hwslices; j++) {
50                         s->hwspr[idx][0] = s->hwspr[idx + 1][0] = (sx >> 1) | (sy << 8);
51                         s->hwspr[idx][1] = s->hwspr[idx + 1][1] = (vstop << 8) |
52                                 ((vstop >> 7) & 2) | (sx & 1) | 0x80;
53                 }
54         }
55 }
56
57 void draw_sprite(struct sprite *s, int x, int y)
58 {
59         if(s->img) {
60                 /* blitter sprite */
61         } else {
62                 /* hw sprites */
63                 spr[scount++] = s;
64                 s->x = x - origx;
65                 s->y = y - origy;
66         }
67 }