Skip to content

Commit 6853bb5

Browse files
committed
Fix offset calculations for tilemaps when saving
1 parent dc30e2a commit 6853bb5

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
@@ -27,6 +27,7 @@
2727
#include "supertux/debug.hpp"
2828
#include "supertux/gameconfig.hpp"
2929
#include "supertux/globals.hpp"
30+
#include "supertux/level.hpp"
3031
#include "supertux/resources.hpp"
3132
#include "supertux/sector.hpp"
3233
#include "supertux/tile.hpp"
@@ -53,7 +54,7 @@ TileMap::TileMap(const TileSet *new_tileset) :
5354
m_width(0),
5455
m_height(0),
5556
m_z_pos(0),
56-
m_offset(Vector(0, 32)),
57+
m_offset(Vector(0, 0)),
5758
m_movement(0, 0),
5859
m_objects_hit_bottom(),
5960
m_ground_movement_manager(nullptr),
@@ -87,7 +88,7 @@ TileMap::TileMap(const TileSet *tileset_, const ReaderMapping& reader) :
8788
m_width(-1),
8889
m_height(-1),
8990
m_z_pos(0),
90-
m_offset(Vector(0, 32)),
91+
m_offset(Vector(0, 0)),
9192
m_movement(Vector(0, 0)),
9293
m_objects_hit_bottom(),
9394
m_ground_movement_manager(nullptr),
@@ -393,7 +394,7 @@ TileMap::after_editor_set()
393394
}
394395
} else {
395396
if (m_add_path) {
396-
init_path_pos(m_offset);
397+
init_path_pos(get_offset());
397398
}
398399
}
399400

@@ -677,15 +678,27 @@ TileMap::resize(int new_width, int new_height, int fill_id,
677678
apply_offset_y(fill_id, yoffset);
678679
}
679680

680-
void TileMap::resize(const Size& newsize, const Size& resize_offset) {
681+
void
682+
TileMap::resize(const Size& newsize, const Size& resize_offset) {
681683
resize(newsize.width, newsize.height, 0, resize_offset.width, resize_offset.height);
682684
}
683685

686+
Vector
687+
TileMap::get_offset() const
688+
{
689+
// Apply custom offset for tilemap to fit buttons at the top of the editor.
690+
// This extra offset isn't saved.
691+
if (Editor::is_active() && !Level::current()->is_saving_in_progress())
692+
return m_offset + Vector(0, 32);
693+
694+
return m_offset;
695+
}
696+
684697
Rect
685698
TileMap::get_tiles_overlapping(const Rectf &rect) const
686699
{
687700
Rectf rect2 = rect;
688-
rect2.move(-m_offset);
701+
rect2.move(-get_offset());
689702

690703
int t_left = std::max(0 , int(floorf(rect2.get_left () / 32)));
691704
int t_right = std::min(m_width , int(ceilf (rect2.get_right () / 32)));
@@ -738,7 +751,7 @@ TileMap::get_tile_id(const Vector& pos) const
738751
bool
739752
TileMap::is_outside_bounds(const Vector& pos) const
740753
{
741-
auto pos_ = (pos - m_offset) / 32.0f;
754+
auto pos_ = (pos - get_offset()) / 32.0f;
742755
float width = static_cast<float>(m_width);
743756
float height = static_cast<float>(m_height);
744757
return pos_.x < 0 || pos_.x >= width || pos_.y < 0 || pos_.y >= height;
@@ -754,7 +767,7 @@ TileMap::get_tile(int x, int y) const
754767
uint32_t
755768
TileMap::get_tile_id_at(const Vector& pos) const
756769
{
757-
Vector xy = (pos - m_offset) / 32.0f;
770+
Vector xy = (pos - get_offset()) / 32.0f;
758771
return get_tile_id(static_cast<int>(xy.x), static_cast<int>(xy.y));
759772
}
760773

@@ -789,7 +802,7 @@ TileMap::change(int idx, uint32_t newtile)
789802
void
790803
TileMap::change_at(const Vector& pos, uint32_t newtile)
791804
{
792-
Vector xy = (pos - m_offset) / 32.0f;
805+
Vector xy = (pos - get_offset()) / 32.0f;
793806
change(int(xy.x), int(xy.y), newtile);
794807
}
795808

@@ -1021,11 +1034,11 @@ void
10211034
TileMap::move_by(const Vector& shift)
10221035
{
10231036
if (!get_path()) {
1024-
init_path_pos(m_offset);
1037+
init_path_pos(get_offset());
10251038
m_add_path = true;
10261039
}
10271040
get_path()->move_by(shift);
1028-
m_offset += shift;
1041+
set_offset(get_offset() + shift);
10291042
}
10301043

10311044
void

src/object/tilemap.hpp

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

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

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

131131
Rectf get_bbox() const {
132132
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)