Skip to content

Commit 649c68e

Browse files
committed
Fix offset calculations for tilemaps when saving
1 parent a4a27db commit 649c68e

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/object/tilemap.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "supertux/debug.hpp"
2727
#include "supertux/gameconfig.hpp"
2828
#include "supertux/globals.hpp"
29+
#include "supertux/level.hpp"
2930
#include "supertux/resources.hpp"
3031
#include "supertux/sector.hpp"
3132
#include "supertux/tile.hpp"
@@ -52,7 +53,7 @@ TileMap::TileMap(const TileSet *new_tileset) :
5253
m_width(0),
5354
m_height(0),
5455
m_z_pos(0),
55-
m_offset(Vector(0, 32)),
56+
m_offset(Vector(0, 0)),
5657
m_movement(0, 0),
5758
m_objects_hit_bottom(),
5859
m_ground_movement_manager(nullptr),
@@ -86,7 +87,7 @@ TileMap::TileMap(const TileSet *tileset_, const ReaderMapping& reader) :
8687
m_width(-1),
8788
m_height(-1),
8889
m_z_pos(0),
89-
m_offset(Vector(0, 32)),
90+
m_offset(Vector(0, 0)),
9091
m_movement(Vector(0, 0)),
9192
m_objects_hit_bottom(),
9293
m_ground_movement_manager(nullptr),
@@ -344,7 +345,7 @@ TileMap::after_editor_set()
344345
}
345346
} else {
346347
if (m_add_path) {
347-
init_path_pos(m_offset);
348+
init_path_pos(get_offset());
348349
}
349350
}
350351

@@ -620,15 +621,27 @@ TileMap::resize(int new_width, int new_height, int fill_id,
620621
apply_offset_y(fill_id, yoffset);
621622
}
622623

623-
void TileMap::resize(const Size& newsize, const Size& resize_offset) {
624+
void
625+
TileMap::resize(const Size& newsize, const Size& resize_offset) {
624626
resize(newsize.width, newsize.height, 0, resize_offset.width, resize_offset.height);
625627
}
626628

629+
Vector
630+
TileMap::get_offset() const
631+
{
632+
// Apply custom offset for tilemap to fit buttons at the top of the editor.
633+
// This extra offset isn't saved.
634+
if (Editor::is_active() && !Level::current()->is_saving_in_progress())
635+
return m_offset + Vector(0, 32);
636+
637+
return m_offset;
638+
}
639+
627640
Rect
628641
TileMap::get_tiles_overlapping(const Rectf &rect) const
629642
{
630643
Rectf rect2 = rect;
631-
rect2.move(-m_offset);
644+
rect2.move(-get_offset());
632645

633646
int t_left = std::max(0 , int(floorf(rect2.get_left () / 32)));
634647
int t_right = std::min(m_width , int(ceilf (rect2.get_right () / 32)));
@@ -681,7 +694,7 @@ TileMap::get_tile_id(const Vector& pos) const
681694
bool
682695
TileMap::is_outside_bounds(const Vector& pos) const
683696
{
684-
auto pos_ = (pos - m_offset) / 32.0f;
697+
auto pos_ = (pos - get_offset()) / 32.0f;
685698
float width = static_cast<float>(m_width);
686699
float height = static_cast<float>(m_height);
687700
return pos_.x < 0 || pos_.x >= width || pos_.y < 0 || pos_.y >= height;
@@ -697,7 +710,7 @@ TileMap::get_tile(int x, int y) const
697710
uint32_t
698711
TileMap::get_tile_id_at(const Vector& pos) const
699712
{
700-
Vector xy = (pos - m_offset) / 32.0f;
713+
Vector xy = (pos - get_offset()) / 32.0f;
701714
return get_tile_id(static_cast<int>(xy.x), static_cast<int>(xy.y));
702715
}
703716

@@ -732,7 +745,7 @@ TileMap::change(int idx, uint32_t newtile)
732745
void
733746
TileMap::change_at(const Vector& pos, uint32_t newtile)
734747
{
735-
Vector xy = (pos - m_offset) / 32.0f;
748+
Vector xy = (pos - get_offset()) / 32.0f;
736749
change(int(xy.x), int(xy.y), newtile);
737750
}
738751

@@ -964,11 +977,11 @@ void
964977
TileMap::move_by(const Vector& shift)
965978
{
966979
if (!get_path()) {
967-
init_path_pos(m_offset);
980+
init_path_pos(get_offset());
968981
m_add_path = true;
969982
}
970983
get_path()->move_by(shift);
971-
m_offset += shift;
984+
set_offset(get_offset() + shift);
972985
}
973986

974987
void

src/object/tilemap.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class TileMap final : public LayerObject,
9999
inline Size get_size() const { return Size(m_width, m_height); }
100100

101101
inline void set_offset(const Vector &offset_) { m_offset = offset_; }
102-
inline Vector get_offset() const { return m_offset; }
102+
Vector get_offset() const;
103103

104104
void set_ground_movement_manager(const std::shared_ptr<CollisionGroundMovementManager>& movement_manager)
105105
{
@@ -124,7 +124,7 @@ class TileMap final : public LayerObject,
124124
/** Returns the position of the upper-left corner of tile (x, y) in
125125
the sector. */
126126
Vector get_tile_position(int x, int y) const
127-
{ return m_offset + Vector(static_cast<float>(x), static_cast<float>(y)) * 32.0f; }
127+
{ return get_offset() + Vector(static_cast<float>(x), static_cast<float>(y)) * 32.0f; }
128128

129129
Rectf get_bbox() const {
130130
return Rectf(get_tile_position(0, 0),

src/supertux/level.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ Level::Level(bool worldmap) :
5454
m_skip_cutscene(false),
5555
m_icon(),
5656
m_icon_locked(),
57-
m_wmselect_bkg()
57+
m_wmselect_bkg(),
58+
m_saving_in_progress(false)
5859
{
5960
s_current = this;
6061

@@ -174,6 +175,8 @@ Level::save(const std::string& filepath, bool retry)
174175
void
175176
Level::save(Writer& writer)
176177
{
178+
m_saving_in_progress = true;
179+
177180
writer.start_list("supertux-level");
178181
// Starts writing to supertux level file. Keep this at the very beginning.
179182

@@ -220,6 +223,8 @@ Level::save(Writer& writer)
220223

221224
// Ends writing to supertux level file. Keep this at the very end.
222225
writer.end_list("supertux-level");
226+
227+
m_saving_in_progress = false;
223228
}
224229

225230
std::string

src/supertux/level.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class Level final
6464
int get_total_badguys() const;
6565
int get_total_secrets() const;
6666

67+
bool is_saving_in_progress() const { return m_saving_in_progress; }
68+
6769
void reactivate();
6870

6971
inline bool is_worldmap() const { return m_is_worldmap; }
@@ -114,6 +116,9 @@ class Level final
114116
std::string m_icon_locked;
115117
std::string m_wmselect_bkg;
116118

119+
private:
120+
bool m_saving_in_progress;
121+
117122
private:
118123
Level(const Level&) = delete;
119124
Level& operator=(const Level&) = delete;

0 commit comments

Comments
 (0)