Cleanup SCad format

This commit is contained in:
scawful
2023-08-17 23:18:07 -04:00
parent eda294d9de
commit c8dce74a89
2 changed files with 126 additions and 146 deletions

View File

@@ -15,73 +15,6 @@ namespace yaze {
namespace app {
namespace gfx {
CgxHeader ExtractCgxHeader(std::vector<uint8_t>& cgx_header) {
CgxHeader header;
memcpy(&header, cgx_header.data(), sizeof(CgxHeader));
return header;
}
absl::Status LoadScr(std::string_view filename, uint8_t input_value,
std::vector<uint8_t>& map_data) {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
return absl::NotFoundError("SCR/PNL/MAP file not found.");
}
// Check if file extension is PNL
bool pnl = false;
if (pnl) {
std::vector<uint8_t> scr_data;
map_data.resize(0x8000);
scr_data.resize(0x8000);
// Read from file for 0x8000 bytes
std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
scr_data = std::vector<uint8_t>(file_content.begin(), file_content.end());
int md = 0x100;
for (int i = input_value * 0x400; i < 0x1000 + input_value * 0x400;
i += 2) {
auto b1_pos = (i - (input_value * 0x400));
map_data[b1_pos] = gfx::TileInfoToShort(
gfx::GetTilesInfo((ushort)scr_data[md + (i * 2)]));
// new Tile((scrdata[md + (i * 2) + 1]), (scrdata[md + (i * 2)]))
// .getshortileinfo();
auto b2_pos = (i - (input_value * 0x400) + 1);
map_data[b2_pos] = gfx::TileInfoToShort(
gfx::GetTilesInfo((ushort)scr_data[md + (i * 2) + 2]));
// new Tile((scrdata[md + (i * 2) + 3]), (scrdata[md + (i * 2) + 2]))
// .getshortileinfo();
}
// 0x900
} else {
int offset = 0;
std::vector<uint8_t> scr_data;
map_data.resize(0x2000);
scr_data.resize(0x2000);
// read from file for 0x2000 bytes
std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
scr_data = std::vector<uint8_t>(file_content.begin(), file_content.end());
for (int i = 0; i < 0x1000 - offset; i++) {
map_data[i] = gfx::TileInfoToShort(
gfx::GetTilesInfo((ushort)scr_data[((i + offset) * 2)]));
// new Tile(scrdata[((i + offset) * 2)],
// scrdata[((i + offset) * 2) + 1])
// .getshortileinfo();
}
}
return absl::OkStatus();
}
absl::Status LoadCgx(uint8_t bpp, std::string_view filename,
std::vector<uint8_t>& cgx_data,
std::vector<uint8_t>& cgx_loaded,
@@ -107,6 +40,132 @@ absl::Status LoadCgx(uint8_t bpp, std::string_view filename,
return absl::OkStatus();
}
absl::Status LoadScr(std::string_view filename, uint8_t input_value,
std::vector<uint8_t>& map_data) {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
return absl::NotFoundError("SCR/PNL/MAP file not found.");
}
// Check if file extension is PNL
bool pnl = false;
if (filename.find("PNL") != std::string::npos) {
std::vector<uint8_t> scr_data;
map_data.resize(0x8000);
scr_data.resize(0x8000);
// Read from file for 0x8000 bytes
std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
scr_data = std::vector<uint8_t>(file_content.begin(), file_content.end());
int md = 0x100;
for (int i = input_value * 0x400; i < 0x1000 + input_value * 0x400;
i += 2) {
auto b1_pos = (i - (input_value * 0x400));
map_data[b1_pos] = gfx::TileInfoToShort(
gfx::GetTilesInfo((ushort)scr_data[md + (i * 2)]));
auto b2_pos = (i - (input_value * 0x400) + 1);
map_data[b2_pos] = gfx::TileInfoToShort(
gfx::GetTilesInfo((ushort)scr_data[md + (i * 2) + 2]));
}
// 0x900
} else {
int offset = 0;
std::vector<uint8_t> scr_data;
map_data.resize(0x2000);
scr_data.resize(0x2000);
// read from file for 0x2000 bytes
std::vector<uint8_t> file_content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
scr_data = std::vector<uint8_t>(file_content.begin(), file_content.end());
for (int i = 0; i < 0x1000 - offset; i++) {
map_data[i] = gfx::TileInfoToShort(
gfx::GetTilesInfo((ushort)scr_data[((i + offset) * 2)]));
}
}
return absl::OkStatus();
}
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) {
const std::vector<uint16_t> dimensions = {0x000, 0x400, 0x800, 0xC00};
uint8_t p = 0;
for (const auto each_dimension : dimensions) {
p = each_dimension;
// for each tile on the tile buffer
for (int i = 0; i < 0x400; i++) {
if (map_data[i + p] != 0xFFFF) {
auto t = gfx::GetTilesInfo(map_data[i + p]);
for (auto yl = 0; yl < 8; yl++) {
for (auto xl = 0; xl < 8; xl++) {
int mx = xl * (1 - t.horizontal_mirror_) +
(7 - xl) * (t.horizontal_mirror_);
int my =
yl * (1 - t.vertical_mirror_) + (7 - yl) * (t.vertical_mirror_);
int ty = (t.id_ / 16) * 1024;
int tx = (t.id_ % 16) * 8;
auto pixel = cgx_loaded[(tx + ty) + (yl * 128) + xl];
int index = (((i % 32) * 8) + ((i / 32) * 2048) + mx + (my * 256));
if (bpp != 8) {
map_bitmap_data[index] =
(uchar)((pixel & 0xFF) + t.palette_ * 16);
} else {
map_bitmap_data[index] = (uchar)(pixel & 0xFF);
}
}
}
}
}
}
return absl::OkStatus();
}
std::vector<SDL_Color> DecodeColFile(const std::string& filename) {
std::vector<SDL_Color> decoded_col;
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
return decoded_col; // Return an empty vector if the file couldn't be
// opened.
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (file.read(buffer.data(), size)) {
buffer.resize(size - 0x200);
int k = 0;
for (size_t i = 0; i < buffer.size() / 2; i++) {
uint16_t current_color = static_cast<unsigned char>(buffer[k]) |
(static_cast<unsigned char>(buffer[k + 1]) << 8);
SDL_Color color;
color.r = (current_color & 31) << 3;
color.g = ((current_color >> 5) & 31) << 3;
color.b = ((current_color >> 10) & 31) << 3;
color.a = (i & 0xF) == 0 ? 0 : 255;
decoded_col.push_back(color);
k += 2;
}
}
return decoded_col;
}
absl::Status DecodeObjFile(
std::string_view filename, std::vector<uint8_t>& obj_data,
std::vector<uint8_t> actual_obj_data,
@@ -191,83 +250,6 @@ absl::Status DecodeObjFile(
return absl::OkStatus();
}
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) {
const std::vector<uint16_t> dimensions = {0x000, 0x400, 0x800, 0xC00};
uint8_t p = 0;
for (const auto each_dimension : dimensions) {
p = each_dimension;
// for each tile on the tile buffer
for (int i = 0; i < 0x400; i++) {
if (cgx_loaded[i + p] != 0xFFFF) {
auto t = gfx::GetTilesInfo(cgx_loaded[i + p]);
for (auto yl = 0; yl < 8; yl++) {
for (auto xl = 0; xl < 8; xl++) {
int mx = xl * (1 - t.horizontal_mirror_) +
(7 - xl) * (t.horizontal_mirror_);
int my =
yl * (1 - t.vertical_mirror_) + (7 - yl) * (t.vertical_mirror_);
int ty = (t.id_ / 16) * 1024;
int tx = (t.id_ % 16) * 8;
auto pixel = cgx_loaded[(tx + ty) + (yl * 128) + xl];
int index =
(((i % 32) * 8) + ((i / 32) * 2048) + (mx) + (my * 256));
if (bpp != 8) {
map_bitmap_data[index] =
(uchar)(((pixel)&0xFF) + t.palette_ * 16);
} else {
map_bitmap_data[index] = (uchar)(((pixel)&0xFF));
}
// + t.palette * 16
}
}
}
}
}
return absl::OkStatus();
}
std::vector<SDL_Color> DecodeColFile(const std::string& filename) {
std::vector<SDL_Color> decoded_col;
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
return decoded_col; // Return an empty vector if the file couldn't be
// opened.
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (file.read(buffer.data(), size)) {
buffer.resize(size - 0x200);
int k = 0;
for (size_t i = 0; i < buffer.size() / 2; i++) {
uint16_t current_color = static_cast<unsigned char>(buffer[k]) |
(static_cast<unsigned char>(buffer[k + 1]) << 8);
SDL_Color color;
color.r = (current_color & 31) << 3;
color.g = ((current_color >> 5) & 31) << 3;
color.b = ((current_color >> 10) & 31) << 3;
color.a = (i & 0xF) == 0 ? 0 : 255;
decoded_col.push_back(color);
k += 2;
}
}
return decoded_col;
}
} // namespace gfx
} // namespace app
} // namespace yaze

View File

@@ -46,8 +46,6 @@ struct CgxHeader {
uint8_t color_path[0x100];
};
CgxHeader ExtractCgxHeader(std::vector<uint8_t>& cgx_header);
absl::Status LoadScr(std::string_view filename, uint8_t input_value,
std::vector<uint8_t>& map_data);