-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load and save the tileset with nlohmann::json. The tile prototypes stored in the tileset can be modified at runtime, and the new values are kept between a run and another.
- Loading branch information
Showing
6 changed files
with
106 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <nlohmann/json.hpp> | ||
|
||
|
||
namespace spot::jam | ||
{ | ||
|
||
|
||
class Tileset; | ||
|
||
void to_json( nlohmann::json& j, const Tileset& t ); | ||
|
||
|
||
} // namespace spot::jam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "spot/jam/serialization/tileset.h" | ||
#include "spot/jam/tileset.h" | ||
|
||
#include <spot/file/ifstream.h> | ||
#include <spot/gfx/model.h> | ||
|
||
#include "spot/jam/serialization/tile.h" | ||
|
||
namespace spot::jam | ||
{ | ||
|
||
|
||
void to_json( nlohmann::json& j, const Tileset& t ) | ||
{ | ||
j["image"] = t.image->uri; | ||
|
||
auto tiles = nlohmann::json::array(); | ||
for ( auto& [_,pair] : t.tiles ) | ||
{ | ||
const Tile& tile = pair.first; | ||
tiles.emplace_back( tile ); | ||
} | ||
j["tiles"] = tiles; | ||
} | ||
|
||
|
||
Tileset Tileset::from_json( const char* path, gfx::Model& model ) | ||
{ | ||
auto file = file::Ifstream( path ); | ||
nlohmann::json j; | ||
file >> j; | ||
|
||
auto image_path = j["image"].get<std::string>(); | ||
|
||
auto tileset = Tileset( image_path.c_str(), model ); | ||
|
||
for ( auto& tile : j["tiles"] ) | ||
{ | ||
tileset.emplace( tile.get<Tile>(), model ); | ||
} | ||
|
||
return tileset; | ||
} | ||
|
||
|
||
void Tileset::save( const char* path ) const | ||
{ | ||
nlohmann::json j = *this; | ||
auto file = std::fstream( path, std::ios::trunc | std::ios::out ); | ||
file << j; | ||
} | ||
|
||
|
||
} // namespace spot::jam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters