From 539483d329151bf6d63f024474541db5361305d5 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 12 Jun 2022 10:41:53 -0600 Subject: [PATCH] Use `std::array` --- examples/X11/X11.cpp | 7 ++++--- examples/island/Island.cpp | 16 ++++++++-------- examples/joystick/Joystick.cpp | 3 ++- examples/opengl/OpenGL.cpp | 7 ++++--- examples/vulkan/Vulkan.cpp | 9 +++++---- examples/window/Window.cpp | 7 ++++--- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/examples/X11/X11.cpp b/examples/X11/X11.cpp index f6a4b9d3..624df662 100644 --- a/examples/X11/X11.cpp +++ b/examples/X11/X11.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -93,7 +94,7 @@ glRotatef(elapsedTime * 18.f, 0.f, 0.f, 1.f); // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) - static const GLfloat cube[] = + constexpr std::array cube = { // positions // colors -50, -50, -50, 1, 1, 0, @@ -140,8 +141,8 @@ }; // Draw the cube - glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube); - glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube + 3); + glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data()); + glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data() + 3); glDrawArrays(GL_TRIANGLES, 0, 36); return true; diff --git a/examples/island/Island.cpp b/examples/island/Island.cpp index da0d9792..9a0f95ec 100644 --- a/examples/island/Island.cpp +++ b/examples/island/Island.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -161,8 +162,8 @@ int main() statusText.setPosition({(windowWidth - statusText.getLocalBounds().width) / 2.f, (windowHeight - statusText.getLocalBounds().height) / 2.f}); // Set up an array of pointers to our settings for arrow navigation - Setting settings[] = - { + constexpr std::array settings = + {{ {"perlinFrequency", &perlinFrequency}, {"perlinFrequencyBase", &perlinFrequencyBase}, {"heightBase", &heightBase}, @@ -172,10 +173,9 @@ int main() {"heightFactor", &heightFactor}, {"heightFlatten", &heightFlatten}, {"lightFactor", &lightFactor} - }; + }}; - const int settingCount = 9; - int currentSetting = 0; + std::size_t currentSetting = 0; std::ostringstream osstr; sf::Clock clock; @@ -199,8 +199,8 @@ int main() switch (event.key.code) { case sf::Keyboard::Enter: generateTerrain(terrainStagingBuffer.data()); break; - case sf::Keyboard::Down: currentSetting = (currentSetting + 1) % settingCount; break; - case sf::Keyboard::Up: currentSetting = (currentSetting + settingCount - 1) % settingCount; break; + case sf::Keyboard::Down: currentSetting = (currentSetting + 1) % settings.size(); break; + case sf::Keyboard::Up: currentSetting = (currentSetting + settings.size() - 1) % settings.size(); break; case sf::Keyboard::Left: *(settings[currentSetting].value) -= 0.1f; break; case sf::Keyboard::Right: *(settings[currentSetting].value) += 0.1f; break; default: break; @@ -244,7 +244,7 @@ int main() << "perlinOctaves: " << perlinOctaves << "\n\n" << "Use the arrow keys to change the values.\nUse the return key to regenerate the terrain.\n\n"; - for (int i = 0; i < settingCount; ++i) + for (std::size_t i = 0; i < settings.size(); ++i) osstr << ((i == currentSetting) ? ">> " : " ") << settings[i].name << ": " << *(settings[i].value) << '\n'; hudText.setString(osstr.str()); diff --git a/examples/joystick/Joystick.cpp b/examples/joystick/Joystick.cpp index 90c7fa6c..154c7a8f 100644 --- a/examples/joystick/Joystick.cpp +++ b/examples/joystick/Joystick.cpp @@ -4,6 +4,7 @@ //////////////////////////////////////////////////////////// #include #include +#include #include #include #include @@ -23,7 +24,7 @@ namespace float threshold = 0.1f; // Axes labels in as C strings - const char* axislabels[] = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"}; + constexpr std::array axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"}; // Helper to set text entries to a specified value template diff --git a/examples/opengl/OpenGL.cpp b/examples/opengl/OpenGL.cpp index 2a77aad7..e838e427 100644 --- a/examples/opengl/OpenGL.cpp +++ b/examples/opengl/OpenGL.cpp @@ -3,6 +3,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include @@ -124,7 +125,7 @@ int main() sf::Texture::bind(&texture); // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) - static const GLfloat cube[] = + constexpr std::array cube = { // positions // texture coordinates -20, -20, -20, 0, 0, @@ -173,8 +174,8 @@ int main() // Enable position and texture coordinates vertex components glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube); - glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube + 3); + glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube.data()); + glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube.data() + 3); // Disable normal and color vertex components glDisableClientState(GL_NORMAL_ARRAY); diff --git a/examples/vulkan/Vulkan.cpp b/examples/vulkan/Vulkan.cpp index 12ffa131..3f286ed2 100644 --- a/examples/vulkan/Vulkan.cpp +++ b/examples/vulkan/Vulkan.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -1370,7 +1371,7 @@ public: // Create our vertex buffer and upload its data void setupVertexBuffer() { - float vertexData[] = { + constexpr std::array vertexData = { // X Y Z R G B A U V -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, @@ -1432,7 +1433,7 @@ public: } // Copy the vertex data into the buffer - std::memcpy(ptr, vertexData, sizeof(vertexData)); + std::memcpy(ptr, vertexData.data(), sizeof(vertexData)); // Unmap the buffer vkUnmapMemory(device, stagingBufferMemory); @@ -1464,7 +1465,7 @@ public: // Create our index buffer and upload its data void setupIndexBuffer() { - uint16_t indexData[] = { + constexpr std::array indexData = { 0, 1, 2, 2, 3, 0, @@ -1513,7 +1514,7 @@ public: } // Copy the index data into the buffer - std::memcpy(ptr, indexData, sizeof(indexData)); + std::memcpy(ptr, indexData.data(), sizeof(indexData)); // Unmap the buffer vkUnmapMemory(device, stagingBufferMemory); diff --git a/examples/window/Window.cpp b/examples/window/Window.cpp index fe5e0cdc..4526f20e 100644 --- a/examples/window/Window.cpp +++ b/examples/window/Window.cpp @@ -11,6 +11,7 @@ #include #endif +#include #include #include @@ -73,7 +74,7 @@ int main() #endif // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) - GLfloat cube[] = + constexpr std::array cube = { // positions // colors (r, g, b, a) -50, -50, -50, 0, 0, 1, 1, @@ -122,8 +123,8 @@ int main() // Enable position and color vertex components glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); - glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), cube); - glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), cube + 3); + glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), cube.data()); + glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), cube.data() + 3); // Disable normal and texture coordinates vertex components glDisableClientState(GL_NORMAL_ARRAY);