summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan.goodliffe@octal.co.uk>2026-04-01 12:07:58 +0100
committerDan Goodliffe <dan.goodliffe@octal.co.uk>2026-04-01 12:07:58 +0100
commit56207fbf4e8662b6cf93632193ba68d2576c2d4e (patch)
treedec327a00b301149e76f691728053c7ef5bb9119
parentMove light vertex structs out (diff)
downloadilt-56207fbf4e8662b6cf93632193ba68d2576c2d4e.tar.bz2
ilt-56207fbf4e8662b6cf93632193ba68d2576c2d4e.tar.xz
ilt-56207fbf4e8662b6cf93632193ba68d2576c2d4e.zip
Helper to instantiate transient static values as required
-rw-r--r--game/scenary/foliage.cpp7
-rw-r--r--game/scenary/illuminator.cpp4
-rw-r--r--gfx/renderable.cpp5
-rw-r--r--lib/util.h12
4 files changed, 19 insertions, 9 deletions
diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp
index 5902e09..f27ac26 100644
--- a/game/scenary/foliage.cpp
+++ b/game/scenary/foliage.cpp
@@ -4,6 +4,7 @@
#include "gfx/gl/sceneShader.h"
#include "gfx/gl/shadowMapper.h"
#include "gfx/gl/shadowStenciller.h"
+#include "util.h"
#include <location.h>
static_assert(std::is_constructible_v<Foliage>);
@@ -40,12 +41,10 @@ Foliage::postLoad()
{
texture = getTexture();
glDebugScope _ {0};
- if (!(instanceVAO = commonInstanceVAO.lock())) {
- commonInstanceVAO = instanceVAO = std::make_shared<glVertexArray>();
+ if (createIfRequired(instanceVAO, commonInstanceVAO)) {
bodyMesh->configureVAO(*instanceVAO, 0).addAttribs<InstanceVertex, &InstanceVertex::location>(1);
}
- if (!(instancePointVAO = commonInstancePointVAO.lock())) {
- commonInstancePointVAO = instancePointVAO = std::make_shared<glVertexArray>();
+ if (createIfRequired(instancePointVAO, commonInstancePointVAO)) {
instancePointVAO->configure().addAttribs<InstanceVertex, &InstanceVertex::location>(0);
}
const auto & size = bodyMesh->getDimensions().size;
diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp
index e991baa..3b73cd3 100644
--- a/game/scenary/illuminator.cpp
+++ b/game/scenary/illuminator.cpp
@@ -1,6 +1,7 @@
#include "illuminator.h"
#include "gfx/gl/sceneShader.h"
#include "gfx/models/texture.h" // IWYU pragma: keep
+#include "util.h"
#include <location.h>
static_assert(std::is_constructible_v<Illuminator>);
@@ -44,8 +45,7 @@ Illuminator::postLoad()
}
texture = getTexture();
glDebugScope _ {0};
- if (!(instanceVAO = commonInstanceVAO.lock())) {
- commonInstanceVAO = instanceVAO = std::make_shared<glVertexArray>();
+ if (createIfRequired(instanceVAO, commonInstanceVAO)) {
bodyMesh->configureVAO(*instanceVAO, 0).addAttribs<InstanceVertex, &InstanceVertex::location>(1);
}
if (!spotLight.empty()) {
diff --git a/gfx/renderable.cpp b/gfx/renderable.cpp
index 4597597..90035c3 100644
--- a/gfx/renderable.cpp
+++ b/gfx/renderable.cpp
@@ -2,6 +2,7 @@
#include "gl_traits.h"
#include "location.h"
#include "maths.h"
+#include "util.h"
std::weak_ptr<Renderable::CommonLocationData> Renderable::commonLocationData;
@@ -21,9 +22,7 @@ Renderable::CommonLocation ::operator=(Location const & location)
Renderable::Renderable()
{
- if (!(locationData = commonLocationData.lock())) {
- commonLocationData = locationData = std::make_shared<CommonLocationData>();
- }
+ createIfRequired(locationData, commonLocationData);
}
GLuint
diff --git a/lib/util.h b/lib/util.h
index cd7971b..b60b093 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -3,6 +3,7 @@
#include <algorithm> // IWYU pragma: keep
#include <array>
#include <cstddef>
+#include <memory>
#include <tuple>
template<typename T, std::size_t N>
@@ -33,3 +34,14 @@ template<size_t... N> inline constexpr auto Nth = GetNth<N...> {};
inline constexpr auto GetFirst = Nth<0>;
inline constexpr auto GetSecond = Nth<1>;
inline constexpr auto GetSwapped = Nth<0, 1>;
+
+template<typename T>
+bool
+createIfRequired(std::shared_ptr<T> & instance, std::weak_ptr<T> & common)
+{
+ if (!instance && !(instance = common.lock())) {
+ common = instance = std::make_shared<T>();
+ return true;
+ }
+ return false;
+}