Skip to content

Commit

Permalink
Refactored the moving platform tile arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Sep 21, 2024
1 parent 3d46464 commit 7e7ebe3
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 212 deletions.
78 changes: 38 additions & 40 deletions src/common/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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)
{
short iStartX = startX >> iSize;
short iStartY = startY >> iSize;
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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<int>(platform->iTileType[iCol][iRow]));
mapfile.write_i32(static_cast<int>(platform->tileTypeAt(iCol, iRow)));
}
}

Expand Down Expand Up @@ -1662,30 +1661,29 @@ 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

animatedtile->fBackgroundAnimated = false;
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};
Expand Down
2 changes: 1 addition & 1 deletion src/common/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TilesetTile>& tiles,
short startX, short startY, short endX, short endY,
float angle, float radiusX, float radiusY,
short iSize, short iPlatformWidth, short iPlatformHeight,
Expand Down
6 changes: 4 additions & 2 deletions src/common/map/MapReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "FileIO.h"

#include <vector>

class CMap;
class MovingPlatformPath;
struct TilesetTile;
Expand Down Expand Up @@ -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<TilesetTile>, std::vector<TileType>> read_platform_tiles(CMap&, BinaryFile&, short w, short h);
MovingPlatformPath* read_platform_path_details(BinaryFile&, short type, bool preview);

unsigned char patch_version;
Expand Down Expand Up @@ -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<TilesetTile>, std::vector<TileType>> read_platform_tiles(CMap&, BinaryFile&, short w, short h);

private:
short iMaxTilesetID;
Expand Down
42 changes: 22 additions & 20 deletions src/common/map/MapReader17xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<TilesetTile>, std::vector<TileType>>
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<TilesetTile> tiles;
std::vector<TileType> 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)
Expand Down
50 changes: 27 additions & 23 deletions src/common/map/MapReader18xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<TilesetTile>, std::vector<TileType>>
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<TilesetTile> tiles;
std::vector<TileType> 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<TileType>(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)
Expand Down
Loading

0 comments on commit 7e7ebe3

Please sign in to comment.