diff --git a/src/common/map.cpp b/src/common/map.cpp index 3eaf0b54..c07e03a3 100644 --- a/src/common/map.cpp +++ b/src/common/map.cpp @@ -227,7 +227,7 @@ void DrawMapHazard(const MapHazard& hazard, short iSize, bool fDrawCenter) } } -void DrawPlatform(PlatformPathType pathtype, TilesetTile ** tiles, short startX, short startY, short endX, short endY, float angle, float radiusX, float radiusY, short iSize, short iPlatformWidth, short iPlatformHeight, bool fDrawPlatform, bool fDrawShadow) +void DrawPlatform(PlatformPathType pathtype, const std::vector& tiles, short startX, short startY, short endX, short endY, float angle, float radiusX, float radiusY, short iSize, short iPlatformWidth, short iPlatformHeight, bool fDrawPlatform, bool fDrawShadow) { short iStartX = startX >> iSize; short iStartY = startY >> iSize; @@ -243,7 +243,7 @@ void DrawPlatform(PlatformPathType pathtype, TilesetTile ** tiles, short startX, if (fDrawPlatform) { for (short iPlatformX = 0; iPlatformX < iPlatformWidth; iPlatformX++) { for (short iPlatformY = 0; iPlatformY < iPlatformHeight; iPlatformY++) { - TilesetTile * tile = &tiles[iPlatformX][iPlatformY]; + const TilesetTile& tile = tiles[iPlatformX * iPlatformHeight + iPlatformY]; int iDstX = 0; int iDstY = 0; @@ -257,11 +257,11 @@ void DrawPlatform(PlatformPathType pathtype, TilesetTile ** tiles, short startX, } SDL_Rect bltrect = {iDstX, iDstY, iTileSize, iTileSize}; - if (tile->iID >= 0) { - SDL_BlitSurface(g_tilesetmanager->tileset(tile->iID)->surface(iSize), g_tilesetmanager->rect(iSize, tile->iCol, tile->iRow), blitdest, &bltrect); - } else if (tile->iID == TILESETANIMATED) { - SDL_BlitSurface(rm->spr_tileanimation[iSize].getSurface(), g_tilesetmanager->rect(iSize, tile->iCol * 4, tile->iRow), blitdest, &bltrect); - } else if (tile->iID == TILESETUNKNOWN) { + if (tile.iID >= 0) { + SDL_BlitSurface(g_tilesetmanager->tileset(tile.iID)->surface(iSize), g_tilesetmanager->rect(iSize, tile.iCol, tile.iRow), blitdest, &bltrect); + } else if (tile.iID == TILESETANIMATED) { + SDL_BlitSurface(rm->spr_tileanimation[iSize].getSurface(), g_tilesetmanager->rect(iSize, tile.iCol * 4, tile.iRow), blitdest, &bltrect); + } else if (tile.iID == TILESETUNKNOWN) { //Draw unknown tile SDL_BlitSurface(rm->spr_unknowntile[iSize].getSurface(), g_tilesetmanager->rect(iSize, 0, 0), blitdest, &bltrect); } @@ -281,11 +281,11 @@ void DrawPlatform(PlatformPathType pathtype, TilesetTile ** tiles, short startX, bltrect.w = iTileSize; bltrect.h = iTileSize; - if (tile->iID >= 0) - SDL_BlitSurface(g_tilesetmanager->tileset(tile->iID)->surface(iSize), g_tilesetmanager->rect(iSize, tile->iCol, tile->iRow), blitdest, &bltrect); - else if (tile->iID == TILESETANIMATED) - SDL_BlitSurface(rm->spr_tileanimation[iSize].getSurface(), g_tilesetmanager->rect(iSize, tile->iCol * 4, tile->iRow), blitdest, &bltrect); - else if (tile->iID == TILESETUNKNOWN) + if (tile.iID >= 0) + SDL_BlitSurface(g_tilesetmanager->tileset(tile.iID)->surface(iSize), g_tilesetmanager->rect(iSize, tile.iCol, tile.iRow), blitdest, &bltrect); + else if (tile.iID == TILESETANIMATED) + SDL_BlitSurface(rm->spr_tileanimation[iSize].getSurface(), g_tilesetmanager->rect(iSize, tile.iCol * 4, tile.iRow), blitdest, &bltrect); + else if (tile.iID == TILESETUNKNOWN) SDL_BlitSurface(rm->spr_unknowntile[iSize].getSurface(), g_tilesetmanager->rect(iSize, 0, 0), blitdest, &bltrect); } } @@ -298,14 +298,14 @@ void DrawPlatform(PlatformPathType pathtype, TilesetTile ** tiles, short startX, if (fDrawShadow) { for (short iCol = 0; iCol < iPlatformWidth; iCol++) { for (short iRow = 0; iRow < iPlatformHeight; iRow++) { - if (tiles[iCol][iRow].iID != -2) + if (tiles[iCol * iPlatformHeight + iRow].iID != -2) rm->spr_platformstarttile.draw(iStartX - (iPlatformWidth << (iSizeShift - 1)) + (iCol << iSizeShift), iStartY - (iPlatformHeight << (iSizeShift - 1)) + (iRow << iSizeShift), 0, 0, iTileSize, iTileSize); } } for (short iCol = 0; iCol < iPlatformWidth; iCol++) { for (short iRow = 0; iRow < iPlatformHeight; iRow++) { - if (tiles[iCol][iRow].iID != -2) + if (tiles[iCol * iPlatformHeight + iRow].iID != -2) rm->spr_platformendtile.draw(iEndX - (iPlatformWidth << (iSizeShift - 1)) + (iCol << iSizeShift), iEndY - (iPlatformHeight << (iSizeShift - 1)) + (iRow << iSizeShift), 0, 0, iTileSize, iTileSize); } } @@ -335,7 +335,7 @@ void DrawPlatform(PlatformPathType pathtype, TilesetTile ** tiles, short startX, if (fDrawShadow) { for (short iCol = 0; iCol < iPlatformWidth; iCol++) { for (short iRow = 0; iRow < iPlatformHeight; iRow++) { - if (tiles[iCol][iRow].iID != -2) + if (tiles[iCol * iPlatformHeight + iRow].iID != -2) rm->spr_platformstarttile.draw(iStartX - (iPlatformWidth << (iSizeShift - 1)) + (iCol << iSizeShift), iStartY - (iPlatformHeight << (iSizeShift - 1)) + (iRow << iSizeShift), 0, 0, iTileSize, iTileSize); } } @@ -386,7 +386,7 @@ void DrawPlatform(PlatformPathType pathtype, TilesetTile ** tiles, short startX, for (short iCol = 0; iCol < iPlatformWidth; iCol++) { for (short iRow = 0; iRow < iPlatformHeight; iRow++) { - if (tiles[iCol][iRow].iID != -2) + if (tiles[iCol * iPlatformHeight + iRow].iID != -2) rm->spr_platformstarttile.draw(iEllipseStartX + (iCol << iSizeShift), iEllipseStartY + (iRow << iSizeShift), 0, 0, iTileSize, iTileSize); } } @@ -740,12 +740,12 @@ void CMap::saveMap(const std::string& file) for (short iRow = 0; iRow < platform->iTileHeight; iRow++) { //Set the tile type flags for each tile - TileType iType = platform->iTileType[iCol][iRow]; + TileType iType = platform->tileTypeAt(iCol, iRow); unsigned short iFlags = tileToFlags(iType); - TilesetTile * tile = &platform->iTileData[iCol][iRow]; + const TilesetTile& tile = platform->tileAt(iCol, iRow); - if (tile->iID != TILESETNONE) + if (tile.iID != TILESETNONE) iPlatformCount++; if (iFlags & tile_flag_has_death) @@ -867,10 +867,9 @@ void CMap::saveMap(const std::string& file) for (MovingPlatform* platform : platforms) { for (short iCol = 0; iCol < platform->iTileWidth; iCol++) { for (short iRow = 0; iRow < platform->iTileHeight; iRow++) { - TilesetTile * tile = &platform->iTileData[iCol][iRow]; - - if (tile->iID >= 0) - fTilesetUsed[tile->iID] = true; + const TilesetTile& tile = platform->tileAt(iCol, iRow); + if (tile.iID >= 0) + fTilesetUsed[tile.iID] = true; } } } @@ -941,22 +940,22 @@ void CMap::saveMap(const std::string& file) for (short iCol = 0; iCol < platform->iTileWidth; iCol++) { for (short iRow = 0; iRow < platform->iTileHeight; iRow++) { - TilesetTile * tile = &platform->iTileData[iCol][iRow]; + TilesetTile tile = platform->tileAt(iCol, iRow); //Make sure the tile's col and row are within the tileset - if (tile->iID >= 0) { - if (tile->iCol < 0 || tile->iCol >= g_tilesetmanager->tileset(tile->iID)->width()) - tile->iCol = 0; + if (tile.iID >= 0) { + if (tile.iCol < 0 || tile.iCol >= g_tilesetmanager->tileset(tile.iID)->width()) + tile.iCol = 0; - if (tile->iRow < 0 || tile->iRow >= g_tilesetmanager->tileset(tile->iID)->height()) - tile->iRow = 0; + if (tile.iRow < 0 || tile.iRow >= g_tilesetmanager->tileset(tile.iID)->height()) + tile.iRow = 0; } - mapfile.write_i8(tile->iID); - mapfile.write_i8(tile->iCol); - mapfile.write_i8(tile->iRow); + mapfile.write_i8(tile.iID); + mapfile.write_i8(tile.iCol); + mapfile.write_i8(tile.iRow); - mapfile.write_i32(static_cast(platform->iTileType[iCol][iRow])); + mapfile.write_i32(static_cast(platform->tileTypeAt(iCol, iRow))); } } @@ -1662,14 +1661,13 @@ void CMap::addPlatformAnimatedTiles() for (MovingPlatform* platform : platforms) { short iHeight = platform->iTileHeight; short iWidth = platform->iTileWidth; - TilesetTile ** tiles = platform->iTileData; short iDestX = 0; short iDestY = 0; for (short iRow = 0; iRow < iHeight; iRow++) { for (short iCol = 0; iCol < iWidth; iCol++) { - if (tiles[iCol][iRow].iID == TILESETANIMATED) { + if (platform->tileAt(iCol, iRow).iID == TILESETANIMATED) { AnimatedTile * animatedtile = new AnimatedTile(); animatedtile->id = -1; //we don't want this ID to collide with an animated map tile @@ -1677,15 +1675,15 @@ void CMap::addPlatformAnimatedTiles() animatedtile->fForegroundAnimated = false; animatedtile->pPlatform = platform; - TilesetTile * tile = &tiles[iCol][iRow]; + const TilesetTile& tile = platform->tileAt(iCol, iRow); TilesetTile * toTile = &animatedtile->layers[0]; - toTile->iID = tile->iID; - toTile->iCol = tile->iCol; - toTile->iRow = tile->iRow; + toTile->iID = tile.iID; + toTile->iCol = tile.iCol; + toTile->iRow = tile.iRow; for (short iRect = 0; iRect < 4; iRect++) { - (animatedtile->rSrc[0][iRect]) = {(iRect + (tile->iCol << 2)) << 5, tile->iRow << 5, TILESIZE, TILESIZE}; + (animatedtile->rSrc[0][iRect]) = {(iRect + (tile.iCol << 2)) << 5, tile.iRow << 5, TILESIZE, TILESIZE}; } (animatedtile->rDest) = {iDestX, iDestY, TILESIZE, TILESIZE}; diff --git a/src/common/map.h b/src/common/map.h index 228f6b4b..020fbbed 100644 --- a/src/common/map.h +++ b/src/common/map.h @@ -156,7 +156,7 @@ struct MapBlock { class IO_Block; void DrawMapHazard(const MapHazard& hazard, short iSize, bool fDrawCenter); -void DrawPlatform(PlatformPathType pathtype, TilesetTile ** tiles, +void DrawPlatform(PlatformPathType pathtype, const std::vector& tiles, short startX, short startY, short endX, short endY, float angle, float radiusX, float radiusY, short iSize, short iPlatformWidth, short iPlatformHeight, diff --git a/src/common/map/MapReader.h b/src/common/map/MapReader.h index e3061f3c..f67378d2 100644 --- a/src/common/map/MapReader.h +++ b/src/common/map/MapReader.h @@ -3,6 +3,8 @@ #include "FileIO.h" +#include + class CMap; class MovingPlatformPath; struct TilesetTile; @@ -93,7 +95,7 @@ class MapReader1700 : public MapReader1600 /* ignore 1.6 patches */ { virtual bool read_spawn_areas(CMap&, BinaryFile&); virtual void read_platforms(CMap&, BinaryFile&, bool preview); - virtual void read_platform_tiles(CMap&, BinaryFile&, short w, short h, TilesetTile**&, TileType**&); + virtual std::pair, std::vector> read_platform_tiles(CMap&, BinaryFile&, short w, short h); MovingPlatformPath* read_platform_path_details(BinaryFile&, short type, bool preview); unsigned char patch_version; @@ -142,7 +144,7 @@ class MapReader1800 : public MapReader1702 { virtual void read_extra_tiledata(CMap&, BinaryFile&); virtual void read_gamemode_settings(CMap&, BinaryFile&); virtual void read_platforms(CMap&, BinaryFile&, bool preview); - virtual void read_platform_tiles(CMap&, BinaryFile&, short w, short h, TilesetTile**&, TileType**&); + virtual std::pair, std::vector> read_platform_tiles(CMap&, BinaryFile&, short w, short h); private: short iMaxTilesetID; diff --git a/src/common/map/MapReader17xx.cpp b/src/common/map/MapReader17xx.cpp index da70e486..e9c8fb1c 100644 --- a/src/common/map/MapReader17xx.cpp +++ b/src/common/map/MapReader17xx.cpp @@ -230,10 +230,7 @@ void MapReader1700::read_platforms(CMap& map, BinaryFile& mapfile, bool fPreview short iWidth = (short)mapfile.read_i32(); short iHeight = (short)mapfile.read_i32(); - TilesetTile ** tiles = new TilesetTile*[iWidth]; - TileType ** types = new TileType*[iWidth]; - - read_platform_tiles(map, mapfile, iWidth, iHeight, tiles, types); + auto [tiles, types] = read_platform_tiles(map, mapfile, iWidth, iHeight); short iDrawLayer = 2; //printf("Layer: %d\n", iDrawLayer); @@ -245,42 +242,47 @@ void MapReader1700::read_platforms(CMap& map, BinaryFile& mapfile, bool fPreview if (!path) continue; - MovingPlatform* platform = new MovingPlatform(tiles, types, iWidth, iHeight, iDrawLayer, path, fPreview); + MovingPlatform* platform = new MovingPlatform(std::move(tiles), std::move(types), iWidth, iHeight, iDrawLayer, path, fPreview); map.platforms.emplace_back(platform); map.platformdrawlayer[iDrawLayer].push_back(platform); } } -void MapReader1700::read_platform_tiles(CMap& map, BinaryFile& mapfile, - short iWidth, short iHeight, TilesetTile**& tiles, TileType**& types) +std::pair, std::vector> +MapReader1700::read_platform_tiles(CMap& map, BinaryFile& mapfile, short iWidth, short iHeight) { - for (short iCol = 0; iCol < iWidth; iCol++) { - tiles[iCol] = new TilesetTile[iHeight]; - types[iCol] = new TileType[iHeight]; + std::vector tiles; + std::vector types; + tiles.reserve(iWidth * iHeight); + types.reserve(iWidth * iHeight); + for (short iCol = 0; iCol < iWidth; iCol++) { for (short iRow = 0; iRow < iHeight; iRow++) { - TilesetTile * tile = &tiles[iCol][iRow]; - short iTile = mapfile.read_i32(); + + TilesetTile tile; TileType type; if (iTile == TILESETSIZE) { - tile->iID = TILESETNONE; - tile->iCol = 0; - tile->iRow = 0; + tile.iID = TILESETNONE; + tile.iCol = 0; + tile.iRow = 0; type = TileType::NonSolid; } else { - tile->iID = g_tilesetmanager->classicTilesetIndex(); - tile->iCol = iTile % TILESETWIDTH; - tile->iRow = iTile / TILESETWIDTH; + tile.iID = g_tilesetmanager->classicTilesetIndex(); + tile.iCol = iTile % TILESETWIDTH; + tile.iRow = iTile / TILESETWIDTH; - type = g_tilesetmanager->classicTileset().tileType(tile->iCol, tile->iRow); + type = g_tilesetmanager->classicTileset().tileType(tile.iCol, tile.iRow); } - types[iCol][iRow] = type; + tiles.emplace_back(std::move(tile)); + types.emplace_back(std::move(type)); } } + + return {tiles, types}; } MovingPlatformPath* MapReader1700::read_platform_path_details(BinaryFile& mapfile, short iPathType, bool fPreview) diff --git a/src/common/map/MapReader18xx.cpp b/src/common/map/MapReader18xx.cpp index ad58763c..d92f4af4 100644 --- a/src/common/map/MapReader18xx.cpp +++ b/src/common/map/MapReader18xx.cpp @@ -285,10 +285,7 @@ void MapReader1800::read_platforms(CMap& map, BinaryFile& mapfile, bool fPreview short iWidth = (short)mapfile.read_i32(); short iHeight = (short)mapfile.read_i32(); - TilesetTile ** tiles = new TilesetTile*[iWidth]; - TileType ** types = new TileType*[iWidth]; - - read_platform_tiles(map, mapfile, iWidth, iHeight, tiles, types); + auto [tiles, types] = read_platform_tiles(map, mapfile, iWidth, iHeight); short iDrawLayer = 2; if (patch_version >= 1) @@ -306,45 +303,52 @@ void MapReader1800::read_platforms(CMap& map, BinaryFile& mapfile, bool fPreview if (!path) continue; - MovingPlatform* platform = new MovingPlatform(tiles, types, iWidth, iHeight, iDrawLayer, path, fPreview); + MovingPlatform* platform = new MovingPlatform(std::move(tiles), std::move(types), iWidth, iHeight, iDrawLayer, path, fPreview); map.platforms.emplace_back(platform); map.platformdrawlayer[iDrawLayer].push_back(platform); } } -void MapReader1800::read_platform_tiles(CMap& map, BinaryFile& mapfile, - short iWidth, short iHeight, TilesetTile**& tiles, TileType**& types) +std::pair, std::vector> +MapReader1800::read_platform_tiles(CMap& map, BinaryFile& mapfile, short iWidth, short iHeight) { - for (short iCol = 0; iCol < iWidth; iCol++) { - tiles[iCol] = new TilesetTile[iHeight]; - types[iCol] = new TileType[iHeight]; + std::vector tiles; + std::vector types; + tiles.reserve(iWidth * iHeight); + types.reserve(iWidth * iHeight); + for (short iCol = 0; iCol < iWidth; iCol++) { for (short iRow = 0; iRow < iHeight; iRow++) { - TilesetTile * tile = &tiles[iCol][iRow]; + TilesetTile tile; - tile->iID = mapfile.read_i8(); - tile->iCol = mapfile.read_i8(); - tile->iRow = mapfile.read_i8(); + tile.iID = mapfile.read_i8(); + tile.iCol = mapfile.read_i8(); + tile.iRow = mapfile.read_i8(); - if (tile->iID >= 0) { - if (iMaxTilesetID != -1 && tile->iID > iMaxTilesetID) - tile->iID = 0; + if (tile.iID >= 0) { + if (iMaxTilesetID != -1 && tile.iID > iMaxTilesetID) + tile.iID = 0; //Make sure the column and row we read in is within the bounds of the tileset - if (tile->iCol < 0 || (tilesetwidths && tile->iCol >= tilesetwidths[tile->iID])) - tile->iCol = 0; + if (tile.iCol < 0 || (tilesetwidths && tile.iCol >= tilesetwidths[tile.iID])) + tile.iCol = 0; - if (tile->iRow < 0 || (tilesetheights && tile->iRow >= tilesetheights[tile->iID])) - tile->iRow = 0; + if (tile.iRow < 0 || (tilesetheights && tile.iRow >= tilesetheights[tile.iID])) + tile.iRow = 0; //Convert tileset ids into the current game's tileset's ids if (translationid) - tile->iID = translationid[tile->iID]; + tile.iID = translationid[tile.iID]; } - types[iCol][iRow] = (TileType)mapfile.read_i32();; + TileType type = static_cast(mapfile.read_i32()); + + tiles.emplace_back(std::move(tile)); + types.emplace_back(std::move(type)); } } + + return {tiles, types}; } bool MapReader1800::load(CMap& map, BinaryFile& mapfile/*, const char* filename*/, ReadType readtype) diff --git a/src/common/movingplatform.cpp b/src/common/movingplatform.cpp index bd0a9614..dcb7bb43 100644 --- a/src/common/movingplatform.cpp +++ b/src/common/movingplatform.cpp @@ -34,7 +34,7 @@ enum CollisionStyle {collision_none, collision_normal, collision_overlap_left, c // Moving Platform //------------------------------------------------------------------------------ -MovingPlatform::MovingPlatform(TilesetTile ** tiledata, TileType ** tiletypes, short w, short h, short drawlayer, MovingPlatformPath * path, bool fPreview) +MovingPlatform::MovingPlatform(std::vector&& tiledata, std::vector&& tiletypes, short w, short h, short drawlayer, MovingPlatformPath * path, bool fPreview) { fDead = false; iPlayerId = -1; @@ -47,8 +47,10 @@ MovingPlatform::MovingPlatform(TilesetTile ** tiledata, TileType ** tiletypes, s iTileSizeIndex = 1; } - iTileData = tiledata; - iTileType = tiletypes; + iTileData = std::move(tiledata); + iTileType = std::move(tiletypes); + assert(iTileData.size() == iTileType.size()); + assert(iTileData.size() == static_cast(w * h)); iTileWidth = w; iTileHeight = h; @@ -80,16 +82,16 @@ MovingPlatform::MovingPlatform(TilesetTile ** tiledata, TileType ** tiletypes, s for (short iSurface = 0; iSurface < 2; iSurface++) { for (short iCol = 0; iCol < iTileWidth; iCol++) { for (short iRow = 0; iRow < iTileHeight; iRow++) { - TilesetTile * tile = &iTileData[iCol][iRow]; + const TilesetTile& tile = iTileData[iCol * iTileHeight + iRow]; - if (tile->iID == TILESETNONE) + if (tile.iID == TILESETNONE) continue; - if (tile->iID >= 0) { - g_tilesetmanager->Draw(sSurface[iSurface], tile->iID, iTileSizeIndex, tile->iCol, tile->iRow, iCol, iRow); - } else if (tile->iID == TILESETANIMATED) { - SDL_BlitSurface(rm->spr_tileanimation[iTileSizeIndex].getSurface(), g_tilesetmanager->rect(iTileSizeIndex, tile->iCol * 4, tile->iRow), sSurface[iSurface], g_tilesetmanager->rect(iTileSizeIndex, iCol, iRow)); - } else if (tile->iID == TILESETUNKNOWN) { + if (tile.iID >= 0) { + g_tilesetmanager->Draw(sSurface[iSurface], tile.iID, iTileSizeIndex, tile.iCol, tile.iRow, iCol, iRow); + } else if (tile.iID == TILESETANIMATED) { + SDL_BlitSurface(rm->spr_tileanimation[iTileSizeIndex].getSurface(), g_tilesetmanager->rect(iTileSizeIndex, tile.iCol * 4, tile.iRow), sSurface[iSurface], g_tilesetmanager->rect(iTileSizeIndex, iCol, iRow)); + } else if (tile.iID == TILESETUNKNOWN) { SDL_BlitSurface(rm->spr_unknowntile[iTileSizeIndex].getSurface(), g_tilesetmanager->rect(iTileSizeIndex, 0, 0), sSurface[iSurface], g_tilesetmanager->rect(iTileSizeIndex, iCol, iRow)); } } @@ -115,20 +117,22 @@ MovingPlatform::MovingPlatform(TilesetTile ** tiledata, TileType ** tiletypes, s MovingPlatform::~MovingPlatform() { - for (short iCol = 0; iCol < iTileWidth; iCol++) { - delete [] iTileData[iCol]; - delete [] iTileType[iCol]; - } - - delete [] iTileData; - delete [] iTileType; - delete pPath; SDL_FreeSurface(sSurface[0]); SDL_FreeSurface(sSurface[1]); } +const TilesetTile& MovingPlatform::tileAt(size_t col, size_t row) const +{ + return iTileData[col * iTileHeight + row]; +} + +TileType MovingPlatform::tileTypeAt(size_t col, size_t row) const +{ + return iTileType[col * iTileHeight + row]; +} + void MovingPlatform::draw() { //Comment this back in to see the no spawn area of the platform @@ -350,10 +354,10 @@ void MovingPlatform::collide(CPlayer * player) int t2 = tile_flag_nonsolid; if (fRelativeY1 >= 0.0f && fRelativeY1 < iHeight) - t1 = tileToFlags(iTileType[tx][(short)fRelativeY1 / TILESIZE]); + t1 = tileToFlags(tileTypeAt(tx, (short)fRelativeY1 / TILESIZE)); if (fRelativeY2 >= 0.0f && fRelativeY2 < iHeight) - t2 = tileToFlags(iTileType[tx][(short)fRelativeY2 / TILESIZE]); + t2 = tileToFlags(tileTypeAt(tx, (short)fRelativeY2 / TILESIZE)); if ((t1 & tile_flag_solid) || (t2 & tile_flag_solid)) { bool fDeathTileToLeft = ((t1 & tile_flag_death_on_left) && (t2 & tile_flag_death_on_left)) || @@ -413,10 +417,10 @@ void MovingPlatform::collide(CPlayer * player) int t2 = tile_flag_nonsolid; if (fRelativeY1 >= 0 && fRelativeY1 < iHeight) - t1 = tileToFlags(iTileType[tx][(short)fRelativeY1 / TILESIZE]); + t1 = tileToFlags(tileTypeAt(tx, (short)fRelativeY1 / TILESIZE)); if (fRelativeY2 >= 0 && fRelativeY2 < iHeight) - t2 = tileToFlags(iTileType[tx][(short)fRelativeY2 / TILESIZE]); + t2 = tileToFlags(tileTypeAt(tx, (short)fRelativeY2 / TILESIZE)); if ((t1 & tile_flag_solid) || (t2 & tile_flag_solid)) { bool fDeathTileToRight = ((t1 & tile_flag_death_on_right) && (t2 & tile_flag_death_on_right)) || @@ -496,10 +500,10 @@ void MovingPlatform::collide(CPlayer * player) int t2 = tile_flag_nonsolid; if (fRelativeX1 >= 0.0f && fRelativeX1 < iWidth) - t1 = tileToFlags(iTileType[(short)fRelativeX1 / TILESIZE][ty]); + t1 = tileToFlags(tileTypeAt((short)fRelativeX1 / TILESIZE, ty)); if (fRelativeX2 >= 0.0f && fRelativeX2 < iWidth) - t2 = tileToFlags(iTileType[(short)fRelativeX2 / TILESIZE][ty]); + t2 = tileToFlags(tileTypeAt((short)fRelativeX2 / TILESIZE, ty)); bool fSolidTileOverPlayer = (t1 & tile_flag_solid) || (t2 & tile_flag_solid); @@ -551,10 +555,10 @@ void MovingPlatform::collide(CPlayer * player) int t2 = tile_flag_nonsolid; if (fRelativeX1 >= 0.0f && fRelativeX1 < iWidth) - t1 = tileToFlags(iTileType[(short)fRelativeX1 / TILESIZE][ty]); + t1 = tileToFlags(tileTypeAt((short)fRelativeX1 / TILESIZE, ty)); if (fRelativeX2 >= 0.0f && fRelativeX2 < iWidth) - t2 = tileToFlags(iTileType[(short)fRelativeX2 / TILESIZE][ty]); + t2 = tileToFlags(tileTypeAt((short)fRelativeX2 / TILESIZE, ty)); bool fSolidTileUnderPlayer = (t1 & tile_flag_solid) || (t2 & tile_flag_solid); @@ -762,10 +766,10 @@ bool MovingPlatform::collision_detection_check_sides(IO_MovingObject * object) int t2 = tile_flag_nonsolid; if (tyTop >= 0) - t1 = tileToFlags(iTileType[txLeft][tyTop]); + t1 = tileToFlags(tileTypeAt(txLeft, tyTop)); if (tyBottom >= 0) - t2 = tileToFlags(iTileType[txLeft][tyBottom]); + t2 = tileToFlags(tileTypeAt(txLeft, tyBottom)); if (t1 & tile_flag_solid) iCase |= 0x01; @@ -779,10 +783,10 @@ bool MovingPlatform::collision_detection_check_sides(IO_MovingObject * object) int t2 = tile_flag_nonsolid; if (tyTop >= 0) - t1 = tileToFlags(iTileType[txRight][tyTop]); + t1 = tileToFlags(tileTypeAt(txRight, tyTop)); if (tyBottom >= 0) - t2 = tileToFlags(iTileType[txRight][tyBottom]); + t2 = tileToFlags(tileTypeAt(txRight, tyBottom)); if (t1 & tile_flag_solid) iCase |= 0x02; @@ -1009,10 +1013,10 @@ void MovingPlatform::GetTileTypesFromPlayer(CPlayer * player, int * lefttile, in fRelativeX2 = player->fx + PW - fx + iHalfWidth; if (fRelativeX1 >= 0.0f && fRelativeX1 < iWidth) - *lefttile = tileToFlags(iTileType[(short)fRelativeX1 / TILESIZE][ty]); + *lefttile = tileToFlags(tileTypeAt((short)fRelativeX1 / TILESIZE, ty)); if (fRelativeX2 >= 0.0f && fRelativeX2 < iWidth) - *righttile = tileToFlags(iTileType[(short)fRelativeX2 / TILESIZE][ty]); + *righttile = tileToFlags(tileTypeAt((short)fRelativeX2 / TILESIZE, ty)); } int MovingPlatform::GetTileTypeFromCoord(short x, short y) @@ -1032,7 +1036,7 @@ int MovingPlatform::GetTileTypeFromCoord(short x, short y) if (fRelativeX < 0.0f || fRelativeX >= iWidth) return tile_flag_nonsolid; - return tileToFlags(iTileType[(short)fRelativeX / TILESIZE][(short)fRelativeY / TILESIZE]); + return tileToFlags(tileTypeAt((short)fRelativeX / TILESIZE, (short)fRelativeY / TILESIZE)); } void MovingPlatform::collide(IO_MovingObject * object) @@ -1095,10 +1099,10 @@ void MovingPlatform::collide(IO_MovingObject * object) int t2 = tile_flag_nonsolid; if (fRelativeY1 >= 0.0f && fRelativeY1 < iHeight) - t1 = tileToFlags(iTileType[tx][(short)fRelativeY1 / TILESIZE]); + t1 = tileToFlags(tileTypeAt(tx, (short)fRelativeY1 / TILESIZE)); if (fRelativeY2 >= 0.0f && fRelativeY2 < iHeight) - t2 = tileToFlags(iTileType[tx][(short)fRelativeY2 / TILESIZE]); + t2 = tileToFlags(tileTypeAt(tx, (short)fRelativeY2 / TILESIZE)); if ((t1 & tile_flag_solid) || (t2 & tile_flag_solid)) { if (object->iHorizontalPlatformCollision == 3) { @@ -1161,10 +1165,10 @@ void MovingPlatform::collide(IO_MovingObject * object) int t2 = tile_flag_nonsolid; if (fRelativeY1 >= 0 && fRelativeY1 < iHeight) - t1 = tileToFlags(iTileType[tx][(short)fRelativeY1 / TILESIZE]); + t1 = tileToFlags(tileTypeAt(tx, (short)fRelativeY1 / TILESIZE)); if (fRelativeY2 >= 0 && fRelativeY2 < iHeight) - t2 = tileToFlags(iTileType[tx][(short)fRelativeY2 / TILESIZE]); + t2 = tileToFlags(tileTypeAt(tx, (short)fRelativeY2 / TILESIZE)); if ((t1 & tile_flag_solid) || (t2 & tile_flag_solid)) { if (object->iHorizontalPlatformCollision == 1) { @@ -1242,10 +1246,10 @@ void MovingPlatform::collide(IO_MovingObject * object) int t2 = tile_flag_nonsolid; if (fRelativeX1 >= 0.0f && fRelativeX1 < iWidth) - t1 = tileToFlags(iTileType[(short)fRelativeX1 / TILESIZE][ty]); + t1 = tileToFlags(tileTypeAt((short)fRelativeX1 / TILESIZE, ty)); if (fRelativeX2 >= 0.0f && fRelativeX2 < iWidth) - t2 = tileToFlags(iTileType[(short)fRelativeX2 / TILESIZE][ty]); + t2 = tileToFlags(tileTypeAt((short)fRelativeX2 / TILESIZE, ty)); if ((t1 & tile_flag_solid) || (t2 & tile_flag_solid)) { if (object->iVerticalPlatformCollision == 2) { @@ -1277,10 +1281,10 @@ void MovingPlatform::collide(IO_MovingObject * object) int t2 = tile_flag_nonsolid; if (fRelativeX1 >= 0.0f && fRelativeX1 < iWidth) - t1 = tileToFlags(iTileType[(short)fRelativeX1 / TILESIZE][ty]); + t1 = tileToFlags(tileTypeAt((short)fRelativeX1 / TILESIZE, ty)); if (fRelativeX2 >= 0.0f && fRelativeX2 < iWidth) - t2 = tileToFlags(iTileType[(short)fRelativeX2 / TILESIZE][ty]); + t2 = tileToFlags(tileTypeAt((short)fRelativeX2 / TILESIZE, ty)); if (((t1 & tile_flag_solid_on_top) || (t2 & tile_flag_solid_on_top)) && object->fOldY + object->collisionHeight <= (ty << 5) + fOldY - iHalfHeight) { if (object->iVerticalPlatformCollision == 0) { @@ -1399,7 +1403,7 @@ bool MovingPlatform::IsInNoSpawnZone(short iX, short iY, short w, short h) short tx = iRelativeX[sX] / TILESIZE; short ty = iRelativeY[sY] / TILESIZE; - int t = tileToFlags(iTileType[tx][ty]); + int t = tileToFlags(tileTypeAt(tx, ty)); if (t & tile_flag_solid) { return true; diff --git a/src/common/movingplatform.h b/src/common/movingplatform.h index a3b24895..dad91cc8 100644 --- a/src/common/movingplatform.h +++ b/src/common/movingplatform.h @@ -11,7 +11,7 @@ class IO_MovingObject; class MovingPlatform { public: - MovingPlatform(TilesetTile ** tiledata, TileType ** tiletypes, short w, short h, short drawlayer, MovingPlatformPath * path, bool preview); + MovingPlatform(std::vector&& tiledata, std::vector&& tiletypes, short w, short h, short drawlayer, MovingPlatformPath * path, bool preview); ~MovingPlatform(); void draw(); @@ -50,6 +50,9 @@ class MovingPlatform iPlayerId = playerId; } + const TilesetTile& tileAt(size_t col, size_t row) const; + TileType tileTypeAt(size_t col, size_t row) const; + protected: void check_map_collision_right(CPlayer * player); @@ -60,8 +63,8 @@ class MovingPlatform bool collision_detection_check_sides(IO_MovingObject * object); - TilesetTile ** iTileData; - TileType ** iTileType; + std::vector iTileData; + std::vector iTileType; short ix, iy; short iWidth, iHeight; short iTileWidth, iTileHeight; diff --git a/src/leveleditor/leveleditor.cpp b/src/leveleditor/leveleditor.cpp index bb798b33..539e9aa5 100644 --- a/src/leveleditor/leveleditor.cpp +++ b/src/leveleditor/leveleditor.cpp @@ -205,24 +205,15 @@ IO_MovingObject* createpowerup(short iType, short ix, short iy, bool side, bool class MapPlatform { public: - MapPlatform() { - tiles = new TilesetTile*[MAPWIDTH]; - - for (short i = 0; i < MAPWIDTH; i++) - tiles[i] = new TilesetTile[MAPHEIGHT]; - - preview = NULL; - } + MapPlatform() + : tiles(MAPWIDTH * MAPHEIGHT) + , types(MAPWIDTH * MAPHEIGHT) + {} ~MapPlatform() { - for (short i = 0; i < MAPWIDTH; i++) - delete [] tiles[i]; - - delete [] tiles; - - if (preview) - SDL_FreeSurface(preview); - } + if (preview) + SDL_FreeSurface(preview); + } void UpdatePreview() { if (!preview) { @@ -238,7 +229,7 @@ class MapPlatform for (short iPlatformX = 0; iPlatformX < MAPWIDTH; iPlatformX++) { for (short iPlatformY = 0; iPlatformY < MAPHEIGHT; iPlatformY++) { - TilesetTile * tile = &tiles[iPlatformX][iPlatformY]; + TilesetTile * tile = &tiles[iPlatformX * MAPHEIGHT + iPlatformY]; SDL_Rect bltrect = {iPlatformX << 3, iPlatformY << 3, THUMBTILESIZE, THUMBTILESIZE}; if (tile->iID >= 0) { @@ -253,8 +244,8 @@ class MapPlatform } } - TilesetTile ** tiles; - TileType types[MAPWIDTH][MAPHEIGHT]; + std::vector tiles; + std::vector types; short iVelocity; short iStartX; short iStartY; @@ -270,7 +261,7 @@ class MapPlatform short iDrawLayer; SDL_Rect rIcon[2]; - SDL_Surface * preview; + SDL_Surface * preview = nullptr; }; TileType * animatedtiletypes; @@ -365,11 +356,11 @@ std::string g_szMessageTitle = ""; std::string g_szMessageLine[3]; void DrawMessage(); -void CopyTilesetTile(TilesetTile * to, TilesetTile * from) +void CopyTilesetTile(TilesetTile& to, const TilesetTile& from) { - to->iID = from->iID; - to->iCol = from->iCol; - to->iRow = from->iRow; + to.iID = from.iID; + to.iCol = from.iCol; + to.iRow = from.iRow; } void SetTilesetTile(TilesetTile * tile, short iTileset, short iCol, short iRow) @@ -583,8 +574,8 @@ int main(int argc, char *argv[]) for (short iCol = 0; iCol < MAPWIDTH; iCol++) { for (short iRow = 0; iRow < MAPHEIGHT; iRow++) { - ClearTilesetTile(&g_Platforms[iPlatform].tiles[iCol][iRow]); - g_Platforms[iPlatform].types[iCol][iRow] = TileType::NonSolid; + ClearTilesetTile(&g_Platforms[iPlatform].tiles[iCol * MAPHEIGHT + iRow]); + g_Platforms[iPlatform].types[iCol * MAPHEIGHT + iRow] = TileType::NonSolid; } } @@ -2469,8 +2460,8 @@ int editor_platforms() for (short iPlatform = iEditPlatform; iPlatform < g_iNumPlatforms - 1; iPlatform++) { for (short iCol = 0; iCol < MAPWIDTH; iCol++) { for (short iRow = 0; iRow < MAPHEIGHT; iRow++) { - CopyTilesetTile(&g_Platforms[iPlatform].tiles[iCol][iRow], &g_Platforms[iPlatform + 1].tiles[iCol][iRow]); - g_Platforms[iPlatform].types[iCol][iRow] = g_Platforms[iPlatform + 1].types[iCol][iRow]; + CopyTilesetTile(g_Platforms[iPlatform].tiles[iCol * MAPHEIGHT + iRow], g_Platforms[iPlatform + 1].tiles[iCol * MAPHEIGHT + iRow]); + g_Platforms[iPlatform].types[iCol * MAPHEIGHT + iRow] = g_Platforms[iPlatform + 1].types[iCol * MAPHEIGHT + iRow]; } } @@ -2609,9 +2600,9 @@ int editor_platforms() for (short i = 0; i < set_tile_cols; i++) { for (short j = 0; j < set_tile_rows; j++) { if (ix + i >= 0 && ix + i < MAPWIDTH && iy + j >= 0 && iy + j < MAPHEIGHT) { - TilesetTile * tile = &g_Platforms[iEditPlatform].tiles[ix + i][iy + j]; + TilesetTile * tile = &g_Platforms[iEditPlatform].tiles[(ix + i) * MAPHEIGHT + iy + j]; SetTilesetTile(tile, set_tile_tileset, set_tile_start_x + i, set_tile_start_y + j); - g_Platforms[iEditPlatform].types[ix + i][iy + j] = g_tilesetmanager->tileset(tile->iID)->tileType(tile->iCol, tile->iRow); + g_Platforms[iEditPlatform].types[(ix + i) * MAPHEIGHT + iy + j] = g_tilesetmanager->tileset(tile->iID)->tileType(tile->iCol, tile->iRow); } } } @@ -2624,9 +2615,9 @@ int editor_platforms() for (short i = 0; i < set_tile_cols; i++) { for (short j = 0; j < set_tile_rows; j++) { if (ix + i >= 0 && ix + i < MAPWIDTH && iy + j >= 0 && iy + j < MAPHEIGHT) { - TilesetTile * tile = &g_Platforms[iEditPlatform].tiles[ix + i][iy + j]; + TilesetTile * tile = &g_Platforms[iEditPlatform].tiles[(ix + i) * MAPHEIGHT + iy + j]; SetTilesetTile(tile, TILESETANIMATED, set_tile_start_y + j, set_tile_start_x + i); - g_Platforms[iEditPlatform].types[ix + i][iy + j] = animatedtiletypes[tile->iRow + (tile->iCol << 5)]; + g_Platforms[iEditPlatform].types[(ix + i) * MAPHEIGHT + iy + j] = animatedtiletypes[tile->iRow + (tile->iCol << 5)]; } } } @@ -2636,7 +2627,7 @@ int editor_platforms() short ix = event.button.x / TILESIZE; short iy = event.button.y / TILESIZE; - g_Platforms[iEditPlatform].types[ix][iy] = set_tiletype; + g_Platforms[iEditPlatform].types[ix * MAPHEIGHT + iy] = set_tiletype; } } else if (PLATFORM_EDIT_STATE_PATH == iPlatformEditState) { #if defined(USE_SDL2) || defined(__EMSCRIPTEN__) @@ -2657,10 +2648,10 @@ int editor_platforms() short iy = event.button.y / TILESIZE; if (PLATFORM_EDIT_STATE_EDIT == iPlatformEditState || PLATFORM_EDIT_STATE_ANIMATED == iPlatformEditState) { - ClearTilesetTile(&g_Platforms[iEditPlatform].tiles[ix][iy]); - g_Platforms[iEditPlatform].types[ix][iy] = TileType::NonSolid; + ClearTilesetTile(&g_Platforms[iEditPlatform].tiles[ix * MAPHEIGHT + iy]); + g_Platforms[iEditPlatform].types[ix * MAPHEIGHT + iy] = TileType::NonSolid; } else if (PLATFORM_EDIT_STATE_TILETYPE == iPlatformEditState) { - g_Platforms[iEditPlatform].types[ix][iy] = TileType::NonSolid; + g_Platforms[iEditPlatform].types[ix * MAPHEIGHT + iy] = TileType::NonSolid; } else if (PLATFORM_EDIT_STATE_PATH == iPlatformEditState) { #if defined(USE_SDL2) || defined(__EMSCRIPTEN__) const Uint8 * keystate = SDL_GetKeyboardState(NULL); @@ -2685,27 +2676,27 @@ int editor_platforms() for (short i = 0; i < set_tile_cols; i++) { for (short j = 0; j < set_tile_rows; j++) { if (ix + i >= 0 && ix + i < MAPWIDTH && iy + j >= 0 && iy + j < MAPHEIGHT) { - TilesetTile * tile = &g_Platforms[iEditPlatform].tiles[ix + i][iy + j]; + TilesetTile * tile = &g_Platforms[iEditPlatform].tiles[(ix + i) * MAPHEIGHT + iy + j]; if (PLATFORM_EDIT_STATE_EDIT == iPlatformEditState) { SetTilesetTile(tile, set_tile_tileset, set_tile_start_x + i, set_tile_start_y + j); - g_Platforms[iEditPlatform].types[ix + i][iy + j] = g_tilesetmanager->tileset(tile->iID)->tileType(tile->iCol, tile->iRow); + g_Platforms[iEditPlatform].types[(ix + i) * MAPHEIGHT + iy + j] = g_tilesetmanager->tileset(tile->iID)->tileType(tile->iCol, tile->iRow); } else { SetTilesetTile(tile, TILESETANIMATED, set_tile_start_y + j, set_tile_start_x + i); - g_Platforms[iEditPlatform].types[ix + i][iy + j] = animatedtiletypes[tile->iRow + (tile->iCol << 5)]; + g_Platforms[iEditPlatform].types[(ix + i) * MAPHEIGHT + iy + j] = animatedtiletypes[tile->iRow + (tile->iCol << 5)]; } } } } } else if (event.motion.state == SDL_BUTTON(SDL_BUTTON_RIGHT)) { - ClearTilesetTile(&g_Platforms[iEditPlatform].tiles[ix][iy]); - g_Platforms[iEditPlatform].types[ix][iy] = TileType::NonSolid; + ClearTilesetTile(&g_Platforms[iEditPlatform].tiles[ix * MAPHEIGHT + iy]); + g_Platforms[iEditPlatform].types[ix * MAPHEIGHT + iy] = TileType::NonSolid; } } else if (PLATFORM_EDIT_STATE_TILETYPE == iPlatformEditState) { if (event.motion.state == SDL_BUTTON(SDL_BUTTON_LEFT) && !ignoreclick) - g_Platforms[iEditPlatform].types[ix][iy] = set_tiletype; + g_Platforms[iEditPlatform].types[ix * MAPHEIGHT + iy] = set_tiletype; else if (event.motion.state == SDL_BUTTON(SDL_BUTTON_RIGHT)) - g_Platforms[iEditPlatform].types[ix][iy] = TileType::NonSolid; + g_Platforms[iEditPlatform].types[ix * MAPHEIGHT + iy] = TileType::NonSolid; } else if (PLATFORM_EDIT_STATE_PATH == iPlatformEditState) { if (event.motion.state == SDL_BUTTON(SDL_BUTTON_LEFT)) { #if defined(USE_SDL2) || defined(__EMSCRIPTEN__) @@ -2900,8 +2891,8 @@ void CopyPlatform(MapPlatform * toPlatform, MapPlatform * fromPlatform) for (short iRow = 0; iRow < MAPHEIGHT; iRow++) { for (short iCol = 0; iCol < MAPWIDTH; iCol++) { - CopyTilesetTile(&toPlatform->tiles[iCol][iRow], &fromPlatform->tiles[iCol][iRow]); - toPlatform->types[iCol][iRow] = fromPlatform->types[iCol][iRow]; + CopyTilesetTile(toPlatform->tiles[iCol * MAPHEIGHT + iRow], fromPlatform->tiles[iCol * MAPHEIGHT + iRow]); + toPlatform->types[iCol * MAPHEIGHT + iRow] = fromPlatform->types[iCol * MAPHEIGHT + iRow]; } } } @@ -3003,7 +2994,7 @@ void draw_platform(short iPlatform, bool fDrawTileTypes) { for (short iCol = 0; iCol < MAPWIDTH; iCol++) { for (short iRow = 0; iRow < MAPHEIGHT; iRow++) { - TilesetTile * tile = &g_Platforms[iPlatform].tiles[iCol][iRow]; + TilesetTile * tile = &g_Platforms[iPlatform].tiles[iCol * MAPHEIGHT + iRow]; if (tile->iID >= 0) { g_tilesetmanager->Draw(screen, tile->iID, 0, tile->iCol, tile->iRow, iCol, iRow); @@ -3023,7 +3014,7 @@ void draw_platform(short iPlatform, bool fDrawTileTypes) } if (fDrawTileTypes) { - TileType type = g_Platforms[iPlatform].types[iCol][iRow]; + TileType type = g_Platforms[iPlatform].types[iCol * MAPHEIGHT + iRow]; rm->spr_transparenttiles.draw(iCol * TILESIZE, iRow * TILESIZE, static_cast(PrevTileType(type)) * TILESIZE, 0, TILESIZE, TILESIZE); } } @@ -4835,11 +4826,11 @@ void loadcurrentmap() for (short iCol = 0; iCol < MAPWIDTH; iCol++) { for (short iRow = 0; iRow < MAPHEIGHT; iRow++) { if (iCol < g_map->platforms[iPlatform]->iTileWidth && iRow < g_map->platforms[iPlatform]->iTileHeight) { - CopyTilesetTile(&g_Platforms[iPlatform].tiles[iCol][iRow], &g_map->platforms[iPlatform]->iTileData[iCol][iRow]); - g_Platforms[iPlatform].types[iCol][iRow] = g_map->platforms[iPlatform]->iTileType[iCol][iRow]; + CopyTilesetTile(g_Platforms[iPlatform].tiles[iCol * MAPHEIGHT + iRow], g_map->platforms[iPlatform]->tileAt(iCol, iRow)); + g_Platforms[iPlatform].types[iCol * MAPHEIGHT + iRow] = g_map->platforms[iPlatform]->tileTypeAt(iCol, iRow); } else { - ClearTilesetTile(&g_Platforms[iPlatform].tiles[iCol][iRow]); - g_Platforms[iPlatform].types[iCol][iRow] = TileType::NonSolid; + ClearTilesetTile(&g_Platforms[iPlatform].tiles[iCol * MAPHEIGHT + iRow]); + g_Platforms[iPlatform].types[iCol * MAPHEIGHT + iRow] = TileType::NonSolid; } } } @@ -4875,8 +4866,8 @@ void SetPlatformToDefaults(short iPlatform) { for (short iCol = 0; iCol < MAPWIDTH; iCol++) { for (short iRow = 0; iRow < MAPHEIGHT; iRow++) { - ClearTilesetTile(&g_Platforms[iPlatform].tiles[iCol][iRow]); - g_Platforms[iPlatform].types[iCol][iRow] = TileType::NonSolid; + ClearTilesetTile(&g_Platforms[iPlatform].tiles[iCol * MAPHEIGHT + iRow]); + g_Platforms[iPlatform].types[iCol * MAPHEIGHT + iRow] = TileType::NonSolid; } } @@ -4914,21 +4905,18 @@ void insert_platforms_into_map() g_map->platforms.reserve(g_iNumPlatforms); for (short iPlatform = 0; iPlatform < g_iNumPlatforms; iPlatform++) { - short iTop, iLeft, iWidth, iHeight; - CalculatePlatformDims(iPlatform, &iLeft, &iTop, &iWidth, &iHeight); + short iTop, iLeft, iWidth, iHeight; + CalculatePlatformDims(iPlatform, &iLeft, &iTop, &iWidth, &iHeight); - TilesetTile ** tiles = new TilesetTile*[iWidth]; - TileType ** types = new TileType*[iWidth]; + std::vector tiles(iWidth * iHeight); + std::vector types(iWidth * iHeight); for (short iCol = 0; iCol < iWidth; iCol++) { - tiles[iCol] = new TilesetTile[iHeight]; - types[iCol] = new TileType[iHeight]; - for (short iRow = 0; iRow < iHeight; iRow++) { - CopyTilesetTile(&tiles[iCol][iRow], &g_Platforms[iPlatform].tiles[iCol + iLeft][iRow + iTop]); - types[iCol][iRow] = g_Platforms[iPlatform].types[iCol + iLeft][iRow + iTop]; - } - } + CopyTilesetTile(tiles[iCol * iHeight + iRow], g_Platforms[iPlatform].tiles[(iCol + iLeft) * iHeight + iRow + iTop]); + types[iCol * iHeight + iRow] = g_Platforms[iPlatform].types[(iCol + iLeft) * iHeight + iRow + iTop]; + } + } short iDrawLayer = g_Platforms[iPlatform].iDrawLayer; @@ -4952,8 +4940,8 @@ void insert_platforms_into_map() path = new EllipsePath(fVelocity, g_Platforms[iPlatform].fAngle, Vec2f(radiusX, radiusY), Vec2f(fStartX, fStartY), false); } - g_map->AddPermanentPlatform(new MovingPlatform(tiles, types, iWidth, iHeight, iDrawLayer, path, false)); - } + g_map->AddPermanentPlatform(new MovingPlatform(std::move(tiles), std::move(types), iWidth, iHeight, iDrawLayer, path, false)); + } } void save_map(const std::string &file) @@ -4970,7 +4958,7 @@ void CalculatePlatformDims(short iPlatform, short * ix, short * iy, short * iw, //Calculate the height and width of the platform for (short iCol = 0; iCol < MAPWIDTH; iCol++) { for (short iRow = 0; iRow < MAPHEIGHT; iRow++) { - if (g_Platforms[iPlatform].tiles[iCol][iRow].iID != TILESETNONE) { + if (g_Platforms[iPlatform].tiles[iCol * MAPHEIGHT + iRow].iID != TILESETNONE) { if (iTop > iRow) iTop = iRow; @@ -5067,15 +5055,15 @@ void pastemoveselection(int movex, int movey) bool copyselectedtiles() { - //Copy the selected tiles and remove tiles from map - bool ret = false; + //Copy the selected tiles and remove tiles from map + bool ret = false; for (int k = 0; k < MAPHEIGHT; k++) { for (int j = 0; j < MAPWIDTH; j++) { if (selectedtiles[j][k]) { - ret = true; + ret = true; for (short iLayer = 0; iLayer < MAPLAYERS; iLayer++) { - CopyTilesetTile(&copiedtiles[j][k].tile[iLayer], &g_map->mapdata[j][k][iLayer]); - } + CopyTilesetTile(copiedtiles[j][k].tile[iLayer], g_map->mapdata[j][k][iLayer]); + } copiedtiles[j][k].block.iType = g_map->objectdata[j][k].iType; for (short iSetting = 0; iSetting < NUM_BLOCK_SETTINGS; iSetting++) diff --git a/src/smw/objects/blocks/DonutBlock.cpp b/src/smw/objects/blocks/DonutBlock.cpp index e6636b3f..7e41868a 100644 --- a/src/smw/objects/blocks/DonutBlock.cpp +++ b/src/smw/objects/blocks/DonutBlock.cpp @@ -62,18 +62,15 @@ void B_DonutBlock::triggerBehavior(short iPlayerId) { //eyecandy[2].add(new EC_FallingObject(&rm->spr_donutblock, ix, iy, 0.0f, 0, 0, 0, 0)); - TilesetTile ** tiledata = new TilesetTile*[1]; - tiledata[0] = new TilesetTile[1]; - tiledata[0][0].iID = g_tilesetmanager->classicTilesetIndex(); - tiledata[0][0].iCol = 29; - tiledata[0][0].iRow = 15; + TilesetTile tile; + tile.iID = g_tilesetmanager->classicTilesetIndex(); + tile.iCol = 29; + tile.iRow = 15; - TileType ** typedata = new TileType*[1]; - typedata[0] = new TileType[1]; - typedata[0][0] = TileType::Solid; + TileType type = TileType::Solid; MovingPlatformPath * path = new FallingPath(Vec2f((float)ix + 16.0f, (float)iy + 15.8f)); - MovingPlatform * platform = new MovingPlatform(tiledata, typedata, 1, 1, 2, path, false); + MovingPlatform * platform = new MovingPlatform({ tile }, { type }, 1, 1, 2, path, false); platform->SetPlayerId(iPlayerId); g_map->AddTemporaryPlatform(platform);