From 3529eba77e2e5af672369e73d73c1084616c10e9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 15:09:38 +0000 Subject: Add glContainer::at override to update a single item --- lib/glContainer.h | 16 ++++++++++++++++ test/test-glContainer.cpp | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) 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(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)); -- cgit v1.2.3