Skip to content

Commit

Permalink
#5532: Extend material parser to be able to deal with the guisurf var…
Browse files Browse the repository at this point in the history
…iants
  • Loading branch information
codereader committed Feb 28, 2021
1 parent cf8035a commit 154e776
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 4 deletions.
9 changes: 8 additions & 1 deletion include/ishaders.h
Expand Up @@ -95,7 +95,10 @@ class Material
SURF_NODAMAGE = 1 << 21,
SURF_LADDER = 1 << 22,
SURF_NOSTEPS = 1 << 23,
SURF_ENTITYGUI = 1 << 24,
SURF_GUISURF = 1 << 24, // has guisurf in its material def
SURF_ENTITYGUI = 1 << 25, // guisurf entity
SURF_ENTITYGUI2 = 1 << 26, // guisurf entity2
SURF_ENTITYGUI3 = 1 << 27, // guisurf entity3
};

// Surface Type (plastic, stone, etc.)
Expand Down Expand Up @@ -362,6 +365,10 @@ class Material

// Returns the argument string after the renderbumpflat keyword, or an empty string if no statement is present
virtual std::string getRenderBumpFlatArguments() = 0;

// The argument to the "guisurf" keyword, if not entity[2]3].
// In case entity[2]3] is set, the corresponding surface flags are enabled
virtual const std::string& getGuiSurfArgument() = 0;
};

typedef std::shared_ptr<Material> MaterialPtr;
Expand Down
5 changes: 5 additions & 0 deletions radiantcore/shaders/CShader.cpp
Expand Up @@ -223,6 +223,11 @@ std::string CShader::getRenderBumpFlatArguments()
return _template->getRenderBumpFlagArguments();
}

const std::string& CShader::getGuiSurfArgument()
{
return _template->getGuiSurfArgument();
}

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

void CShader::realise() {
Expand Down
2 changes: 2 additions & 0 deletions radiantcore/shaders/CShader.h
Expand Up @@ -108,6 +108,8 @@ class CShader
std::string getRenderBumpArguments() override;
std::string getRenderBumpFlatArguments() override;

const std::string& getGuiSurfArgument() override;

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

void realise();
Expand Down
24 changes: 21 additions & 3 deletions radiantcore/shaders/ShaderTemplate.cpp
Expand Up @@ -1126,9 +1126,27 @@ bool ShaderTemplate::parseSurfaceFlags(parser::DefTokeniser& tokeniser,
}
else if (token == "guisurf")
{
_surfaceFlags |= Material::SURF_ENTITYGUI;
// Something like "guisurf blah.gui" or "guisurf entity2", skip the argument and proceed
tokeniser.skipTokens(1);
// "guisurf blah.gui" or "guisurf entity[2|3]"
_surfaceFlags |= Material::SURF_GUISURF;

auto argument = tokeniser.nextToken();

if (string::to_lower_copy(argument) == "entity")
{
_surfaceFlags |= Material::SURF_ENTITYGUI;
}
else if (string::to_lower_copy(argument) == "entity2")
{
_surfaceFlags |= Material::SURF_ENTITYGUI2;
}
else if (string::to_lower_copy(argument) == "entity3")
{
_surfaceFlags |= Material::SURF_ENTITYGUI3;
}
else
{
_guiDeclName = argument;
}
}
else
{
Expand Down
9 changes: 9 additions & 0 deletions radiantcore/shaders/ShaderTemplate.h
Expand Up @@ -98,6 +98,9 @@ class ShaderTemplate

int _parseFlags;

// The string value specified by the guisurf keyword, if other than entity[2]3]
std::string _guiDeclName;

public:

/**
Expand Down Expand Up @@ -308,6 +311,12 @@ class ShaderTemplate
// renderbumpflat argument string
std::string getRenderBumpFlagArguments();

const std::string& getGuiSurfArgument()
{
if (!_parsed) parseDefinition();
return _guiDeclName;
}

private:

// Add the given layer and assigns editor preview layer if applicable
Expand Down
26 changes: 26 additions & 0 deletions test/Materials.cpp
Expand Up @@ -442,4 +442,30 @@ TEST_F(MaterialsTest, MaterialParserStageFragmentProgram)
EXPECT_TRUE(material->getAllLayers().empty()); // failure to parse should end up with an empty material
}

TEST_F(MaterialsTest, MaterialParserGuiSurf)
{
auto material = GlobalMaterialManager().getMaterialForName("textures/parsertest/guisurf1");
EXPECT_TRUE(material->getSurfaceFlags() & Material::SURF_GUISURF);
EXPECT_FALSE(material->getSurfaceFlags() & (Material::SURF_ENTITYGUI| Material::SURF_ENTITYGUI2| Material::SURF_ENTITYGUI3));
EXPECT_EQ(material->getGuiSurfArgument(), "guis/lvlmaps/genericmap.gui");

material = GlobalMaterialManager().getMaterialForName("textures/parsertest/guisurf2");
EXPECT_TRUE(material->getSurfaceFlags() & Material::SURF_GUISURF);
EXPECT_TRUE(material->getSurfaceFlags() & Material::SURF_ENTITYGUI);
EXPECT_FALSE(material->getSurfaceFlags() & (Material::SURF_ENTITYGUI2 | Material::SURF_ENTITYGUI3));
EXPECT_EQ(material->getGuiSurfArgument(), "");

material = GlobalMaterialManager().getMaterialForName("textures/parsertest/guisurf3");
EXPECT_TRUE(material->getSurfaceFlags() & Material::SURF_GUISURF);
EXPECT_TRUE(material->getSurfaceFlags() & Material::SURF_ENTITYGUI2);
EXPECT_FALSE(material->getSurfaceFlags() & (Material::SURF_ENTITYGUI | Material::SURF_ENTITYGUI3));
EXPECT_EQ(material->getGuiSurfArgument(), "");

material = GlobalMaterialManager().getMaterialForName("textures/parsertest/guisurf4");
EXPECT_TRUE(material->getSurfaceFlags() & Material::SURF_GUISURF);
EXPECT_TRUE(material->getSurfaceFlags() & Material::SURF_ENTITYGUI3);
EXPECT_FALSE(material->getSurfaceFlags() & (Material::SURF_ENTITYGUI | Material::SURF_ENTITYGUI2));
EXPECT_EQ(material->getGuiSurfArgument(), "");
}

}
32 changes: 32 additions & 0 deletions test/resources/tdm/materials/parsertest.mtr
Expand Up @@ -289,3 +289,35 @@ textures/parsertest/program/fragmentProgram3
fragmentMap 8 temp/texture
}
}

textures/parsertest/guisurf1
{
qer_editorimage textures/editor/entityGui.tga
discrete
guiSurf guis/lvlmaps/genericmap.gui

}

textures/parsertest/guisurf2
{
qer_editorimage textures/editor/entityGui.tga
discrete
playerclip
guiSurf entity
}

textures/parsertest/guisurf3
{
qer_editorimage textures/editor/entityGui2.tga
discrete
playerclip
guiSurf entity2
}

textures/parsertest/guisurf4
{
qer_editorimage textures/editor/entityGui3.tga
discrete
guiSurf entity3
playerclip
}

0 comments on commit 154e776

Please sign in to comment.