From 270a0488d61dc096def5d4a662e188ccd17c3163 Mon Sep 17 00:00:00 2001 From: Armin <armin.co@hs-bochum.de> Date: Sun, 31 Jan 2021 12:36:33 +0100 Subject: [PATCH] Refactoring done --- App/GameLayer.cpp | 65 ++++++++++++++++++++++++++++------------- App/GameLayer.hpp | 2 +- App/Window.cpp | 4 +-- CMakeLists.txt | 4 +-- GuiCore/Application.cpp | 2 +- GuiCore/ImGuiLayer.cpp | 12 +------- GuiCore/ImGuiLayer.hpp | 1 - QPong/Bat.cpp | 10 +++++++ QPong/Bat.hpp | 2 ++ QPong/Particle.cpp | 4 +++ 10 files changed, 66 insertions(+), 40 deletions(-) diff --git a/App/GameLayer.cpp b/App/GameLayer.cpp index d6b7e6b..0fd7b14 100644 --- a/App/GameLayer.cpp +++ b/App/GameLayer.cpp @@ -28,26 +28,52 @@ constexpr QPong::Particle::Properties initialParticleSettings = { .sharpness = 64.0, .startValue = 0.01, .hbar = 0.000125, - .threads = 8, - .game = true + .threads = 8 }; constexpr QPong::Particle::GameOptions initialOptions = { .potentialsEnabled = true, .absorbtionEnabled = true, - .momentumEnabled = false + .momentumEnabled = false, + .game = true }; +constexpr int pointsPerPosition { 3 }; +constexpr int indicesPerQuad { 6 }; // two triangles + QPong::GameLayer::GameLayer(int resolution, int threads) :Layer("GameLayer") - , m_arraySize{resolution} + , m_properties {initialParticleSettings} + , m_options{initialOptions} + , m_particle {nullptr} , m_leftBat(-0.8, 0.0, Key::W, Key::S) , m_rightBat(0.8, 0.0, Key::Up, Key::Down) - , m_propagate{false} - , m_options{initialOptions} - , m_properties {initialParticleSettings} - , m_speedScale{5.0f} + , m_playerOne() + , m_playerTwo() + , m_arraySize{resolution} + , m_numberOfQuads {(m_arraySize - 1) * (m_arraySize - 1)} + , m_numberOfIndices {m_numberOfQuads * indicesPerQuad} + , m_memorySize {0} + , m_shader {nullptr} + , m_vertexArray {0} + , m_pointsBuffer {0} + , m_indicesBuffer {0} + , m_colorBuffer {0} + , m_potentialBuffer {0} + , m_particleViewPositions {nullptr} + , m_momentumViewPositions {nullptr} + , m_staticBorderPotential {nullptr} + , m_dynamicPotential {nullptr} + , m_isMomentumEnabled {0} + , m_viewProjectionLocation {0} + , m_showPotentialsLocation {0} + , m_viewProjectionPosition() + , m_gameCounts {true} + , m_restart {false} + , m_propagate {false} , m_randomMomentum {true} + , m_speedScale{5.0f} + , m_openGlTimer {0.0} , m_gaming {true} { srand(time(nullptr)); @@ -56,10 +82,6 @@ QPong::GameLayer::GameLayer(int resolution, int threads) m_properties.threads = threads; m_memorySize = sizeof(fftw_complex) * m_properties.elements(); - constexpr int indicesPerQuad { 6 }; // two triangles - m_numberOfQuads = (m_arraySize - 1) * (m_arraySize - 1); - m_numberOfIndices = m_numberOfQuads * indicesPerQuad; - m_staticBorderPotential = new float[m_properties.elements()]; m_dynamicPotential = new float[m_properties.elements()]; m_particle = std::make_unique<Particle>( @@ -82,7 +104,6 @@ void QPong::GameLayer::onAttach() fragmentShaderPath); // static verticie positions - constexpr int pointsPerPosition { 3 }; m_particleViewPositions = new float[m_properties.elements() * pointsPerPosition]; calculatePointPositions(m_particleViewPositions, m_arraySize); @@ -113,7 +134,7 @@ void QPong::GameLayer::onAttach() glVertexAttribPointer(1, 2, GL_DOUBLE, GL_FALSE, sizeof(fftw_complex), 0); // Setup momentum view - m_momentumViewPositions = new float[m_properties.elements() * 3]; + m_momentumViewPositions = new float[m_properties.elements() * pointsPerPosition]; calculateMomentumViewPositions(m_momentumViewPositions, m_arraySize); glGenBuffers(1, &m_potentialBuffer); @@ -161,6 +182,9 @@ void QPong::GameLayer::reset() m_properties.momentum.y = (1.0f - (2.0f * positive)) * (1.0 + static_cast<float>(yRand) / 120.0f); } m_options.game = m_gaming; + m_options.absorbtionEnabled = m_gaming; + m_options.momentumEnabled = !m_gaming; + m_particle.reset(new Particle( m_properties, m_options, @@ -173,7 +197,8 @@ void QPong::GameLayer::reset() m_restart = false; m_gameCounts = true; - m_particle->update(0.0); + constexpr double veryLittleInitialStep(0.00025); + m_particle->update(veryLittleInitialStep); } void QPong::GameLayer::onUpdate(GuiCore::Timestep ts) @@ -210,7 +235,7 @@ void QPong::GameLayer::onUpdate(GuiCore::Timestep ts) GuiCore::Timer openGlTimer{"", false}; // Particle glBindBuffer(GL_ARRAY_BUFFER, m_pointsBuffer); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * m_properties.elements() * 3, m_particleViewPositions); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * m_properties.elements() * pointsPerPosition, m_particleViewPositions); glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer); glBufferSubData(GL_ARRAY_BUFFER, 0, m_memorySize, m_particle->getPsi()); @@ -229,7 +254,7 @@ void QPong::GameLayer::onUpdate(GuiCore::Timestep ts) if (m_options.momentumEnabled) { glBindBuffer(GL_ARRAY_BUFFER, m_pointsBuffer); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * m_properties.elements() * 3, m_momentumViewPositions); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * m_properties.elements() * pointsPerPosition, m_momentumViewPositions); glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer); glBufferSubData(GL_ARRAY_BUFFER, 0, m_memorySize, m_particle->getMomentum()); @@ -305,17 +330,17 @@ void QPong::GameLayer::onGuiRender() } ImGui::Text("Player 1: %d", m_playerOne.getTotalScore()); ImGui::SameLine(); - ImGui::Text(" | Live Score: %.1f\%%", m_playerOne.getCurrentGameScore()); + ImGui::Text(" | Live Score: %.1f%%", m_playerOne.getCurrentGameScore()); ImGui::Text("Player 2: %d", m_playerTwo.getTotalScore()); ImGui::SameLine(); - ImGui::Text(" | Live Score: %.1f\%%", m_playerTwo.getCurrentGameScore()); + ImGui::Text(" | Live Score: %.1f%%", m_playerTwo.getCurrentGameScore()); ImGui::Spacing(); ImGui::Text("FFT took %0.1lfms", m_particle->getTimeFFT()); ImGui::Text("OpenGL took %.1lfms", m_openGlTimer); - if (ImGui::Checkbox("Gaming", &m_gaming)) + if (ImGui::Checkbox("Toggle Gaming | Simulation", &m_gaming)) { m_propagate = false; m_randomMomentum = false; diff --git a/App/GameLayer.hpp b/App/GameLayer.hpp index a7d3ada..e94e4f5 100644 --- a/App/GameLayer.hpp +++ b/App/GameLayer.hpp @@ -68,7 +68,7 @@ namespace QPong bool m_restart; bool m_propagate; bool m_randomMomentum; - float m_speedScale{1.0f}; + float m_speedScale; double m_openGlTimer; bool m_gaming; diff --git a/App/Window.cpp b/App/Window.cpp index fd8cade..6435ae5 100644 --- a/App/Window.cpp +++ b/App/Window.cpp @@ -14,8 +14,6 @@ static bool s_GLFW_Initialized = false; static void GLFWErrorCallback(int error, const char *description) { - /// @todo add GuiCore::Logger - // GuiCore::Log::get()->error("{}", error); GuiCore::Log::get()->error("{}", description); } @@ -44,7 +42,7 @@ void Window::setup(const GuiCore::WindowProperties &properties) if (!s_GLFW_Initialized) { glfwSetErrorCallback(GLFWErrorCallback); - auto success = glfwInit(); + glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); diff --git a/CMakeLists.txt b/CMakeLists.txt index 90cce88..4dbc4b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,7 @@ project(QPong VERSION 0.0.1 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_BUILD_TYPE Release) -list(APPEND basic_flags "-O3" "-Wextra" "-Wall" "-Wshadow" "-Wold-style-cast") -list(APPEND basic_flags "-Wno-unused-variable") -list(APPEND pedantic_flags "-pedantic" "-Wpedantic") +list(APPEND basic_flags "-O3" "-Wextra" "-Wall" "-Wshadow" "-Wold-style-cast" "-pedantic" "-Wpedantic") option(LOG_DEBUG_INFO "Enable logging t console" OFF) if(LOG_DEBUG_INFO) diff --git a/GuiCore/Application.cpp b/GuiCore/Application.cpp index 816300e..7d2e737 100644 --- a/GuiCore/Application.cpp +++ b/GuiCore/Application.cpp @@ -91,7 +91,7 @@ namespace GuiCore } } - bool Application::onWindowClose(Event &event) + bool Application::onWindowClose([[maybe_unused]] Event &event) { m_isRunnig = false; return true; diff --git a/GuiCore/ImGuiLayer.cpp b/GuiCore/ImGuiLayer.cpp index 63c044e..ac94a7c 100644 --- a/GuiCore/ImGuiLayer.cpp +++ b/GuiCore/ImGuiLayer.cpp @@ -28,13 +28,8 @@ void ImGuiLayer::onAttach() GuiCore::Log::get()->trace("onAttach ImGuiLayer"); IMGUI_CHECKVERSION(); ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); - - ImGui::StyleColorsDark(); - Application& app = Application::get(); - GLFWwindow *window = static_cast<GLFWwindow*>(app.getWindow().getNativeWindow()); ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init("#version 330"); @@ -67,12 +62,7 @@ void ImGuiLayer::end() ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } -void ImGuiLayer::onEvent(Event &event) +void ImGuiLayer::onEvent([[maybe_unused]] Event &event) { } - -bool ImGuiLayer::onMouseButtonPressed([[maybe_unused]] Event &event) -{ - return false; -} diff --git a/GuiCore/ImGuiLayer.hpp b/GuiCore/ImGuiLayer.hpp index 1156f8a..3b6ed72 100644 --- a/GuiCore/ImGuiLayer.hpp +++ b/GuiCore/ImGuiLayer.hpp @@ -24,7 +24,6 @@ namespace GuiCore void end(); virtual void onEvent(Event &event); - bool onMouseButtonPressed(Event &event); private: float m_time = 0.0f; diff --git a/QPong/Bat.cpp b/QPong/Bat.cpp index 4d46d7f..da160c7 100644 --- a/QPong/Bat.cpp +++ b/QPong/Bat.cpp @@ -87,4 +87,14 @@ double QPong::Bat::getPotential(double x, double y) const double QPong::Bat::getX() const { return m_x; +} + +void QPong::Bat::moveToStartingPosition() +{ + m_y = 0.0; +} + +void QPong::Bat::moveAway() +{ + m_y = 1.5; } \ No newline at end of file diff --git a/QPong/Bat.hpp b/QPong/Bat.hpp index 9ab01a2..5616b0a 100644 --- a/QPong/Bat.hpp +++ b/QPong/Bat.hpp @@ -17,6 +17,8 @@ namespace QPong double getY() const; void onUpdate(GuiCore::Timestep ts); double getPotential(double x, double y) const; + void moveToStartingPosition(); + void moveAway(); private: double m_x; double m_y; diff --git a/QPong/Particle.cpp b/QPong/Particle.cpp index ae1f9ee..51c22d2 100644 --- a/QPong/Particle.cpp +++ b/QPong/Particle.cpp @@ -78,11 +78,15 @@ Particle::Particle(Properties properties, GameOptions &options, Player &playerOn if (options.game == true) { calculateBorderPotential(m_staticBorders, m_yAt, m_properties.elements()); + m_leftBat.moveToStartingPosition(); + m_rightBat.moveToStartingPosition(); } else { // "Simulation" calculateGaussBox(m_staticBorders, m_xAt, m_yAt, m_properties.elements()); + m_leftBat.moveAway(); + m_rightBat.moveAway(); } fftw_init_threads(); -- GitLab