Building Editor Library, libtest executable, and testgame executable.
This commit is contained in:
@@ -54,7 +54,8 @@ if (WIN32)
|
||||
ADD_LIBRARY(Editor2D STATIC ${EDITOR_SRC}
|
||||
include/Preferences.hpp
|
||||
src/Utils.cpp
|
||||
include/NewMapDialog.hpp)
|
||||
include/NewMapDialog.hpp
|
||||
src/Tileset.cpp)
|
||||
endif()
|
||||
|
||||
set_target_properties(Editor2D PROPERTIES LINKER_LANGUAGE CXX)
|
||||
@@ -74,4 +75,12 @@ target_link_libraries(Editor2D PUBLIC Event J3ML jlog ReWindow JGL JUI mcolor jj
|
||||
|
||||
|
||||
add_executable(Editor2DApp main.cpp)
|
||||
target_link_libraries(Editor2DApp PUBLIC Editor2D)
|
||||
target_link_libraries(Editor2DApp PUBLIC Editor2D)
|
||||
|
||||
|
||||
add_executable(testgame testgame.cpp)
|
||||
target_link_libraries(testgame PUBLIC Editor2D)
|
||||
|
||||
|
||||
add_executable(libtest libtest.cpp)
|
||||
target_link_libraries(libtest PUBLIC Editor2D)
|
||||
|
@@ -19,21 +19,36 @@ public:
|
||||
std::vector<std::string> tags;
|
||||
int rows;
|
||||
int cols;
|
||||
|
||||
|
||||
|
||||
std::vector<TileLayer> tile_layers;
|
||||
std::string tileset_path;
|
||||
|
||||
explicit Level(const json::value& data) {
|
||||
|
||||
explicit Level(const std::filesystem::path& path)
|
||||
{
|
||||
auto [json, err] = json::parse(read_file_contents(path));
|
||||
Deserialize(json);
|
||||
}
|
||||
|
||||
explicit Level(const json::value& json) {
|
||||
Deserialize(json);
|
||||
}
|
||||
|
||||
void Deserialize(const json::value& json)
|
||||
{
|
||||
name = json["name"].as_string();
|
||||
description = json["description"].as_string();
|
||||
author = json["author"].as_string();
|
||||
tileset_path = json["tileset_path"].as_string();
|
||||
|
||||
}
|
||||
|
||||
json::value Serialize() const
|
||||
{
|
||||
json::object data;
|
||||
data["name"] = name;
|
||||
data["description"] = description;
|
||||
data["author"] = author;
|
||||
data["tileset-path"] = tileset_path;
|
||||
data["editor-version"] = REDACTED_EDITOR_LIB_VERSION;
|
||||
data["map-type"] = "OrthogonalTileMap";
|
||||
data["map-rows"] = (double)rows;
|
||||
@@ -47,7 +62,7 @@ public:
|
||||
return data;
|
||||
}
|
||||
|
||||
void Deserialize() const {
|
||||
void Deserialize(const json::value& json) const {
|
||||
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <JJX/JSON.hpp>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <vector>
|
||||
#include <Utils.hpp>
|
||||
|
||||
#include <J3ML/Math.hpp>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include "JGL/types/Texture.h"
|
||||
@@ -18,6 +20,14 @@ struct Quad {
|
||||
int h;
|
||||
};
|
||||
|
||||
struct Tile
|
||||
{
|
||||
int id;
|
||||
std::string name;
|
||||
Quad quad;
|
||||
json::value metadata;
|
||||
};
|
||||
|
||||
class Tileset {
|
||||
public:
|
||||
std::string name;
|
||||
@@ -39,39 +49,23 @@ public:
|
||||
|
||||
std::vector<Quad> quads;
|
||||
std::vector<json::value> tile_metadata;
|
||||
std::vector<Tile> tiles;
|
||||
|
||||
std::string texture_path;
|
||||
JGL::Texture* texture;
|
||||
|
||||
public:
|
||||
Tileset(const std::string& name, const std::filesystem::path& texture_path, int tile_width, int tile_height, int tile_spacing) {
|
||||
this->name = name;
|
||||
this->file_path = std::format("{}.tileset", name);
|
||||
/// This constructor is used for the initial definition of a Tileset. i.e. From Editor->Tileset->New
|
||||
Tileset(const std::string& name, const std::filesystem::path& texture_path, int tex_width, int tex_height, int tile_width, int tile_height, int tile_spacing);
|
||||
|
||||
this->texture = new JGL::Texture(texture_path);
|
||||
auto texture_size = this->texture->GetDimensions();
|
||||
this->tex_width = texture_size.x;
|
||||
this->tex_height = texture_size.y;
|
||||
this->texture_path = texture_path.string();
|
||||
|
||||
this->tile_width = tile_width;
|
||||
this->tile_height = tile_height;
|
||||
this->tile_spacing = tile_spacing;
|
||||
|
||||
this->rows;
|
||||
this->cols;
|
||||
|
||||
|
||||
this->rows = J3ML::Math::Floor(this->tex_width / this->tile_width);
|
||||
this->cols = J3ML::Math::Floor(this->tex_height / this->tile_height);
|
||||
|
||||
|
||||
ComputeQuads();
|
||||
|
||||
}
|
||||
/// Constructs the Tileset from a json data structure.
|
||||
explicit Tileset(const json::value& json);
|
||||
|
||||
/// Loads the Tileset from a file-path containing a json-string.
|
||||
explicit Tileset(const std::filesystem::path& filePath);
|
||||
|
||||
/// Loads the Tileset parameters from a json data structure.
|
||||
void Deserialize(const json::value& json);
|
||||
|
||||
json::value Serialize() const;
|
||||
|
||||
|
||||
@@ -81,18 +75,9 @@ public:
|
||||
|
||||
protected:
|
||||
|
||||
Vector2i IndexToCell(int index) {
|
||||
return {index % rows, index / rows};
|
||||
}
|
||||
inline int CellToIndex(int cx, int cy) {
|
||||
return cy*rows + cx;
|
||||
}
|
||||
void ComputeQuads() {
|
||||
quads.reserve(rows*cols);
|
||||
Vector2i IndexToCell(int index) const;
|
||||
|
||||
for (int i = 0; i < rows*cols; i++) {
|
||||
Vector2i cell = IndexToCell(i);
|
||||
quads[i] = Quad{cell.x*tile_width, cell.y*tile_height, tile_width, tile_height};
|
||||
}
|
||||
}
|
||||
inline int CellToIndex(int cx, int cy);
|
||||
|
||||
void ComputeQuads();
|
||||
};
|
47
libtest.cpp
Normal file
47
libtest.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <Tileset.hpp>
|
||||
#include <Level.hpp>
|
||||
#include <Entity.hpp>
|
||||
#include <Layer.hpp>
|
||||
|
||||
|
||||
void CreateSampleLayer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CreateSampleLevel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
if (!std::filesystem::exists("tileset.tileset"))
|
||||
CreateSampleTileset();
|
||||
|
||||
if (!std::filesystem::exists("test_level"))
|
||||
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"));
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
@@ -2,6 +2,8 @@
|
||||
|
||||
#include "NewMapDialog.hpp"
|
||||
|
||||
#include <Tileset.hpp>
|
||||
|
||||
void EditorApp::PopulateQuads()
|
||||
{
|
||||
quads.reserve(tileset_width*tileset_height);
|
||||
@@ -66,6 +68,8 @@ void EditorApp::SavePreferences()
|
||||
|
||||
void EditorApp::LoadTestFile()
|
||||
{
|
||||
|
||||
|
||||
if (!std::filesystem::exists("test_level"))
|
||||
std::filesystem::create_directory("test_level");
|
||||
|
||||
@@ -124,6 +128,12 @@ void EditorApp::SaveTestFile()
|
||||
|
||||
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();
|
||||
|
141
src/Tileset.cpp
Normal file
141
src/Tileset.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
#include <Tileset.hpp>
|
||||
|
||||
Tileset::Tileset(const std::string& name, const std::filesystem::path& texture_path, int tex_width, int tex_height, int tile_width, int tile_height,
|
||||
int tile_spacing)
|
||||
{
|
||||
this->name = name;
|
||||
this->file_path = std::format("{}.tileset", name);
|
||||
|
||||
this->tex_width = tex_width;
|
||||
this->tex_height = tex_height;
|
||||
this->texture_path = texture_path.string();
|
||||
|
||||
this->tile_width = tile_width;
|
||||
this->tile_height = tile_height;
|
||||
this->tile_spacing = tile_spacing;
|
||||
|
||||
this->rows;
|
||||
this->cols;
|
||||
|
||||
this->rows = J3ML::Math::Floor(this->tex_width / this->tile_width);
|
||||
this->cols = J3ML::Math::Floor(this->tex_height / this->tile_height);
|
||||
|
||||
|
||||
|
||||
ComputeQuads();
|
||||
|
||||
|
||||
// Generate some tiles
|
||||
for (int i = 0 ; i < this->rows*this->cols; i++)
|
||||
{
|
||||
Tile tile;
|
||||
tile.id = i;
|
||||
tile.quad = quads[i];
|
||||
tile.name = std::format("tile_{}", i);
|
||||
tile.metadata = json::value();
|
||||
tiles.push_back(tile);
|
||||
}
|
||||
}
|
||||
|
||||
Tileset::Tileset(const json::value& json)
|
||||
{
|
||||
Deserialize(json);
|
||||
}
|
||||
|
||||
Tileset::Tileset(const std::filesystem::path& filePath)
|
||||
{
|
||||
auto [json, err] = json::parse(read_file_contents(filePath));
|
||||
Deserialize(json);
|
||||
}
|
||||
|
||||
void Tileset::Deserialize(const json::value& json)
|
||||
{
|
||||
this->name = json["name"].as_string();
|
||||
this->author = json["author"].as_string();
|
||||
this->file_path = json["file-path"].as_string();
|
||||
this->texture_path = json["texture-path"].as_string();
|
||||
this->tile_width = (int)json["tile-width"].number.value();
|
||||
this->tile_height = (int)json["tile-height"].number.value();
|
||||
this->tile_spacing = (int)json["tile-spacing"].number.value();
|
||||
this->tex_width = (int)json["texture-width"].number.value();
|
||||
this->tex_height = (int)json["texture-height"].number.value();
|
||||
|
||||
auto tilemeta = json["tiledata"].as_array();
|
||||
for (json::value& v : tilemeta)
|
||||
{
|
||||
this->tile_metadata.push_back(v);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
json::value Tileset::Serialize() const
|
||||
{
|
||||
json::object tileset;
|
||||
tileset["name"] = this->name;
|
||||
tileset["file-path"] = this->file_path;
|
||||
tileset["texture-path"] = this->texture_path;
|
||||
tileset["author"] = this->author;
|
||||
|
||||
|
||||
tileset["tile-width"] = (float) this->tile_width;
|
||||
tileset["tile-height"] = (float) this->tile_height;
|
||||
tileset["tile-spacing"] = (float)this->tile_spacing;
|
||||
tileset["texture-width"] = (float)this->tex_width;
|
||||
tileset["texture-height"] = (float)this->tex_height;
|
||||
|
||||
tileset["first-gid"] = 0.f;
|
||||
tileset["rows"] = (float)this->rows;
|
||||
tileset["cols"] = (float)this->cols;
|
||||
|
||||
/*json::array quad_array;
|
||||
for (int i = 0; i < this->quads.size(); ++i)
|
||||
{
|
||||
json::array quad;
|
||||
quad.push_back((float)quads[i].x);
|
||||
quad.push_back((float)quads[i].y);
|
||||
quad.push_back((float)quads[i].h);
|
||||
quad.push_back((float)quads[i].w);
|
||||
quad_array.push_back(quad);
|
||||
}
|
||||
|
||||
tileset["quads"] = quad_array;*/
|
||||
|
||||
json::array tiles;
|
||||
for (auto& tile : this->tiles)
|
||||
{
|
||||
json::object tile_json;
|
||||
tile_json["name"] = tile.name;
|
||||
tile_json["id"] = (float)tile.id;
|
||||
tile_json["metadata"] = tile.metadata;
|
||||
json::array quad;
|
||||
quad.push_back((float)tile.quad.x);
|
||||
quad.push_back((float)tile.quad.y);
|
||||
quad.push_back((float)tile.quad.w);
|
||||
quad.push_back((float)tile.quad.h);
|
||||
tile_json["quad"] = quad;
|
||||
tiles.push_back(tile_json);
|
||||
}
|
||||
tileset["tiles"] = tiles;
|
||||
|
||||
return tileset;
|
||||
}
|
||||
|
||||
Vector2i Tileset::IndexToCell(int index) const
|
||||
{
|
||||
return {index % rows, index / rows};
|
||||
}
|
||||
|
||||
int Tileset::CellToIndex(int cx, int cy)
|
||||
{
|
||||
return cy*rows + cx;
|
||||
}
|
||||
|
||||
void Tileset::ComputeQuads()
|
||||
{
|
||||
quads.reserve(rows*cols);
|
||||
|
||||
for (int i = 0; i < rows*cols; i++) {
|
||||
Vector2i cell = IndexToCell(i);
|
||||
quads.push_back(Quad{cell.x*tile_width, cell.y*tile_height, tile_width, tile_height});
|
||||
}
|
||||
}
|
42
testgame.cpp
Normal file
42
testgame.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <ReWindow/types/Window.h>
|
||||
|
||||
#include "ReWindow/Logger.h"
|
||||
|
||||
class TestGameAppWindow : public ReWindow::OpenGLWindow
|
||||
{
|
||||
public:
|
||||
TestGameAppWindow() : ReWindow::OpenGLWindow("TestGameAppWindow", 1024, 768, 2, 1)
|
||||
{
|
||||
|
||||
}
|
||||
~TestGameAppWindow() override
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
ReWindow::Logger::Debug.EnableConsole(false);
|
||||
|
||||
TestGameAppWindow* app = new TestGameAppWindow();
|
||||
|
||||
bool success = app->Open();
|
||||
|
||||
if (!success)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
while (app->IsOpen())
|
||||
{
|
||||
app->ManagedRefresh();
|
||||
}
|
||||
|
||||
|
||||
app->Close();
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
Reference in New Issue
Block a user