- most of lights.cpp, including turning the helper macros into functions.

This commit is contained in:
Christoph Oelckers 2021-12-24 23:34:21 +01:00
parent eba5d222c0
commit 2aba992d63
2 changed files with 76 additions and 85 deletions

View file

@ -103,19 +103,17 @@ void DiffuseLighting(DSWActor* actor)
SWStatIterator it(STAT_LIGHTING_DIFFUSE); SWStatIterator it(STAT_LIGHTING_DIFFUSE);
while (auto itActor = it.Next()) while (auto itActor = it.Next())
{ {
dsp = &itActor->s();
// make sure matchs match // make sure matchs match
if (LIGHT_Match(itActor) != LIGHT_Match(actor)) if (LIGHT_Match(itActor) != LIGHT_Match(actor))
continue; continue;
shade = sp->shade + ((LIGHT_DiffuseNum(itActor) + 1) * LIGHT_DiffuseMult(dsp)); shade = sp->shade + ((LIGHT_DiffuseNum(itActor) + 1) * LIGHT_DiffuseMult(itActor));
if (shade > LIGHT_MaxDark(sp)) if (shade > LIGHT_MaxDark(actor))
shade = LIGHT_MaxDark(sp); shade = LIGHT_MaxDark(actor);
if (!TEST_BOOL6(dsp)) if (!TEST_BOOL6(itActor))
dsp->pal = sp->pal; itActor->spr.pal = sp->pal;
SectorLightShade(itActor, shade); SectorLightShade(itActor, shade);
@ -125,13 +123,10 @@ void DiffuseLighting(DSWActor* actor)
void DoLightingMatch(short match, short state) void DoLightingMatch(short match, short state)
{ {
SPRITEp sp;
SWStatIterator it(STAT_LIGHTING); SWStatIterator it(STAT_LIGHTING);
while (auto itActor = it.Next()) while (auto itActor = it.Next())
{ {
auto u = itActor->u(); auto u = itActor->u();
sp = &itActor->s();
if (LIGHT_Match(itActor) != match) if (LIGHT_Match(itActor) != match)
continue; continue;
@ -141,26 +136,26 @@ void DoLightingMatch(short match, short state)
case LIGHT_CONSTANT: case LIGHT_CONSTANT:
// initialized // initialized
SET_BOOL9(sp); SET_BOOL9(itActor);
// toggle // toggle
if (state == -1) if (state == -1)
state = !TEST_BOOL1(sp); state = !TEST_BOOL1(itActor);
if (state == ON) if (state == ON)
{ {
SET_BOOL1(sp); SET_BOOL1(itActor);
sp->shade = -LIGHT_MaxBright(sp); itActor->spr.shade = -LIGHT_MaxBright(itActor);
sp->pal = u->spal; // on itActor->spr.pal = u->spal; // on
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, itActor->spr.shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
} }
else else
{ {
RESET_BOOL1(sp); RESET_BOOL1(itActor);
sp->shade = LIGHT_MaxDark(sp); itActor->spr.shade = LIGHT_MaxDark(itActor);
sp->pal = 0; // off itActor->spr.pal = 0; // off
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, itActor->spr.shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
} }
break; break;
@ -168,22 +163,22 @@ void DoLightingMatch(short match, short state)
case LIGHT_FLICKER: case LIGHT_FLICKER:
case LIGHT_FADE: case LIGHT_FADE:
// initialized // initialized
SET_BOOL9(sp); SET_BOOL9(itActor);
// toggle // toggle
if (state == -1) if (state == -1)
state = !TEST_BOOL1(sp); state = !TEST_BOOL1(itActor);
if (state == ON) if (state == ON)
{ {
// allow fade or flicker // allow fade or flicker
SET_BOOL1(sp); SET_BOOL1(itActor);
} }
else else
{ {
RESET_BOOL1(sp); RESET_BOOL1(itActor);
sp->shade = LIGHT_MaxDark(sp); itActor->spr.shade = LIGHT_MaxDark(itActor);
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, itActor->spr.shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
} }
break; break;
@ -191,55 +186,55 @@ void DoLightingMatch(short match, short state)
case LIGHT_FADE_TO_ON_OFF: case LIGHT_FADE_TO_ON_OFF:
// initialized // initialized
SET_BOOL9(sp); SET_BOOL9(itActor);
// toggle // toggle
//if (state == -1) //if (state == -1)
// state = !TEST_BOOL1(sp); // state = !TEST_BOOL1(itActor);
if (state == ON) if (state == ON)
{ {
if (LIGHT_Dir(sp) == 1) if (LIGHT_Dir(itActor) == 1)
{ {
LIGHT_DirChange(sp); LIGHT_DirChange(itActor);
} }
} }
else if (state == OFF) else if (state == OFF)
{ {
if (LIGHT_Dir(sp) == 0) if (LIGHT_Dir(itActor) == 0)
{ {
LIGHT_DirChange(sp); LIGHT_DirChange(itActor);
} }
} }
// allow fade or flicker // allow fade or flicker
SET_BOOL1(sp); SET_BOOL1(itActor);
break; break;
case LIGHT_FLICKER_ON: case LIGHT_FLICKER_ON:
// initialized // initialized
SET_BOOL9(sp); SET_BOOL9(itActor);
// toggle // toggle
if (state == -1) if (state == -1)
state = !TEST_BOOL1(sp); state = !TEST_BOOL1(itActor);
if (state == ON) if (state == ON)
{ {
// allow fade or flicker // allow fade or flicker
SET_BOOL1(sp); SET_BOOL1(itActor);
} }
else else
{ {
// turn it off till next switch // turn it off till next switch
auto spal = sp->pal; auto spal = itActor->spr.pal;
RESET_BOOL1(sp); RESET_BOOL1(itActor);
sp->pal = 0; itActor->spr.pal = 0;
sp->shade = LIGHT_MaxDark(sp); itActor->spr.shade = LIGHT_MaxDark(itActor);
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, itActor->spr.shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
sp->pal = spal; itActor->spr.pal = spal;
} }
break; break;
} }
@ -248,18 +243,14 @@ void DoLightingMatch(short match, short state)
void InitLighting(void) void InitLighting(void)
{ {
SPRITEp sp;
// processed on level startup // processed on level startup
// puts lights in correct state // puts lights in correct state
SWStatIterator it(STAT_LIGHTING); SWStatIterator it(STAT_LIGHTING);
while (auto actor = it.Next()) while (auto actor = it.Next())
{ {
sp = &actor->s(); if (!TEST_BOOL9(actor))
if (!TEST_BOOL9(sp))
{ {
DoLightingMatch(LIGHT_Match(actor), !!TEST_BOOL1(sp)); DoLightingMatch(LIGHT_Match(actor), !!TEST_BOOL1(actor));
} }
} }
} }
@ -286,13 +277,13 @@ void DoLighting(void)
case LIGHT_FLICKER: case LIGHT_FLICKER:
LIGHT_Tics(itActor) += synctics; LIGHT_Tics(itActor) += synctics;
while (LIGHT_Tics(itActor) >= LIGHT_MaxTics(sp)) while (LIGHT_Tics(itActor) >= LIGHT_MaxTics(itActor))
{ {
LIGHT_Tics(itActor) -= LIGHT_MaxTics(sp); LIGHT_Tics(itActor) -= LIGHT_MaxTics(itActor);
if ((RANDOM_P2(128 << 8) >> 8) > 64) if ((RANDOM_P2(128 << 8) >> 8) > 64)
{ {
sp->shade = -LIGHT_MaxBright(sp) + RandomRange(LIGHT_MaxBright(sp) + LIGHT_MaxDark(sp)); sp->shade = -LIGHT_MaxBright(itActor) + RandomRange(LIGHT_MaxBright(itActor) + LIGHT_MaxDark(itActor));
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, sp->shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
} }
@ -301,7 +292,7 @@ void DoLighting(void)
// turn off lighting - even colored lighting // turn off lighting - even colored lighting
auto spal = sp->pal; auto spal = sp->pal;
sp->pal = 0; sp->pal = 0;
sp->shade = LIGHT_MaxDark(sp); sp->shade = LIGHT_MaxDark(itActor);
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, sp->shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
sp->pal = spal; sp->pal = spal;
@ -314,21 +305,21 @@ void DoLighting(void)
LIGHT_Tics(itActor) += synctics; LIGHT_Tics(itActor) += synctics;
while (LIGHT_Tics(itActor) >= LIGHT_MaxTics(sp)) while (LIGHT_Tics(itActor) >= LIGHT_MaxTics(itActor))
{ {
LIGHT_Tics(itActor) -= LIGHT_MaxTics(sp); LIGHT_Tics(itActor) -= LIGHT_MaxTics(itActor);
if (LIGHT_Dir(sp) == 1) if (LIGHT_Dir(itActor) == 1)
{ {
sp->shade += LIGHT_ShadeInc(sp); sp->shade += LIGHT_ShadeInc(itActor);
if (sp->shade >= LIGHT_MaxDark(sp)) if (sp->shade >= LIGHT_MaxDark(itActor))
LIGHT_DirChange(sp); LIGHT_DirChange(itActor);
} }
else else
{ {
sp->shade -= LIGHT_ShadeInc(sp); sp->shade -= LIGHT_ShadeInc(itActor);
if (sp->shade <= -LIGHT_MaxBright(sp)) if (sp->shade <= -LIGHT_MaxBright(itActor))
LIGHT_DirChange(sp); LIGHT_DirChange(itActor);
} }
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, sp->shade);
@ -341,30 +332,30 @@ void DoLighting(void)
LIGHT_Tics(itActor) += synctics; LIGHT_Tics(itActor) += synctics;
while (LIGHT_Tics(itActor) >= LIGHT_MaxTics(sp)) while (LIGHT_Tics(itActor) >= LIGHT_MaxTics(itActor))
{ {
LIGHT_Tics(itActor) -= LIGHT_MaxTics(sp); LIGHT_Tics(itActor) -= LIGHT_MaxTics(itActor);
if (LIGHT_Dir(sp) == 1) if (LIGHT_Dir(itActor) == 1)
{ {
sp->shade += LIGHT_ShadeInc(sp); sp->shade += LIGHT_ShadeInc(itActor);
if (sp->shade >= LIGHT_MaxDark(sp)) if (sp->shade >= LIGHT_MaxDark(itActor))
{ {
sp->pal = 0; // off sp->pal = 0; // off
LIGHT_DirChange(sp); LIGHT_DirChange(itActor);
// stop it until switch is hit // stop it until switch is hit
RESET_BOOL1(sp); RESET_BOOL1(itActor);
} }
} }
else else
{ {
sp->shade -= LIGHT_ShadeInc(sp); sp->shade -= LIGHT_ShadeInc(itActor);
sp->pal = u->spal; // on sp->pal = u->spal; // on
if (sp->shade <= -LIGHT_MaxBright(sp)) if (sp->shade <= -LIGHT_MaxBright(itActor))
{ {
LIGHT_DirChange(sp); LIGHT_DirChange(itActor);
// stop it until switch is hit // stop it until switch is hit
RESET_BOOL1(sp); RESET_BOOL1(itActor);
} }
} }
@ -378,13 +369,13 @@ void DoLighting(void)
LIGHT_Tics(itActor) += synctics; LIGHT_Tics(itActor) += synctics;
while (LIGHT_Tics(itActor) >= LIGHT_MaxTics(sp)) while (LIGHT_Tics(itActor) >= LIGHT_MaxTics(itActor))
{ {
LIGHT_Tics(itActor) -= LIGHT_MaxTics(sp); LIGHT_Tics(itActor) -= LIGHT_MaxTics(itActor);
if ((RANDOM_P2(128 << 8) >> 8) > 64) if ((RANDOM_P2(128 << 8) >> 8) > 64)
{ {
sp->shade = -LIGHT_MaxBright(sp) + RandomRange(LIGHT_MaxBright(sp) + LIGHT_MaxDark(sp)); sp->shade = -LIGHT_MaxBright(itActor) + RandomRange(LIGHT_MaxBright(itActor) + LIGHT_MaxDark(itActor));
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, sp->shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
} }
@ -393,7 +384,7 @@ void DoLighting(void)
// turn off lighting - even colored lighting // turn off lighting - even colored lighting
auto spal = sp->pal; auto spal = sp->pal;
sp->pal = 0; sp->pal = 0;
sp->shade = LIGHT_MaxDark(sp); sp->shade = LIGHT_MaxDark(itActor);
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, sp->shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
sp->pal = spal; sp->pal = spal;
@ -402,11 +393,11 @@ void DoLighting(void)
if ((RANDOM_P2(128 << 8) >> 8) < 8) if ((RANDOM_P2(128 << 8) >> 8) < 8)
{ {
// set to full brightness // set to full brightness
sp->shade = -LIGHT_MaxBright(sp); sp->shade = -LIGHT_MaxBright(itActor);
SectorLightShade(itActor, sp->shade); SectorLightShade(itActor, sp->shade);
DiffuseLighting(itActor); DiffuseLighting(itActor);
// turn it off until a swith happens // turn it off until a swith happens
RESET_BOOL1(sp); RESET_BOOL1(itActor);
} }
} }
break; break;

View file

@ -37,20 +37,20 @@ void DoLighting(void);
inline int LIGHT_Match(DSWActor* sp) { return SP_TAG2(sp); } inline int LIGHT_Match(DSWActor* sp) { return SP_TAG2(sp); }
inline int LIGHT_Type(DSWActor* sp) { return SP_TAG3(sp); } inline int LIGHT_Type(DSWActor* sp) { return SP_TAG3(sp); }
#define LIGHT_MaxTics(sp) (SP_TAG4((sp))) inline int16_t LIGHT_MaxTics(DSWActor* sp) { return SP_TAG4((sp)); }
inline int8_t LIGHT_MaxBright(spritetype* sp) { return int8_t(SP_TAG5(sp)); } inline int8_t LIGHT_MaxBright(DSWActor* a) { return int8_t(SP_TAG5(a)); }
inline int8_t LIGHT_MaxDark(spritetype* sp) { return int8_t(SP_TAG6(sp)); } inline int8_t LIGHT_MaxDark(DSWActor* sp) { return int8_t(SP_TAG6(sp)); }
#define LIGHT_ShadeInc(sp) (SP_TAG7((sp))) inline uint8_t& LIGHT_ShadeInc(DSWActor* sp) { return SP_TAG7(sp); }
#define LIGHT_Dir(sp) (!!(TEST((sp)->extra, SPRX_BOOL10))) inline bool LIGHT_Dir(DSWActor* sp) { return (!!(TEST(sp->spr.extra, SPRX_BOOL10))); }
#define LIGHT_DirChange(sp) (FLIP((sp)->extra, SPRX_BOOL10)) inline void LIGHT_DirChange(DSWActor* sp) { (FLIP(sp->spr.extra, SPRX_BOOL10)); }
int8_t& LIGHT_FloorShade(DSWActor* a) { return a->spr.xoffset; } int8_t& LIGHT_FloorShade(DSWActor* a) { return a->spr.xoffset; }
int8_t& LIGHT_CeilingShade(DSWActor* a) { return a->spr.yoffset; } int8_t& LIGHT_CeilingShade(DSWActor* a) { return a->spr.yoffset; }
int& LIGHT_Tics(DSWActor* a) { return a->spr.pos.Z; } int& LIGHT_Tics(DSWActor* a) { return a->spr.pos.Z; }
inline int LIGHT_DiffuseNum(DSWActor* sp) { return SP_TAG3(sp); } inline int LIGHT_DiffuseNum(DSWActor* sp) { return SP_TAG3(sp); }
#define LIGHT_DiffuseMult(sp) (SP_TAG4((sp))) inline int16_t LIGHT_DiffuseMult(DSWActor* sp) { return SP_TAG4((sp)); }
enum LightTypes {LIGHT_CONSTANT, LIGHT_FLICKER, LIGHT_FADE, LIGHT_FLICKER_ON, LIGHT_FADE_TO_ON_OFF}; enum LightTypes {LIGHT_CONSTANT, LIGHT_FLICKER, LIGHT_FADE, LIGHT_FLICKER_ON, LIGHT_FADE_TO_ON_OFF};
END_SW_NS END_SW_NS