Skip to content

Commit

Permalink
- made SPROFS a more usable feature.
Browse files Browse the repository at this point in the history
This was originally invented to fix the sprite offsets for the hardware renderer.
Changed it so that it doesn't override the original offsets but acts as a second set.
A new CVAR has been added to allow controlling the behavior per renderer.
  • Loading branch information
coelckers committed Mar 31, 2018
1 parent 5bdea62 commit 6d61963
Show file tree
Hide file tree
Showing 35 changed files with 322 additions and 274 deletions.
2 changes: 1 addition & 1 deletion src/d_main.cpp
Expand Up @@ -889,7 +889,7 @@ void D_Display ()

tex = TexMan(gameinfo.PauseSign);
x = (SCREENWIDTH - tex->GetScaledWidth() * CleanXfac)/2 +
tex->GetScaledLeftOffset() * CleanXfac;
tex->GetScaledLeftOffset(0) * CleanXfac;
screen->DrawTexture (tex, x, 4, DTA_CleanNoMove, true, TAG_DONE);
if (paused && multiplayer)
{
Expand Down
2 changes: 1 addition & 1 deletion src/g_shared/a_decals.cpp
Expand Up @@ -504,7 +504,7 @@ void DBaseDecal::Spread (const FDecalTemplate *tpl, side_t *wall, double x, doub
int dwidth = tex->GetWidth ();

DecalWidth = dwidth * ScaleX;
DecalLeft = tex->LeftOffset * ScaleX;
DecalLeft = tex->GetLeftOffset(0) * ScaleX;
DecalRight = DecalWidth - DecalLeft;
SpreadSource = this;
SpreadTemplate = tpl;
Expand Down
4 changes: 2 additions & 2 deletions src/g_shared/shared_hud.cpp
Expand Up @@ -178,8 +178,8 @@ static void DrawHudText(FFont *font, int color, char * text, int x, int y, doubl
FTexture *texc = font->GetChar(text[i], &width);
if (texc != NULL)
{
double offset = texc->GetScaledTopOffsetDouble()
- tex_zero->GetScaledTopOffsetDouble()
double offset = texc->GetScaledTopOffsetDouble(0)
- tex_zero->GetScaledTopOffsetDouble(0)
+ tex_zero->GetScaledHeightDouble();

screen->DrawChar(font, color, x, y, text[i],
Expand Down
32 changes: 16 additions & 16 deletions src/g_statusbar/sbarinfo.cpp
Expand Up @@ -1199,12 +1199,12 @@ class DSBarInfo

if((offsetflags & SBarInfoCommand::CENTER) == SBarInfoCommand::CENTER)
{
if (forceWidth < 0) dx -= (texture->GetScaledWidthDouble()/2.0)-texture->GetScaledLeftOffsetDouble();
else dx -= forceWidth*(0.5-(texture->GetScaledLeftOffsetDouble()/texture->GetScaledWidthDouble()));
if (forceWidth < 0) dx -= (texture->GetScaledWidthDouble()/2.0)-texture->GetScaledLeftOffsetDouble(0);
else dx -= forceWidth*(0.5-(texture->GetScaledLeftOffsetDouble(0)/texture->GetScaledWidthDouble()));
//Unoptimalized ^^formula is dx -= forceWidth/2.0-(texture->GetScaledLeftOffsetDouble()*forceWidth/texture->GetScaledWidthDouble());

if (forceHeight < 0) dy -= (texture->GetScaledHeightDouble()/2.0)-texture->GetScaledTopOffsetDouble();
else dy -= forceHeight*(0.5-(texture->GetScaledTopOffsetDouble()/texture->GetScaledHeightDouble()));
if (forceHeight < 0) dy -= (texture->GetScaledHeightDouble()/2.0)-texture->GetScaledTopOffsetDouble(0);
else dy -= forceHeight*(0.5-(texture->GetScaledTopOffsetDouble(0)/texture->GetScaledHeightDouble()));
}

dx += xOffset;
Expand All @@ -1215,10 +1215,10 @@ class DSBarInfo
double tmp = 0;
w = forceWidth < 0 ? texture->GetScaledWidthDouble() : forceWidth;
h = forceHeight < 0 ? texture->GetScaledHeightDouble() : forceHeight;
double dcx = clip[0] == 0 ? 0 : dx + clip[0] - texture->GetScaledLeftOffsetDouble();
double dcy = clip[1] == 0 ? 0 : dy + clip[1] - texture->GetScaledTopOffsetDouble();
double dcr = clip[2] == 0 ? INT_MAX : dx + w - clip[2] - texture->GetScaledLeftOffsetDouble();
double dcb = clip[3] == 0 ? INT_MAX : dy + h - clip[3] - texture->GetScaledTopOffsetDouble();
double dcx = clip[0] == 0 ? 0 : dx + clip[0] - texture->GetScaledLeftOffsetDouble(0);
double dcy = clip[1] == 0 ? 0 : dy + clip[1] - texture->GetScaledTopOffsetDouble(0);
double dcr = clip[2] == 0 ? INT_MAX : dx + w - clip[2] - texture->GetScaledLeftOffsetDouble(0);
double dcb = clip[3] == 0 ? INT_MAX : dy + h - clip[3] - texture->GetScaledTopOffsetDouble(0);

if(clip[0] != 0 || clip[1] != 0)
{
Expand Down Expand Up @@ -1300,10 +1300,10 @@ class DSBarInfo
// Check for clipping
if(clip[0] != 0 || clip[1] != 0 || clip[2] != 0 || clip[3] != 0)
{
rcx = clip[0] == 0 ? 0 : rx+((clip[0] - texture->GetScaledLeftOffsetDouble())*Scale.X);
rcy = clip[1] == 0 ? 0 : ry+((clip[1] - texture->GetScaledTopOffsetDouble())*Scale.Y);
rcr = clip[2] == 0 ? INT_MAX : rx+w-((clip[2] + texture->GetScaledLeftOffsetDouble())*Scale.X);
rcb = clip[3] == 0 ? INT_MAX : ry+h-((clip[3] + texture->GetScaledTopOffsetDouble())*Scale.Y);
rcx = clip[0] == 0 ? 0 : rx+((clip[0] - texture->GetScaledLeftOffsetDouble(0))*Scale.X);
rcy = clip[1] == 0 ? 0 : ry+((clip[1] - texture->GetScaledTopOffsetDouble(0))*Scale.Y);
rcr = clip[2] == 0 ? INT_MAX : rx+w-((clip[2] + texture->GetScaledLeftOffsetDouble(0))*Scale.X);
rcb = clip[3] == 0 ? INT_MAX : ry+h-((clip[3] + texture->GetScaledTopOffsetDouble(0))*Scale.Y);
}

if(clearDontDraw)
Expand Down Expand Up @@ -1399,8 +1399,8 @@ class DSBarInfo
}
int character = (unsigned char)*str;

if(script->spacingCharacter == '\0') //If we are monospaced lets use the offset
ax += (c->LeftOffset+1); //ignore x offsets since we adapt to character size
if (script->spacingCharacter == '\0') //If we are monospaced lets use the offset
ax += (c->GetLeftOffset(0) + 1); //ignore x offsets since we adapt to character size

double rx, ry, rw, rh;
rx = ax + xOffset;
Expand Down Expand Up @@ -1463,8 +1463,8 @@ class DSBarInfo
DTA_DestHeightF, rh,
DTA_Alpha, Alpha,
TAG_DONE);
if(script->spacingCharacter == '\0')
ax += width + spacing - (c->LeftOffset+1);
if (script->spacingCharacter == '\0')
ax += width + spacing - (c->GetLeftOffset(0) + 1);
else //width gets changed at the call to GetChar()
ax += font->GetCharWidth((unsigned char) script->spacingCharacter) + spacing;
str++;
Expand Down
8 changes: 4 additions & 4 deletions src/g_statusbar/shared_sbar.cpp
Expand Up @@ -1578,14 +1578,14 @@ void DBaseStatusBar::DrawGraphic(FTextureID texture, double x, double y, int fla
{
case DI_ITEM_HCENTER: x -= boxwidth / 2; break;
case DI_ITEM_RIGHT: x -= boxwidth; break;
case DI_ITEM_HOFFSET: x -= tex->GetScaledLeftOffsetDouble() * boxwidth / texwidth; break;
case DI_ITEM_HOFFSET: x -= tex->GetScaledLeftOffsetDouble(0) * boxwidth / texwidth; break;
}

switch (flags & DI_ITEM_VMASK)
{
case DI_ITEM_VCENTER: y -= boxheight / 2; break;
case DI_ITEM_BOTTOM: y -= boxheight; break;
case DI_ITEM_VOFFSET: y -= tex->GetScaledTopOffsetDouble() * boxheight / texheight; break;
case DI_ITEM_VOFFSET: y -= tex->GetScaledTopOffsetDouble(0) * boxheight / texheight; break;
}

if (!fullscreenOffsets)
Expand Down Expand Up @@ -1792,7 +1792,7 @@ void DBaseStatusBar::DrawString(FFont *font, const FString &cstring, double x, d
}

if (!monospaced) //If we are monospaced lets use the offset
x += (c->LeftOffset + 1); //ignore x offsets since we adapt to character size
x += (c->GetLeftOffset(0) + 1); //ignore x offsets since we adapt to character size

double rx, ry, rw, rh;
rx = x + drawOffset.X;
Expand Down Expand Up @@ -1832,7 +1832,7 @@ void DBaseStatusBar::DrawString(FFont *font, const FString &cstring, double x, d
TAG_DONE);

if (!monospaced)
x += width + spacing - (c->LeftOffset + 1);
x += width + spacing - (c->GetLeftOffset(0) + 1);
else
x += spacing;
}
Expand Down
82 changes: 0 additions & 82 deletions src/gl/data/gl_data.cpp
Expand Up @@ -70,87 +70,6 @@ CUSTOM_CVAR(Bool, gl_notexturefill, false, 0)
void gl_CreateSections();
void AddAutoMaterials();

//-----------------------------------------------------------------------------
//
// Adjust sprite offsets for GL rendering (IWAD resources only)
//
//-----------------------------------------------------------------------------

void AdjustSpriteOffsets()
{
int lump, lastlump = 0;
int sprid;
TMap<int, bool> donotprocess;

int numtex = Wads.GetNumLumps();

for (int i = 0; i < numtex; i++)
{
if (Wads.GetLumpFile(i) > Wads.GetIwadNum()) break; // we are past the IWAD
if (Wads.GetLumpNamespace(i) == ns_sprites && Wads.GetLumpFile(i) == Wads.GetIwadNum())
{
char str[9];
Wads.GetLumpName(str, i);
str[8] = 0;
FTextureID texid = TexMan.CheckForTexture(str, ETextureType::Sprite, 0);
if (texid.isValid() && Wads.GetLumpFile(TexMan[texid]->SourceLump) > Wads.GetIwadNum())
{
// This texture has been replaced by some PWAD.
memcpy(&sprid, str, 4);
donotprocess[sprid] = true;
}
}
}

while ((lump = Wads.FindLump("SPROFS", &lastlump, false)) != -1)
{
FScanner sc;
sc.OpenLumpNum(lump);
sc.SetCMode(true);
GLRenderer->FlushTextures();
int ofslumpno = Wads.GetLumpFile(lump);
while (sc.GetString())
{
int x,y;
bool iwadonly = false;
bool forced = false;
FTextureID texno = TexMan.CheckForTexture(sc.String, ETextureType::Sprite);
sc.MustGetStringName(",");
sc.MustGetNumber();
x=sc.Number;
sc.MustGetStringName(",");
sc.MustGetNumber();
y=sc.Number;
if (sc.CheckString(","))
{
sc.MustGetString();
if (sc.Compare("iwad")) iwadonly = true;
if (sc.Compare("iwadforced")) forced = iwadonly = true;
}
if (texno.isValid())
{
FTexture * tex = TexMan[texno];

int lumpnum = tex->GetSourceLump();
// We only want to change texture offsets for sprites in the IWAD or the file this lump originated from.
if (lumpnum >= 0 && lumpnum < Wads.GetNumLumps())
{
int wadno = Wads.GetLumpFile(lumpnum);
if ((iwadonly && wadno==Wads.GetIwadNum()) || (!iwadonly && wadno == ofslumpno))
{
if (wadno == Wads.GetIwadNum() && !forced && iwadonly)
{
memcpy(&sprid, &tex->Name[0], 4);
if (donotprocess.CheckKey(sprid)) continue; // do not alter sprites that only get partially replaced.
}
tex->LeftOffset=x;
tex->TopOffset=y;
}
}
}
}
}
}



Expand Down Expand Up @@ -362,7 +281,6 @@ void gl_RecalcVertexHeights(vertex_t * v)

void gl_InitData()
{
AdjustSpriteOffsets();
AddAutoMaterials();
}

Expand Down
102 changes: 61 additions & 41 deletions src/gl/textures/gl_material.cpp
Expand Up @@ -520,8 +520,8 @@ FMaterial::FMaterial(FTexture * tx, bool expanded)

mWidth = tx->GetWidth();
mHeight = tx->GetHeight();
mLeftOffset = tx->LeftOffset;
mTopOffset = tx->TopOffset;
mLeftOffset = tx->GetLeftOffset(0); // These only get used by decals and decals should not use renderer-specific offsets.
mTopOffset = tx->GetTopOffset(0);
mRenderWidth = tx->GetScaledWidth();
mRenderHeight = tx->GetScaledHeight();
mSpriteU[0] = mSpriteV[0] = 0.f;
Expand All @@ -534,59 +534,26 @@ FMaterial::FMaterial(FTexture * tx, bool expanded)
mBaseLayer = ValidateSysTexture(basetex, expanded);
}

float fxScale = tx->Scale.X;
float fyScale = tx->Scale.Y;

// mSpriteRect is for positioning the sprite in the scene.
mSpriteRect.left = -mLeftOffset / fxScale;
mSpriteRect.top = -mTopOffset / fyScale;
mSpriteRect.width = mWidth / fxScale;
mSpriteRect.height = mHeight / fyScale;

mExpanded = expanded;
if (expanded)
{
// a little adjustment to make sprites look better with texture filtering:
// create a 1 pixel wide empty frame around them.
int trim[4];
bool trimmed = TrimBorders(trim); // get the trim size before adding the empty frame

int oldwidth = mWidth;
int oldheight = mHeight;

mWidth+=2;
mHeight+=2;
mLeftOffset+=1;
mTopOffset+=1;
mTrimResult = TrimBorders(trim); // get the trim size before adding the empty frame
mWidth += 2;
mHeight += 2;
mRenderWidth = mRenderWidth * mWidth / oldwidth;
mRenderHeight = mRenderHeight * mHeight / oldheight;

// Reposition the sprite with the frame considered
mSpriteRect.left = -mLeftOffset / fxScale;
mSpriteRect.top = -mTopOffset / fyScale;
mSpriteRect.width = mWidth / fxScale;
mSpriteRect.height = mHeight / fyScale;

if (trimmed)
{
mSpriteRect.left += trim[0] / fxScale;
mSpriteRect.top += trim[1] / fyScale;

mSpriteRect.width -= (oldwidth - trim[2]) / fxScale;
mSpriteRect.height -= (oldheight - trim[3]) / fyScale;

mSpriteU[0] = trim[0] / (float)mWidth;
mSpriteV[0] = trim[1] / (float)mHeight;
mSpriteU[1] -= (oldwidth - trim[0] - trim[2]) / (float)mWidth;
mSpriteV[1] -= (oldheight - trim[1] - trim[3]) / (float)mHeight;
}
}
SetSpriteRect();

mTextureLayers.ShrinkToFit();
mMaxBound = -1;
mMaterials.Push(this);
tx->gl_info.Material[expanded] = this;
if (tx->bHasCanvas) tx->gl_info.mIsTransparent = 0;
mExpanded = expanded;
}

//===========================================================================
Expand All @@ -608,6 +575,59 @@ FMaterial::~FMaterial()

}

//===========================================================================
//
// Set the sprite rectangle
//
//===========================================================================

void FMaterial::SetSpriteRect()
{
auto leftOffset = tex->GetLeftOffsetHW();
auto topOffset = tex->GetTopOffsetHW();

float fxScale = tex->Scale.X;
float fyScale = tex->Scale.Y;

// mSpriteRect is for positioning the sprite in the scene.
mSpriteRect.left = -leftOffset / fxScale;
mSpriteRect.top = -topOffset / fyScale;
mSpriteRect.width = mWidth / fxScale;
mSpriteRect.height = mHeight / fyScale;

if (mExpanded)
{
// a little adjustment to make sprites look better with texture filtering:
// create a 1 pixel wide empty frame around them.

int oldwidth = mWidth - 2;
int oldheight = mHeight - 2;

leftOffset += 1;
topOffset += 1;

// Reposition the sprite with the frame considered
mSpriteRect.left = -leftOffset / fxScale;
mSpriteRect.top = -topOffset / fyScale;
mSpriteRect.width = mWidth / fxScale;
mSpriteRect.height = mHeight / fyScale;

if (mTrimResult)
{
mSpriteRect.left += trim[0] / fxScale;
mSpriteRect.top += trim[1] / fyScale;

mSpriteRect.width -= (oldwidth - trim[2]) / fxScale;
mSpriteRect.height -= (oldheight - trim[3]) / fyScale;

mSpriteU[0] = trim[0] / (float)mWidth;
mSpriteV[0] = trim[1] / (float)mHeight;
mSpriteU[1] -= (oldwidth - trim[0] - trim[2]) / (float)mWidth;
mSpriteV[1] -= (oldheight - trim[1] - trim[3]) / (float)mHeight;
}
}
}


//===========================================================================
//
Expand All @@ -616,7 +636,7 @@ FMaterial::~FMaterial()
//
//===========================================================================

bool FMaterial::TrimBorders(int *rect)
bool FMaterial::TrimBorders(uint16_t *rect)
{
PalEntry col;
int w;
Expand Down

0 comments on commit 6d61963

Please sign in to comment.