yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
scad_format.cc
Go to the documentation of this file.
1#include "scad_format.h"
2
3#include <cstdint>
4#include <cstdlib>
5#include <cstring>
6#include <fstream>
7#include <iostream>
8#include <vector>
9
10#include "absl/status/status.h"
11#include "app/gfx/snes_tile.h"
12#include "util/macro.h"
13
14namespace yaze {
15namespace gfx {
16
18 int matching_position = -1;
19 bool matched = false;
20 std::vector<uint8_t> cgx_rom;
21 std::vector<uint8_t> raw_data_;
22 for (int i = 0;
23 i < cgx_rom.size() - sizeof(kMatchedBytes) - kOffsetFromMatchedBytesEnd;
24 i++) {
25 raw_data_.push_back(cgx_rom[i]);
26 bool is_match = std::equal(std::begin(kMatchedBytes),
27 std::end(kMatchedBytes), &cgx_rom[i]);
28 if (is_match) {
29 matching_position = i;
30 matched = true;
31 break;
32 }
33 }
34 if (matched) {
35 int bpp_marker_position =
36 matching_position + sizeof(kMatchedBytes) + kOffsetFromMatchedBytesEnd;
37 int bpp_marker = cgx_rom[bpp_marker_position];
38 std::string bpp_type = (bpp_marker == 0x31) ? "8bpp" : "4bpp";
39 }
40}
41
42absl::Status LoadCgx(uint8_t bpp, std::string_view filename,
43 std::vector<uint8_t>& cgx_data,
44 std::vector<uint8_t>& cgx_loaded,
45 std::vector<uint8_t>& cgx_header) {
46 std::ifstream file(filename.data(), std::ios::binary);
47 if (!file.is_open()) {
48 return absl::NotFoundError("CGX file not found.");
49 }
50 std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
51 std::istreambuf_iterator<char>());
52 cgx_data =
53 std::vector<uint8_t>(file_content.begin(), file_content.end() - 0x500);
54 file.seekg(cgx_data.size() + 0x100);
55 cgx_header = std::vector<uint8_t>((std::istreambuf_iterator<char>(file)),
56 std::istreambuf_iterator<char>());
57 file.close();
58
59 if (bpp > 8) {
60 cgx_loaded = gfx::Bpp8SnesToIndexed(cgx_data, 40);
61 return absl::OkStatus();
62 }
63 cgx_loaded = gfx::Bpp8SnesToIndexed(cgx_data, bpp);
64 return absl::OkStatus();
65}
66
67absl::Status LoadScr(std::string_view filename, uint8_t input_value,
68 std::vector<uint8_t>& map_data) {
69 std::ifstream file(filename.data(), std::ios::binary);
70 if (!file.is_open()) {
71 return absl::NotFoundError("SCR/PNL/MAP file not found.");
72 }
73
74 // Check if file extension is PNL
75 bool pnl = false;
76 if (filename.find("PNL") != std::string::npos) {
77 std::vector<uint8_t> scr_data;
78 map_data.resize(0x8000);
79 scr_data.resize(0x8000);
80
81 // Read from file for 0x8000 bytes
82 std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
83 std::istreambuf_iterator<char>());
84 scr_data = std::vector<uint8_t>(file_content.begin(), file_content.end());
85
86 int md = 0x100;
87
88 for (int i = input_value * 0x400; i < 0x1000 + input_value * 0x400;
89 i += 2) {
90 auto b1_pos = (i - (input_value * 0x400));
91 map_data[b1_pos] = gfx::TileInfoToShort(
92 gfx::GetTilesInfo((uint16_t)scr_data[md + (i * 2)]));
93
94 auto b2_pos = (i - (input_value * 0x400) + 1);
95 map_data[b2_pos] = gfx::TileInfoToShort(
96 gfx::GetTilesInfo((uint16_t)scr_data[md + (i * 2) + 2]));
97 }
98 // 0x900
99
100 } else {
101 int offset = 0;
102 std::vector<uint8_t> scr_data;
103 map_data.resize(0x2000);
104 scr_data.resize(0x2000);
105
106 // read from file for 0x2000 bytes
107 std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
108 std::istreambuf_iterator<char>());
109 scr_data = std::vector<uint8_t>(file_content.begin(), file_content.end());
110
111 for (int i = 0; i < 0x1000 - offset; i++) {
112 map_data[i] = gfx::TileInfoToShort(
113 gfx::GetTilesInfo((uint16_t)scr_data[((i + offset) * 2)]));
114 }
115 }
116 return absl::OkStatus();
117}
118
119absl::Status DrawScrWithCgx(uint8_t bpp, std::vector<uint8_t>& map_data,
120 std::vector<uint8_t>& map_bitmap_data,
121 std::vector<uint8_t>& cgx_loaded) {
122 const std::vector<uint16_t> dimensions = {0x000, 0x400, 0x800, 0xC00};
123 uint8_t p = 0;
124 for (const auto each_dimension : dimensions) {
125 p = each_dimension;
126 // for each tile on the tile buffer
127 for (int i = 0; i < 0x400; i++) {
128 if (map_data[i + p] != 0xFF) {
129 auto t = gfx::GetTilesInfo(map_data[i + p]);
130
131 for (auto yl = 0; yl < 8; yl++) {
132 for (auto xl = 0; xl < 8; xl++) {
133 int mx = xl * (1 - t.horizontal_mirror_) +
134 (7 - xl) * (t.horizontal_mirror_);
135 int my =
136 yl * (1 - t.vertical_mirror_) + (7 - yl) * (t.vertical_mirror_);
137
138 int ty = (t.id_ / 16) * 1024;
139 int tx = (t.id_ % 16) * 8;
140 auto pixel = cgx_loaded[(tx + ty) + (yl * 128) + xl];
141
142 int index = (((i % 32) * 8) + ((i / 32) * 2048) + mx + (my * 256));
143
144 if (bpp != 8) {
145 map_bitmap_data[index] =
146 (uint8_t)((pixel & 0xFF) + t.palette_ * 16);
147 } else {
148 map_bitmap_data[index] = (uint8_t)(pixel & 0xFF);
149 }
150 }
151 }
152 }
153 }
154 }
155 return absl::OkStatus();
156}
157
158std::vector<SDL_Color> DecodeColFile(const std::string_view filename) {
159 std::vector<SDL_Color> decoded_col;
160 std::ifstream file(filename.data(), std::ios::binary | std::ios::ate);
161
162 if (!file.is_open()) {
163 return decoded_col; // Return an empty vector if the file couldn't be
164 // opened.
165 }
166
167 std::streamsize size = file.tellg();
168 file.seekg(0, std::ios::beg);
169
170 std::vector<char> buffer(size);
171 if (file.read(buffer.data(), size)) {
172 buffer.resize(size - 0x200);
173
174 int k = 0;
175 for (size_t i = 0; i < buffer.size() / 2; i++) {
176 uint16_t current_color = static_cast<unsigned char>(buffer[k]) |
177 (static_cast<unsigned char>(buffer[k + 1]) << 8);
178
179 SDL_Color color;
180 color.r = (current_color & 31) << 3;
181 color.g = ((current_color >> 5) & 31) << 3;
182 color.b = ((current_color >> 10) & 31) << 3;
183 color.a = (i & 0xF) == 0 ? 0 : 255;
184
185 decoded_col.push_back(color);
186 k += 2;
187 }
188 }
189
190 return decoded_col;
191}
192
193absl::Status DecodeObjFile(
194 std::string_view filename, std::vector<uint8_t>& obj_data,
195 std::vector<uint8_t> actual_obj_data,
196 std::unordered_map<std::string, std::vector<uint8_t>> decoded_obj,
197 std::vector<uint8_t>& decoded_extra_obj, int& obj_loaded) {
198 std::vector<uint8_t> header_obj;
199 int obj_range;
200 int expected_cut;
201 if (obj_loaded == 0) {
202 obj_range = 0x180;
203 expected_cut = 0x500;
204 } else {
205 obj_range = 0x300;
206 expected_cut = 0x900;
207 }
208
209 std::ifstream file(filename.data(), std::ios::binary);
210 if (!file.is_open()) {
211 return absl::NotFoundError("OBJ file not found.");
212 }
213
214 std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
215 std::istreambuf_iterator<char>());
216 obj_data = file_content;
217 file.close();
218
219 int cut = obj_data.size() & 0x0FFF;
220 actual_obj_data =
221 std::vector<uint8_t>(obj_data.begin(), obj_data.end() - cut);
222 decoded_extra_obj =
223 std::vector<uint8_t>(obj_data.begin() + actual_obj_data.size(),
224 obj_data.begin() + actual_obj_data.size() + 0x100);
225 header_obj = std::vector<uint8_t>(
226 actual_obj_data.begin() + actual_obj_data.size(), actual_obj_data.end());
227
228 if (cut > expected_cut) {
229 std::vector<uint8_t> scad_data;
230 int j = 0;
231 int k = (obj_loaded == 0) ? 63 : 127;
232
233 for (size_t i = 0; i < (actual_obj_data.size() / 6); i++) {
234 std::vector<uint8_t> data = {
235 actual_obj_data[k * 6 + 0 + j], // display
236 actual_obj_data[k * 6 + 1 + j], // unknown
237 actual_obj_data[k * 6 + 2 + j], // y-disp
238 actual_obj_data[k * 6 + 3 + j], // x-disp
239 actual_obj_data[k * 6 + 5 + j], // props
240 actual_obj_data[k * 6 + 4 + j] // tile
241 };
242 scad_data.insert(scad_data.end(), data.begin(), data.end());
243
244 k = k - 1;
245 if (k == -1) {
246 k = (obj_loaded == 0) ? 63 : 127;
247 j = j + ((k + 1) * 6);
248 }
249 }
250
251 int extra_data_range = 0x400 * (obj_loaded + 1) + 0x100;
252 for (int i = 0; i < extra_data_range; i++) {
253 scad_data.push_back(header_obj[i]);
254 }
255
256 obj_data = scad_data;
257 actual_obj_data =
258 std::vector<uint8_t>(obj_data.begin(), obj_data.end() - cut);
259 }
260
261 decoded_obj.clear();
262 for (int k = 0; k < 128; k++) {
263 decoded_obj["frame " + std::to_string(k)] = std::vector<uint8_t>(obj_range);
264 for (int i = 0; i < obj_range; i++) {
265 try {
266 decoded_obj["frame " + std::to_string(k)][i] =
267 obj_data[i + (obj_range * k)];
268 } catch (...) {
269 decoded_obj["frame " + std::to_string(k)][i] = 0;
270 }
271 }
272 }
273
274 return absl::OkStatus();
275}
276
277} // namespace gfx
278} // namespace yaze
Contains classes for handling graphical data.
Definition bitmap.cc:16
absl::Status DecodeObjFile(std::string_view filename, std::vector< uint8_t > &obj_data, std::vector< uint8_t > actual_obj_data, std::unordered_map< std::string, std::vector< uint8_t > > decoded_obj, std::vector< uint8_t > &decoded_extra_obj, int &obj_loaded)
Decode obj file.
absl::Status LoadScr(std::string_view filename, uint8_t input_value, std::vector< uint8_t > &map_data)
Load Scr file (screen data)
std::vector< SDL_Color > DecodeColFile(const std::string_view filename)
Decode color file.
absl::Status LoadCgx(uint8_t bpp, std::string_view filename, std::vector< uint8_t > &cgx_data, std::vector< uint8_t > &cgx_loaded, std::vector< uint8_t > &cgx_header)
Load Cgx file (graphical content)
absl::Status DrawScrWithCgx(uint8_t bpp, std::vector< uint8_t > &map_data, std::vector< uint8_t > &map_bitmap_data, std::vector< uint8_t > &cgx_loaded)
Draw screen tilemap with graphical data.
uint16_t TileInfoToShort(TileInfo tile_info)
Definition snes_tile.cc:333
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
Definition snes_tile.cc:199
void FindMetastamp()
Find metastamp in CGX file.
constexpr uint16_t kMatchedBytes[]
Definition scad_format.h:47
constexpr uint16_t kOffsetFromMatchedBytesEnd
Definition scad_format.h:48
TileInfo GetTilesInfo(uint16_t tile)
Definition snes_tile.cc:364
Main namespace for the application.
Definition controller.cc:18