Skip to content

Commit

Permalink
Added to_string() and fixed a bunch of crap
Browse files Browse the repository at this point in the history
  • Loading branch information
Raikiri committed Oct 17, 2024
1 parent 48bb545 commit 4e06740
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
35 changes: 27 additions & 8 deletions LegitScript/source/RenderGraphScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct RenderGraphScript::Impl
void RegisterImageType();
template<typename VecType, size_t CompCount>
void RegisterVecType(std::string type_name, std::string comp_type_name);
void RegisterBasicToString();


struct ImageInfo
Expand Down Expand Up @@ -255,13 +256,31 @@ void RenderGraphScript::Impl::RegisterAsScriptGlobals()
std::string text = *(std::string*)gen->GetArgObject(0);
this->text_func(text);
});

RegisterVecType<ls::vec2, 2>("vec2", "float");
RegisterVecType<ls::vec3, 3>("vec3", "float");
RegisterVecType<ls::vec4, 4>("vec4", "float");
RegisterVecType<ls::ivec2, 2>("ivec2", "int");
RegisterVecType<ls::ivec3, 3>("ivec3", "int");
RegisterVecType<ls::ivec4, 4>("ivec4", "int");
RegisterImageType();
RegisterBasicToString();
}

void RenderGraphScript::Impl::RegisterBasicToString()
{
as_script_engine->RegisterGlobalFunction("string to_string(float v)", [](asIScriptGeneric *gen)
{
float arg = gen->GetArgFloat(0);
std::string res = std::to_string(arg);
gen->SetReturnObject(&res);
});
as_script_engine->RegisterGlobalFunction("string to_string(int v)", [](asIScriptGeneric *gen)
{
int arg = gen->GetArgDWord(0);
std::string res = std::to_string(arg);
gen->SetReturnObject(&res);
});
}

void RenderGraphScript::Impl::RegisterImageType()
Expand Down Expand Up @@ -449,20 +468,20 @@ void RenderGraphScript::Impl::RegisterVecType(std::string type_name, std::string
gen->SetReturnObject(&res);
});

as_script_engine->RegisterMethod(type_name.c_str(), "void Print()", [=](asIScriptGeneric *gen)
as_script_engine->RegisterGlobalFunction(std::string("string to_string(") + type_name + " v)", [](asIScriptGeneric *gen)
{
auto *this_ptr = (VecType*)gen->GetObject();
std::cout << "Printing " << type_name << ": [";
auto *arg_ptr = (VecType*)gen->GetArgObject(0);

std::string res = "[";
bool is_first = true;
for(size_t i = 0; i < CompCount; i++)
{
if(!is_first)
std::cout << ", ";
if(!is_first) res += ", ";
is_first = false;
auto comp = GetComp<VecType, CompType>(*this_ptr, i);
std::cout << comp;
res += std::to_string(GetComp<VecType, CompType>(*arg_ptr, i));
}
std::cout << "]\n";
res += "]";
gen->SetReturnObject(&res);
});
}

Expand Down
26 changes: 11 additions & 15 deletions bin/data/Scripts/main.ls
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
void ColorPass(in float r, in float g, in float b, out vec4 out_color)
void ColorPass(in vec3 in_color, out vec4 out_color)
{{
void main()
{
out_color = vec4(r, g, b + 0.5f, 1.0f);
out_color = vec4(in_color, 1.0f);
}
}}

Expand All @@ -11,22 +11,18 @@ void RenderGraphMain()
{{
void main()
{
vec3 v1 = vec3(1, 3, 0);
vec3 v2 = vec3(1, 0, 3.1);
((v1 + v2) * 3).Print();

Image img = GetImage(ivec2(128, 128), rgba8);
Text("Mips count: " + img.GetMipsCount() + ". Mip 2 size: " + img.GetMip(0).GetSize());

//Image img = GetImage(128, 128, rgba8);
//ColorPass(
// SliderFloat("R", 0.0f, 1.0f) + 0.5f,
// SliderFloat("G", 0.0f, 1.0f),
// SliderFloat("B", 0.0f, 1.0f),
// GetSwapchainImage());
//int a = SliderInt("Int param", -42, 42, 5);
//float b = SliderFloat("Float param", -42.0f, 42.0f);
//float e = SliderFloat("Float param", -42.0f, 42.0f);
//Text("script int: " + formatInt(a) + " float: " + formatFloat(b));
vec3 color = vec3(
SliderFloat("R", 0.0f, 1.0f, 0.3f),
SliderFloat("G", 0.0f, 1.0f, 0.4f),
SliderFloat("B", 0.0f, 1.0f, 0.5f));
Text("vec3: " + to_string(color) + " pi: " + to_string(3.1415f));
Text("vec3: " + color + " pi: " + 3.1415f);
ColorPass(
color,
GetSwapchainImage());
}
}}
4 changes: 2 additions & 2 deletions tests/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ void RunTestJson()
std::cout << "Test starts\n";
ls::InitScript([](std::string name, float val, float min_val, float max_val) -> float
{
std::cout << "Slider int: " << val << "[" << min_val << ", " << max_val << "]\n";
std::cout << "Slider float: " << val << "[" << min_val << ", " << max_val << "]\n";
return val;
},
[](std::string name, int val, int min_val, int max_val) -> int
{
std::cout << "Slider float: " << val << "[" << min_val << ", " << max_val << "]\n";
std::cout << "Slider int: " << val << "[" << min_val << ", " << max_val << "]\n";
return val;
},
[](std::string text) -> void
Expand Down

0 comments on commit 4e06740

Please sign in to comment.