Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace stack code with IMaterial #72

Merged
merged 5 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions source/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,28 @@ Vector MakeVector(const float x, const float y, const float z)
return v;
}

std::string GetMaterialString(ILuaBase* LUA, const std::string& key)
IMaterialVar* GetMaterialVar(IMaterial* mat, const std::string& key) {
IMaterialVar* result = nullptr;
bool found = false;

result = mat->FindVar(key.c_str(), &found, false);

if (found) {
return result;
} else {
return nullptr;
}
}

std::string GetMaterialString(IMaterial* mat, const std::string& key)
{
std::string val = "";
LUA->GetField(-1, "GetString");
LUA->Push(-2);
LUA->PushString(key.c_str());
LUA->Call(2, 1);
if (LUA->IsType(-1, Type::String)) val = LUA->GetString();
LUA->Pop();
// Check if the variable is even present
IMaterialVar* var = GetMaterialVar(mat, key);
if (var) {
return var->GetStringValue();
}

return val;
return "";
}

bool ValidVector(const glm::vec3& v)
Expand Down
16 changes: 13 additions & 3 deletions source/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "glm/glm.hpp"

#include "vistrace/IVTFTexture.h"
#include "SourceTypes.h"

#include <string>
#include <vector>
Expand Down Expand Up @@ -31,12 +32,21 @@ Vector MakeVector(const float n);
Vector MakeVector(const float x, const float y, const float z);

/// <summary>
/// Gets a string from the material at the top of the stack
/// Gets a string from the material
/// </summary>
/// <param name="LUA">ILuaBase pointer</param>
/// <param name="mat">Material pointer</param>
/// <param name="key">String key</param>
/// <returns>Value at the key or an empty string</returns>
std::string GetMaterialString(GarrysMod::Lua::ILuaBase* LUA, const std::string& key);
std::string GetMaterialString(IMaterial* mat, const std::string& key);

/// <summary>
/// Gets a material variable from the given IMaterial
/// The reason for this function existing is to handle the extra "found" variable in ->FindVar and to support std::string
/// </summary>
/// <param name="mat">Material pointer</param>
/// <param name="key">String key</param>
/// <returns>Pointer to the IMaterialVar or a nullptr</returns>
IMaterialVar* GetMaterialVar(IMaterial* mat, const std::string& key);

/// <summary>
/// VMT flags
Expand Down
1 change: 1 addition & 0 deletions source/VisTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Sampler.h"
#include "RenderTarget.h"
#include "VTFTexture.h"
#include "SourceTypes.h"

#include "TraceResult.h"
#include "AccelStruct.h"
Expand Down
Loading