- moved switch definitions into texture manager and split all associated code off p_switches.cpp into its own file.

SVN r3027 (trunk)
This commit is contained in:
Christoph Oelckers 2010-12-11 23:02:46 +00:00
parent 6309d90be0
commit 24ab37613a
8 changed files with 493 additions and 379 deletions

View file

@ -4055,7 +4055,6 @@ void P_Init ()
atterm (P_Shutdown);
P_InitEffects (); // [RH]
P_InitSwitchList ();
P_InitTerrainTypes ();
P_InitKeyMessages ();
R_InitSprites ();

View file

@ -396,9 +396,6 @@ void EV_StartLightFading (int tag, int value, int tics);
bool P_ChangeSwitchTexture (side_t *side, int useAgain, BYTE special, bool *quest=NULL);
bool P_CheckSwitchRange(AActor *user, line_t *line, int sideno);
void P_InitSwitchList ();
void P_ProcessSwitchDef (FScanner &sc);
//
// P_PLATS
//

View file

@ -46,12 +46,9 @@
#include "w_wad.h"
#include "tarray.h"
#include "cmdlib.h"
#include "sc_man.h"
#include "gi.h"
#define MAX_FRAMES 128
static FRandom pr_switchanim ("AnimSwitch");
class DActiveButton : public DThinker
@ -76,338 +73,14 @@ protected:
bool AdvanceFrame ();
};
struct FSwitchDef
{
FTextureID PreTexture; // texture to switch from
WORD PairIndex; // switch def to use to return to PreTexture
WORD NumFrames; // # of animation frames
FSoundID Sound; // sound to play at start of animation
bool QuestPanel; // Special texture for Strife mission
struct frame // Array of times followed by array of textures
{ // actual length of each array is <NumFrames>
DWORD Time;
FTextureID Texture;
} u[1];
};
static int STACK_ARGS SortSwitchDefs (const void *a, const void *b);
static FSwitchDef *ParseSwitchDef (FScanner &sc, bool ignoreBad);
static WORD AddSwitchDef (FSwitchDef *def);
//
// CHANGE THE TEXTURE OF A WALL SWITCH TO ITS OPPOSITE
//
class DeletingSwitchArray : public TArray<FSwitchDef *>
{
public:
~DeletingSwitchArray()
{
for(unsigned i=0;i<Size();i++)
{
if ((*this)[i] != NULL)
{
M_Free((*this)[i]);
(*this)[i] = NULL;
}
}
}
};
static DeletingSwitchArray SwitchList;
//
// P_InitSwitchList
// Only called at game initialization.
//
// [RH] Rewritten to use a BOOM-style SWITCHES lump and remove the
// MAXSWITCHES limit.
void P_InitSwitchList ()
{
const BITFIELD texflags = FTextureManager::TEXMAN_Overridable | FTextureManager::TEXMAN_TryAny;
int lump = Wads.CheckNumForName ("SWITCHES");
FSwitchDef **origMap;
int i, j;
if (lump != -1)
{
FMemLump lumpdata = Wads.ReadLump (lump);
const char *alphSwitchList = (const char *)lumpdata.GetMem();
const char *list_p;
FSwitchDef *def1, *def2;
for (list_p = alphSwitchList; list_p[18] || list_p[19]; list_p += 20)
{
// [RH] Check for switches that aren't really switches
if (stricmp (list_p, list_p+9) == 0)
{
Printf ("Switch %s in SWITCHES has the same 'on' state\n", list_p);
continue;
}
// [RH] Skip this switch if its textures can't be found.
if (TexMan.CheckForTexture (list_p /* .name1 */, FTexture::TEX_Wall, texflags).Exists() &&
TexMan.CheckForTexture (list_p + 9 /* .name2 */, FTexture::TEX_Wall, texflags).Exists())
{
def1 = (FSwitchDef *)M_Malloc (sizeof(FSwitchDef));
def2 = (FSwitchDef *)M_Malloc (sizeof(FSwitchDef));
def1->PreTexture = def2->u[0].Texture = TexMan.CheckForTexture (list_p /* .name1 */, FTexture::TEX_Wall, texflags);
def2->PreTexture = def1->u[0].Texture = TexMan.CheckForTexture (list_p + 9, FTexture::TEX_Wall, texflags);
def1->Sound = def2->Sound = 0;
def1->NumFrames = def2->NumFrames = 1;
def1->u[0].Time = def2->u[0].Time = 0;
def2->PairIndex = AddSwitchDef (def1);
def1->PairIndex = AddSwitchDef (def2);
}
}
}
SwitchList.ShrinkToFit ();
// Sort SwitchList for quick searching
origMap = new FSwitchDef *[SwitchList.Size ()];
for (i = 0; i < (int)SwitchList.Size (); i++)
{
origMap[i] = SwitchList[i];
}
qsort (&SwitchList[0], i, sizeof(FSwitchDef *), SortSwitchDefs);
// Correct the PairIndex of each switch def, since the sorting broke them
for (i = (int)(SwitchList.Size () - 1); i >= 0; i--)
{
FSwitchDef *def = SwitchList[i];
if (def->PairIndex != 65535)
{
for (j = (int)(SwitchList.Size () - 1); j >= 0; j--)
{
if (SwitchList[j] == origMap[def->PairIndex])
{
def->PairIndex = (WORD)j;
break;
}
}
}
}
delete[] origMap;
}
static int STACK_ARGS SortSwitchDefs (const void *a, const void *b)
{
return (*(FSwitchDef **)a)->PreTexture - (*(FSwitchDef **)b)->PreTexture;
}
// Parse a switch block in ANIMDEFS and add the definitions to SwitchList
void P_ProcessSwitchDef (FScanner &sc)
{
const BITFIELD texflags = FTextureManager::TEXMAN_Overridable | FTextureManager::TEXMAN_TryAny;
FString picname;
FSwitchDef *def1, *def2;
FTextureID picnum;
int gametype;
bool quest = false;
def1 = def2 = NULL;
sc.MustGetString ();
if (sc.Compare ("doom"))
{
gametype = GAME_DoomChex;
sc.CheckNumber(); // skip the gamemission filter
}
else if (sc.Compare ("heretic"))
{
gametype = GAME_Heretic;
}
else if (sc.Compare ("hexen"))
{
gametype = GAME_Hexen;
}
else if (sc.Compare ("strife"))
{
gametype = GAME_Strife;
}
else if (sc.Compare ("any"))
{
gametype = GAME_Any;
}
else
{
// There is no game specified; just treat as any
//max = 240;
gametype = GAME_Any;
sc.UnGet ();
}
sc.MustGetString ();
picnum = TexMan.CheckForTexture (sc.String, FTexture::TEX_Wall, texflags);
picname = sc.String;
while (sc.GetString ())
{
if (sc.Compare ("quest"))
{
quest = true;
}
else if (sc.Compare ("on"))
{
if (def1 != NULL)
{
sc.ScriptError ("Switch already has an on state");
}
def1 = ParseSwitchDef (sc, !picnum.Exists());
}
else if (sc.Compare ("off"))
{
if (def2 != NULL)
{
sc.ScriptError ("Switch already has an off state");
}
def2 = ParseSwitchDef (sc, !picnum.Exists());
}
else
{
sc.UnGet ();
break;
}
}
if (def1 == NULL || !picnum.Exists() ||
(gametype != GAME_Any && !(gametype & gameinfo.gametype)))
{
if (def2 != NULL)
{
M_Free (def2);
}
if (def1 != NULL)
{
M_Free (def1);
}
return;
}
// If the switch did not have an off state, create one that just returns
// it to the original texture without doing anything interesting
if (def2 == NULL)
{
def2 = (FSwitchDef *)M_Malloc (sizeof(FSwitchDef));
def2->Sound = def1->Sound;
def2->NumFrames = 1;
def2->u[0].Time = 0;
def2->u[0].Texture = picnum;
}
def1->PreTexture = picnum;
def2->PreTexture = def1->u[def1->NumFrames-1].Texture;
if (def1->PreTexture == def2->PreTexture)
{
sc.ScriptError ("The on state for switch %s must end with a texture other than %s", picname.GetChars(), picname.GetChars());
}
def2->PairIndex = AddSwitchDef (def1);
def1->PairIndex = AddSwitchDef (def2);
def1->QuestPanel = def2->QuestPanel = quest;
}
FSwitchDef *ParseSwitchDef (FScanner &sc, bool ignoreBad)
{
const BITFIELD texflags = FTextureManager::TEXMAN_Overridable | FTextureManager::TEXMAN_TryAny;
FSwitchDef *def;
TArray<FSwitchDef::frame> frames;
FSwitchDef::frame thisframe;
FTextureID picnum;
bool bad;
FSoundID sound;
bad = false;
while (sc.GetString ())
{
if (sc.Compare ("sound"))
{
if (sound != 0)
{
sc.ScriptError ("Switch state already has a sound");
}
sc.MustGetString ();
sound = sc.String;
}
else if (sc.Compare ("pic"))
{
sc.MustGetString ();
picnum = TexMan.CheckForTexture (sc.String, FTexture::TEX_Wall, texflags);
if (!picnum.Exists() && !ignoreBad)
{
//Printf ("Unknown switch texture %s\n", sc.String);
bad = true;
}
thisframe.Texture = picnum;
sc.MustGetString ();
if (sc.Compare ("tics"))
{
sc.MustGetNumber ();
thisframe.Time = sc.Number & 65535;
}
else if (sc.Compare ("rand"))
{
int min, max;
sc.MustGetNumber ();
min = sc.Number & 65535;
sc.MustGetNumber ();
max = sc.Number & 65535;
if (min > max)
{
swapvalues (min, max);
}
thisframe.Time = ((max - min + 1) << 16) | min;
}
else
{
thisframe.Time = 0; // Shush, GCC.
sc.ScriptError ("Must specify a duration for switch frame");
}
frames.Push(thisframe);
}
else
{
sc.UnGet ();
break;
}
}
if (frames.Size() == 0)
{
sc.ScriptError ("Switch state needs at least one frame");
}
if (bad)
{
return NULL;
}
def = (FSwitchDef *)M_Malloc (myoffsetof (FSwitchDef, u[0]) + frames.Size()*sizeof(frames[0]));
def->Sound = sound;
def->NumFrames = frames.Size();
memcpy (&def->u[0], &frames[0], frames.Size() * sizeof(frames[0]));
def->PairIndex = 65535;
return def;
}
static WORD AddSwitchDef (FSwitchDef *def)
{
unsigned int i;
for (i = SwitchList.Size (); i-- > 0; )
{
if (SwitchList[i]->PreTexture == def->PreTexture)
{
free (SwitchList[i]);
SwitchList[i] = def;
return (WORD)i;
}
}
return (WORD)SwitchList.Push (def);
}
//==========================================================================
//
// Start a button counting down till it turns off.
// [RH] Rewritten to remove MAXBUTTONS limit.
//
//==========================================================================
static bool P_StartButton (side_t *side, int Where, int switchnum,
fixed_t x, fixed_t y, bool useagain)
{
@ -428,40 +101,14 @@ static bool P_StartButton (side_t *side, int Where, int switchnum,
return true;
}
static int TryFindSwitch (side_t *side, int Where)
{
int mid, low, high;
FTextureID texture = side->GetTexture(Where);
high = (int)(SwitchList.Size () - 1);
if (high >= 0)
{
low = 0;
do
{
mid = (high + low) / 2;
if (SwitchList[mid]->PreTexture == texture)
{
return mid;
}
else if (texture < SwitchList[mid]->PreTexture)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
} while (low <= high);
}
return -1;
}
//==========================================================================
//
// Checks whether a switch is reachable
// This is optional because old maps can rely on being able to
// use non-reachable switches.
//
//==========================================================================
bool P_CheckSwitchRange(AActor *user, line_t *line, int sideno)
{
// Activated from an empty side -> always succeed
@ -521,15 +168,15 @@ bool P_CheckSwitchRange(AActor *user, line_t *line, int sideno)
if (open.range <= 0)
goto onesided;
if ((TryFindSwitch (side, side_t::top)) != -1)
if ((TexMan.FindSwitch (side->GetTexture(side_t::top))) != -1)
{
return (user->z + user->height >= open.top);
}
else if ((TryFindSwitch (side, side_t::bottom)) != -1)
else if ((TexMan.FindSwitch (side->GetTexture(side_t::bottom))) != -1)
{
return (user->z <= open.bottom);
}
else if ((line->flags & (ML_3DMIDTEX)) || (TryFindSwitch (side, side_t::mid)) != -1)
else if ((line->flags & (ML_3DMIDTEX)) || (TexMan.FindSwitch (side->GetTexture(side_t::mid))) != -1)
{
// 3DMIDTEX lines will force a mid texture check if no switch is found on this line
// to keep compatibility with Eternity's implementation.
@ -544,24 +191,29 @@ bool P_CheckSwitchRange(AActor *user, line_t *line, int sideno)
}
}
//==========================================================================
//
// Function that changes wall texture.
// Tell it if switch is ok to use again (1=yes, it's a button).
//
//==========================================================================
bool P_ChangeSwitchTexture (side_t *side, int useAgain, BYTE special, bool *quest)
{
int texture;
int i, sound;
int sound;
int i;
FSwitchDef *Switch;
if ((i = TryFindSwitch (side, side_t::top)) != -1)
if ((i = TexMan.FindSwitch (side->GetTexture(side_t::top))) != -1)
{
texture = side_t::top;
}
else if ((i = TryFindSwitch (side, side_t::bottom)) != -1)
else if ((i = TexMan.FindSwitch (side->GetTexture(side_t::bottom))) != -1)
{
texture = side_t::bottom;
}
else if ((i = TryFindSwitch (side, side_t::mid)) != -1)
else if ((i = TexMan.FindSwitch (side->GetTexture(side_t::mid))) != -1)
{
texture = side_t::mid;
}
@ -573,11 +225,12 @@ bool P_ChangeSwitchTexture (side_t *side, int useAgain, BYTE special, bool *ques
}
return false;
}
Switch = TexMan.GetSwitch(i);
// EXIT SWITCH?
if (SwitchList[i]->Sound != 0)
if (Switch->Sound != 0)
{
sound = SwitchList[i]->Sound;
sound = Switch->Sound;
}
else
{
@ -599,20 +252,32 @@ bool P_ChangeSwitchTexture (side_t *side, int useAgain, BYTE special, bool *ques
pt[0] = line->v1->x + (line->dx >> 1);
pt[1] = line->v1->y + (line->dy >> 1);
side->SetTexture(texture, SwitchList[i]->u[0].Texture);
if (useAgain || SwitchList[i]->NumFrames > 1)
side->SetTexture(texture, Switch->u[0].Texture);
if (useAgain || Switch->NumFrames > 1)
{
playsound = P_StartButton (side, texture, i, pt[0], pt[1], !!useAgain);
}
else
{
playsound = true;
}
if (playsound)
{
S_Sound (pt[0], pt[1], 0, CHAN_VOICE|CHAN_LISTENERZ, sound, 1, ATTN_STATIC);
}
if (quest != NULL)
{
*quest = SwitchList[i]->QuestPanel;
*quest = Switch->QuestPanel;
}
return true;
}
//==========================================================================
//
// Button thinker
//
//==========================================================================
IMPLEMENT_CLASS (DActiveButton)
DActiveButton::DActiveButton ()
@ -640,6 +305,12 @@ DActiveButton::DActiveButton (side_t *side, int Where, WORD switchnum,
AdvanceFrame ();
}
//==========================================================================
//
//
//
//==========================================================================
void DActiveButton::Serialize (FArchive &arc)
{
SDWORD sidenum;
@ -656,17 +327,23 @@ void DActiveButton::Serialize (FArchive &arc)
}
}
//==========================================================================
//
//
//
//==========================================================================
void DActiveButton::Tick ()
{
if (--m_Timer == 0)
{
FSwitchDef *def = SwitchList[m_SwitchDef];
FSwitchDef *def = TexMan.GetSwitch(m_SwitchDef);
if (m_Frame == def->NumFrames - 1)
{
m_SwitchDef = def->PairIndex;
if (m_SwitchDef != 65535)
{
def = SwitchList[def->PairIndex];
def = TexMan.GetSwitch(def->PairIndex);
m_Frame = 65535;
S_Sound (m_X, m_Y, 0, CHAN_VOICE|CHAN_LISTENERZ,
def->Sound != 0 ? def->Sound : FSoundID("switches/normbutn"),
@ -690,10 +367,16 @@ void DActiveButton::Tick ()
}
}
//==========================================================================
//
//
//
//==========================================================================
bool DActiveButton::AdvanceFrame ()
{
bool ret = false;
FSwitchDef *def = SwitchList[m_SwitchDef];
FSwitchDef *def = TexMan.GetSwitch(m_SwitchDef);
if (++m_Frame == def->NumFrames - 1)
{

View file

@ -0,0 +1,390 @@
/*
** p_switch.cpp
** Switch and button maintenance and animation
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include "templates.h"
#include "textures/textures.h"
#include "doomdef.h"
#include "g_game.h"
#include "s_sound.h"
#include "doomstat.h"
#include "r_state.h"
#include "w_wad.h"
#include "cmdlib.h"
#include "sc_man.h"
#include "gi.h"
static int STACK_ARGS SortSwitchDefs (const void *a, const void *b)
{
return (*(FSwitchDef **)a)->PreTexture - (*(FSwitchDef **)b)->PreTexture;
}
//==========================================================================
//
// P_InitSwitchList
// Only called at game initialization.
//
// [RH] Rewritten to use a BOOM-style SWITCHES lump and remove the
// MAXSWITCHES limit.
//
//==========================================================================
void FTextureManager::InitSwitchList ()
{
const BITFIELD texflags = TEXMAN_Overridable | TEXMAN_TryAny;
int lump = Wads.CheckNumForName ("SWITCHES");
FSwitchDef **origMap;
int i, j;
if (lump != -1)
{
FMemLump lumpdata = Wads.ReadLump (lump);
const char *alphSwitchList = (const char *)lumpdata.GetMem();
const char *list_p;
FSwitchDef *def1, *def2;
for (list_p = alphSwitchList; list_p[18] || list_p[19]; list_p += 20)
{
// [RH] Check for switches that aren't really switches
if (stricmp (list_p, list_p+9) == 0)
{
Printf ("Switch %s in SWITCHES has the same 'on' state\n", list_p);
continue;
}
// [RH] Skip this switch if its textures can't be found.
if (CheckForTexture (list_p /* .name1 */, FTexture::TEX_Wall, texflags).Exists() &&
CheckForTexture (list_p + 9 /* .name2 */, FTexture::TEX_Wall, texflags).Exists())
{
def1 = (FSwitchDef *)M_Malloc (sizeof(FSwitchDef));
def2 = (FSwitchDef *)M_Malloc (sizeof(FSwitchDef));
def1->PreTexture = def2->u[0].Texture = CheckForTexture (list_p /* .name1 */, FTexture::TEX_Wall, texflags);
def2->PreTexture = def1->u[0].Texture = CheckForTexture (list_p + 9, FTexture::TEX_Wall, texflags);
def1->Sound = def2->Sound = 0;
def1->NumFrames = def2->NumFrames = 1;
def1->u[0].Time = def2->u[0].Time = 0;
def2->PairIndex = AddSwitchDef (def1);
def1->PairIndex = AddSwitchDef (def2);
}
}
}
mSwitchDefs.ShrinkToFit ();
// Sort mSwitchDefs for quick searching
origMap = new FSwitchDef *[mSwitchDefs.Size ()];
for (i = 0; i < (int)mSwitchDefs.Size (); i++)
{
origMap[i] = mSwitchDefs[i];
}
qsort (&mSwitchDefs[0], i, sizeof(FSwitchDef *), SortSwitchDefs);
// Correct the PairIndex of each switch def, since the sorting broke them
for (i = (int)(mSwitchDefs.Size () - 1); i >= 0; i--)
{
FSwitchDef *def = mSwitchDefs[i];
if (def->PairIndex != 65535)
{
for (j = (int)(mSwitchDefs.Size () - 1); j >= 0; j--)
{
if (mSwitchDefs[j] == origMap[def->PairIndex])
{
def->PairIndex = (WORD)j;
break;
}
}
}
}
delete[] origMap;
}
//==========================================================================
//
// Parse a switch block in ANIMDEFS and add the definitions to mSwitchDefs
//
//==========================================================================
void FTextureManager::ProcessSwitchDef (FScanner &sc)
{
const BITFIELD texflags = TEXMAN_Overridable | TEXMAN_TryAny;
FString picname;
FSwitchDef *def1, *def2;
FTextureID picnum;
int gametype;
bool quest = false;
def1 = def2 = NULL;
sc.MustGetString ();
if (sc.Compare ("doom"))
{
gametype = GAME_DoomChex;
sc.CheckNumber(); // skip the gamemission filter
}
else if (sc.Compare ("heretic"))
{
gametype = GAME_Heretic;
}
else if (sc.Compare ("hexen"))
{
gametype = GAME_Hexen;
}
else if (sc.Compare ("strife"))
{
gametype = GAME_Strife;
}
else if (sc.Compare ("any"))
{
gametype = GAME_Any;
}
else
{
// There is no game specified; just treat as any
//max = 240;
gametype = GAME_Any;
sc.UnGet ();
}
sc.MustGetString ();
picnum = CheckForTexture (sc.String, FTexture::TEX_Wall, texflags);
picname = sc.String;
while (sc.GetString ())
{
if (sc.Compare ("quest"))
{
quest = true;
}
else if (sc.Compare ("on"))
{
if (def1 != NULL)
{
sc.ScriptError ("Switch already has an on state");
}
def1 = ParseSwitchDef (sc, !picnum.Exists());
}
else if (sc.Compare ("off"))
{
if (def2 != NULL)
{
sc.ScriptError ("Switch already has an off state");
}
def2 = ParseSwitchDef (sc, !picnum.Exists());
}
else
{
sc.UnGet ();
break;
}
}
if (def1 == NULL || !picnum.Exists() ||
(gametype != GAME_Any && !(gametype & gameinfo.gametype)))
{
if (def2 != NULL)
{
M_Free (def2);
}
if (def1 != NULL)
{
M_Free (def1);
}
return;
}
// If the switch did not have an off state, create one that just returns
// it to the original texture without doing anything interesting
if (def2 == NULL)
{
def2 = (FSwitchDef *)M_Malloc (sizeof(FSwitchDef));
def2->Sound = def1->Sound;
def2->NumFrames = 1;
def2->u[0].Time = 0;
def2->u[0].Texture = picnum;
}
def1->PreTexture = picnum;
def2->PreTexture = def1->u[def1->NumFrames-1].Texture;
if (def1->PreTexture == def2->PreTexture)
{
sc.ScriptError ("The on state for switch %s must end with a texture other than %s", picname.GetChars(), picname.GetChars());
}
def2->PairIndex = AddSwitchDef (def1);
def1->PairIndex = AddSwitchDef (def2);
def1->QuestPanel = def2->QuestPanel = quest;
}
//==========================================================================
//
// Parse one switch frame
//
//==========================================================================
FSwitchDef *FTextureManager::ParseSwitchDef (FScanner &sc, bool ignoreBad)
{
const BITFIELD texflags = TEXMAN_Overridable | TEXMAN_TryAny;
FSwitchDef *def;
TArray<FSwitchDef::frame> frames;
FSwitchDef::frame thisframe;
FTextureID picnum;
bool bad;
FSoundID sound;
bad = false;
while (sc.GetString ())
{
if (sc.Compare ("sound"))
{
if (sound != 0)
{
sc.ScriptError ("Switch state already has a sound");
}
sc.MustGetString ();
sound = sc.String;
}
else if (sc.Compare ("pic"))
{
sc.MustGetString ();
picnum = CheckForTexture (sc.String, FTexture::TEX_Wall, texflags);
if (!picnum.Exists() && !ignoreBad)
{
//Printf ("Unknown switch texture %s\n", sc.String);
bad = true;
}
thisframe.Texture = picnum;
sc.MustGetString ();
if (sc.Compare ("tics"))
{
sc.MustGetNumber ();
thisframe.Time = sc.Number & 65535;
}
else if (sc.Compare ("rand"))
{
int min, max;
sc.MustGetNumber ();
min = sc.Number & 65535;
sc.MustGetNumber ();
max = sc.Number & 65535;
if (min > max)
{
swapvalues (min, max);
}
thisframe.Time = ((max - min + 1) << 16) | min;
}
else
{
thisframe.Time = 0; // Shush, GCC.
sc.ScriptError ("Must specify a duration for switch frame");
}
frames.Push(thisframe);
}
else
{
sc.UnGet ();
break;
}
}
if (frames.Size() == 0)
{
sc.ScriptError ("Switch state needs at least one frame");
}
if (bad)
{
return NULL;
}
def = (FSwitchDef *)M_Malloc (myoffsetof (FSwitchDef, u[0]) + frames.Size()*sizeof(frames[0]));
def->Sound = sound;
def->NumFrames = frames.Size();
memcpy (&def->u[0], &frames[0], frames.Size() * sizeof(frames[0]));
def->PairIndex = 65535;
return def;
}
//==========================================================================
//
//
//
//==========================================================================
WORD FTextureManager::AddSwitchDef (FSwitchDef *def)
{
unsigned int i;
for (i = mSwitchDefs.Size (); i-- > 0; )
{
if (mSwitchDefs[i]->PreTexture == def->PreTexture)
{
M_Free (mSwitchDefs[i]);
mSwitchDefs[i] = def;
return (WORD)i;
}
}
return (WORD)mSwitchDefs.Push (def);
}
//==========================================================================
//
//
//
//==========================================================================
int FTextureManager::FindSwitch (FTextureID texture)
{
int mid, low, high;
high = (int)(mSwitchDefs.Size () - 1);
if (high >= 0)
{
low = 0;
do
{
mid = (high + low) / 2;
if (mSwitchDefs[mid]->PreTexture == texture)
{
return mid;
}
else if (texture < mSwitchDefs[mid]->PreTexture)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
} while (low <= high);
}
return -1;
}

View file

@ -269,7 +269,7 @@ void FTextureManager::InitAnimDefs ()
}
else if (sc.Compare ("switch"))
{
P_ProcessSwitchDef (sc);
ProcessSwitchDef (sc);
}
// [GRB] Added warping type 2
else if (sc.Compare ("warp") || sc.Compare ("warp2"))

View file

@ -104,6 +104,17 @@ void FTextureManager::DeleteAll()
}
mAnimations.Clear();
for (unsigned i = 0; i < mSwitchDefs.Size(); i++)
{
if (mSwitchDefs[i] != NULL)
{
M_Free (mSwitchDefs[i]);
mSwitchDefs[i] = NULL;
}
}
mSwitchDefs.Clear();
for (unsigned int i = 0; i < BuildTileFiles.Size(); ++i)
{
delete[] BuildTileFiles[i];
@ -978,6 +989,7 @@ void FTextureManager::Init()
InitAnimated();
InitAnimDefs();
FixAnimations();
InitSwitchList();
}
//==========================================================================

View file

@ -82,6 +82,20 @@ struct FAnimDef
void SetSwitchTime (DWORD mstime);
};
struct FSwitchDef
{
FTextureID PreTexture; // texture to switch from
WORD PairIndex; // switch def to use to return to PreTexture
WORD NumFrames; // # of animation frames
int Sound; // sound to play at start of animation. Changed to int to avoiud having to include s_sound here.
bool QuestPanel; // Special texture for Strife mission
struct frame // Array of times followed by array of textures
{ // actual length of each array is <NumFrames>
DWORD Time;
FTextureID Texture;
} u[1];
};
// Patches.
// A patch holds one or more columns.
@ -373,6 +387,13 @@ public:
void UpdateAnimations (DWORD mstime);
int GuesstimateNumTextures ();
int FindSwitch (FTextureID texture);
FSwitchDef *GetSwitch (unsigned int index)
{
if (index < mSwitchDefs.Size()) return mSwitchDefs[index];
else return NULL;
}
private:
// texture counting
@ -402,6 +423,13 @@ private:
FTexture *Texture(FTextureID id) { return Textures[id.GetIndex()].Texture; }
void SetTranslation (FTextureID fromtexnum, FTextureID totexnum);
// Switches
void InitSwitchList ();
void ProcessSwitchDef (FScanner &sc);
FSwitchDef *ParseSwitchDef (FScanner &sc, bool ignoreBad);
WORD AddSwitchDef (FSwitchDef *def);
struct TextureHash
{
FTexture *Texture;
@ -415,6 +443,7 @@ private:
TArray<int> FirstTextureForFile;
TArray<FAnimDef *> mAnimations;
TArray<FSwitchDef *> mSwitchDefs;
TArray<BYTE *> BuildTileFiles;
};

View file

@ -2419,6 +2419,10 @@
<Filter
Name="Textures"
>
<File
RelativePath=".\src\textures\anim_switches.cpp"
>
</File>
<File
RelativePath=".\src\textures\animations.cpp"
>