summary refs log tree commit diff stats
path: root/tools/sprite_dumper/tileset_dumper.cpp
blob: c0cea76685c5d50ca4573ee5027d300ca4f5c2f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <Magick++.h>
#include <vector>
#include <string>
#include <map>
#include "common.h"

const int NUM_ROOMS = 1000;

struct RoomInfo {
  int width;
  int height;

  static std::map<int, RoomInfo> ReadFromRom(BufferView m3) {
    const int BASE_ADDR = 0xD2E1D8 + 12;
    const int ENTRY_LEN = 28;

    std::map<int, RoomInfo> output;

    for (int i=0; i<NUM_ROOMS; i++) {
      RoomInfo& ri = output[i];

      ri.width = ((m3.ReadByte(BASE_ADDR + i*ENTRY_LEN + 20) & 7) + 1) << 4;
      ri.height = (((m3.ReadByte(BASE_ADDR + i*ENTRY_LEN + 24) & 0x3F) >> 3) + 1) << 4;
    }

    return output;
  }
};

int main(int argc, char** argv) {
  if (argc < 3) {
    std::cout << "Usage: ./tileset_dumper [path to rom] {map ID}" << std::endl;
    return -1;
  }

  Magick::InitializeMagick(nullptr);

  Rom m3(argv[1]);

  int roomNum = std::stoi(argv[2]);
  auto roomInfos = RoomInfo::ReadFromRom(m3.buffer());
  std::cout << roomInfos[roomNum].width << "," << roomInfos[roomNum].height << std::endl;
  /*const unsigned long ROOM_TILES_BASE = 0x104D9CC;
  unsigned long metatilesInfoAddr = ROOM_TILES_BASE + 4 + (roomNum << 2);
  unsigned long metatilesOffset = m3.buffer().ReadFourBytes(metatilesInfoAddr);
  unsigned long metatilesAddr = metatilesOffset + ROOM_TILES_BASE;
  std::vector<char> metatiles = m3.buffer().Decompress(metatilesAddr);
  std::cout << (metatiles.size()/8) << std::endl;

  for (int i=0; i<10; i++) {
    int tile00 = metatiles[i*8+4];
    int tile01 = metatiles[i*8+5];
    int tile10 = metatiles[i*8+6];
    int tile11 = metatiles[i*8+7];

    int mask = metatiles[i*8+2];
    if ((mask & 0x1) == 0) tile00 = -1;
    if ((mask & 0x2) == 0) tile01 = -1;
    if ((mask & 0x4) == 0) tile10 = -1;
    if ((mask & 0x8) == 0) tile11 = -1;

    std::cout << tile00 << "," << tile01 << "," << tile10 << "," << tile11 << std::endl;
  }*/
}