This commit is contained in:
2025-05-27 17:35:33 -05:00
parent 264457e283
commit d4f1253207
9 changed files with 214 additions and 59 deletions

10
README.md Normal file
View File

@@ -0,0 +1,10 @@
# Editor2D Project
C++ Tools for 2D Level Creation.
* Editor Library
* Level
* Layer
* Tileset
* Tile
* Entity
* Editor2D Application

View File

@@ -13,6 +13,7 @@
#include "NewMapDialog.hpp"
#include "JUI/Widgets/ColorPicker.hpp"
#include "Preferences.hpp"
#include <Level.hpp>
#define GL_VER_MAJOR 2
#define GL_VER_MINOR 1
@@ -72,6 +73,8 @@ public:
NewMapDialog* nmd = nullptr;
Level loaded_level;
Preferences preferences;
JUI::Window* bg_color_tool_window = nullptr;
@@ -159,6 +162,7 @@ public:
/// Saves the current editor's data to test.lvl.
/// @note Placeholder until FileDialogs are added.
void SaveTestFile();
void LoadLevel(const std::filesystem::path& level_meta_path);
/// Loads test-data, including a tilesheet, and a binary level-data file.
/// @note Placeholder until FileDialogs are added.

View File

@@ -1,7 +1,140 @@
#pragma once
#include <string>
#include <vector>
#include <Layer.hpp>
#include <JJX/JSON.hpp>
#include <Colors.hpp>
#include <Utils.hpp>
class Layer
{
public:
std::string binary_path;
std::string name;
int rows;
int cols;
int cell_width;
int cell_height;
int** cells;
Layer(int rows, int cols, int cell_width, int cell_height)
{
this->rows = rows;
this->cols = cols;
this->cell_width = cell_width;
this->cell_height = cell_height;
}
explicit Layer(const json::value& json)
{
Deserialize(json);
}
explicit Layer(const std::filesystem::path& path)
{
auto [json, err] = json::parse(read_file_contents(path));
Deserialize(json);
}
json::value Serialize() const
{
json::object data;
data["name"] = name;
data["binary-path"] = binary_path;
data["rows"] = (float)rows;
data["cols"] = (float)cols;
data["cell-width"] = (float)cell_width;
data["cell-height"] = (float)cell_height;
class Layer {
public:
return data;
}
};
void Deserialize(const json::value& json)
{
name = json["name"].as_string();
binary_path = json["binary-path"].as_string();
}
void InitGrid()
{
cells = new int*[rows];
for (int i = 0; i < rows; i++)
cells[i] = new int[cols];
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
cells[x][y] = -1;
}
}
}
void DeleteGrid()
{
for (int i = 0; i < rows; i++)
delete[] cells[i];
delete[] cells;
}
void LoadFromDataBuffer(const int* buffer)
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
{
int index = (x * cols + y);
cells[x][y] = buffer[index];
}
}
}
void WriteToDataBuffer(int* buffer)
{
//int* buffer = new int[rows*cols];
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
{
int index = (x*cols + y);
buffer[index] = cells[x][y];
}
}
}
void WriteToFile()
{
int* buffer = new int[rows*cols];
WriteToDataBuffer(buffer);
std::ofstream output;
output.open(binary_path, std::ios::out | std::ios::binary);
output.write(reinterpret_cast<const char*>(buffer), rows * cols * sizeof(int));
output.close();
delete[] buffer;
}
void ReadFromFile()
{
std::ifstream input;
input.open(binary_path, std::ios::binary | std::ios::in);
input.seekg(0, std::ios::end);
int data_length = input.tellg();
input.seekg(0, std::ios::beg);
char* buffer = new char[data_length];
input.read(buffer, data_length);
input.close();
LoadFromDataBuffer(reinterpret_cast<int*>(buffer));
delete[] buffer;
}
~Layer() { }
};

View File

@@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <vector>
#include <TileLayer.hpp>
#include <Layer.hpp>
#include <JJX/JSON.hpp>
#include <Colors.hpp>
#include <Utils.hpp>
@@ -10,7 +10,6 @@ using namespace JJX;
const float REDACTED_EDITOR_LIB_VERSION = 1;
class Level {
public:
std::string name;
@@ -19,9 +18,10 @@ public:
std::vector<std::string> tags;
int rows;
int cols;
std::vector<TileLayer> tile_layers;
std::vector<Layer> layers;
std::string tileset_path;
Level() {}
explicit Level(const std::filesystem::path& path)
{
@@ -38,7 +38,14 @@ public:
name = json["name"].as_string();
description = json["description"].as_string();
author = json["author"].as_string();
tileset_path = json["tileset_path"].as_string();
tileset_path = json["tileset-path"].as_string();
auto layer_json_array = json["layers"].as_array();
for (auto& layer_json : layer_json_array)
{
layers.push_back(Layer(layer_json));
}
}
@@ -57,7 +64,13 @@ public:
data["tile-height"] = 16.f;
data["bgcolor"] = JsonConversions::deparse_color_to_hex(Colors::Black);
data["tags"] = JsonConversions::deparse_string_list(tags);
data["layers"] = json::array();
json::array layers;
for (auto& layer : this->layers)
{
layers.push_back(layer.Serialize());
}
data["layers"] = layers;
return data;
}

View File

@@ -1,24 +0,0 @@
#pragma once
struct TileCell {
int ID;
bool flipH;
bool flipV;
};
class TileLayer {
public:
int rows;
int cols;
int cell_width;
int cell_height;
TileCell** cells;
TileLayer(int rows, int cols, int cell_width, int cell_height)
{
}
protected:
private:
};

View File

@@ -28,22 +28,19 @@ struct Tile
json::value metadata;
};
class Tileset {
public:
std::string name;
std::string author;
std::string file_path;
int tile_width;
int tile_height;
int tile_spacing;
int tile_count;
int tex_width;
int tex_height;
int tiles_per_row;
int rows;
int cols;

View File

@@ -11,7 +11,14 @@ void CreateSampleLayer()
void CreateSampleLevel()
{
Level level;
level.name = "Test Level";
level.author = "Automated Tooling";
level.rows = 128;
level.cols = 128;
level.tileset_path = "tileset.json";
write_file_contents("level.json", json::deparse(level.Serialize()));
}
void CreateSampleTileset()
@@ -19,29 +26,35 @@ void CreateSampleTileset()
// TODO: Read texture size via std_image.
Tileset tileset("tileset", "../megacommando.png", 1024, 1280, 16, 16, 0);
write_file_contents("tileset.tileset", json::deparse(tileset.Serialize()));
write_file_contents("tileset.json", json::deparse(tileset.Serialize()));
}
int main()
{
if (!std::filesystem::exists("tileset.tileset"))
if (!std::filesystem::exists("tileset.json"))
CreateSampleTileset();
if (!std::filesystem::exists("test_level"))
if (!std::filesystem::exists("level.json"))
CreateSampleLevel();
/*Tileset tileset("tileset", "../megacommando.png", 1024, 1280, 16, 16, 0);
if (!std::filesystem::exists("tileset.tileset"))
CreateSampleTileset();
else
tileset = Tileset(std::filesystem::path("tileset.tileset"));*/
//Level level("test_level");
Tileset tileset = Tileset(std::filesystem::path("tileset.tileset"));
Tileset tileset = Tileset(std::filesystem::path("tileset.json"));
Level level = Level(std::filesystem::path("level.json"));
Layer layer(128, 128, 16, 16);
layer.binary_path = "test.lvl";
layer.ReadFromFile();
level.layers.push_back(layer);
write_file_contents("level.json", json::deparse(level.Serialize()));
std::cout << "Completed all tests" << std::endl;
return 0;
}

View File

@@ -126,19 +126,24 @@ void EditorApp::SaveTestFile()
delete[] buffer;
}
void EditorApp::LoadLevel(const std::filesystem::path& level_meta_path)
{
}
void EditorApp::LoadMisc()
{
Tileset tileset("tileset", "../megacommando.png", 1024, 1280, 16, 16, 0);
if (!std::filesystem::exists("../tileset.json"))
write_file_contents("tileset.tileset", json::deparse(tileset.Serialize()));
else
tileset = Tileset(std::filesystem::path("../tileset.tileset"));
test_tilesheet = new JGL::Texture("../megacommando.png");
auto texture_size = test_tilesheet->GetDimensions();
tileset_width = texture_size.x / grid_pixel_width;
tileset_height = texture_size.y / grid_pixel_height;
Tileset tileset(std::filesystem::path("tileset.json"));
test_tilesheet = new JGL::Texture(tileset.texture_path);
//auto texture_size = test_tilesheet->GetDimensions();
tileset_width = tileset.rows;//texture_size.x / grid_pixel_width;
tileset_height = tileset.cols;//texture_size.y / grid_pixel_height;
PopulateQuads();

View File

@@ -60,11 +60,15 @@ void Tileset::Deserialize(const json::value& json)
this->tex_width = (int)json["texture-width"].number.value();
this->tex_height = (int)json["texture-height"].number.value();
auto tilemeta = json["tiledata"].as_array();
/*auto tilemeta = json["tiledata"].as_array();
for (json::value& v : tilemeta)
{
this->tile_metadata.push_back(v);
}
}*/
this->rows = J3ML::Math::Floor(this->tex_width / this->tile_width);
this->cols = J3ML::Math::Floor(this->tex_height / this->tile_height);
ComputeQuads();
}
@@ -100,7 +104,7 @@ json::value Tileset::Serialize() const
tileset["quads"] = quad_array;*/
json::array tiles;
/*json::array tiles;
for (auto& tile : this->tiles)
{
json::object tile_json;
@@ -115,7 +119,7 @@ json::value Tileset::Serialize() const
tile_json["quad"] = quad;
tiles.push_back(tile_json);
}
tileset["tiles"] = tiles;
tileset["tiles"] = tiles;*/
return tileset;
}