# Changes

3ds Backend:
  - switch to shaderProgramUse
Desktop Backend
  - Add Pre Alpha Text Input and Keyboard Support
  - Move Shader Attrib Setup into a function and callit every time we need a set up vbo
  - Move to Mat4 api
Core:
  - Add fquat support
  - Add LoadFile2Str
  - Move Mat4 Lib from Project     n73 to Palladium
  - Add full supprot for vec cross types
  - Add Normalize, Distance and Dot to all
  - Add Cross to vec3
Drivers:
  - Add a SetViewPort func to GFX
  - Add Keyboard keys and Flasg to Hid
Image:
  - Add Vertical Flipping
  - Add Horizontal flipping
UI7:
  - Fix Critical Bug in IO Viewport handler
  - Fix library list (error on MinGW for some reason)
Lazyvec:
  - Split into multiple source files
  - Generate new functions (see core updates)
This commit is contained in:
2025-07-23 23:21:34 +02:00
parent 31a0c3656f
commit 87910b57de
31 changed files with 1085 additions and 276 deletions

View File

@ -0,0 +1,75 @@
#include <lazyvec.hpp>
constexpr std::string_view _construct = R"text(
constexpr vec{0}() : {1} {{}}
template <typename T1>
constexpr vec{0}(T1 v) {{
{2}
}}
template <typename T1>
constexpr vec{0}(const vec{0}<T1>& v) {{
{3}
}}
constexpr explicit vec{0}({4}) : {5} {{}}{6}
)text";
constexpr std::string_view _extended3 = R"(
// Extended Constructors
template <typename T1>
constexpr explicit vec3(const vec2<T1>& xy, T1 z) {{
x = (T)xy.x;
y = (T)xy.y;
this->z = (T)z;
}}
)";
constexpr std::string_view _extended4 = R"(
// Extended Constructors
template <typename T1>
constexpr explicit vec4(const vec2<T1>& xy, const vec2<T1>& zw) {{
x = (T)xy.x;
y = (T)xy.y;
z = (T)zw.x;
w = (T)zw.y;
}}
template <typename T1>
constexpr explicit vec4(const vec3<T1>& xyz, T1 w) {{
x = (T)xyz.x;
y = (T)xyz.y;
z = (T)xyz.z;
this->w = (T)w;
}}
)";
namespace LVec {
std::string MakeConstructors(int n) {
std::stringstream s1, s2, s3, s4, s5;
for (int i = 0; i < n; i++) {
s1 << elems[i] << "(0)";
s2 << " " << elems[i] << " = (T)v;";
s3 << " " << elems[i] << " = (T)v." << elems[i] << ";";
s4 << "T " << elems[i];
s5 << elems[i] << "(" << elems[i] << ")";
if (i != n - 1) {
s1 << ", ";
s2 << std::endl;
s3 << std::endl;
s4 << ", ";
s5 << ", ";
}
}
std::string extended;
if (n == 3) {
extended = _extended3;
} else if (n == 4) {
extended = _extended4;
}
return std::format(_construct, n, s1.str(), s2.str(), s3.str(), s4.str(),
s5.str(), extended);
}
} // namespace LVec

View File

@ -0,0 +1,50 @@
#include <lazyvec.hpp>
constexpr std::string_view _funcs = R"text(
double Len() const {{ return std::sqrt(SqLen()); }}
double SqLen() const {{ return {1}; }}
template <typename T1>
double Distance(const vec{0}<T1>& v) const {{
return (*this - v).Len();
}}
vec{0}<T> Normalize() const {{
double l = Len();
if(l == 0) {{
return *this;
}}
return *this / (T)l;
}}
template <typename T1>
T Dot(const vec{0}<T1>&v) const {{
return {2};
}}
)text";
constexpr std::string_view _cross = R"text(
template <typename T1>
vec3<T> Cross(const vec3<T1>& v) const {
return vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
)text";
namespace LVec {
std::string MakeFunctions(int n) {
std::stringstream s1, s2;
for (int i = 0; i < n; i++) {
s1 << elems[i] << " * " << elems[i];
s2 << elems[i] << " * (T)v." << elems[i];
if (i != n - 1) {
s1 << " + ";
s2 << " + ";
}
}
std::string ret = std::format(_funcs, n, s1.str(), s2.str());
if (n == 3) {
ret += _cross;
}
return ret;
}
} // namespace LVec

View File

@ -0,0 +1,47 @@
#include <lazyvec.hpp>
constexpr std::string_view _op_template = R"text(
template <typename T1>
vec{0}<T>& operator{1}=(T1 v) {{
{2}
return *this;
}}
template <typename T1>
vec{0}<T>& operator{1}=(const vec{0}<T1>& v) {{
{3}
return *this;
}}
template <typename T1>
vec{0}<T> operator{1}(T1 v) const {{
return vec{0}<T>({4});
}}
template <typename T1>
vec{0}<T> operator{1}(const vec{0}<T1>& v) const {{
return vec{0}<T>({5});
}}
)text";
namespace LVec {
std::string MakeOperationFor(char op, int n) {
const std::string& toff = " ";
// Create for streams for the operations functions
std::stringstream s1, s2, s3, s4;
for (int i = 0; i < n; i++) {
s1 << toff << elems[i] << " " << op << "= (T)v;";
s2 << toff << elems[i] << " " << op << "= (T)v." << elems[i] << ";";
s3 << elems[i] << " " << op << " (T)v";
s4 << elems[i] << " " << op << " (T)v." << elems[i];
if (i != n - 1) {
s1 << std::endl;
s2 << std::endl;
s3 << ", ";
s4 << ", ";
}
}
return std::format(_op_template, n, op, s1.str(), s2.str(), s3.str(),
s4.str());
}
} // namespace LVec

View File

@ -0,0 +1,30 @@
#include <lazyvec.hpp>
/**
* Diffrence between lazyoperation.cpp and lazyops.cpp ?
* One is for Operators with an input sym like +, -, * or /
* the other (this) is for generic operations
*/
constexpr std::string_view _generic_ops = R"text(
vec{0} operator-() const {{ return vec{0}({1}); }}
template <typename T1>
bool operator==(const vec{0}<T1>& v) const {{ return {2}; }}
template <typename T1>
bool operator!=(const vec{0}<T1>& v) const {{ return !(*this == v); }}
)text";
namespace LVec {
std::string GenericOperations(int n) {
std::stringstream s1, s2;
for (int i = 0; i < n; i++) {
s1 << "-" << elems[i];
s2 << elems[i] << " == (T)v." << elems[i];
if (i != n - 1) {
s1 << ", ";
s2 << " && ";
}
}
return std::format(_generic_ops, n, s1.str(), s2.str());
}
} // namespace LVec

View File

@ -0,0 +1,25 @@
#include <lazyvec.hpp>
#include <unordered_set>
namespace LVec {
std::string MakeSwap(int n) {
std::stringstream s;
std::unordered_set<std::string> done;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::string a = elems[i];
std::string b = elems[j];
/** Make sure we generate nothing twice */
if (a == b || done.count(b + a)) {
continue;
}
s << " void Swap" << (char)toupper(a[0]) << (char)toupper(b[0])
<< "() {\n";
s << " T t = " << a << ";\n " << a << " = " << b << ";\n";
s << " " << b << " = t;\n }\n";
done.insert(a + b);
}
}
return s.str();
}
} // namespace LVec

View File

@ -0,0 +1,77 @@
#include <lazyvec.hpp>
constexpr std::string_view _template = R"text(#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file is generated by lazyvec 2.0.0
#include <pd/core/common.hpp>
{7}
namespace PD {{
template <typename T>
class vec{0} {{
public:
{1}
// Constructors
{2}
// Operations
{3}
// Generic Operations
{4}
// Functions
{5}
// Swap Functions
{6}
}};
using fvec{0} = vec{0}<float>;
using ivec{0} = vec{0}<int>;
using dvec{0} = vec{0}<double>;
}} // namespace PD
)text";
namespace LVec {
std::string GenerateHeader(int n) {
std::stringstream ops, data, extended;
for (int i = 0; i < n; i++) {
data << "T " << elems[i] << ";" << std::endl;
}
ops << MakeOperationFor('+', n);
ops << MakeOperationFor('-', n);
ops << MakeOperationFor('*', n);
ops << MakeOperationFor('/', n);
if (n > 2) {
extended << "// Extended includes (rename if you use other filenames/paths)"
<< std::endl;
extended << "#include <vec2.hpp>" << std::endl;
if (n == 4) {
extended << "#include <vec3.hpp>" << std::endl;
}
}
return std::format(_template, n, data.str(), MakeConstructors(n), ops.str(),
GenericOperations(n), MakeFunctions(n), MakeSwap(n),
extended.str());
}
} // namespace LVec

View File

@ -21,194 +21,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
const char* license_text = R"(/*
MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
)";
const std::vector<std::string> elems = {"x", "y", "z", "w"};
void MakeOperationFor(std::fstream& off, char op, int n) {
off << " template <typename T1>\n";
off << " vec" << n << "<T>& operator" << op << "=(T1 v) {\n";
for (int i = 0; i < n; i++) {
off << " " << elems[i] << " " << op << "= (T)v;\n";
}
off << " return *this;\n";
off << " }\n\n";
off << " template <typename T1>\n";
off << " vec" << n << "<T>& operator" << op << "=(const vec" << n
<< "<T1>& v) {\n";
for (int i = 0; i < n; i++) {
off << " " << elems[i] << " " << op << "= (T)v." << elems[i] << ";\n";
}
off << " return *this;\n";
off << " }\n\n";
off << " template <typename T1>\n";
off << " vec" << n << "<T> operator" << op << "(T1 v) const {\n";
off << " return vec" << n << "<T>(";
for (int i = 0; i < n; i++) {
if (i > 0) off << ", ";
off << elems[i] << " " << op << " (T)v";
}
off << ");\n }\n\n";
off << " template <typename T1>\n";
off << " vec" << n << "<T> operator" << op << "(const vec" << n
<< "<T1>& v) const {\n";
off << " return vec" << n << "<T>(";
for (int i = 0; i < n; i++) {
if (i > 0) off << ", ";
off << elems[i] << " " << op << " (T)v." << elems[i];
}
off << ");\n }\n\n";
}
void SwapHaxx(std::fstream& off, int n) {
std::unordered_set<std::string> done;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::string a = elems[i];
std::string b = elems[j];
/** Make sure we generate nothing twice */
if (a == b || done.count(b + a)) {
continue;
}
off << " void Swap" << (char)toupper(a[0]) << (char)toupper(b[0])
<< "() {\n";
off << " T t = " << a << ";\n " << a << " = " << b << ";\n";
off << " " << b << " = t;\n }\n";
done.insert(a + b);
}
}
}
void GenerateVec(int n) {
if (n < 2 || n > 4) {
std::cout << "Only 2 to 4 supported.\n";
return;
}
std::fstream off("vec" + std::to_string(n) + ".hpp", std::ios::out);
off << "#pragma once\n" << std::endl;
off << license_text << std::endl;
off << "// This file is generated by lazyvec\n#include "
"<pd/core/common.hpp>\n\n";
off << "namespace PD {" << std::endl;
off << "template <typename T>\nclass vec" << n << " {\npublic:\n";
for (int i = 0; i < n; i++) {
off << " T " << elems[i] << ";\n";
}
off << "\n";
off << " vec" << n << "(): ";
for (int i = 0; i < n; i++) {
if (i > 0) {
off << ", ";
}
off << elems[i] << "(0)";
}
off << " {}" << std::endl;
// Magic Construtor (support for anytype vec)
off << " template <typename T1>\n";
off << " explicit vec" << n << "(T1 v) {\n";
for (int i = 0; i < n; i++) {
off << " " << elems[i] << " = (T)v;\n";
}
off << " }\n\n";
// Magic Constructor 2
off << " template <typename T1>\n";
off << " explicit vec" << n << "(vec" << n << "<T1> v) {\n";
for (int i = 0; i < n; i++) {
off << " " << elems[i] << " = (T)v. " << elems[i] << ";\n";
}
off << " }\n\n";
off << " vec" << n << "(";
for (int i = 0; i < n; i++) {
if (i > 0) off << ", ";
off << "T " << elems[i];
}
off << ") : ";
for (int i = 0; i < n; i++) {
if (i > 0) off << ", ";
off << elems[i] << "(" << elems[i] << ")";
}
off << " {}\n\n";
MakeOperationFor(off, '+', n);
MakeOperationFor(off, '-', n);
MakeOperationFor(off, '*', n);
MakeOperationFor(off, '/', n);
off << " vec" << n << " operator-() const {return vec" << n << "(";
for (int i = 0; i < n; i++) {
off << "-" << elems[i];
if (i != n - 1) {
off << ", ";
}
}
off << ");}\n\n";
off << " bool operator==(const vec" << n << "& v) const { return ";
for (int i = 0; i < n; i++) {
off << elems[i] << " == v." << elems[i];
if (i != n - 1) {
off << " && ";
}
}
off << ";}\n";
off << " bool operator!=(const vec" << n
<< "&v) const { return !(*this == v); }\n\n";
off << " double Len() const {return std::sqrt(SqLen()); }\n";
off << " double SqLen() const { return ";
for (int i = 0; i < n; i++) {
off << elems[i] << " * " << elems[i];
if (i != n - 1) {
off << " + ";
}
}
off << "; }\n\n";
SwapHaxx(off, n);
off << "};\n";
off << "using fvec" << n << " = vec" << n << "<float>;\n";
off << "using dvec" << n << " = vec" << n << "<double>;\n";
off << "using ivec" << n << " = vec" << n << "<int>;\n";
off << "}\n";
off.close();
}
/**
* Yet another Stupid Code generation tool
* Why ?
@ -216,11 +28,21 @@ void GenerateVec(int n) {
* manually writeup vec2 to vec4
*/
#include <iostream>
#include <lazyvec.hpp>
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cout << argv[0] << " <num (2 to 4)>" << std::endl;
return 0;
}
GenerateVec(std::stoi(argv[1]));
int l = std::stoi(argv[1]);
if (l < 2 || l > 4) {
std::cout << argv[0] << " <num (2 to 4)>" << std::endl;
return 0;
}
std::ofstream f("vec" + std::to_string(l) + ".hpp");
f << LVec::GenerateHeader(l);
f.close();
return 0;
}