Skip to content

Commit

Permalink
- building the new base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Oct 31, 2020
1 parent 783d532 commit 1169922
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 126 deletions.
226 changes: 212 additions & 14 deletions src/g_statusbar/base_sbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ IMPLEMENT_CLASS(DHUDFont, true, false);


CVAR(Color, crosshaircolor, 0xff0000, CVAR_ARCHIVE);
CVAR(Int, crosshairhealth, 1, CVAR_ARCHIVE);
CVAR(Int, crosshairhealth, 2, CVAR_ARCHIVE);
CVAR(Float, crosshairscale, 1.0, CVAR_ARCHIVE);
CVAR(Bool, crosshairgrow, false, CVAR_ARCHIVE);
EXTERN_CVAR(Bool, vid_fps)


EXTERN_CVAR(Float, hud_scalefactor)

void ST_LoadCrosshair(int num, bool alwaysload)
{
Expand Down Expand Up @@ -249,6 +249,178 @@ void DStatusBarCore::ValidateResolution(int& hres, int& vres) const
}


//============================================================================
//
//
//
//============================================================================

void DStatusBarCore::SetSize(int reltop, int hres, int vres, int hhres, int hvres)
{
ValidateResolution(hres, vres);

BaseRelTop = reltop;
BaseSBarHorizontalResolution = hres;
BaseSBarVerticalResolution = vres;
BaseHUDHorizontalResolution = hhres < 0 ? hres : hhres;
BaseHUDVerticalResolution = hvres < 0 ? vres : hvres;
SetDrawSize(reltop, hres, vres);
}

//============================================================================
//
// calculates a clean scale for the status bar
//
//============================================================================

static void ST_CalcCleanFacs(int designwidth, int designheight, int realwidth, int realheight, int* cleanx, int* cleany)
{
float ratio;
int cwidth;
int cheight;
int cx1, cy1, cx2, cy2;

ratio = ActiveRatio(realwidth, realheight);
if (AspectTallerThanWide(ratio))
{
cwidth = realwidth;
cheight = realheight * AspectMultiplier(ratio) / 48;
}
else
{
cwidth = realwidth * AspectMultiplier(ratio) / 48;
cheight = realheight;
}
// Use whichever pair of cwidth/cheight or width/height that produces less difference
// between CleanXfac and CleanYfac.
cx1 = MAX(cwidth / designwidth, 1);
cy1 = MAX(cheight / designheight, 1);
cx2 = MAX(realwidth / designwidth, 1);
cy2 = MAX(realheight / designheight, 1);
if (abs(cx1 - cy1) <= abs(cx2 - cy2) || MAX(cx1, cx2) >= 4)
{ // e.g. 640x360 looks better with this.
*cleanx = cx1;
*cleany = cy1;
}
else
{ // e.g. 720x480 looks better with this.
*cleanx = cx2;
*cleany = cy2;
}

if (*cleanx < *cleany)
*cleany = *cleanx;
else
*cleanx = *cleany;
}

//============================================================================
//
//
//
//============================================================================

void DStatusBarCore::SetDrawSize(int reltop, int hres, int vres)
{
ValidateResolution(hres, vres);

RelTop = reltop;
HorizontalResolution = hres;
VerticalResolution = vres;

int x, y;
ST_CalcCleanFacs(hres, vres, twod->GetWidth(), twod->GetHeight(), &x, &y);
defaultScale = { (double)x, (double)y };

SetScale(); // recalculate positioning info.
}

//---------------------------------------------------------------------------
//
// PROC SetScaled
//
//---------------------------------------------------------------------------

void DStatusBarCore::SetScale()
{
ValidateResolution(HorizontalResolution, VerticalResolution);

int w = twod->GetWidth();
int h = twod->GetHeight();
double refw, refh;

int horz = HorizontalResolution;
int vert = VerticalResolution;
double refaspect = horz / double(vert);
double screenaspect = w / double(h);

if ((horz == 320 && vert == 200) || (horz == 640 && vert == 400))
{
refaspect = 1.333;
}

if (screenaspect < refaspect)
{
refw = w;
refh = w / refaspect;
}
else
{
refh = h;
refw = h * refaspect;
}
refw *= hud_scalefactor;
refh *= hud_scalefactor;

int sby = VerticalResolution - RelTop;
// Use full pixels for destination size.

ST_X = xs_CRoundToInt((w - refw) / 2);
ST_Y = xs_CRoundToInt(h - refh);
SBarTop = Scale(sby, h, VerticalResolution);
SBarScale.X = refw / horz;
SBarScale.Y = refh / vert;
}

//---------------------------------------------------------------------------
//
// PROC GetHUDScale
//
//---------------------------------------------------------------------------

DVector2 DStatusBarCore::GetHUDScale() const
{
return SBarScale;
}

//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------

void DStatusBarCore::BeginStatusBar(int resW, int resH, int relTop, bool forceScaled)
{
SetDrawSize(relTop < 0 ? BaseRelTop : relTop, resW < 0 ? BaseSBarHorizontalResolution : resW, resH < 0 ? BaseSBarVerticalResolution : resH);
ForcedScale = forceScaled;
fullscreenOffsets = false;
}

//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------

void DStatusBarCore::BeginHUD(int resW, int resH, double Alpha, bool forcescaled)
{
SetDrawSize(RelTop, resW < 0 ? BaseHUDHorizontalResolution : resW, resH < 0 ? BaseHUDVerticalResolution : resH);
this->Alpha = Alpha;
ForcedScale = forcescaled;
CompleteBorder = false;
fullscreenOffsets = true;
}

//============================================================================
//
// draw stuff
Expand Down Expand Up @@ -280,15 +452,22 @@ void DStatusBarCore::StatusbarToRealCoords(double& x, double& y, double& w, doub
//
//============================================================================

void DStatusBarCore::DrawGraphic(FTextureID texture, double x, double y, int flags, double Alpha, double boxwidth, double boxheight, double scaleX, double scaleY)
void DStatusBarCore::DrawGraphic(FTextureID texture, double x, double y, int flags, double Alpha, double boxwidth, double boxheight, double scaleX, double scaleY, PalEntry color, int translation, double rotate, ERenderStyle style)
{
if (!texture.isValid())
return;

FGameTexture* tex = TexMan.GetGameTexture(texture, !(flags & DI_DONTANIMATE));
DrawGraphic(tex, x, y, flags, Alpha, boxwidth, boxheight, scaleX, scaleY, color, translation, rotate, style);
}

void DStatusBarCore::DrawGraphic(FGameTexture* tex, double x, double y, int flags, double Alpha, double boxwidth, double boxheight, double scaleX, double scaleY, PalEntry color, int translation, double rotate, ERenderStyle style)
{
double texwidth = tex->GetDisplayWidth() * scaleX;
double texheight = tex->GetDisplayHeight() * scaleY;
double texleftoffs = tex->GetDisplayLeftOffset() * scaleY;
double textopoffs = tex->GetDisplayTopOffset() * scaleY;
double boxleftoffs, boxtopoffs;

if (boxwidth > 0 || boxheight > 0)
{
Expand All @@ -314,12 +493,16 @@ void DStatusBarCore::DrawGraphic(FTextureID texture, double x, double y, int fla

boxwidth = texwidth * scale1;
boxheight = texheight * scale1;
boxleftoffs = texleftoffs * scale1;
boxtopoffs = textopoffs * scale1;
}
}
else
{
boxwidth = texwidth;
boxheight = texheight;
boxleftoffs = texleftoffs;
boxtopoffs = textopoffs;
}

// resolve auto-alignment before making any adjustments to the position values.
Expand All @@ -336,18 +519,28 @@ void DStatusBarCore::DrawGraphic(FTextureID texture, double x, double y, int fla
x += drawOffset.X;
y += drawOffset.Y;

switch (flags & DI_ITEM_HMASK)
if (flags & DI_ITEM_RELCENTER)
{
case DI_ITEM_HCENTER: x -= boxwidth / 2; break;
case DI_ITEM_RIGHT: x -= boxwidth; break;
case DI_ITEM_HOFFSET: x -= tex->GetDisplayLeftOffset() * boxwidth / texwidth; break;
if (flags & DI_MIRROR) boxleftoffs = -boxleftoffs;
if (flags & DI_MIRRORY) boxtopoffs = -boxtopoffs;
x -= boxwidth / 2 + boxleftoffs;
y -= boxheight / 2 + boxtopoffs;
}

switch (flags & DI_ITEM_VMASK)
else
{
case DI_ITEM_VCENTER: y -= boxheight / 2; break;
case DI_ITEM_BOTTOM: y -= boxheight; break;
case DI_ITEM_VOFFSET: y -= tex->GetDisplayTopOffset() * boxheight / texheight; break;
switch (flags & DI_ITEM_HMASK)
{
case DI_ITEM_HCENTER: x -= boxwidth / 2; break;
case DI_ITEM_RIGHT: x -= boxwidth; break;
case DI_ITEM_HOFFSET: x -= boxleftoffs; 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 -= boxtopoffs; break;
}
}

if (!fullscreenOffsets)
Expand Down Expand Up @@ -389,12 +582,16 @@ void DStatusBarCore::DrawGraphic(FTextureID texture, double x, double y, int fla
DTA_LeftOffset, 0,
DTA_DestWidthF, boxwidth,
DTA_DestHeightF, boxheight,
DTA_TranslationIndex, (flags & DI_TRANSLATABLE) ? GetTranslation() : 0,
DTA_Color, color,
DTA_TranslationIndex, translation? translation : (flags & DI_TRANSLATABLE) ? GetTranslation() : 0,
DTA_ColorOverlay, (flags & DI_DIM) ? MAKEARGB(170, 0, 0, 0) : 0,
DTA_Alpha, Alpha,
DTA_AlphaChannel, !!(flags & DI_ALPHAMAPPED),
DTA_FillColor, (flags & DI_ALPHAMAPPED) ? 0 : -1,
DTA_FlipX, !!(flags & DI_MIRROR),
DTA_FlipY, !!(flags& DI_MIRRORY),
DTA_Rotate, rotate,
DTA_LegacyRenderStyle, style,
TAG_DONE);
}

Expand Down Expand Up @@ -476,11 +673,12 @@ void DStatusBarCore::DrawString(FFont* font, const FString& cstring, double x, d
}

int width;
auto c = font->GetChar(ch, fontcolor, &width);
FGameTexture* c = font->GetChar(ch, fontcolor, &width);
if (c == NULL) //missing character.
{
continue;
}
width += font->GetDefaultKerning();

if (!monospaced) //If we are monospaced lets use the offset
x += (c->GetDisplayLeftOffset() + 1); //ignore x offsets since we adapt to character size
Expand Down
24 changes: 21 additions & 3 deletions src/g_statusbar/base_sbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ enum DI_Flags
DI_SKIPSPAWN = 0x4,
DI_SKIPREADY = 0x8,
DI_ALTICONFIRST = 0x10,


DI_TRANSLATABLE = 0x20,
DI_FORCESCALE = 0x40,
DI_DIM = 0x80,
Expand All @@ -28,6 +30,8 @@ enum DI_Flags
DI_DIMDEPLETED = 0x400,
DI_DONTANIMATE = 0x800, // do not animate the texture
DI_MIRROR = 0x1000, // flip the texture horizontally, like a mirror
DI_ITEM_RELCENTER = 0x2000,
DI_MIRRORY = 0x40000000,

DI_SCREEN_AUTO = 0, // decide based on given offsets.
DI_SCREEN_MANUAL_ALIGN = 0x4000, // If this is on, the following flags will have an effect
Expand Down Expand Up @@ -148,21 +152,35 @@ class DStatusBarCore : public DObject
int ST_X;
int ST_Y;
int SBarTop;
DVector2 SBarScale;
int RelTop;
int HorizontalResolution, VerticalResolution;
double Alpha = 1.;
DVector2 SBarScale;
DVector2 drawOffset = { 0,0 }; // can be set by subclasses to offset drawing operations
DVector2 defaultScale; // factor for clean fully scaled display.
double drawClip[4] = { 0,0,0,0 }; // defines a clipping rectangle (not used yet)
bool fullscreenOffsets = false; // current screen is displayed with fullscreen behavior.
bool ForcedScale = false;
bool CompleteBorder = false;

int BaseRelTop;
int BaseSBarHorizontalResolution;
int BaseSBarVerticalResolution;
int BaseHUDHorizontalResolution;
int BaseHUDVerticalResolution;


virtual DVector2 GetHUDScale() const = 0;
void BeginStatusBar(int resW, int resH, int relTop, bool forceScaled = false);
void BeginHUD(int resW, int resH, double Alpha, bool forceScaled = false);
void SetSize(int reltop = 32, int hres = 320, int vres = 200, int hhres = -1, int hvres = -1);
virtual DVector2 GetHUDScale() const;
virtual uint32_t GetTranslation() const { return 0; }
void SetDrawSize(int reltop, int hres, int vres);
virtual void SetScale();
void ValidateResolution(int& hres, int& vres) const;
void StatusbarToRealCoords(double& x, double& y, double& w, double& h) const;
void DrawGraphic(FTextureID texture, double x, double y, int flags, double Alpha, double boxwidth, double boxheight, double scaleX, double scaleY);
void DrawGraphic(FGameTexture* texture, double x, double y, int flags, double Alpha, double boxwidth, double boxheight, double scaleX, double scaleY, PalEntry color = 0xffffffff, int translation = 0, double rotate = 0, ERenderStyle style = STYLE_Translucent);
void DrawGraphic(FTextureID texture, double x, double y, int flags, double Alpha, double boxwidth, double boxheight, double scaleX, double scaleY, PalEntry color = 0xffffffff, int translation = 0, double rotate = 0, ERenderStyle style = STYLE_Translucent);
void DrawString(FFont* font, const FString& cstring, double x, double y, int flags, double Alpha, int translation, int spacing, EMonospacing monospacing, int shadowX, int shadowY, double scaleX, double scaleY);
void TransformRect(double& x, double& y, double& w, double& h, int flags = 0);
void Fill(PalEntry color, double x, double y, double w, double h, int flags = 0);
Expand Down
Loading

0 comments on commit 1169922

Please sign in to comment.