summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-01-13 15:09:38 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2024-01-13 15:09:38 +0000
commit3529eba77e2e5af672369e73d73c1084616c10e9 (patch)
treeeda097c081a6c15945f5a681afbc39bba1f33741
parentUse std::span for iterator/data pointer in glContainer (diff)
downloadilt-3529eba77e2e5af672369e73d73c1084616c10e9.tar.bz2
ilt-3529eba77e2e5af672369e73d73c1084616c10e9.tar.xz
ilt-3529eba77e2e5af672369e73d73c1084616c10e9.zip
Add glContainer::at override to update a single item
-rw-r--r--lib/glContainer.h16
-rw-r--r--test/test-glContainer.cpp23
2 files changed, 39 insertions, 0 deletions
diff --git a/lib/glContainer.h b/lib/glContainer.h
index e5f53e2..ce88916 100644
--- a/lib/glContainer.h
+++ b/lib/glContainer.h
@@ -136,6 +136,22 @@ public:
return size_;
}
+ void
+ at(size_type pos, const T & value)
+ {
+ if (pos >= size()) {
+ throw std::out_of_range {__FUNCTION__};
+ }
+ if (data_.data()) {
+ data_[pos] = value;
+ }
+ else {
+ glBindBuffer(GL_ARRAY_BUFFER, buffer_);
+ glBufferSubData(GL_ARRAY_BUFFER, static_cast<GLintptr>(pos * sizeof(T)), sizeof(value), &value);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ }
+ }
+
[[nodiscard]] reference_type
at(size_type pos)
{
diff --git a/test/test-glContainer.cpp b/test/test-glContainer.cpp
index 9d4f198..0597470 100644
--- a/test/test-glContainer.cpp
+++ b/test/test-glContainer.cpp
@@ -204,6 +204,29 @@ BOOST_AUTO_TEST_CASE(random_access)
BOOST_CHECK_EQUAL(1, *i);
}
+BOOST_AUTO_TEST_CASE(random_write)
+{
+ BOOST_CHECK_NO_THROW(resize(3));
+ BOOST_CHECK_EQUAL(size(), 3);
+ BOOST_CHECK_NO_THROW(unmap());
+ BOOST_REQUIRE(!data_.data());
+ BOOST_CHECK_NO_THROW(at(0, 10));
+ BOOST_CHECK_NO_THROW(at(1, 20));
+ BOOST_CHECK_NO_THROW(at(2, 30));
+ BOOST_CHECK(!data_.data());
+ {
+ std::array expected1 {10, 20, 30};
+ BOOST_CHECK_EQUAL_COLLECTIONS(begin(), end(), expected1.begin(), expected1.end());
+ }
+ BOOST_CHECK(data_.data());
+ BOOST_CHECK_NO_THROW(at(1, 40));
+ {
+ std::array expected1 {10, 40, 30};
+ BOOST_CHECK_EQUAL_COLLECTIONS(begin(), end(), expected1.begin(), expected1.end());
+ }
+ BOOST_CHECK_THROW(at(4, 0), std::out_of_range);
+}
+
BOOST_AUTO_TEST_CASE(insert_remove_test)
{
BOOST_CHECK_NO_THROW(emplace_back(1));