starting up on the sprite system
[retrocrawl] / src / game.c
index b4baeca..37c39b2 100644 (file)
 #include <string.h>
 #include "game.h"
-#include "data_test.h"
 #include "gfx.h"
+#include "copper.h"
+#include "data.h"
 
-#define TILE_W 32
-#define TILE_H 16
+#include "hwregs.h"    /* XXX */
+
+#define TILE_W 64
+#define TILE_H 32
 
 void draw_tile(int tid, int x, int y, int light);
-void convert_tile_data(unsigned char *dest, const char *src);
 
-static unsigned char test_tiles[2][TILE_W * TILE_H / 8 * NBPL];
+static uint16_t *sprdata[NUM_HWSPRITES];
+static uint16_t nullspr[] = {
+       0x2000, 0x2100,
+       0x0000, 0x0000,
+       0x0000, 0x0000
+};
+
+#define SPR_POS(x, y) \
+       (((uint16_t)((x) + 0x40) & 0xff) | ((uint16_t)((y) + 0x2c) << 8))
+
 
 int game_init(void)
 {
-       int i;
+       int i, sprpos[] = {136, 76};
+       uint32_t nullspr_addr = (intptr_t)nullspr;
+
+       REG_COLOR0 = 0x221;
+       REG_COLOR1 = 0x222;
+       REG_COLOR2 = 0x332;
+       REG_COLOR3 = 0x433;
+       REG_COLOR4 = 0x543;
+       REG_COLOR5 = 0x554;
+       REG_COLOR6 = 0x654;
+       REG_COLOR7 = 0x765;
+
+       for(i=0; i<16; i++) {
+               REG_COLOR_PTR[i + 16] = sprpal[i];
+       }
 
-       for(i=0; i<2; i++) {
-               convert_tile_data(test_tiles[i], test_tiles_cpix[i][0]);
+       sprdata[0] = spr0a;
+       sprdata[1] = spr0b;
+       sprdata[2] = spr1a;
+       sprdata[3] = spr1b;
+       sprdata[4] = spr2a;
+       sprdata[5] = spr2b;
+
+       for(i=0; i<8; i++) {
+               int reg = REGN_SPR0PTH + i * 4;
+               if(i < NUM_HWSPRITES) {
+                       uint16_t sx, sy, vstop;
+                       /* initialize active sprites, and set the sprite pointers */
+                       uint32_t addr = (intptr_t)sprdata[i];
+                       add_copper(COPPER_MOVE(reg, addr >> 16));
+                       add_copper(COPPER_MOVE(reg + 2, addr));
+
+                       sx = 0x80 + sprpos[0] + (i >> 1) * 16;
+                       sy = 0x2c + sprpos[1];
+                       vstop = sy + SPRITE_HEIGHT;
+
+                       sprdata[i][0] = (sx >> 1) | (sy << 8);
+                       sprdata[i][1] = (vstop << 8) | ((vstop >> 7) & 2) |
+                               (sx & 1) | ((i & 1) ? 0x80 : 0);
+               } else {
+                       /* point the sprite pointers to the null sprite for the rest */
+                       add_copper(COPPER_MOVE(reg, nullspr_addr >> 16));
+                       add_copper(COPPER_MOVE(reg + 2, nullspr_addr));
+               }
        }
-       return 0;
-}
 
-void game_draw(void)
-{
-       draw_tile(0, 32, 16, 0);
+       *copperlist_end = COPPER_END;
+
+       return 0;
 }
 
+#define XTILES 5
+#define YTILES 13
 
-void draw_tile(int tid, int x, int y, int light)
+void game_draw(void)
 {
-       int i;
+       int i, j, xoffs, yoffs, ntiles;
 
-       unsigned char *dest = bplptr[0] + (y * SCANSZ * NBPL) + x / 8;
-       unsigned char *src = test_tiles[tid];
+       /* reset sprite data */
 
-       for(i=0; i<TILE_H * NBPL; i++) {
-               memcpy(dest, src, TILE_W / 8);
-               dest += SCANSZ;
-               src += TILE_W / 8;
+       yoffs = 0;
+       for(i=0; i<YTILES; i++) {
+               xoffs = i & 1 ? TILE_W / 2 : 0;
+               ntiles = i & 1 ? XTILES - 1 : XTILES;
+               for(j=0; j<ntiles; j++) {
+                       draw_tile(0, xoffs, yoffs, 0);
+                       xoffs += TILE_W;
+               }
+               yoffs += TILE_H / 2;
        }
 }
 
-static inline int charpix_color(char c)
-{
-       switch(c) {
-       case '#':
-               return 1;
-       case 'x':
-               return 2;
-       case '@':
-               return 3;
-       case 'o':
-               return 4;
-       default:
-       case '.':
-               break;
-       }
-       return 0;
-}
 
-#define TILE_SCANSZ    (TILE_W / 8)
-void convert_tile_data(unsigned char *dest, const char *src)
+void draw_tile(int tid, int x, int y, int light)
 {
-       int i, j, k;
-       unsigned char *bptr[NBPL];
-
-       for(i=0; i<NBPL; i++) {
-               bptr[i] = dest + TILE_SCANSZ * i;
-       }
-
-       for(i=0; i<TILE_H; i++) {
-               for(j=0; j<TILE_W; j++) {
-                       int col = charpix_color(*src++);
-
-                       for(k=0; k<NBPL; k++) {
-                               *bptr[k] = (*bptr[k] << 1) | (col & 1);
-                               col >>= 1;
-
-                               if((j & 7) == 7) {
-                                       bptr[k]++;
-                               }
-                       }
-               }
-
-               for(j=0; j<NBPL; j++) {
-                       bptr[j] += TILE_SCANSZ * (NBPL - 1);
-               }
-       }
+       unsigned char *dest = bplptr[0] + (y * SCANSZ * NBPL) + x / 8;
+       unsigned char *src = test_tile;
+
+       wait_blit();
+
+       REG32_BLTCON = BLTCON_USEA | BLTCON_USEB | BLTCON_USEC | BLTCON_USED |
+               BLTCON_LF(0xca);
+       REG32_BLTAFLWM = 0xffffffff;
+       REG_BLTAMOD = 0;
+       REG_BLTBMOD = 0;
+       REG_BLTCMOD = SCANSZ - TILE_W / 8;
+       REG_BLTDMOD = SCANSZ - TILE_W / 8;
+       REG32_BLTAPT = (intptr_t)test_tile_mask;
+       REG32_BLTBPT = (intptr_t)src;
+       REG32_BLTCPT = (intptr_t)dest;
+       REG32_BLTDPT = (intptr_t)dest;
+       REG_BLTSIZE = BLTSIZE(TILE_W, TILE_H * NBPL);
 }