Skip to content

Commit 7aed1d0

Browse files
Address review feedback
1 parent a9c0570 commit 7aed1d0

5 files changed

Lines changed: 47 additions & 48 deletions

File tree

CIO/CFile.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,13 @@ bool CFile::read_lbm(FILE* fp, const boost::filesystem::path& filepath)
348348
// skip unknown data (length - 20 x 1 Byte)
349349
// fseek(fp, length-20, SEEK_CUR);
350350

351+
const Uint16 tilesetSlot = static_cast<Uint16>(bmpArray - global::bmpArray.data());
352+
int chunkIdx = 0;
353+
351354
/* READ SECOND CHUNK "CMAP" */
352355

353356
// chunk-identifier (4 Bytes)
354357
// search for the "CMAP" and skip other chunk-types
355-
const Uint16 tilesetSlot = static_cast<Uint16>(bmpArray - global::bmpArray.data());
356-
int chunkIdx = 0;
357-
358358
while(!feof(fp))
359359
{
360360
CHECK_READ(libendian::read(chunk_identifier.data(), 4, fp));
@@ -401,24 +401,28 @@ bool CFile::read_lbm(FILE* fp, const boost::filesystem::path& filepath)
401401
CHECK_READ(libendian::be_read_ui(&chunkLen, fp));
402402
if(chunkLen >= 8)
403403
{
404-
PaletteAnimation anim;
405404
uint16_t padding, rate, flags;
406405
CHECK_READ(libendian::be_read_us(&padding, fp));
407406
CHECK_READ(libendian::be_read_us(&rate, fp));
408407
CHECK_READ(libendian::be_read_us(&flags, fp));
409408
uint8_t firstClr, lastClr;
410409
CHECK_READ(libendian::read(&firstClr, 1, fp));
411410
CHECK_READ(libendian::read(&lastClr, 1, fp));
412-
anim.isActive = (flags & 1) != 0;
413-
anim.moveUp = (flags & 2) != 0;
414-
if(rate)
415-
anim.isActive = anim.moveUp = true;
416-
anim.rate = rate;
417-
anim.firstClr = firstClr;
418-
anim.lastClr = lastClr;
419-
anim.currentOffset = 0;
420-
anim.lastUpdateTime = SDL_GetTicks();
421-
global::paletteAnimations[tilesetSlot][chunkIdx] = anim;
411+
// Only register valid animations: at least 2 colors and non-zero rate
412+
if(rate > 0 && lastClr > firstClr)
413+
{
414+
PaletteAnimation anim;
415+
anim.moveUp = (flags & 2) != 0;
416+
if(rate)
417+
anim.moveUp = true;
418+
anim.rate = rate;
419+
anim.firstClr = firstClr;
420+
anim.lastClr = lastClr;
421+
anim.currentOffset = 0;
422+
anim.lastAppliedOffset = 0;
423+
anim.lastUpdateTime = SDL_GetTicks();
424+
global::paletteAnimations[tilesetSlot][chunkIdx] = anim;
425+
}
422426
if(chunkLen > 8)
423427
{
424428
uint32_t remaining = chunkLen - 8;

CSurface.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// locking. Was originally called in CGame::Init().
2323
static bool sgeLockOff = (sge_Lock_OFF(), true);
2424

25-
static Uint16 tilesetIdxForMapType(MapType mapType, bool want32bit);
25+
static unsigned tilesetIdxForMapType(MapType mapType, bool want32bit);
2626

2727
namespace {
2828
const TerrainDesc* getTerrainDesc(const bobMAP& map, Uint8 rawTextureId)
@@ -1512,7 +1512,7 @@ float CSurface::absf(float a)
15121512
}
15131513

15141514
/// Map a MapType to the 8-bit or 32-bit tileset bmpArray slot
1515-
static Uint16 tilesetIdxForMapType(MapType mapType, bool want32bit)
1515+
static unsigned tilesetIdxForMapType(MapType mapType, bool want32bit)
15161516
{
15171517
switch(mapType)
15181518
{
@@ -1538,8 +1538,8 @@ static void rotatePaletteRange(SDL_Palette* pal, uint8_t firstClr, int colorCoun
15381538

15391539
void CSurface::UpdatePaletteAnimations(MapType mapType)
15401540
{
1541-
const Uint16 tilesetIdx8 = tilesetIdxForMapType(mapType, false);
1542-
auto animIt = global::paletteAnimations.find(tilesetIdx8);
1541+
const auto tilesetIdx8 = tilesetIdxForMapType(mapType, false);
1542+
auto animIt = global::paletteAnimations.find(static_cast<Uint16>(tilesetIdx8));
15431543
if(animIt == global::paletteAnimations.end() || animIt->second.empty())
15441544
return;
15451545

@@ -1552,15 +1552,15 @@ void CSurface::UpdatePaletteAnimations(MapType mapType)
15521552
(global::s2 && global::s2->getMapObj()) ? global::s2->getMapObj()->getSurfacePalette() : nullptr;
15531553

15541554
// 32-bit tileset surface for blitting after palette updates
1555-
const Uint16 tilesetIdx32 = tilesetIdxForMapType(mapType, true);
1556-
auto* surf32 = global::bmpArray[tilesetIdx32].surface.get();
1555+
const auto tilesetIdx32 = tilesetIdxForMapType(mapType, true);
1556+
auto& surf32 = *global::bmpArray[tilesetIdx32].surface;
15571557

1558-
const Uint32 now = SDL_GetTicks();
1558+
const unsigned now = SDL_GetTicks();
15591559
bool anyUpdate = false;
15601560

15611561
for(auto& [palAnimIdx, anim] : animMap)
15621562
{
1563-
if(!anim.isActive || anim.rate == 0)
1563+
if(anim.rate == 0)
15641564
continue;
15651565

15661566
const int colorCount = anim.lastClr - anim.firstClr + 1;
@@ -1570,12 +1570,12 @@ void CSurface::UpdatePaletteAnimations(MapType mapType)
15701570
// Time per step: (8192/30) / rate seconds, converted to ms
15711571
const float intervalMs = (8192.0f / 30.0f) * 1000.0f / anim.rate;
15721572

1573-
const uint32_t elapsed = now - anim.lastUpdateTime;
1574-
if(elapsed < static_cast<uint32_t>(intervalMs))
1573+
const unsigned elapsed = now - anim.lastUpdateTime;
1574+
if(elapsed < static_cast<unsigned>(intervalMs))
15751575
continue;
15761576

15771577
int steps = static_cast<int>(elapsed / intervalMs);
1578-
anim.lastUpdateTime += static_cast<uint32_t>(steps * intervalMs);
1578+
anim.lastUpdateTime += static_cast<unsigned>(steps * intervalMs);
15791579

15801580
int newOffset = anim.moveUp ? (anim.currentOffset + steps) % colorCount :
15811581
(anim.currentOffset - steps + colorCount * steps) % colorCount;
@@ -1595,16 +1595,13 @@ void CSurface::UpdatePaletteAnimations(MapType mapType)
15951595
return;
15961596

15971597
// Blit 8-bit tileset to 32-bit so the rotated palette takes effect in 32bpp mode
1598-
if(surf32 && surf8)
1599-
{
1600-
if(SDL_MUSTLOCK(surf32))
1601-
SDL_LockSurface(surf32);
1602-
if(SDL_MUSTLOCK(surf8))
1603-
SDL_LockSurface(surf8);
1604-
SDL_BlitSurface(surf8, nullptr, surf32, nullptr);
1605-
if(SDL_MUSTLOCK(surf8))
1606-
SDL_UnlockSurface(surf8);
1607-
if(SDL_MUSTLOCK(surf32))
1608-
SDL_UnlockSurface(surf32);
1609-
}
1598+
if(SDL_MUSTLOCK(&surf32))
1599+
SDL_LockSurface(&surf32);
1600+
if(SDL_MUSTLOCK(surf8))
1601+
SDL_LockSurface(surf8);
1602+
SDL_BlitSurface(surf8, nullptr, &surf32, nullptr);
1603+
if(SDL_MUSTLOCK(surf8))
1604+
SDL_UnlockSurface(surf8);
1605+
if(SDL_MUSTLOCK(&surf32))
1606+
SDL_UnlockSurface(&surf32);
16101607
}

globals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ std::vector<bobBMP> global::bmpArray(MAXBOBBMP);
1212
std::vector<bobSHADOW> global::shadowArray(MAXBOBSHADOW);
1313
// array for all palettes
1414
std::vector<bobPAL> global::palArray(MAXBOBPAL);
15-
// Palette animation entries keyed by the bmpArray slot of the 8-bit tileset surface.
15+
// Palette animations keyed by (bmpArray slot of 8-bit tileset) -> (CRNG chunk index).
1616
std::map<Uint16, std::map<int, PaletteAnimation>> global::paletteAnimations;
1717
// the game object
1818
CGame* global::s2;

globals.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ extern std::vector<bobBMP> bmpArray;
2424
extern std::vector<bobSHADOW> shadowArray;
2525
// array for all palettes
2626
extern std::vector<bobPAL> palArray;
27-
// Palette animation entries keyed by the bmpArray slot of the 8-bit tileset surface.
28-
// Inner map keyed by the CRNG chunk's index in the tileset LBM file.
27+
// Palette animations keyed by (bmpArray slot of 8-bit tileset) -> (CRNG chunk index).
2928
extern std::map<Uint16, std::map<int, PaletteAnimation>> paletteAnimations;
3029
// the game object
3130
extern CGame* s2;

include/defines.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,13 @@ struct bobPAL
9494
// Palette animation parsed from a CRNG chunk in an LBM file
9595
struct PaletteAnimation
9696
{
97-
bool isActive = false;
98-
bool moveUp = true;
99-
uint16_t rate = 16384;
100-
uint8_t firstClr = 0;
101-
uint8_t lastClr = 0;
102-
int currentOffset = 0;
103-
int lastAppliedOffset = 0;
104-
uint32_t lastUpdateTime = 0;
97+
bool moveUp;
98+
uint16_t rate;
99+
uint8_t firstClr;
100+
uint8_t lastClr;
101+
int currentOffset;
102+
int lastAppliedOffset;
103+
unsigned lastUpdateTime;
105104
};
106105

107106
// Structure for Bobtype 7 (Shadow-Bitmaps)

0 commit comments

Comments
 (0)