Use std::array

This commit is contained in:
Chris Thrasher 2022-06-12 10:41:53 -06:00 committed by Lukas Dürrenberger
parent 27a7774786
commit 539483d329
6 changed files with 27 additions and 22 deletions

View File

@ -8,6 +8,7 @@
#include <gl.h> #include <gl.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <array>
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
@ -93,7 +94,7 @@
glRotatef(elapsedTime * 18.f, 0.f, 0.f, 1.f); glRotatef(elapsedTime * 18.f, 0.f, 0.f, 1.f);
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
static const GLfloat cube[] = constexpr std::array<GLfloat, 216> cube =
{ {
// positions // colors // positions // colors
-50, -50, -50, 1, 1, 0, -50, -50, -50, 1, 1, 0,
@ -140,8 +141,8 @@
}; };
// Draw the cube // Draw the cube
glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube); glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data());
glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube + 3); glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data() + 3);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
return true; return true;

View File

@ -8,6 +8,7 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <algorithm> #include <algorithm>
#include <array>
#include <deque> #include <deque>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
@ -161,8 +162,8 @@ int main()
statusText.setPosition({(windowWidth - statusText.getLocalBounds().width) / 2.f, (windowHeight - statusText.getLocalBounds().height) / 2.f}); 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 // Set up an array of pointers to our settings for arrow navigation
Setting settings[] = constexpr std::array<Setting, 9> settings =
{ {{
{"perlinFrequency", &perlinFrequency}, {"perlinFrequency", &perlinFrequency},
{"perlinFrequencyBase", &perlinFrequencyBase}, {"perlinFrequencyBase", &perlinFrequencyBase},
{"heightBase", &heightBase}, {"heightBase", &heightBase},
@ -172,10 +173,9 @@ int main()
{"heightFactor", &heightFactor}, {"heightFactor", &heightFactor},
{"heightFlatten", &heightFlatten}, {"heightFlatten", &heightFlatten},
{"lightFactor", &lightFactor} {"lightFactor", &lightFactor}
}; }};
const int settingCount = 9; std::size_t currentSetting = 0;
int currentSetting = 0;
std::ostringstream osstr; std::ostringstream osstr;
sf::Clock clock; sf::Clock clock;
@ -199,8 +199,8 @@ int main()
switch (event.key.code) switch (event.key.code)
{ {
case sf::Keyboard::Enter: generateTerrain(terrainStagingBuffer.data()); break; case sf::Keyboard::Enter: generateTerrain(terrainStagingBuffer.data()); break;
case sf::Keyboard::Down: currentSetting = (currentSetting + 1) % settingCount; break; case sf::Keyboard::Down: currentSetting = (currentSetting + 1) % settings.size(); break;
case sf::Keyboard::Up: currentSetting = (currentSetting + settingCount - 1) % settingCount; 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::Left: *(settings[currentSetting].value) -= 0.1f; break;
case sf::Keyboard::Right: *(settings[currentSetting].value) += 0.1f; break; case sf::Keyboard::Right: *(settings[currentSetting].value) += 0.1f; break;
default: break; default: break;
@ -244,7 +244,7 @@ int main()
<< "perlinOctaves: " << perlinOctaves << "\n\n" << "perlinOctaves: " << perlinOctaves << "\n\n"
<< "Use the arrow keys to change the values.\nUse the return key to regenerate the terrain.\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'; osstr << ((i == currentSetting) ? ">> " : " ") << settings[i].name << ": " << *(settings[i].value) << '\n';
hudText.setString(osstr.str()); hudText.setString(osstr.str());

View File

@ -4,6 +4,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <algorithm> #include <algorithm>
#include <array>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@ -23,7 +24,7 @@ namespace
float threshold = 0.1f; float threshold = 0.1f;
// Axes labels in as C strings // 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 // Helper to set text entries to a specified value
template<typename T> template<typename T>

View File

@ -3,6 +3,7 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <array>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
@ -124,7 +125,7 @@ int main()
sf::Texture::bind(&texture); sf::Texture::bind(&texture);
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
static const GLfloat cube[] = constexpr std::array<GLfloat, 180> cube =
{ {
// positions // texture coordinates // positions // texture coordinates
-20, -20, -20, 0, 0, -20, -20, -20, 0, 0,
@ -173,8 +174,8 @@ int main()
// Enable position and texture coordinates vertex components // Enable position and texture coordinates vertex components
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube); glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube.data());
glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube + 3); glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube.data() + 3);
// Disable normal and color vertex components // Disable normal and color vertex components
glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_NORMAL_ARRAY);

View File

@ -9,6 +9,7 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include <array>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <vector> #include <vector>
@ -1370,7 +1371,7 @@ public:
// Create our vertex buffer and upload its data // Create our vertex buffer and upload its data
void setupVertexBuffer() void setupVertexBuffer()
{ {
float vertexData[] = { constexpr std::array vertexData = {
// X Y Z R G B A U V // 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, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.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 // Copy the vertex data into the buffer
std::memcpy(ptr, vertexData, sizeof(vertexData)); std::memcpy(ptr, vertexData.data(), sizeof(vertexData));
// Unmap the buffer // Unmap the buffer
vkUnmapMemory(device, stagingBufferMemory); vkUnmapMemory(device, stagingBufferMemory);
@ -1464,7 +1465,7 @@ public:
// Create our index buffer and upload its data // Create our index buffer and upload its data
void setupIndexBuffer() void setupIndexBuffer()
{ {
uint16_t indexData[] = { constexpr std::array<std::uint16_t, 36> indexData = {
0, 1, 2, 0, 1, 2,
2, 3, 0, 2, 3, 0,
@ -1513,7 +1514,7 @@ public:
} }
// Copy the index data into the buffer // Copy the index data into the buffer
std::memcpy(ptr, indexData, sizeof(indexData)); std::memcpy(ptr, indexData.data(), sizeof(indexData));
// Unmap the buffer // Unmap the buffer
vkUnmapMemory(device, stagingBufferMemory); vkUnmapMemory(device, stagingBufferMemory);

View File

@ -11,6 +11,7 @@
#include <SFML/Main.hpp> #include <SFML/Main.hpp>
#endif #endif
#include <array>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
@ -73,7 +74,7 @@ int main()
#endif #endif
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
GLfloat cube[] = constexpr std::array<GLfloat, 252> cube =
{ {
// positions // colors (r, g, b, a) // positions // colors (r, g, b, a)
-50, -50, -50, 0, 0, 1, 1, -50, -50, -50, 0, 0, 1, 1,
@ -122,8 +123,8 @@ int main()
// Enable position and color vertex components // Enable position and color vertex components
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), cube); glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), cube.data());
glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), cube + 3); glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), cube.data() + 3);
// Disable normal and texture coordinates vertex components // Disable normal and texture coordinates vertex components
glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_NORMAL_ARRAY);