Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) # Ensure strictly standard C++
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(WEIRD_ENGINE_ENABLE_ASAN "Enable AddressSanitizer for memory leak detection" OFF)
if(WEIRD_ENGINE_ENABLE_ASAN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
endif()

message(STATUS "WeirdEngine path: ${CMAKE_CURRENT_SOURCE_DIR}")


Expand Down
2 changes: 1 addition & 1 deletion examples/sample-scenes/include/DestroyScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DestroyScene : public Scene2D
float y = (std::rand() % 100) - 50.0f;
float w = (float)(std::rand() % 4 + 1);
float h = (float)(std::rand() % 4 + 1);
float variables[4]{w, y, x, h};
float variables[8]{w, y, x, h, 0.0f, 0.0f, 0.0f, 0.0f};
uint16_t material = std::rand() % 16;
Entity shape = addShape(DefaultShapes::BOX, variables, material, CombinationType::Addition);
m_testShapes.push_back(shape);
Expand Down
1 change: 1 addition & 0 deletions include/weird-engine/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace WeirdEngine
static void warning(const std::string& message);
static void error(const std::string& message);

static bool s_enableConsoleOutput;
static void drawImGuiConsole();

private:
Expand Down
4 changes: 2 additions & 2 deletions include/weird-engine/SceneManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace WeirdEngine

static SceneManager& getInstance()
{
static SceneManager* _instance = new SceneManager();
return *_instance;
static SceneManager _instance;
return _instance;
};

void loadNextScene();
Expand Down
13 changes: 10 additions & 3 deletions src/weird-engine/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@ namespace WeirdEngine
{
std::vector<LogMessage> Logger::s_messages;
std::mutex Logger::s_mutex;
bool Logger::s_enableConsoleOutput = true;

void Logger::log(const std::string& message)
{
std::lock_guard<std::mutex> lock(s_mutex);
s_messages.push_back({LogLevel::Info, message});
std::cout << "[INFO] " << message << std::endl;
if (s_messages.size() > 1000) s_messages.erase(s_messages.begin());
if (s_enableConsoleOutput)
std::cout << "[INFO] " << message << std::endl;
}

void Logger::warning(const std::string& message)
{
std::lock_guard<std::mutex> lock(s_mutex);
s_messages.push_back({LogLevel::Warning, message});
std::cout << "[WARN] " << message << std::endl;
if (s_messages.size() > 1000) s_messages.erase(s_messages.begin());
if (s_enableConsoleOutput)
std::cout << "[WARN] " << message << std::endl;
}

void Logger::error(const std::string& message)
{
std::lock_guard<std::mutex> lock(s_mutex);
s_messages.push_back({LogLevel::Error, message});
std::cerr << "[ERROR] " << message << std::endl;
if (s_messages.size() > 1000) s_messages.erase(s_messages.begin());
if (s_enableConsoleOutput)
std::cerr << "[ERROR] " << message << std::endl;
}

void Logger::drawImGuiConsole()
Expand Down
53 changes: 25 additions & 28 deletions src/weird-engine/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,32 @@ namespace WeirdEngine
{
EntityShapeCollisionEvent entityEvent{ev, getEntityForSimulationId(ev.body)};
onEntityShapeCollision(m_ecs, entityEvent);

const float m_soundFalloff = 0.1f;
bool spatialAudio = false;
auto camPosition = getCamera().position;
float speed = glm::length2(ev.velocity);
float distanceMultiplier =
1.0f / (1.0f + (m_soundFalloff * glm::distance2(camPosition, vec3(ev.position, 0.0f))));
float frictionSample = ev.friction * 0.01f * speed * (spatialAudio ? distanceMultiplier : 1.0f);

m_frictionSoundLevel = std::max(frictionSample, m_frictionSoundLevel);

if (ev.state == CollisionState::START)
{
float penetrationFactor = std::sqrt((std::min)(2.0f * ev.penetration, 1.0f));
float volume = penetrationFactor;

float freqFactor = std::abs(glm::dot(ev.normal, (ev.velocity)));
freqFactor *= 0.01f;
float frequency = 200.0f + (freqFactor * 300.0f);

playSound(WeirdRenderer::SimpleAudioRequest{volume, frequency, false, vec3(ev.position, 0.0f)});
}
}

m_frictionSoundLevelRead.store(m_frictionSoundLevel, std::memory_order_release);
m_frictionSoundLevel = 0.0f;

onUpdate(delta, m_ecs);
}
Expand All @@ -230,9 +255,6 @@ namespace WeirdEngine
{
Scene* self = static_cast<Scene*>(userData);
self->onPhysicsStep(self->m_simulation2D);

self->m_frictionSoundLevelRead.store(self->m_frictionSoundLevel, std::memory_order_release);
self->m_frictionSoundLevel = 0.0f;
}

void Scene::handleCollision(CollisionEvent& event, void* userData)
Expand All @@ -253,31 +275,6 @@ namespace WeirdEngine
std::lock_guard<std::mutex> lock(self->m_collisionQueueMutex);
self->m_queuedShapeCollisions.push_back(event);
}

const float m_soundFalloff = 0.1f;
bool spatialAudio = false;
auto camPosition = self->getCamera().position; // Mutex?
float speed = glm::length2(event.velocity);
float distanceMultiplier =
1.0f / (1.0f + (m_soundFalloff * glm::distance2(camPosition, vec3(event.position, 0.0f))));
float frictionSample = event.friction * 0.01f * speed * (spatialAudio ? distanceMultiplier : 1.0f);

self->m_frictionSoundLevel = std::max(frictionSample, self->m_frictionSoundLevel);

if (event.state == CollisionState::START)
{
// float speedFactor = std::sqrt((std::min)(0.001f * speed, 1.0f));
float penetrationFactor = std::sqrt((std::min)(2.0f * event.penetration, 1.0f));
float volume = penetrationFactor;

float freqFactor = std::abs(glm::dot(event.normal, (event.velocity)));
freqFactor *= 0.01f;
// freqFactor = freqFactor * freqFactor;
float frequency = 200.0f + (freqFactor * 300.0f);

self->m_audioQueue.push(
WeirdRenderer::SimpleAudioRequest{volume, frequency, false, vec3(event.position, 0.0f)});
}
}

// RENDER
Expand Down
1 change: 1 addition & 0 deletions src/weird-physics/Simulation2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ namespace WeirdEngine
delete[] m_velocitiesRead;
delete[] m_velocitiesAux;
delete[] m_forces;
delete[] m_externalForces;
delete[] m_mass;
delete[] m_invMass;
}
Expand Down
1 change: 1 addition & 0 deletions src/weird-renderer/core/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ namespace WeirdEngine

if (ImGui::BeginTabItem("Console"))
{
ImGui::Checkbox("Print to std::cout", &Logger::s_enableConsoleOutput);
Logger::drawImGuiConsole();
ImGui::EndTabItem();
}
Expand Down
Loading