git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-support/png2asset/tiles.h

      1 #pragma once
      2 #include <vector>
      3 
      4 using namespace std;
      5 
      6 #define BIT(VALUE, INDEX) (1 & ((VALUE) >> (INDEX)))
      7 
      8 struct Tile
      9 {
     10     vector< unsigned char > data;
     11     unsigned char pal;
     12 
     13     Tile(size_t size = 0) : data(size), pal(0) {}
     14     bool operator==(const Tile& t) const
     15     {
     16 //        return data == t.data && pal == t.pal; // probably, sometimes we need to take palette into account?
     17         return data == t.data;
     18     }
     19 
     20     const Tile& operator=(const Tile& t)
     21     {
     22         data = t.data;
     23         pal = t.pal;
     24         return *this;
     25     }
     26 
     27     enum PackMode {
     28         GB,
     29         NES,
     30         SGB,
     31         SMS,
     32         BPP1
     33     };
     34 
     35     vector< unsigned char > GetPackedData(PackMode pack_mode, int tile_w, int tile_h, int bpp) {
     36         vector< unsigned char > ret((tile_w / 8) * tile_h * bpp, 0);
     37         if(pack_mode == GB) {
     38             for(int j = 0; j < tile_h; ++j) {
     39                 for(int i = 0; i < 8; ++i) {
     40                     unsigned char col = data[8 * j + i];
     41                     ret[j * 2] |= BIT(col, 0) << (7 - i);
     42                     ret[j * 2 + 1] |= BIT(col, 1) << (7 - i);
     43                 }
     44             }
     45         }        
     46         else if(pack_mode == NES) {
     47             for(int j = 0; j < tile_h; ++j) {
     48                 for(int i = 0; i < 8; ++i) {
     49                     unsigned char col = data[8 * j + i];
     50                     ret[j] |= BIT(col, 0) << (7 - i);
     51                     ret[j + 8] |= BIT(col, 1) << (7 - i);
     52                 }
     53             }
     54         }
     55         else if(pack_mode == SGB)
     56         {
     57             for(int j = 0; j < tile_h; ++j) {
     58                 for(int i = 0; i < 8; ++i) {
     59                     unsigned char col = data[8 * j + i];
     60                     ret[j * 2] |= BIT(col, 0) << (7 - i);
     61                     ret[j * 2 + 1] |= BIT(col, 1) << (7 - i);
     62                     ret[(tile_h + j) * 2] |= BIT(col, 2) << (7 - i);
     63                     ret[(tile_h + j) * 2 + 1] |= BIT(col, 3) << (7 - i);
     64                 }
     65             }
     66         }
     67         else if(pack_mode == SMS)
     68         {
     69             for(int j = 0; j < tile_h; ++j) {
     70                 for(int i = 0; i < 8; ++i) {
     71                     unsigned char col = data[8 * j + i];
     72                     ret[j * 4] |= BIT(col, 0) << (7 - i);
     73                     ret[j * 4 + 1] |= BIT(col, 1) << (7 - i);
     74                     ret[j * 4 + 2] |= BIT(col, 2) << (7 - i);
     75                     ret[j * 4 + 3] |= BIT(col, 3) << (7 - i);
     76                 }
     77             }
     78         }
     79         else if(pack_mode == BPP1)
     80         {
     81             // Packs 8 pixel wide rows in order set by ExtractTile**()
     82             // Process all rows of pixels in the tile
     83             for(int j = 0; j < ((tile_w / 8) * tile_h); j++) {
     84                 // Pack each row of 8 pixels into one byte
     85                 for(int i = 0; i < 8; i++) {
     86                     unsigned char col = data[8 * j + i];
     87                     ret[j] |= BIT(col, 0) << (7 - i);
     88                 }
     89             }
     90         }
     91         return ret;
     92     }
     93 };
     94 
     95 
     96 Tile FlipH(const Tile& tile);
     97 Tile FlipV(const Tile& tile);

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.