- streamlined font handling for scripts a bit.

- moved the two 'you raised the alarm' messages for Strife to the string table
This commit is contained in:
Christoph Oelckers 2017-02-05 13:14:22 +01:00
commit b570d0819b
14 changed files with 144 additions and 35 deletions

View file

@ -1420,6 +1420,76 @@ ExpEmit FxSoundCast::Emit(VMFunctionBuilder *build)
return to;
}
//==========================================================================
//
//
//
//==========================================================================
FxFontCast::FxFontCast(FxExpression *x)
: FxExpression(EFX_FontCast, x->ScriptPosition)
{
basex = x;
ValueType = TypeSound;
}
//==========================================================================
//
//
//
//==========================================================================
FxFontCast::~FxFontCast()
{
SAFE_DELETE(basex);
}
//==========================================================================
//
//
//
//==========================================================================
FxExpression *FxFontCast::Resolve(FCompileContext &ctx)
{
CHECKRESOLVED();
SAFE_RESOLVE(basex, ctx);
if (basex->ValueType == TypeFont)
{
FxExpression *x = basex;
basex = nullptr;
delete this;
return x;
}
// This intentionally does not convert non-constants.
// The sole reason for this cast is to allow passing both font pointers and string constants to printing functions and have the font names checked at compile time.
else if ((basex->ValueType == TypeString || basex->ValueType == TypeName) && basex->isConstant())
{
ExpVal constval = static_cast<FxConstant *>(basex)->GetValue();
FFont *font = V_GetFont(constval.GetString());
// Font must exist. Most internal functions working with fonts do not like null pointers.
// If checking is needed scripts will have to call Font.GetFont themselves.
if (font == nullptr)
{
ScriptPosition.Message(MSG_ERROR, "Unknown font '%s'", constval.GetString().GetChars());
delete this;
return nullptr;
}
FxExpression *x = new FxConstant(font, ScriptPosition);
delete this;
return x;
}
else
{
ScriptPosition.Message(MSG_ERROR, "Cannot convert to font");
delete this;
return nullptr;
}
}
//==========================================================================
//
// generic type cast operator
@ -1649,6 +1719,14 @@ FxExpression *FxTypeCast::Resolve(FCompileContext &ctx)
{
goto basereturn;
}
else if (ValueType == TypeFont)
{
FxExpression *x = new FxFontCast(basex);
x = x->Resolve(ctx);
basex = nullptr;
delete this;
return x;
}
// todo: pointers to class objects.
// All other types are only compatible to themselves and have already been handled above by the equality check.
// Anything that falls through here is not compatible and must print an error.