diff --git a/imgui.ini b/imgui.ini index cd20a40..0259cc4 100644 --- a/imgui.ini +++ b/imgui.ini @@ -12,12 +12,12 @@ Pos=635,26 Size=349,94 [Window][Entities] -Pos=1029,113 +Pos=1419,41 Size=261,703 [Window][Test] -Pos=1323,112 -Size=406,58 +Pos=-6,616 +Size=1023,59 [Window][Inside window] Pos=345,90 @@ -28,6 +28,10 @@ Pos=99,19 Size=1047,729 [Window][SFML Window] -Pos=139,109 -Size=864,851 +Pos=61,37 +Size=1030,694 + +[Window][Paused] +Pos=807,314 +Size=81,54 diff --git a/main b/main index 669ba9a..d6b3d59 100755 Binary files a/main and b/main differ diff --git a/readme.md b/readme.md index 0d89057..9e5ef2a 100644 --- a/readme.md +++ b/readme.md @@ -17,4 +17,5 @@ It was made with the purpouse of making game development easier to read, write, - Highly improved performance (from around 200fps to around 5000fps on an empty scene). - New scene system - Animation system -- Less generic typing (mostly due to how C++ works) \ No newline at end of file +- Less generic typing (mostly due to how C++ works) + diff --git a/src/WoopWoop/Editor/Editor.cpp b/src/WoopWoop/Editor/Editor.cpp index ddf6019..a58dff0 100644 --- a/src/WoopWoop/Editor/Editor.cpp +++ b/src/WoopWoop/Editor/Editor.cpp @@ -1,4 +1,7 @@ #include "Editor.hpp" +#include + +#define DEBUG true namespace wpwp::Editor { @@ -25,6 +28,8 @@ namespace wpwp::Editor void Editor::editorUpdate() { +#pragma region Entities List +#if DEBUG ImGui::Begin("Entities"); // Convert UUIDs to const char* array @@ -37,8 +42,11 @@ namespace wpwp::Editor static int selectedUUIDIndex = -1; static Entity *selectedEntity = nullptr; - static std::vector comps{}; - if (ImGui::BeginCombo("Choose", (selectedUUIDIndex >= 0 && selectedUUIDIndex < uuidschr.size()) ? uuidschr[selectedUUIDIndex] : nullptr)) + static std::vector componentNames{}; + if (ImGui::BeginCombo("Choose", (selectedUUIDIndex >= 0 && + selectedUUIDIndex < uuidschr.size()) + ? uuidschr[selectedUUIDIndex] + : nullptr)) { for (int i = 0; i < uuidschr.size(); ++i) { @@ -48,12 +56,8 @@ namespace wpwp::Editor selectedUUIDIndex = i; selectedEntity = Entity::getEntityWithUUID(m_uuids[selectedUUIDIndex]); - std::vector compies = selectedEntity->getComponents(); - comps = std::vector(); - for (auto &i : compies) - { - comps.push_back(i->getName()); - } + componentNames = VectorUtil::select(selectedEntity->getComponents(), [](Component *c) + { return c->getName(); }); } if (isSelected) { @@ -81,12 +85,31 @@ namespace wpwp::Editor ImGui::SliderAngle("Angle", &rads, 0, 360); selectedEntity->transform->getRotation()->z = rads * 180 / 3.14; - for (auto i : comps) + for (auto i : componentNames) { ImGui::TextUnformatted(i.c_str()); } } + ImGui::End(); +#endif +#pragma endregion Entities List + +#pragma region Debug Screen + sf::Sprite sprite; + sprite.setTexture(Engine::getInstance()->m_renderTexture.getTexture()); + sf::Vector2u textureSize = sprite.getTexture()->getSize(); +#if DEBUG + ImGui::Begin("SFML Window"); + + ImVec2 imageSize(textureSize.x, textureSize.y); + ImGui::Image(Engine::getInstance()->m_renderTexture, sf::Color::White, sf::Color::White); + Input::mouseOffset = sf::Vector2i(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y); ImGui::End(); +#else + Engine::getInstance()->window.draw(sprite); // render usually to the screen if isn't in debug mode +#endif + +#pragma endregion Debug Screen } } diff --git a/src/WoopWoop/Editor/Editor.hpp b/src/WoopWoop/Editor/Editor.hpp index eecda73..7c05ca2 100644 --- a/src/WoopWoop/Editor/Editor.hpp +++ b/src/WoopWoop/Editor/Editor.hpp @@ -1,7 +1,7 @@ #ifndef EDITOR_HPP #define EDITOR_HPP -#include "WoopWoop/WoopWoop.hpp" +#include "../WoopWoop.hpp" namespace wpwp::Editor { diff --git a/src/WoopWoop/Engine.cpp b/src/WoopWoop/Engine.cpp index 99d05be..2234f97 100644 --- a/src/WoopWoop/Engine.cpp +++ b/src/WoopWoop/Engine.cpp @@ -96,29 +96,20 @@ namespace wpwp Util::m_deltaTime = elapsedTime.asSeconds(); onStartOfFrame.invoke(); - m_renderTexture.clear(); + + if (!m_isPaused) + { + m_renderTexture.clear(); + draw(m_fpsText); + } checkForEvents(); updateSequence(); - draw(m_fpsText); m_renderTexture.display(); - // TODO: fix image being drawn upside down - sf::Sprite sprite; - sprite.setTexture(m_renderTexture.getTexture()); - // Get the size of the flipped texture - sf::Vector2u textureSize = sprite.getTexture()->getSize(); window.clear(); - // Draw sprite to ImGui - ImGui::Begin("SFML Window"); - ImVec2 imageSize(textureSize.x, textureSize.y); - // ImTextureID imguiTexture = - ImGui::Image(m_renderTexture, sf::Color::White, sf::Color::White); - // ; - // ImGui::Image(imguiTexture, imageSize); - ImGui::End(); updateSubsystems(); onEndOfFrame.invoke(); @@ -136,10 +127,13 @@ namespace wpwp } void Engine::updateSequence() { - for (auto &ent : wpwp::Entity::getAllEntities()) + if (!m_isPaused) { - if (ent->enabled()) - ent->update(0.0); + for (auto &ent : wpwp::Entity::getAllEntities()) + { + if (ent->enabled()) + ent->update(0.0); + } } } diff --git a/src/WoopWoop/Engine.hpp b/src/WoopWoop/Engine.hpp index 01bb4d4..1a1b0da 100644 --- a/src/WoopWoop/Engine.hpp +++ b/src/WoopWoop/Engine.hpp @@ -12,7 +12,10 @@ namespace wpwp { struct Scene; - + namespace Editor + { + class Editor; + }; /** * @brief The main engine class responsible for managing the game loop, rendering, and event handling. */ @@ -73,6 +76,8 @@ namespace wpwp Signal<> onEndOfFrame; // Signal emitted at the end of each frame. sf::RenderTexture m_renderTexture; // SFML render texture for off-screen rendering. + friend Editor::Editor; + private: /** * @brief Checks for SFML events and emits the appropriate signals. @@ -111,7 +116,8 @@ namespace wpwp static Engine *instance; // Static pointer to the Engine instance. static sf::Font font; // Static font object for rendering text. - bool m_isFocused; // Flag indicating whether the window is focused. + bool m_isFocused; // Flag indicating whether the window is focused. + bool m_isPaused; sf::Clock m_clock; // SFML clock to measure elapsed time. sf::Clock m_deltaClock; // SFML clock to measure delta time. sf::Text m_fpsText; // SFML text object for displaying FPS. diff --git a/src/WoopWoop/Input.cpp b/src/WoopWoop/Input.cpp index 27947d2..1038111 100644 --- a/src/WoopWoop/Input.cpp +++ b/src/WoopWoop/Input.cpp @@ -5,6 +5,7 @@ namespace wpwp { std::map Input::previouslyPressedKeys{}; + sf::Vector2i Input::mouseOffset = sf::Vector2i(0, 0); sf::Vector2i Input::getMouseScreenPosition() { @@ -14,7 +15,7 @@ namespace wpwp sf::Vector2i Input::getMouseWorldPosition() { if (Engine::getInstance()) - return sf::Mouse::getPosition(Engine::getInstance()->window); + return sf::Mouse::getPosition(Engine::getInstance()->window) - mouseOffset; return sf::Vector2i(); } diff --git a/src/WoopWoop/Input.hpp b/src/WoopWoop/Input.hpp index 70006ca..59e0301 100644 --- a/src/WoopWoop/Input.hpp +++ b/src/WoopWoop/Input.hpp @@ -6,6 +6,10 @@ namespace wpwp { + namespace Editor + { + class Editor; + } /** * @brief Subsystem responsible for handling user input. */ @@ -65,8 +69,12 @@ namespace wpwp */ static bool isKeyClicked(sf::Keyboard::Key key); + friend Editor::Editor; + private: - static std::map previouslyPressedKeys; // Map storing the state of previously pressed keys. + static std::map + previouslyPressedKeys; // Map storing the state of previously pressed keys. + static sf::Vector2i mouseOffset; }; } diff --git a/src/WoopWoop/Subsystems/Box2DIntegration.cpp b/src/WoopWoop/Subsystems/Box2DIntegration.cpp index 95d274c..0187b5f 100644 --- a/src/WoopWoop/Subsystems/Box2DIntegration.cpp +++ b/src/WoopWoop/Subsystems/Box2DIntegration.cpp @@ -47,17 +47,17 @@ namespace wpwp void Box2DIntegration::update() { - static float distanceFromBox2DPos = 141; - static float y = 441; - m_World->Step(Util::deltaTime(), 6, 2); - sf::Vertex line[] = - { - sf::Vertex(sf::Vector2f(0, y)), - sf::Vertex(sf::Vector2f(1000, y))}; - - ImGui::Begin("Test"); - ImGui::SliderFloat("Height", &y, 0, 1000); - ImGui::End(); - Engine::getInstance()->window.draw(line, 2, sf::Lines); + // static float distanceFromBox2DPos = 141; + // static float y = 441; + // m_World->Step(Util::deltaTime(), 6, 2); + // sf::Vertex line[] = + // { + // sf::Vertex(sf::Vector2f(0, y)), + // sf::Vertex(sf::Vector2f(1000, y))}; + + // ImGui::Begin("Test"); + // ImGui::SliderFloat("Height", &y, 0, 1000); + // ImGui::End(); + // Engine::getInstance()->window.draw(line, 2, sf::Lines); } } \ No newline at end of file diff --git a/src/WoopWoop/Util/Util.hpp b/src/WoopWoop/Util/Util.hpp index 75e3349..48ff384 100644 --- a/src/WoopWoop/Util/Util.hpp +++ b/src/WoopWoop/Util/Util.hpp @@ -3,10 +3,13 @@ #include #include -#include "../Engine.hpp" +#include +#include namespace wpwp { + class Engine; + /** * @brief Utility class providing various helper functions. */ @@ -19,6 +22,12 @@ namespace wpwp * @return A string representing the generated UUID. */ static std::string generate_uuid_v4(); + + /** + * @brief Gets the delta time. + * + * @return A float representing the delta time. + */ static float deltaTime() { return m_deltaTime; } private: @@ -30,6 +39,49 @@ namespace wpwp friend Engine; }; + + /** + * @brief Utility class for performing operations on vectors. + */ + class VectorUtil + { + public: + /** + * @brief Transforms each element in a vector using a provided function. + * + * @tparam T Type of the elements in the input vector. + * @tparam Func Type of the callable object to transform the elements. + * @param v The input vector containing elements of type T. + * @param func A callable object that takes an element of type T and returns a transformed element. + * @return A vector containing the transformed elements. + */ + template + static auto select(const std::vector &v, Func func) -> std::vector()))> + { + using R = decltype(func(std::declval())); + std::vector results; + forEach(v, [&](T i) + { results.push_back(func(i)); }); + return results; + } + + /** + * @brief Applies a provided function to each element in a vector. + * + * @tparam T Type of the elements in the input vector. + * @tparam Func Type of the callable object to apply to each element. + * @param v The input vector containing elements of type T. + * @param func A callable object that takes an element of type T. + */ + template + static void forEach(const std::vector &v, Func func) + { + for (const auto &i : v) + { + func(i); + } + } + }; } // namespace wpwp #endif // UTIL_HPP