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.");
50 std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
51 std::istreambuf_iterator<char>());
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>());
61 return absl::OkStatus();
64 return absl::OkStatus();
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.");
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);
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());
88 for (
int i = input_value * 0x400; i < 0x1000 + input_value * 0x400;
90 auto b1_pos = (i - (input_value * 0x400));
94 auto b2_pos = (i - (input_value * 0x400) + 1);
102 std::vector<uint8_t> scr_data;
103 map_data.resize(0x2000);
104 scr_data.resize(0x2000);
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());
111 for (
int i = 0; i < 0x1000 - offset; i++) {
116 return absl::OkStatus();
120 std::vector<uint8_t>& map_data,
121 std::vector<uint8_t>& cgx_loaded) {
122 const std::vector<uint16_t> dimensions = {0x000, 0x400, 0x800, 0xC00};
124 for (
const auto each_dimension : dimensions) {
127 for (
int i = 0; i < 0x400; i++) {
128 if (map_data[i + p] != 0xFF) {
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_);
136 yl * (1 - t.vertical_mirror_) + (7 - yl) * (t.vertical_mirror_);
138 int ty = (t.id_ / 16) * 1024;
139 int tx = (t.id_ % 16) * 8;
140 auto pixel = cgx_loaded[(tx + ty) + (yl * 128) + xl];
142 int index = (((i % 32) * 8) + ((i / 32) * 2048) + mx + (my * 256));
145 map_bitmap_data[index] =
146 (uint8_t)((pixel & 0xFF) + t.palette_ * 16);
148 map_bitmap_data[index] = (uint8_t)(pixel & 0xFF);
155 return absl::OkStatus();
158absl::Status
SaveCgx(uint8_t bpp, std::string_view filename,
159 const std::vector<uint8_t>& cgx_data,
160 const std::vector<uint8_t>& cgx_header) {
161 std::ofstream file(filename.data(), std::ios::binary);
162 if (!file.is_open()) {
163 return absl::NotFoundError(
"Could not open file for writing.");
168 std::string bpp_str = std::to_string(bpp) +
"BIT";
169 strncpy(header.
bit_mode, bpp_str.c_str(), 5);
176 file.write(
reinterpret_cast<const char*
>(&header),
sizeof(
CgxHeader));
177 file.write(
reinterpret_cast<const char*
>(cgx_data.data()), cgx_data.size());
178 file.write(
reinterpret_cast<const char*
>(cgx_header.data()), cgx_header.size());
181 return absl::OkStatus();
187 std::vector<SDL_Color> decoded_col;
188 std::ifstream file(filename.data(), std::ios::binary | std::ios::ate);
190 if (!file.is_open()) {
195 std::streamsize size = file.tellg();
196 file.seekg(0, std::ios::beg);
198 std::vector<char> buffer(size);
199 if (file.read(buffer.data(), size)) {
200 buffer.resize(size - 0x200);
203 for (
size_t i = 0; i < buffer.size() / 2; i++) {
204 uint16_t current_color =
static_cast<unsigned char>(buffer[k]) |
205 (
static_cast<unsigned char>(buffer[k + 1]) << 8);
208 color.r = (current_color & 31) << 3;
209 color.g = ((current_color >> 5) & 31) << 3;
210 color.b = ((current_color >> 10) & 31) << 3;
211 color.a = (i & 0xF) == 0 ? 0 : 255;
213 decoded_col.push_back(color);
239 std::string_view filename, std::vector<uint8_t>& obj_data,
240 std::vector<uint8_t> actual_obj_data,
241 std::unordered_map<std::string, std::vector<uint8_t>> decoded_obj,
242 std::vector<uint8_t>& decoded_extra_obj,
int& obj_loaded) {
243 std::vector<uint8_t> header_obj;
246 if (obj_loaded == 0) {
248 expected_cut = 0x500;
251 expected_cut = 0x900;
254 std::ifstream file(filename.data(), std::ios::binary);
255 if (!file.is_open()) {
256 return absl::NotFoundError(
"OBJ file not found.");
259 std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
260 std::istreambuf_iterator<char>());
261 obj_data = file_content;
264 int cut = obj_data.size() & 0x0FFF;
266 std::vector<uint8_t>(obj_data.begin(), obj_data.end() - cut);
268 std::vector<uint8_t>(obj_data.begin() + actual_obj_data.size(),
269 obj_data.begin() + actual_obj_data.size() + 0x100);
270 header_obj = std::vector<uint8_t>(
271 actual_obj_data.begin() + actual_obj_data.size(), actual_obj_data.end());
273 if (cut > expected_cut) {
274 std::vector<uint8_t> scad_data;
276 int k = (obj_loaded == 0) ? 63 : 127;
278 for (
size_t i = 0; i < (actual_obj_data.size() / 6); i++) {
279 std::vector<uint8_t> data = {
280 actual_obj_data[k * 6 + 0 + j],
281 actual_obj_data[k * 6 + 1 + j],
282 actual_obj_data[k * 6 + 2 + j],
283 actual_obj_data[k * 6 + 3 + j],
284 actual_obj_data[k * 6 + 5 + j],
285 actual_obj_data[k * 6 + 4 + j]
287 scad_data.insert(scad_data.end(), data.begin(), data.end());
291 k = (obj_loaded == 0) ? 63 : 127;
292 j = j + ((k + 1) * 6);
296 int extra_data_range = 0x400 * (obj_loaded + 1) + 0x100;
297 for (
int i = 0; i < extra_data_range; i++) {
298 scad_data.push_back(header_obj[i]);
301 obj_data = scad_data;
303 std::vector<uint8_t>(obj_data.begin(), obj_data.end() - cut);
307 for (
int k = 0; k < 128; k++) {
308 decoded_obj[
"frame " + std::to_string(k)] = std::vector<uint8_t>(obj_range);
309 for (
int i = 0; i < obj_range; i++) {
311 decoded_obj[
"frame " + std::to_string(k)][i] =
312 obj_data[i + (obj_range * k)];
314 decoded_obj[
"frame " + std::to_string(k)][i] = 0;
319 return absl::OkStatus();
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 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 SaveCgx(uint8_t bpp, std::string_view filename, const std::vector< uint8_t > &cgx_data, const std::vector< uint8_t > &cgx_header)
Save Cgx file (graphical content)
absl::Status DrawScrWithCgx(uint8_t bpp, std::vector< uint8_t > &map_bitmap_data, std::vector< uint8_t > &map_data, std::vector< uint8_t > &cgx_loaded)
Draw screen tilemap with graphical data.