testing stuff
[retrocrawl] / src / game.c
diff --git a/src/game.c b/src/game.c
new file mode 100644 (file)
index 0000000..b4baeca
--- /dev/null
@@ -0,0 +1,90 @@
+#include <string.h>
+#include "game.h"
+#include "data_test.h"
+#include "gfx.h"
+
+#define TILE_W 32
+#define TILE_H 16
+
+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];
+
+int game_init(void)
+{
+       int i;
+
+       for(i=0; i<2; i++) {
+               convert_tile_data(test_tiles[i], test_tiles_cpix[i][0]);
+       }
+       return 0;
+}
+
+void game_draw(void)
+{
+       draw_tile(0, 32, 16, 0);
+}
+
+
+void draw_tile(int tid, int x, int y, int light)
+{
+       int i;
+
+       unsigned char *dest = bplptr[0] + (y * SCANSZ * NBPL) + x / 8;
+       unsigned char *src = test_tiles[tid];
+
+       for(i=0; i<TILE_H * NBPL; i++) {
+               memcpy(dest, src, TILE_W / 8);
+               dest += SCANSZ;
+               src += TILE_W / 8;
+       }
+}
+
+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)
+{
+       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);
+               }
+       }
+}