housekeeping style format etc

This commit is contained in:
scawful
2022-06-09 18:28:50 -04:00
parent 6f95d72d81
commit 3a4b532979
5 changed files with 346 additions and 312 deletions

View File

@@ -30,7 +30,7 @@
#define X_ std::byte {
#define _X }
#define MY_BUILD_HEADER(command, length) (command << 5) + ((length) - 1)
#define MY_BUILD_HEADER(command, length) (command << 5) + ((length)-1)
namespace yaze {
namespace Application {
@@ -41,18 +41,18 @@ char* ALTTPCompression::DecompressGfx(const char* c_data,
unsigned int max_length,
unsigned int* uncompressed_data_size,
unsigned int* compressed_length) {
char* toret =
std_nintendo_.Decompress(c_data, start, max_length, uncompressed_data_size,
compressed_length, D_NINTENDO_C_MODE2);
char* toret = std_nintendo_.Decompress(c_data, start, max_length,
uncompressed_data_size,
compressed_length, D_NINTENDO_C_MODE2);
return toret;
}
char* ALTTPCompression::DecompressOverworld(
const char* c_data, const unsigned int start, unsigned int max_length,
unsigned int* uncompressed_data_size, unsigned int* compressed_length) {
char* toret =
std_nintendo_.Decompress(c_data, start, max_length, uncompressed_data_size,
compressed_length, D_NINTENDO_C_MODE1);
char* toret = std_nintendo_.Decompress(c_data, start, max_length,
uncompressed_data_size,
compressed_length, D_NINTENDO_C_MODE1);
return toret;
}
@@ -61,7 +61,7 @@ char* ALTTPCompression::CompressGfx(const char* u_data,
const unsigned int length,
unsigned int* compressed_size) {
return std_nintendo_.Compress(u_data, start, length, compressed_size,
D_NINTENDO_C_MODE2);
D_NINTENDO_C_MODE2);
}
char* ALTTPCompression::CompressOverworld(const char* u_data,
@@ -69,7 +69,7 @@ char* ALTTPCompression::CompressOverworld(const char* u_data,
const unsigned int length,
unsigned int* compressed_size) {
return std_nintendo_.Compress(u_data, start, length, compressed_size,
D_NINTENDO_C_MODE1);
D_NINTENDO_C_MODE1);
}
/*
@@ -79,152 +79,160 @@ char* ALTTPCompression::CompressOverworld(const char* u_data,
* Then you have a new header byte and so on, until you hit a header with the
* value FF
*/
char* StdNintendoCompression::Decompress(const char* c_data, const unsigned int start,
unsigned int max_length,
unsigned int* uncompressed_data_size,
unsigned int* compressed_length, char mode) {
char* u_data;
unsigned char header;
unsigned int c_data_pos;
unsigned int u_data_pos;
unsigned int allocated_memory;
unsigned int max_offset;
char* StdNintendoCompression::Decompress(const char* c_data,
const unsigned int start,
unsigned int max_length,
unsigned int* uncompressed_data_size,
unsigned int* compressed_length,
char mode) {
char* u_data;
unsigned char header;
unsigned int c_data_pos;
unsigned int u_data_pos;
unsigned int allocated_memory;
unsigned int max_offset;
max_offset = 0;
if (max_length != 0)
max_offset = start + max_length;
header = c_data[start];
u_data = (char *) malloc(INITIAL_ALLOC_SIZE); // No way to know the final size, we will probably realloc if needed
allocated_memory = INITIAL_ALLOC_SIZE;
u_data_pos = 0;
c_data_pos = start;
while (header != 0xFF)
{
unsigned int length;
char command;
max_offset = 0;
if (max_length != 0) max_offset = start + max_length;
header = c_data[start];
u_data =
(char*)malloc(INITIAL_ALLOC_SIZE); // No way to know the final size, we
// will probably realloc if needed
allocated_memory = INITIAL_ALLOC_SIZE;
u_data_pos = 0;
c_data_pos = start;
command = header >> 5; // 3 hightest bits are the command
length = (header & 0x1F); // The rest is the length
while (header != 0xFF) {
unsigned int length;
char command;
// Extended header, to allow for bigger length value than 32
if (command == 7)
{
// The command are the next 3 bits
command = (header >> 2 ) & 7;
// 2 bits in the original header are the hight bit for the new length
// the next byte is added to this length
command = header >> 5; // 3 hightest bits are the command
length = (header & 0x1F); // The rest is the length
length = ((int)((header & 3) << 8)) + (unsigned char) c_data[c_data_pos + 1];
c_data_pos++;
}
// Extended header, to allow for bigger length value than 32
if (command == 7) {
// The command are the next 3 bits
command = (header >> 2) & 7;
// 2 bits in the original header are the hight bit for the new length
// the next byte is added to this length
//length value starts at 0, 0 is 1
length++;
printf("%d[%d]", command, length);
printf("header %02X - Command : %d , length : %d\n", header, command, length);
if (c_data_pos >= max_offset && max_offset != 0)
{
decompression_error_ = "Compression string exceed the max_length specified";
goto error;
}
if (u_data_pos + length + 1 > allocated_memory) // Adjust allocated memory
{
printf("Memory get reallocated by %d was %d\n", INITIAL_ALLOC_SIZE, allocated_memory);
u_data = (char*) realloc(u_data, allocated_memory + INITIAL_ALLOC_SIZE);
if (u_data == NULL)
{
decompression_error_ = "Can't realloc memory";
return NULL;
}
allocated_memory += INITIAL_ALLOC_SIZE;
}
switch (command)
{
case D_CMD_COPY: { // No compression, data are copied as
if (max_offset != 0 && c_data_pos + 1 + length > max_offset)
{
//decompression_error_ = vasprintf("A copy command exceed the available data %d > %d (max_length specified)\n", c_data_pos + 1 + length, max_offset);
goto error;
}
memcpy(u_data + u_data_pos, c_data + c_data_pos + 1, length);
c_data_pos += length + 1;
break;
}
case D_CMD_BYTE_REPEAT: { // Copy the same byte length time
memset(u_data + u_data_pos, c_data[c_data_pos + 1], length);
c_data_pos += 2;
break;
}
case D_CMD_WORD_REPEAT: { // Next byte is A, the one after is B, copy the sequence AB length times
char a = c_data[c_data_pos + 1];
char b = c_data[c_data_pos + 2];
for (int i = 0; i < length; i = i + 2)
{
u_data[u_data_pos + i] = a;
if ((i + 1) < length)
u_data[u_data_pos + i + 1] = b;
}
c_data_pos += 3;
break;
}
case D_CMD_BYTE_INC: { // Next byte is copied and incremented length time
for (int i = 0; i < length; i++) {
u_data[u_data_pos + i] = c_data[c_data_pos + 1] + i;
}
c_data_pos += 2;
break;
}
case D_CMD_COPY_EXISTING: { // Next 2 bytes form an offset to pick data from the output
//printf("%02X,%02X\n", (unsigned char) c_data[c_data_pos + 1], (unsigned char) c_data[c_data_pos + 2]);
unsigned short offset;
if (mode == D_NINTENDO_C_MODE2)
offset = (unsigned char)(c_data[c_data_pos + 1]) | ((unsigned char) (c_data[c_data_pos + 2]) << 8);
if (mode == D_NINTENDO_C_MODE1)
offset = (unsigned char)(c_data[c_data_pos + 2]) | ((unsigned char) (c_data[c_data_pos + 1]) << 8);
if (offset > u_data_pos)
{
printf("Offset for command copy existing is larger than the current position (Offset : 0x%04X | Pos : 0x%06X\n", offset, u_data_pos);
goto error;
}
if (u_data_pos + length >= allocated_memory)
{
printf("Memory get reallocated by a copy, %d was %d\n", INITIAL_ALLOC_SIZE, allocated_memory);
u_data = (char*) realloc(u_data, allocated_memory + INITIAL_ALLOC_SIZE);
if (u_data == NULL)
{
decompression_error_ = "Can't realloc memory";
return NULL;
}
allocated_memory += INITIAL_ALLOC_SIZE;
}
memcpy(u_data + u_data_pos, u_data + offset, length);
c_data_pos += 3;
break;
}
default: {
decompression_error_ = "Invalid command in the header for decompression";
goto error;
}
}
u_data_pos += length;
//printf("%d|%d\n", c_data_pos, u_data_pos);
header = c_data[c_data_pos];
length =
((int)((header & 3) << 8)) + (unsigned char)c_data[c_data_pos + 1];
c_data_pos++;
}
*uncompressed_data_size = u_data_pos;
*compressed_length = c_data_pos + 1;
//printf("\n");
return u_data;
// yay goto usage :)
// length value starts at 0, 0 is 1
length++;
printf("%d[%d]", command, length);
printf("header %02X - Command : %d , length : %d\n", header, command,
length);
if (c_data_pos >= max_offset && max_offset != 0) {
decompression_error_ =
"Compression string exceed the max_length specified";
goto error;
}
if (u_data_pos + length + 1 > allocated_memory) // Adjust allocated memory
{
printf("Memory get reallocated by %d was %d\n", INITIAL_ALLOC_SIZE,
allocated_memory);
u_data = (char*)realloc(u_data, allocated_memory + INITIAL_ALLOC_SIZE);
if (u_data == NULL) {
decompression_error_ = "Can't realloc memory";
return NULL;
}
allocated_memory += INITIAL_ALLOC_SIZE;
}
switch (command) {
case D_CMD_COPY: { // No compression, data are copied as
if (max_offset != 0 && c_data_pos + 1 + length > max_offset) {
// decompression_error_ = vasprintf("A copy command exceed the
// available data %d > %d (max_length specified)\n", c_data_pos + 1 +
// length, max_offset);
goto error;
}
memcpy(u_data + u_data_pos, c_data + c_data_pos + 1, length);
c_data_pos += length + 1;
break;
}
case D_CMD_BYTE_REPEAT: { // Copy the same byte length time
memset(u_data + u_data_pos, c_data[c_data_pos + 1], length);
c_data_pos += 2;
break;
}
case D_CMD_WORD_REPEAT: { // Next byte is A, the one after is B, copy the
// sequence AB length times
char a = c_data[c_data_pos + 1];
char b = c_data[c_data_pos + 2];
for (int i = 0; i < length; i = i + 2) {
u_data[u_data_pos + i] = a;
if ((i + 1) < length) u_data[u_data_pos + i + 1] = b;
}
c_data_pos += 3;
break;
}
case D_CMD_BYTE_INC: { // Next byte is copied and incremented length time
for (int i = 0; i < length; i++) {
u_data[u_data_pos + i] = c_data[c_data_pos + 1] + i;
}
c_data_pos += 2;
break;
}
case D_CMD_COPY_EXISTING: { // Next 2 bytes form an offset to pick data
// from the output
// printf("%02X,%02X\n", (unsigned char) c_data[c_data_pos + 1],
// (unsigned char) c_data[c_data_pos + 2]);
unsigned short offset;
if (mode == D_NINTENDO_C_MODE2)
offset = (unsigned char)(c_data[c_data_pos + 1]) |
((unsigned char)(c_data[c_data_pos + 2]) << 8);
if (mode == D_NINTENDO_C_MODE1)
offset = (unsigned char)(c_data[c_data_pos + 2]) |
((unsigned char)(c_data[c_data_pos + 1]) << 8);
if (offset > u_data_pos) {
printf(
"Offset for command copy existing is larger than the current "
"position (Offset : 0x%04X | Pos : 0x%06X\n",
offset, u_data_pos);
goto error;
}
if (u_data_pos + length >= allocated_memory) {
printf("Memory get reallocated by a copy, %d was %d\n",
INITIAL_ALLOC_SIZE, allocated_memory);
u_data =
(char*)realloc(u_data, allocated_memory + INITIAL_ALLOC_SIZE);
if (u_data == NULL) {
decompression_error_ = "Can't realloc memory";
return NULL;
}
allocated_memory += INITIAL_ALLOC_SIZE;
}
memcpy(u_data + u_data_pos, u_data + offset, length);
c_data_pos += 3;
break;
}
default: {
decompression_error_ =
"Invalid command in the header for decompression";
goto error;
}
}
u_data_pos += length;
// printf("%d|%d\n", c_data_pos, u_data_pos);
header = c_data[c_data_pos];
}
*uncompressed_data_size = u_data_pos;
*compressed_length = c_data_pos + 1;
// printf("\n");
return u_data;
// yay goto usage :)
error:
free(u_data);
return NULL;
free(u_data);
return NULL;
}
void StdNintendoCompression::PrintComponent(CompressionComponent * piece) {
void StdNintendoCompression::PrintComponent(CompressionComponent* piece) {
printf("Command : %d\n", piece->command);
printf("length : %d\n", piece->length);
printf("Argument length : %d\n", piece->argument_length);
@@ -232,24 +240,25 @@ void StdNintendoCompression::PrintComponent(CompressionComponent * piece) {
auto str = piece->argument;
char* toret = new char[size * 3 + 1];
unsigned int i;
for (i = 0; i < size; i++){
sprintf(toret + i * 3, "%02X ", (unsigned char) str[i]);
for (i = 0; i < size; i++) {
sprintf(toret + i * 3, "%02X ", (unsigned char)str[i]);
}
toret[size * 3] = 0;
printf("Argument :%s\n", toret);
}
StdNintendoCompression::CompressionComponent* StdNintendoCompression::CreateComponent(const char command,
const unsigned int length,
const char* args,
const unsigned int argument_length) {
StdNintendoCompression::CompressionComponent*
StdNintendoCompression::CreateComponent(const char command,
const unsigned int length,
const char* args,
const unsigned int argument_length) {
CompressionComponent* toret =
(CompressionComponent*) malloc(sizeof(CompressionComponent));
(CompressionComponent*)malloc(sizeof(CompressionComponent));
toret->command = command;
toret->length = length;
if (args != NULL) {
toret->argument = (char*) malloc(argument_length);
toret->argument = (char*)malloc(argument_length);
memcpy(toret->argument, args, argument_length);
} else
toret->argument = NULL;
@@ -272,7 +281,8 @@ void StdNintendoCompression::DestroyChain(CompressionComponent* piece) {
}
// Merge consecutive copy if possible
StdNintendoCompression::CompressionComponent* StdNintendoCompression::merge_copy(CompressionComponent* start) {
StdNintendoCompression::CompressionComponent*
StdNintendoCompression::merge_copy(CompressionComponent* start) {
CompressionComponent* piece = start;
while (piece != NULL) {
@@ -281,7 +291,7 @@ StdNintendoCompression::CompressionComponent* StdNintendoCompression::merge_copy
if (piece->length + piece->next->length <= D_max_length) {
unsigned int previous_length = piece->length;
piece->length = piece->length + piece->next->length;
//piece->argument = realloc(piece->argument, piece->length);
// piece->argument = realloc(piece->argument, piece->length);
piece->argument_length = piece->length;
memcpy(piece->argument + previous_length, piece->next->argument,
piece->next->argument_length);
@@ -298,8 +308,8 @@ StdNintendoCompression::CompressionComponent* StdNintendoCompression::merge_copy
return start;
}
unsigned int StdNintendoCompression::create_compression_string(CompressionComponent* start, char* output,
char mode) {
unsigned int StdNintendoCompression::create_compression_string(
CompressionComponent* start, char* output, char mode) {
unsigned int pos = 0;
CompressionComponent* piece = start;
@@ -312,7 +322,7 @@ unsigned int StdNintendoCompression::create_compression_string(CompressionCompon
output[pos++] = (7 << 5) | ((unsigned char)piece->command << 2) |
(((piece->length - 1) & 0xFF00) >> 8);
printf("Building extended header : cmd: %d, length: %d - %02X\n",
piece->command, piece->length, (unsigned char)output[pos - 1]);
piece->command, piece->length, (unsigned char)output[pos - 1]);
output[pos++] = (char)((piece->length - 1) & 0x00FF);
} else { // We need to split the command
unsigned int length_left = piece->length - D_max_length;
@@ -320,29 +330,26 @@ unsigned int StdNintendoCompression::create_compression_string(CompressionCompon
CompressionComponent* new_piece = NULL;
if (piece->command == D_CMD_BYTE_REPEAT ||
piece->command == D_CMD_WORD_REPEAT) {
new_piece =
CreateComponent(piece->command, length_left,
piece->argument, piece->argument_length);
new_piece = CreateComponent(piece->command, length_left,
piece->argument, piece->argument_length);
}
if (piece->command == D_CMD_BYTE_INC) {
new_piece =
CreateComponent(piece->command, length_left,
piece->argument, piece->argument_length);
new_piece = CreateComponent(piece->command, length_left,
piece->argument, piece->argument_length);
new_piece->argument[0] = (char)(piece->argument[0] + D_max_length);
}
if (piece->command == D_CMD_COPY) {
piece->argument_length = D_max_length;
new_piece = CreateComponent(piece->command, length_left, NULL,
length_left);
new_piece =
CreateComponent(piece->command, length_left, NULL, length_left);
memcpy(new_piece->argument, piece->argument + D_max_length,
length_left);
}
if (piece->command == D_CMD_COPY_EXISTING) {
piece->argument_length = D_max_length;
unsigned int offset = piece->argument[0] + (piece->argument[1] << 8);
new_piece =
CreateComponent(piece->command, length_left,
piece->argument, piece->argument_length);
new_piece = CreateComponent(piece->command, length_left,
piece->argument, piece->argument_length);
if (mode == D_NINTENDO_C_MODE2) {
new_piece->argument[0] = (offset + D_max_length) & 0xFF;
new_piece->argument[1] = (offset + D_max_length) >> 8;
@@ -382,7 +389,11 @@ unsigned int StdNintendoCompression::create_compression_string(CompressionCompon
// TODO TEST compressed data border for each cmd
char* StdNintendoCompression::Compress(const char* u_data, const unsigned int start, const unsigned int length, unsigned int* compressed_size, char mode) {
char* StdNintendoCompression::Compress(const char* u_data,
const unsigned int start,
const unsigned int length,
unsigned int* compressed_size,
char mode) {
// we will realloc later
char* compressed_data = (char*)malloc(
length +
@@ -518,8 +529,8 @@ char* StdNintendoCompression::Compress(const char* u_data, const unsigned int st
memcpy(buffer, u_data + u_data_pos - bytes_since_last_compression,
bytes_since_last_compression);
CompressionComponent* new_comp_piece =
CreateComponent(D_CMD_COPY, bytes_since_last_compression,
buffer, bytes_since_last_compression);
CreateComponent(D_CMD_COPY, bytes_since_last_compression, buffer,
bytes_since_last_compression);
compressed_chain->next = new_comp_piece;
compressed_chain = new_comp_piece;
bytes_since_last_compression = 0;
@@ -539,8 +550,8 @@ char* StdNintendoCompression::Compress(const char* u_data, const unsigned int st
memcpy(copy_buff, u_data + u_data_pos - bytes_since_last_compression,
bytes_since_last_compression);
CompressionComponent* copy_chuck =
CreateComponent(D_CMD_COPY, bytes_since_last_compression,
copy_buff, bytes_since_last_compression);
CreateComponent(D_CMD_COPY, bytes_since_last_compression, copy_buff,
bytes_since_last_compression);
compressed_chain->next = copy_chuck;
compressed_chain = copy_chuck;
}

View File

@@ -1,8 +1,8 @@
#ifndef YAZE_APPLICATION_UTILS_COMPRESSION_H
#define YAZE_APPLICATION_UTILS_COMPRESSION_H
#include <cstdlib>
#include <cstddef>
#include <cstdlib>
#include <string>
#include <vector>
@@ -27,9 +27,9 @@ class StdNintendoCompression {
*/
char* Decompress(const char* c_data, const unsigned int start,
unsigned int max_length,
unsigned int* uncompressed_data_size,
unsigned int* compressed_length, char mode);
unsigned int max_length,
unsigned int* uncompressed_data_size,
unsigned int* compressed_length, char mode);
/*
* This function compress u_data following the compression format used by
@@ -42,15 +42,16 @@ class StdNintendoCompression {
*/
char* Compress(const char* u_data, const unsigned int start,
const unsigned int length,
unsigned int* compressed_size, char mode);
const unsigned int length, unsigned int* compressed_size,
char mode);
private:
std::string compression_error_;
std::string decompression_error_;
bool std_nintendo_compression_sanity_check = false;
struct CompressionComponent_;
using CompressionComponent = CompressionComponent_;
using CompressionComponent = CompressionComponent_;
struct CompressionComponent_ {
char command;
unsigned int length;
@@ -59,17 +60,16 @@ class StdNintendoCompression {
CompressionComponent* next;
};
void PrintComponent(CompressionComponent * piece);
void PrintComponent(CompressionComponent* piece);
CompressionComponent* CreateComponent(const char command,
const unsigned int length,
const char* args,
const unsigned int argument_length);
const unsigned int length,
const char* args,
const unsigned int argument_length);
void DestroyComponent(CompressionComponent* piece);
void DestroyChain(CompressionComponent* piece);
CompressionComponent* merge_copy(CompressionComponent* start);
unsigned int create_compression_string(CompressionComponent* start, char* output,
char mode);
unsigned int create_compression_string(CompressionComponent* start,
char* output, char mode);
};
class ALTTPCompression {
@@ -125,7 +125,6 @@ class ALTTPCompression {
std::string decompression_error_;
};
} // namespace Utils
} // namespace Application
} // namespace yaze

View File

@@ -4,8 +4,7 @@ namespace yaze {
namespace Application {
namespace Utils {
void ROM::LoadFromFile(const std::string & path) {
void ROM::LoadFromFile(const std::string& path) {
std::cout << "filename: " << path << std::endl;
std::ifstream stream(path, std::ios::in | std::ios::binary);
@@ -14,21 +13,26 @@ void ROM::LoadFromFile(const std::string & path) {
return;
}
std::vector<char> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
std::vector<char> contents((std::istreambuf_iterator<char>(stream)),
std::istreambuf_iterator<char>());
for(auto i: contents) {
int value = i;
std::cout << "data: " << value << std::endl;
for (auto i : contents) {
int value = i;
std::cout << "data: " << value << std::endl;
}
std::cout << "file size: " << contents.size() << std::endl;
unsigned int uncompressed_data_size = 0;
unsigned int compressed_length = 0;
auto gfx_decompressed_data = alttp_compressor_.DecompressGfx(contents.data(), 0, contents.size(), &uncompressed_data_size, &compressed_length);
auto overworld_decompressed = alttp_compressor_.DecompressOverworld(contents.data(), 0, contents.size(), &uncompressed_data_size, &compressed_length);
auto gfx_decompressed_data = alttp_compressor_.DecompressGfx(
contents.data(), 0, contents.size(), &uncompressed_data_size,
&compressed_length);
auto overworld_decompressed = alttp_compressor_.DecompressOverworld(
contents.data(), 0, contents.size(), &uncompressed_data_size,
&compressed_length);
}
}
}
}
} // namespace Utils
} // namespace Application
} // namespace yaze

View File

@@ -1,13 +1,14 @@
#ifndef YAZE_APPLICATION_UTILS_ROM_H
#define YAZE_APPLICATION_UTILS_ROM_H
#include <bits/postypes.h>
#include <cstddef>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <bits/postypes.h>
#include <memory>
#include <iostream>
#include <fstream>
#include <cstddef>
#include "Compression.h"
#include "Core/Constants.h"
@@ -18,7 +19,7 @@ namespace Utils {
class ROM {
public:
void LoadFromFile(const std::string & path);
void LoadFromFile(const std::string& path);
private:
std::vector<char*> original_rom_;
@@ -27,8 +28,8 @@ class ROM {
ALTTPCompression alttp_compressor_;
};
}
}
}
} // namespace Utils
} // namespace Application
} // namespace yaze
#endif

View File

@@ -49,21 +49,21 @@ void Editor::DrawYazeMenu() {
std::string filePath = ImGuiFileDialog::Instance()->GetCurrentPath();
rom.LoadFromFile(filePathName);
}
// close
ImGuiFileDialog::Instance()->Close();
}
}
void Editor::DrawFileMenu() const {
if (ImGui::MenuItem("Open", "Ctrl+O")) {
// TODO: Add the ability to open ALTTP ROM
ImGuiFileDialog::Instance()->OpenDialog("ChooseFileDlgKey", "Open ROM", ".sfc,.smc", ".");
ImGuiFileDialog::Instance()->OpenDialog("ChooseFileDlgKey", "Open ROM",
".sfc,.smc", ".");
}
if (ImGui::BeginMenu("Open Recent")) {
ImGui::MenuItem("alttp.sfc");
// TODO: Display recently accessed files here
// TODO: Display recently accessed files here
ImGui::EndMenu();
}
if (ImGui::MenuItem("Save", "Ctrl+S")) {
@@ -77,22 +77,20 @@ void Editor::DrawFileMenu() const {
// TODO: Make these options matter
if (ImGui::BeginMenu("Options")) {
static bool enabled = true;
ImGui::MenuItem("Enabled", "", &enabled);
ImGui::BeginChild("child", ImVec2(0, 60), true);
for (int i = 0; i < 10; i++)
ImGui::Text("Scrolling Text %d", i);
ImGui::EndChild();
static float f = 0.5f;
static int n = 0;
ImGui::SliderFloat("Value", &f, 0.0f, 1.0f);
ImGui::InputFloat("Input", &f, 0.1f);
ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0");
ImGui::EndMenu();
static bool enabled = true;
ImGui::MenuItem("Enabled", "", &enabled);
ImGui::BeginChild("child", ImVec2(0, 60), true);
for (int i = 0; i < 10; i++) ImGui::Text("Scrolling Text %d", i);
ImGui::EndChild();
static float f = 0.5f;
static int n = 0;
ImGui::SliderFloat("Value", &f, 0.0f, 1.0f);
ImGui::InputFloat("Input", &f, 0.1f);
ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0");
ImGui::EndMenu();
}
}
void Editor::DrawEditMenu() const {
if (ImGui::MenuItem("Undo", "Ctrl+O")) {
// TODO: Implement this
@@ -116,110 +114,131 @@ void Editor::DrawEditMenu() const {
}
}
// i would not recommend doing dungeon because each objects is drawn separately but you could start with OW
// first step would be to decompress all graphics data from the game (in alttp that's easy they're all located in the same location all the same sheet size 128x32)
// have a code that convert PC address to SNES and vice-versa
// first step would be to decompress all graphics data from the game
// (in alttp that's easy they're all located in the same location all the same
// sheet size 128x32) have a code that convert PC address to SNES and vice-versa
// 1) find the gfx pointers (you could use ZS constant file)
// 2) decompress all the gfx with your lz2 decompressor
// 3) convert the 3bpp snes data into PC 4bpp (probably the hardest part)
// 4) get the tiles32 data
// 5) get the tiles16 data
// 6) get the map32 data (they must be decompressed as well with a lz2 variant not the same as gfx compression but pretty similar)
// 7) get the gfx data of the map yeah i forgot that one and load 4bpp in a pseudo vram and use that to render tiles on screen
// 8) try to render the tiles on the bitmap in black & white to start
// 9) get the palettes data and try to find how they're loaded in the game that's a big puzzle to solve
// then 9 you'll have an overworld map viewer, in less than few hours if are able to understand the data quickly
void Editor::DrawOverworldEditor()
{
if (ImGui::BeginTabItem("Overworld"))
{
static ImVector<ImVec2> points;
static ImVec2 scrolling(0.0f, 0.0f);
static bool opt_enable_grid = true;
static bool opt_enable_context_menu = true;
static bool adding_line = false;
// 6) get the map32 data (they must be decompressed as well with a lz2 variant
// not the same as gfx compression but pretty similar)
// 7) get the gfx data of the map
// yeah i forgot that one and load 4bpp in a pseudo vram and use that to
// render tiles on screen
// 8) try to render the tiles on the bitmap in black & white to start
// 9) get the palettes data and try to find how they're loaded in
// the game that's a big puzzle to solve then 9 you'll have an overworld map
// viewer, in less than few hours if are able to understand the data quickly
void Editor::DrawOverworldEditor() {
if (ImGui::BeginTabItem("Overworld")) {
static ImVector<ImVec2> points;
static ImVec2 scrolling(0.0f, 0.0f);
static bool opt_enable_grid = true;
static bool opt_enable_context_menu = true;
static bool adding_line = false;
ImGui::Checkbox("Enable grid", &opt_enable_grid);
ImGui::Checkbox("Enable context menu", &opt_enable_context_menu);
ImGui::Text("Mouse Left: drag to add lines,\nMouse Right: drag to scroll, click for context menu.");
ImGui::Checkbox("Enable grid", &opt_enable_grid);
ImGui::Checkbox("Enable context menu", &opt_enable_context_menu);
ImGui::Text(
"Mouse Left: drag to add lines,\nMouse Right: drag to scroll, click "
"for context menu.");
ImVec2 canvas_p0 = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates!
ImVec2 canvas_sz = ImGui::GetContentRegionAvail(); // Resize canvas to what's available
if (canvas_sz.x < 50.0f) canvas_sz.x = 50.0f;
if (canvas_sz.y < 50.0f) canvas_sz.y = 50.0f;
ImVec2 canvas_p1 = ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y);
ImVec2 canvas_p0 =
ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates!
ImVec2 canvas_sz =
ImGui::GetContentRegionAvail(); // Resize canvas to what's available
if (canvas_sz.x < 50.0f) canvas_sz.x = 50.0f;
if (canvas_sz.y < 50.0f) canvas_sz.y = 50.0f;
ImVec2 canvas_p1 =
ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y);
// Draw border and background color
const ImGuiIO & io = ImGui::GetIO();
ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(canvas_p0, canvas_p1, IM_COL32(50, 50, 50, 255));
draw_list->AddRect(canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255));
// Draw border and background color
const ImGuiIO& io = ImGui::GetIO();
ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(canvas_p0, canvas_p1, IM_COL32(50, 50, 50, 255));
draw_list->AddRect(canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255));
// This will catch our interactions
ImGui::InvisibleButton("canvas", canvas_sz, ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight);
const bool is_hovered = ImGui::IsItemHovered(); // Hovered
const bool is_active = ImGui::IsItemActive(); // Held
const ImVec2 origin(canvas_p0.x + scrolling.x, canvas_p0.y + scrolling.y); // Lock scrolled origin
const ImVec2 mouse_pos_in_canvas(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
// This will catch our interactions
ImGui::InvisibleButton(
"canvas", canvas_sz,
ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight);
const bool is_hovered = ImGui::IsItemHovered(); // Hovered
const bool is_active = ImGui::IsItemActive(); // Held
const ImVec2 origin(canvas_p0.x + scrolling.x,
canvas_p0.y + scrolling.y); // Lock scrolled origin
const ImVec2 mouse_pos_in_canvas(io.MousePos.x - origin.x,
io.MousePos.y - origin.y);
// Add first and second point
if (is_hovered && !adding_line && ImGui::IsMouseClicked(ImGuiMouseButton_Left))
{
points.push_back(mouse_pos_in_canvas);
points.push_back(mouse_pos_in_canvas);
adding_line = true;
// Add first and second point
if (is_hovered && !adding_line &&
ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
points.push_back(mouse_pos_in_canvas);
points.push_back(mouse_pos_in_canvas);
adding_line = true;
}
if (adding_line) {
points.back() = mouse_pos_in_canvas;
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left)) adding_line = false;
}
// Pan (we use a zero mouse threshold when there's no context menu)
// You may decide to make that threshold dynamic based on whether the mouse
// is hovering something etc.
const float mouse_threshold_for_pan =
opt_enable_context_menu ? -1.0f : 0.0f;
if (is_active && ImGui::IsMouseDragging(ImGuiMouseButton_Right,
mouse_threshold_for_pan)) {
scrolling.x += io.MouseDelta.x;
scrolling.y += io.MouseDelta.y;
}
// Context menu (under default mouse threshold)
ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
if (opt_enable_context_menu && drag_delta.x == 0.0f && drag_delta.y == 0.0f)
ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight);
if (ImGui::BeginPopup("context")) {
if (adding_line) points.resize(points.size() - 2);
adding_line = false;
if (ImGui::MenuItem("Remove one", NULL, false, points.Size > 0)) {
points.resize(points.size() - 2);
}
if (adding_line)
{
points.back() = mouse_pos_in_canvas;
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left))
adding_line = false;
if (ImGui::MenuItem("Remove all", NULL, false, points.Size > 0)) {
points.clear();
}
ImGui::EndPopup();
}
// Pan (we use a zero mouse threshold when there's no context menu)
// You may decide to make that threshold dynamic based on whether the mouse is hovering something etc.
const float mouse_threshold_for_pan = opt_enable_context_menu ? -1.0f : 0.0f;
if (is_active && ImGui::IsMouseDragging(ImGuiMouseButton_Right, mouse_threshold_for_pan))
{
scrolling.x += io.MouseDelta.x;
scrolling.y += io.MouseDelta.y;
}
// Draw grid + all lines in the canvas
draw_list->PushClipRect(canvas_p0, canvas_p1, true);
if (opt_enable_grid) {
const float GRID_STEP = 64.0f;
for (float x = fmodf(scrolling.x, GRID_STEP); x < canvas_sz.x;
x += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y),
ImVec2(canvas_p0.x + x, canvas_p1.y),
IM_COL32(200, 200, 200, 40));
for (float y = fmodf(scrolling.y, GRID_STEP); y < canvas_sz.y;
y += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y),
ImVec2(canvas_p1.x, canvas_p0.y + y),
IM_COL32(200, 200, 200, 40));
}
// Context menu (under default mouse threshold)
ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
if (opt_enable_context_menu && drag_delta.x == 0.0f && drag_delta.y == 0.0f)
ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight);
if (ImGui::BeginPopup("context"))
{
if (adding_line)
points.resize(points.size() - 2);
adding_line = false;
if (ImGui::MenuItem("Remove one", NULL, false, points.Size > 0)) { points.resize(points.size() - 2); }
if (ImGui::MenuItem("Remove all", NULL, false, points.Size > 0)) { points.clear(); }
ImGui::EndPopup();
}
for (int n = 0; n < points.Size; n += 2)
draw_list->AddLine(
ImVec2(origin.x + points[n].x, origin.y + points[n].y),
ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y),
IM_COL32(255, 255, 0, 255), 2.0f);
// Draw grid + all lines in the canvas
draw_list->PushClipRect(canvas_p0, canvas_p1, true);
if (opt_enable_grid)
{
const float GRID_STEP = 64.0f;
for (float x = fmodf(scrolling.x, GRID_STEP); x < canvas_sz.x; x += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y), ImVec2(canvas_p0.x + x, canvas_p1.y), IM_COL32(200, 200, 200, 40));
for (float y = fmodf(scrolling.y, GRID_STEP); y < canvas_sz.y; y += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y), ImVec2(canvas_p1.x, canvas_p0.y + y), IM_COL32(200, 200, 200, 40));
}
for (int n = 0; n < points.Size; n += 2)
draw_list->AddLine(ImVec2(origin.x + points[n].x, origin.y + points[n].y), ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y), IM_COL32(255, 255, 0, 255), 2.0f);
draw_list->PopClipRect();
draw_list->PopClipRect();
ImGui::EndTabItem();
ImGui::EndTabItem();
}
}
} // namespace View
} // namespace Application
} // namespace yaze