Skip to content

Commit 3574f84

Browse files
committed
Fix adding tilesets at an offset
1 parent a7abb8c commit 3574f84

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/tileset.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ bool Tileset::refresh_inactive_image() {
4040

4141
bool Tileset::draw_tile(const Tile_State *ts, int x, int y, bool active) const {
4242
int index = (int)ts->id - _start_id + _offset;
43-
int limit = (int)_num_tiles - _offset;
44-
if (_length > 0 && _length < limit) { limit = _length + _offset; }
43+
int limit = (int)_num_tiles;
44+
if (_length > 0) { limit = MIN(limit, _length + _offset); }
4545
if (index < _offset || index >= limit) { return false; }
4646

4747
Fl_RGB_Image *img = active ? _image : _inactive_image;
@@ -66,8 +66,8 @@ bool Tileset::draw_tile(const Tile_State *ts, int x, int y, bool active) const {
6666

6767
bool Tileset::print_tile(const Tile_State *ts, int x, int y) const {
6868
int index = (int)ts->id - _start_id + _offset;
69-
int limit = (int)_num_tiles - _offset;
70-
if (_length > 0 && _length < limit) { limit = _length + _offset; }
69+
int limit = (int)_num_tiles;
70+
if (_length > 0) { limit = MIN(limit, _length + _offset); }
7171
if (index < _offset || index >= limit || !_image) { return false; }
7272

7373
int wt = _image->w() / TILE_SIZE_2X;
@@ -194,7 +194,10 @@ static void convert_2bpp_row(uchar b1, uchar b2, Hue *row) {
194194
Tileset::Result Tileset::parse_1bpp_data(size_t n, uchar *data) {
195195
n /= BYTES_PER_1BPP_TILE;
196196
_num_tiles = n;
197-
if (_start_id + _num_tiles > NUM_TILES) { delete [] data; return (_result = TILESET_TOO_LARGE); }
197+
198+
int limit = (int)_num_tiles - _offset;
199+
if (_length > 0) { limit = MIN(limit, _length + _offset); }
200+
if (_start_id + limit > NUM_TILES) { delete [] data; return (_result = TILESET_TOO_LARGE); }
198201

199202
Fl_Image_Surface *surface = new Fl_Image_Surface(TILE_SIZE, (int)n * TILE_SIZE);
200203
surface->set_current();
@@ -223,7 +226,10 @@ Tileset::Result Tileset::parse_1bpp_data(size_t n, uchar *data) {
223226
Tileset::Result Tileset::parse_2bpp_data(size_t n, uchar *data) {
224227
n /= BYTES_PER_2BPP_TILE;
225228
_num_tiles = n;
226-
if (_start_id + _num_tiles > NUM_TILES) { delete [] data; return (_result = TILESET_TOO_LARGE); }
229+
230+
int limit = (int)_num_tiles - _offset;
231+
if (_length > 0) { limit = MIN(limit, _length + _offset); }
232+
if (_start_id + limit > NUM_TILES) { delete [] data; return (_result = TILESET_TOO_LARGE); }
227233

228234
Fl_Image_Surface *surface = new Fl_Image_Surface(TILE_SIZE, (int)n * TILE_SIZE);
229235
surface->set_current();
@@ -267,7 +273,10 @@ Tileset::Result Tileset::postprocess_graphics(Fl_RGB_Image *img) {
267273
w /= TILE_SIZE_2X;
268274
h /= TILE_SIZE_2X;
269275
_num_tiles = w * h;
270-
if (_start_id + _num_tiles > NUM_TILES) { return (_result = TILESET_TOO_LARGE); }
276+
277+
int limit = (int)_num_tiles - _offset;
278+
if (_length > 0) { limit = MIN(limit, _length + _offset); }
279+
if (_start_id + limit > NUM_TILES) { return (_result = TILESET_TOO_LARGE); }
271280

272281
return (_result = TILESET_OK);
273282
}

0 commit comments

Comments
 (0)