Skip to content

Commit 790455d

Browse files
committed
Fix legacy level loading and object serialisation and avoid crashes when legacy folder not exist
1 parent 09d29d9 commit 790455d

File tree

8 files changed

+56
-34
lines changed

8 files changed

+56
-34
lines changed

imgui.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ IsChild=1
9292
Size=200,100
9393

9494
[Window][Editor]
95-
Pos=3,26
95+
Pos=2,21
9696
Size=277,265
9797

9898
[Window][Floor]

src/Editor/LegacyFileConverter.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ namespace
5353
constexpr std::array<float, 10> MAX_WALL_HEIGHTS = {4, 3, 2, 1, 2, 3, 4, 4, 4, 3};
5454
constexpr std::array<float, 10> PLATFORM_HEIGHTS = {0, 1, 2, 3};
5555
constexpr std::array<glm::vec2, 4> TRI_WALL_OFFSETS = {
56-
glm::vec2{0, -4},
57-
{0, 4},
58-
{-4, 0},
59-
{4, 0},
56+
glm::vec2{0, -4 * TILE_SIZE_F},
57+
{0, 4 * TILE_SIZE_F},
58+
{-4 * TILE_SIZE_F, 0},
59+
{4 * TILE_SIZE_F, 0},
6060
};
6161
constexpr std::array<glm::vec2, 4> TRIWALL_START_OFFSETS = {
62-
glm::vec2{0, 4},
63-
{0, -4},
64-
{4, 0},
65-
{-4, 0},
62+
glm::vec2{0, 4 * TILE_SIZE_F},
63+
{0, -4 * TILE_SIZE_F},
64+
{4 * TILE_SIZE_F, 0},
65+
{-4 * TILE_SIZE_F, 0},
6666
};
6767

6868
std::pair<float, float> extract_wall_base_and_height(const nlohmann::json& height)
@@ -138,7 +138,7 @@ namespace
138138
auto extract_vec2(float x, float y)
139139
{
140140
// The legacy format has each square as 5 units wide, so the values must be divided by this
141-
return glm::vec2{x, y} / 5.0f;
141+
return glm::vec2{x, y} / 5.0f * TILE_SIZE_F;
142142
}
143143

144144
} // namespace
@@ -229,7 +229,7 @@ void from_json(const nlohmann::json& json, LegacyTriWall& wall)
229229

230230
// TODO: Directions 5,6,7,8 are diagonal and need handling
231231
int direction = json[1][2];
232-
auto offset = direction <= 4 ? TRI_WALL_OFFSETS[direction - 1] : glm::vec2{0, -2};
232+
auto offset = direction <= 4 ? TRI_WALL_OFFSETS[direction - 1] : glm::vec2{0, -2 * TILE_SIZE_F};
233233
start += direction <= 4 ? TRIWALL_START_OFFSETS[direction - 1] : glm::vec2{0, 0};
234234

235235
wall_params.line.start = start;
@@ -272,7 +272,7 @@ void from_json(const nlohmann::json& json, LegacyPlatform& platform)
272272
platform_props.depth = static_cast<float>(props[0]) * 2;
273273

274274
platform_params.position = extract_vec2(position[0], position[1]) -
275-
glm::vec2{platform_props.width / 2.0f};
275+
glm::vec2{(platform_props.width * TILE_SIZE_F) / 2.0f};
276276

277277
if (props.size() > 1)
278278
{
@@ -365,27 +365,27 @@ void from_json(const nlohmann::json& json, LegacyRamp& ramp)
365365
ramp_props.depth = 4;
366366
ramp_props.width = 2;
367367
ramp_props.direction = Direction::Forward; // confirmed
368-
ramp_params.position.x -= 1;
368+
ramp_params.position.x -= TILE_SIZE_F;
369369
break;
370370
case 2:
371371
ramp_props.depth = 4;
372372
ramp_props.width = 2;
373373
ramp_props.direction = Direction::Back;
374-
ramp_params.position.y -= 4;
375-
ramp_params.position.x -= 1;
374+
ramp_params.position.y -= 4 * TILE_SIZE_F;
375+
ramp_params.position.x -= TILE_SIZE_F;
376376
break;
377377
case 3:
378378
ramp_props.depth = 2;
379379
ramp_props.width = 4;
380-
ramp_params.position.y -= 1;
380+
ramp_params.position.y -= TILE_SIZE_F;
381381
ramp_props.direction = Direction::Left; // confirmed
382382
break;
383383
case 4:
384384
ramp_props.depth = 2;
385385
ramp_props.width = 4;
386386
ramp_props.direction = Direction::Right;
387-
ramp_params.position.x -= 4;
388-
ramp_params.position.y -= 1;
387+
ramp_params.position.x -= 4 * TILE_SIZE_F;
388+
ramp_params.position.y -= TILE_SIZE_F;
389389

390390
break;
391391

@@ -451,7 +451,7 @@ void convert_legacy_level(const std::filesystem::path& path)
451451
std::println("Converting to JSON took {}s ({}ms)", time, time * 1000.0f);
452452

453453
// Uncomment to inspect the converted JSON
454-
std::ofstream out_file_og((path.parent_path() / path.stem()).string() + ".json");
454+
// std::ofstream out_file_og((path.parent_path() / path.stem()).string() + ".json");
455455
// out_file_og << legacy_json;
456456
clock.restart();
457457

src/Editor/LevelObjects/Pillar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ SerialiseResponse object_serialise(const PillarObject& pillar)
8787
auto& params = pillar.parameters;
8888
auto& props = pillar.properties;
8989

90-
nlohmann::json json_params = {params.position.x, params.position.y};
90+
nlohmann::json json_params = {params.position.x / TILE_SIZE_F, params.position.y / TILE_SIZE_F};
9191

9292
nlohmann::json json_props = {};
9393
serialise_texture(json_props, props.texture);

src/Editor/LevelObjects/Platform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ SerialiseResponse object_serialise(const PlatformObject& platform)
9595
auto& params = platform.parameters;
9696
auto& props = platform.properties;
9797

98-
nlohmann::json json_params = {params.position.x, params.position.y};
98+
nlohmann::json json_params = {params.position.x / TILE_SIZE_F, params.position.y / TILE_SIZE_F};
9999

100100
nlohmann::json json_props = {};
101101
serialise_texture(json_props, props.texture_top);

src/Editor/LevelObjects/PolygonPlatform.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ SerialiseResponse object_serialise(const PolygonPlatformObject& poly)
104104
auto& params = poly.parameters;
105105
auto& props = poly.properties;
106106

107-
nlohmann::json json_params = {params.corner_top_left.x, params.corner_top_left.y,
108-
params.corner_top_right.x, params.corner_top_right.y,
109-
params.corner_bottom_right.x, params.corner_bottom_right.y,
110-
params.corner_bottom_left.x, params.corner_bottom_left.y};
107+
nlohmann::json json_params = {
108+
params.corner_top_left.x / TILE_SIZE_F, params.corner_top_left.y / TILE_SIZE_F,
109+
params.corner_top_right.x / TILE_SIZE_F, params.corner_top_right.y / TILE_SIZE_F,
110+
params.corner_bottom_right.x / TILE_SIZE_F, params.corner_bottom_right.y / TILE_SIZE_F,
111+
params.corner_bottom_left.x / TILE_SIZE_F, params.corner_bottom_left.y / TILE_SIZE_F};
111112

112113
nlohmann::json json_props = {};
113114
serialise_texture(json_props, props.texture_top);

src/Editor/LevelObjects/Ramp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ SerialiseResponse object_serialise(const RampObject& ramp)
9797
auto& params = ramp.parameters;
9898
auto& props = ramp.properties;
9999

100-
nlohmann::json json_params = {params.position.x, params.position.y};
100+
nlohmann::json json_params = {params.position.x / TILE_SIZE_F, params.position.y / TILE_SIZE_F};
101101

102102
nlohmann::json json_props = {};
103103
serialise_texture(json_props, props.texture_top);

src/Editor/LevelObjects/Wall.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ SerialiseResponse object_serialise(const WallObject& wall)
7676
auto& params = wall.parameters;
7777
auto& props = wall.properties;
7878

79-
nlohmann::json json_params = {params.line.start.x, params.line.start.y, params.line.end.x,
80-
params.line.end.y};
79+
nlohmann::json json_params = {
80+
params.line.start.x / TILE_SIZE_F,
81+
params.line.start.y / TILE_SIZE_F,
82+
params.line.end.x / TILE_SIZE_F,
83+
params.line.end.y / TILE_SIZE_F,
84+
};
8185

8286
nlohmann::json json_props = {};
8387
serialise_texture(json_props, props.texture_back);

src/main.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,34 @@
1616
namespace
1717
{
1818
void handle_event(const sf::Event& event, bool& show_debug_info, bool& close_requested);
19-
} // namespace
2019

21-
int main()
22-
{
23-
for (const auto& entry : std::filesystem::directory_iterator("./levels/legacy/"))
20+
void load_legacy_levels()
2421
{
25-
if (entry.is_regular_file() && entry.path().extension() == ".cy")
22+
if (!std::filesystem::exists("./levels/legacy/"))
2623
{
27-
// convert_legacy_level(entry.path());
24+
std::println("Unable to convert legacy levels as the legacy folder does not exist.");
25+
return;
26+
}
27+
28+
for (const auto& entry : std::filesystem::directory_iterator("./levels/legacy/"))
29+
{
30+
if (entry.is_regular_file() && entry.path().extension() == ".cy")
31+
{
32+
auto original_filename = entry.path().filename().replace_extension(".cly");
33+
auto new_path = std::filesystem::path("./levels/") / original_filename;
34+
35+
if (!std::filesystem::exists(new_path))
36+
{
37+
convert_legacy_level(entry.path());
38+
}
39+
}
2840
}
2941
}
42+
} // namespace
43+
44+
int main()
45+
{
46+
load_legacy_levels();
3047

3148
sf::ContextSettings context_settings;
3249
context_settings.depthBits = 24;

0 commit comments

Comments
 (0)