Skip to content

Commit

Permalink
added vector util, added better debug window toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPeled committed May 17, 2024
1 parent 179c105 commit c7508d4
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 51 deletions.
14 changes: 9 additions & 5 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Binary file modified main
Binary file not shown.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Less generic typing (mostly due to how C++ works)

41 changes: 32 additions & 9 deletions src/WoopWoop/Editor/Editor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "Editor.hpp"
#include <SFML/Graphics.hpp>

#define DEBUG true

namespace wpwp::Editor
{
Expand All @@ -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
Expand All @@ -37,8 +42,11 @@ namespace wpwp::Editor

static int selectedUUIDIndex = -1;
static Entity *selectedEntity = nullptr;
static std::vector<std::string> comps{};
if (ImGui::BeginCombo("Choose", (selectedUUIDIndex >= 0 && selectedUUIDIndex < uuidschr.size()) ? uuidschr[selectedUUIDIndex] : nullptr))
static std::vector<std::string> componentNames{};
if (ImGui::BeginCombo("Choose", (selectedUUIDIndex >= 0 &&
selectedUUIDIndex < uuidschr.size())
? uuidschr[selectedUUIDIndex]
: nullptr))
{
for (int i = 0; i < uuidschr.size(); ++i)
{
Expand All @@ -48,12 +56,8 @@ namespace wpwp::Editor
selectedUUIDIndex = i;
selectedEntity = Entity::getEntityWithUUID(m_uuids[selectedUUIDIndex]);

std::vector<Component *> compies = selectedEntity->getComponents();
comps = std::vector<std::string>();
for (auto &i : compies)
{
comps.push_back(i->getName());
}
componentNames = VectorUtil::select(selectedEntity->getComponents(), [](Component *c)
{ return c->getName(); });
}
if (isSelected)
{
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion src/WoopWoop/Editor/Editor.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef EDITOR_HPP
#define EDITOR_HPP

#include "WoopWoop/WoopWoop.hpp"
#include "../WoopWoop.hpp"

namespace wpwp::Editor
{
Expand Down
30 changes: 12 additions & 18 deletions src/WoopWoop/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/WoopWoop/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/WoopWoop/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace wpwp
{
std::map<sf::Keyboard::Key, bool> Input::previouslyPressedKeys{};
sf::Vector2i Input::mouseOffset = sf::Vector2i(0, 0);

sf::Vector2i Input::getMouseScreenPosition()
{
Expand All @@ -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();
}

Expand Down
10 changes: 9 additions & 1 deletion src/WoopWoop/Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

namespace wpwp
{
namespace Editor
{
class Editor;
}
/**
* @brief Subsystem responsible for handling user input.
*/
Expand Down Expand Up @@ -65,8 +69,12 @@ namespace wpwp
*/
static bool isKeyClicked(sf::Keyboard::Key key);

friend Editor::Editor;

private:
static std::map<sf::Keyboard::Key, bool> previouslyPressedKeys; // Map storing the state of previously pressed keys.
static std::map<sf::Keyboard::Key, bool>
previouslyPressedKeys; // Map storing the state of previously pressed keys.
static sf::Vector2i mouseOffset;
};
}

Expand Down
24 changes: 12 additions & 12 deletions src/WoopWoop/Subsystems/Box2DIntegration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
54 changes: 53 additions & 1 deletion src/WoopWoop/Util/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

#include <random>
#include <sstream>
#include "../Engine.hpp"
#include <vector>
#include <functional>

namespace wpwp
{
class Engine;

/**
* @brief Utility class providing various helper functions.
*/
Expand All @@ -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:
Expand All @@ -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 <typename T, typename Func>
static auto select(const std::vector<T> &v, Func func) -> std::vector<decltype(func(std::declval<T>()))>
{
using R = decltype(func(std::declval<T>()));
std::vector<R> 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 <typename T, typename Func>
static void forEach(const std::vector<T> &v, Func func)
{
for (const auto &i : v)
{
func(i);
}
}
};
} // namespace wpwp

#endif // UTIL_HPP

0 comments on commit c7508d4

Please sign in to comment.