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 <X11/Xlib.h>
#include <array>
#include <iostream>
#include <cmath>
#include <cstdlib>
@ -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<GLfloat, 216> 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;

View File

@ -8,6 +8,7 @@
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <array>
#include <deque>
#include <iostream>
#include <mutex>
@ -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<Setting, 9> 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());

View File

@ -4,6 +4,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <array>
#include <sstream>
#include <string>
#include <unordered_map>
@ -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<typename T>

View File

@ -3,6 +3,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <array>
#include <iostream>
#include <cstdlib>
@ -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<GLfloat, 180> 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);

View File

@ -9,6 +9,7 @@
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <array>
#include <iostream>
#include <limits>
#include <vector>
@ -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<std::uint16_t, 36> 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);

View File

@ -11,6 +11,7 @@
#include <SFML/Main.hpp>
#endif
#include <array>
#include <iostream>
#include <cstdlib>
@ -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<GLfloat, 252> 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);