sprite test
[retrocrawl] / tools / conv_sprite.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5
6 #include HDRFILE
7
8 #define MAX_SPRITE_WIDTH        16
9
10 static void conv_sprite(int slice, unsigned char *img, int width, int height, int pitch);
11 static void conv_img_data(int sub, uint16_t *out, unsigned char *img, int width,
12                 int height, int pitch);
13
14 int main(int argc, char **argv)
15 {
16         int i, nslices;
17
18         nslices = (width + MAX_SPRITE_WIDTH - 1) / MAX_SPRITE_WIDTH;
19         fprintf(stderr, "source image: %dx%d. splitting into %d slices\n",
20                         width, height, nslices);
21
22         printf("\t.data\n\n");
23
24         for(i=0; i<nslices; i++) {
25                 int xstart = i * MAX_SPRITE_WIDTH;
26                 int spr_w = width - xstart;
27                 if(spr_w > MAX_SPRITE_WIDTH) spr_w = MAX_SPRITE_WIDTH;
28
29                 conv_sprite(i, header_data + xstart, spr_w, height, width);
30         }
31
32         printf("\n\t.global sprpal\n");
33         printf("sprpal:\n");
34         for(i=0; i<16; i++) {
35                 int r = header_data_cmap[i][0] >> 4;
36                 int g = header_data_cmap[i][1] >> 4;
37                 int b = header_data_cmap[i][2] >> 4;
38                 unsigned int col = ((r & 0xf) << 8) | ((g & 0xf) << 4) | (b & 0xf);
39                 printf("\t.short 0x%03x\n", col);
40         }
41 }
42
43 static void conv_sprite(int slice, unsigned char *img, int width, int height, int pitch)
44 {
45         int i, j;
46         uint16_t *sprdata;
47
48         for(i=0; i<2; i++) {
49                 printf("\n\t.global spr%d%c\n", slice, i == 0 ? 'a' : 'b');
50                 printf("spr%d%c:\n", slice, i == 0 ? 'a' : 'b');
51                 printf("\t.short 0, 0\n");              /* position x/y, vstop */
52
53                 sprdata = malloc(height * 2 * sizeof *sprdata);
54                 conv_img_data(i, sprdata, img, width, height, pitch);
55
56                 for(j=0; j<height; j++) {
57                         printf("\t.short 0x%x, 0x%x\n", (unsigned int)sprdata[j * 2],
58                                         (unsigned int)sprdata[j * 2 + 1]);
59                 }
60
61                 printf("\t.long 0\n");  /* end of sprite CW */
62                 free(sprdata);
63         }
64 }
65
66 static void conv_img_data(int sub, uint16_t *out, unsigned char *img, int width,
67                 int height, int pitch)
68 {
69         int i, j, inshift;
70         uint16_t bit0, bit1;
71
72         /* sprite 0: low order bits, sprite 1: high order bits */
73         inshift = sub == 0 ? 0 : 2;
74
75         for(i=0; i<height; i++) {
76                 bit0 = bit1 = 0;
77                 for(j=0; j<width; j++) {
78                         bit0 = (bit0 << 1) | ((img[j] >> inshift) & 1);
79                         bit1 = (bit1 << 1) | ((img[j] >> (inshift + 1)) & 1);
80                 }
81                 if(width < MAX_SPRITE_WIDTH) {
82                         bit0 <<= MAX_SPRITE_WIDTH - width;
83                         bit1 <<= MAX_SPRITE_WIDTH - width;
84                 }
85                 *out++ = bit0;
86                 *out++ = bit1;
87                 img += pitch;
88         }
89 }
90