Skip to content

Commit

Permalink
Merge pull request #60 from nightingazer/little_something
Browse files Browse the repository at this point in the history
make a little demo of what possible with the engine so far
  • Loading branch information
Alex Grishan authored Jan 27, 2023
2 parents 36cf40b + 82134fc commit f4466d2
Show file tree
Hide file tree
Showing 19 changed files with 297 additions and 95 deletions.
3 changes: 3 additions & 0 deletions AGE/include/Age/Age.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include "Age/Core/Log.h"
#include "Age/Core/Timer.h"

// Utils
#include "Age/Utils/Random.h"

// Input handling
#include "Age/Core/Input.h"
#include "Age/Core/KeyCodes.h"
Expand Down
1 change: 1 addition & 0 deletions AGE/include/Age/EntryPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Age/Core/Core.h"
#include "Age/Core/Log.h"
#include "Age/Debug/Instrumentor.h"
#include "Age/Utils/Random.h"

extern AGE::Application* AGE::CreateApplication();

Expand Down
14 changes: 8 additions & 6 deletions AGE/src/Age/Renderer/OrthographicCameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@

namespace AGE {
OrthographicCameraController::OrthographicCameraController(float aspectRatio, bool rotation)
: m_AspectRatio(aspectRatio), m_Camera(
-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel
), m_Rotation(rotation) {
: m_AspectRatio(aspectRatio), m_ZoomLevel{1.0f},
m_Bounds{-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel},
m_Camera(
-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel
),
m_Rotation(rotation) {
}

void OrthographicCameraController::OnUpdate(Timestep ts) {
Expand Down Expand Up @@ -79,9 +82,8 @@ namespace AGE {

m_ZoomLevel -= e.YOffset() * 0.25f;
m_ZoomLevel = std::max(m_ZoomLevel, 0.25f);
m_Camera.SetProjection(
-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel
);
m_Bounds = {-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel};
m_Camera.SetProjection(m_Bounds.Left, m_Bounds.Right, m_Bounds.Bottom, m_Bounds.Top);
return false;
}

Expand Down
12 changes: 11 additions & 1 deletion AGE/src/Age/Renderer/OrthographicCameraController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@
namespace AGE {

class OrthographicCameraController {
struct CameraBounds {
float Left, Right;
float Bottom, Top;

[[nodiscard]] inline float Width() const { return Right - Left; }
[[nodiscard]] inline float Height() const { return Top - Bottom; }
};

public:
OrthographicCameraController(float aspectRatio, bool rotation = false);
explicit OrthographicCameraController(float aspectRatio, bool rotation = false);

void OnUpdate(Timestep ts);
void OnEvent(Event& e);
Expand All @@ -30,6 +38,7 @@ namespace AGE {

float ZoomLevel() const { return m_ZoomLevel; }
void SetZoomLevel(float level) { m_ZoomLevel = level; }
[[nodiscard]] inline CameraBounds Bounds() const { return m_Bounds; }

private:
bool OnMouseScrolled(MouseScrolledEvent& e);
Expand All @@ -39,6 +48,7 @@ namespace AGE {
float m_AspectRatio;
float m_ZoomLevel = 1.0f;
OrthographicCamera m_Camera;
CameraBounds m_Bounds;

bool m_Rotation;

Expand Down
22 changes: 11 additions & 11 deletions AGE/src/Age/Renderer/Renderer2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ namespace AGE {

// Reset color to white if texture is corrupted to properly display error texture
glm::vec4 vertexColor{color};
if (!texture->IsCorrect()) {
if (texture && !texture->IsCorrect()) {
vertexColor = {1.0f, 1.0f, 1.0f, 1.0f};
}

Expand Down Expand Up @@ -278,24 +278,24 @@ namespace AGE {
// DRAW ROTATED QUAD //
// /////////////////////////////////////////////////////////////////////////////////////////////////

void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotation, const glm::vec4& color) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotation, nullptr, color, 1.0f);
void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotationDeg, const glm::vec4& color) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, nullptr, color, 1.0f);
}

void Renderer2D::DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, const float rotation, const glm::vec4& color) {
DrawRotatedQuad(pos, size, rotation, nullptr, color, 1.0f);
void Renderer2D::DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, const float rotationDeg, const glm::vec4& color) {
DrawRotatedQuad(pos, size, rotationDeg, nullptr, color, 1.0f);
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotation, const Ref<Texture2D>& texture, float tillingFactor) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotation, nullptr, glm::vec4{1.0f}, 1.0f);
void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, float tillingFactor) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, nullptr, glm::vec4{1.0f}, 1.0f);
}

void Renderer2D::DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, const float rotation, const Ref<Texture2D>& texture, float tillingFactor) {
DrawRotatedQuad(pos, size, rotation, nullptr, glm::vec4{1.0f}, 1.0f);
void Renderer2D::DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, float tillingFactor) {
DrawRotatedQuad(pos, size, rotationDeg, nullptr, glm::vec4{1.0f}, 1.0f);
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotation, const Ref<Texture2D>& texture, const glm::vec4& color) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotation, nullptr, glm::vec4{1.0f}, 1.0f);
void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, const glm::vec4& color) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, nullptr, glm::vec4{1.0f}, 1.0f);
}

void Renderer2D::DrawRotatedQuad(
Expand Down
10 changes: 5 additions & 5 deletions AGE/src/Age/Renderer/Renderer2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ namespace AGE {

#pragma region DrawRotatedQuad

static void DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, float rotation, const glm::vec4& color);
static void DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, float rotation, const glm::vec4& color);
static void DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, float rotation, const Ref<Texture2D>& texture, float tillingFactor = 1.0f);
static void DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, float rotation, const Ref<Texture2D>& texture, float tillingFactor = 1.0f);
static void DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, float rotation, const Ref<Texture2D>& texture, const glm::vec4& color);
static void DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, float rotationDeg, const glm::vec4& color);
static void DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, float rotationDeg, const glm::vec4& color);
static void DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, float rotationDeg, const Ref<Texture2D>& texture, float tillingFactor = 1.0f);
static void DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, float rotationDeg, const Ref<Texture2D>& texture, float tillingFactor = 1.0f);
static void DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, float rotationDeg, const Ref<Texture2D>& texture, const glm::vec4& color);

/**
* @brief Adds a rotated quad to the current batch.
Expand Down
10 changes: 10 additions & 0 deletions AGE/src/Age/Utils/Random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by alex on 1/26/23.
//

#include "Random.h"

namespace AGE {
thread_local std::mt19937 Random::s_RandomEngine{std::random_device()()};
thread_local std::uniform_int_distribution<std::mt19937::result_type> Random::s_Distribution{};
} // AGE
58 changes: 58 additions & 0 deletions AGE/src/Age/Utils/Random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Created by alex on 1/26/23.
//

#ifndef AGE_RANDOM_H
#define AGE_RANDOM_H

#include <random>
#include <cinttypes>

#include <glm/glm.hpp>

namespace AGE {

class Random {
public:

static float Float()
{
return (float)s_Distribution(s_RandomEngine) / (float)std::numeric_limits<uint_fast32_t>::max();
}
static float Float(float min, float max) {
return Float() * (max - min) + min;
}

static uint32_t UInt() {
return s_Distribution(s_RandomEngine);
}
static uint32_t UInt(uint32_t min, uint32_t max) {
return min + (s_Distribution(s_RandomEngine) % (max - min + 1));
}

static glm::vec3 Vec3() {
return {Float(), Float(), Float()};
}
static glm::vec3 Vec3(float min, float max) {
return {Float(min, max), Float(min, max), Float(min, max)};
}

static glm::vec2 Vec2() {
return {Float(), Float()};
}
static glm::vec2 Vec2(float min, float max) {
return {Float(min, max), Float(min, max)};
}

static glm::vec3 InUnitSphere() {
return glm::normalize(Vec3(-1.0f, 1.0f));
}

private:
static thread_local std::mt19937 s_RandomEngine;
static thread_local std::uniform_int_distribution<std::mt19937::result_type> s_Distribution;
};

} // AGE

#endif //AGE_RANDOM_H
6 changes: 4 additions & 2 deletions AGE/src/Platform/OpenGL/OpenGLTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace AGE {
m_InternalFormat(
GL_RGBA8
),
m_DataFormat(GL_RGBA) {
m_DataFormat(GL_RGBA),
m_isCorrect{true} {
AGE_PROFILE_FUNCTION();

glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
Expand All @@ -29,7 +30,8 @@ namespace AGE {
OpenGLTexture2D::OpenGLTexture2D(const age_string_t& path) : m_Path(path), m_Slot{0},
m_isBound{false},
m_InternalFormat(0),
m_DataFormat(0) {
m_DataFormat(0),
m_isCorrect{true} {
AGE_PROFILE_FUNCTION();

int width, height, channels;
Expand Down
2 changes: 1 addition & 1 deletion AGE/vendor/glm
Submodule glm updated 59 files
+1 −1 doc/api/a00242.html
+4 −32 glm/detail/setup.hpp
+2 −2 glm/detail/type_mat2x2.hpp
+2 −2 glm/detail/type_mat2x2.inl
+2 −2 glm/detail/type_mat2x3.hpp
+2 −2 glm/detail/type_mat2x3.inl
+2 −2 glm/detail/type_mat2x4.hpp
+2 −2 glm/detail/type_mat2x4.inl
+2 −2 glm/detail/type_mat3x2.hpp
+2 −2 glm/detail/type_mat3x2.inl
+2 −2 glm/detail/type_mat3x3.hpp
+2 −2 glm/detail/type_mat3x3.inl
+2 −2 glm/detail/type_mat3x4.hpp
+2 −2 glm/detail/type_mat3x4.inl
+2 −2 glm/detail/type_mat4x2.hpp
+2 −2 glm/detail/type_mat4x2.inl
+2 −2 glm/detail/type_mat4x3.hpp
+2 −2 glm/detail/type_mat4x3.inl
+2 −2 glm/detail/type_mat4x4.hpp
+2 −2 glm/detail/type_mat4x4.inl
+0 −13 glm/detail/type_vec4_simd.inl
+1 −1 glm/exponential.hpp
+0 −128 glm/ext/_matrix_vectorize.hpp
+9 −9 glm/ext/matrix_clip_space.hpp
+1 −4 glm/ext/matrix_common.hpp
+0 −18 glm/ext/matrix_common.inl
+1 −1 glm/ext/matrix_float3x2.hpp
+1 −1 glm/ext/matrix_uint2x3.hpp
+1 −1 glm/ext/matrix_uint2x4_sized.hpp
+1 −1 glm/ext/matrix_uint3x2.hpp
+1 −1 glm/ext/quaternion_geometric.hpp
+0 −3 glm/ext/scalar_relational.hpp
+0 −3 glm/ext/scalar_ulp.hpp
+1 −1 glm/ext/vector_reciprocal.hpp
+0 −3 glm/ext/vector_ulp.hpp
+4 −5 glm/glm.hpp
+1 −1 glm/gtc/integer.hpp
+2 −2 glm/gtc/matrix_inverse.hpp
+4 −4 glm/gtc/random.hpp
+0 −3 glm/gtc/ulp.hpp
+1 −1 glm/gtx/associated_min_max.hpp
+1 −1 glm/gtx/easing.hpp
+4 −4 glm/gtx/extended_min_max.hpp
+1 −1 glm/gtx/handed_coordinate_space.hpp
+15 −15 glm/gtx/hash.hpp
+24 −15 glm/gtx/hash.inl
+1 −1 glm/gtx/io.hpp
+1 −1 glm/gtx/normalize_dot.hpp
+4 −8 glm/gtx/pca.hpp
+1 −1 glm/gtx/quaternion.hpp
+1 −8 glm/gtx/scalar_multiplication.hpp
+1 −5 glm/gtx/vec_swizzle.hpp
+1 −1 glm/gtx/vector_query.hpp
+0 −1 glm/simd/platform.h
+46 −64 manual.md
+4 −4 readme.md
+0 −183 test/ext/ext_matrix_common.cpp
+0 −1 test/gtx/CMakeLists.txt
+0 −55 test/gtx/gtx_hash.cpp
2 changes: 1 addition & 1 deletion AGE/vendor/imgui
Submodule imgui updated 79 files
+31 −0 .github/workflows/static-analysis.yml
+0 −3 .gitignore
+41 −64 backends/imgui_impl_allegro5.cpp
+5 −6 backends/imgui_impl_android.cpp
+44 −45 backends/imgui_impl_dx10.cpp
+52 −53 backends/imgui_impl_dx11.cpp
+73 −74 backends/imgui_impl_dx12.cpp
+32 −33 backends/imgui_impl_dx9.cpp
+59 −71 backends/imgui_impl_glfw.cpp
+4 −0 backends/imgui_impl_glfw.h
+3 −5 backends/imgui_impl_glut.cpp
+5 −5 backends/imgui_impl_metal.mm
+7 −8 backends/imgui_impl_opengl2.cpp
+52 −78 backends/imgui_impl_opengl3.cpp
+2 −2 backends/imgui_impl_opengl3.h
+0 −8 backends/imgui_impl_opengl3_loader.h
+1 −2 backends/imgui_impl_osx.h
+9 −17 backends/imgui_impl_osx.mm
+37 −56 backends/imgui_impl_sdl.cpp
+19 −20 backends/imgui_impl_sdlrenderer.cpp
+34 −45 backends/imgui_impl_vulkan.cpp
+2 −4 backends/imgui_impl_vulkan.h
+49 −50 backends/imgui_impl_wgpu.cpp
+2 −2 backends/imgui_impl_wgpu.h
+49 −63 backends/imgui_impl_win32.cpp
+1 −1 backends/imgui_impl_win32.h
+10 −10 docs/BACKENDS.md
+40 −231 docs/CHANGELOG.txt
+26 −26 docs/CONTRIBUTING.md
+1 −1 docs/EXAMPLES.md
+54 −62 docs/FAQ.md
+4 −4 docs/FONTS.md
+69 −44 docs/README.md
+1 −1 docs/TODO.txt
+3 −4 examples/example_allegro5/main.cpp
+10 −13 examples/example_android_opengl3/main.cpp
+4 −5 examples/example_apple_metal/main.mm
+4 −5 examples/example_apple_opengl2/main.mm
+3 −5 examples/example_emscripten_opengl3/main.cpp
+5 −7 examples/example_emscripten_wgpu/main.cpp
+4 −5 examples/example_glfw_metal/main.mm
+2 −2 examples/example_glfw_opengl2/example_glfw_opengl2.vcxproj.filters
+3 −4 examples/example_glfw_opengl2/main.cpp
+2 −2 examples/example_glfw_opengl3/example_glfw_opengl3.vcxproj.filters
+3 −5 examples/example_glfw_opengl3/main.cpp
+2 −2 examples/example_glfw_vulkan/example_glfw_vulkan.vcxproj.filters
+3 −4 examples/example_glfw_vulkan/main.cpp
+3 −5 examples/example_glut_opengl2/main.cpp
+2 −2 examples/example_sdl_directx11/example_sdl_directx11.vcxproj.filters
+4 −5 examples/example_sdl_directx11/main.cpp
+4 −5 examples/example_sdl_metal/main.mm
+2 −2 examples/example_sdl_opengl2/example_sdl_opengl2.vcxproj.filters
+4 −5 examples/example_sdl_opengl2/main.cpp
+2 −2 examples/example_sdl_opengl3/example_sdl_opengl3.vcxproj.filters
+4 −5 examples/example_sdl_opengl3/main.cpp
+1 −1 examples/example_sdl_sdlrenderer/example_sdl_sdlrenderer.vcxproj
+2 −2 examples/example_sdl_sdlrenderer/example_sdl_sdlrenderer.vcxproj.filters
+6 −7 examples/example_sdl_sdlrenderer/main.cpp
+1 −1 examples/example_sdl_vulkan/example_sdl_vulkan.vcxproj.filters
+3 −4 examples/example_sdl_vulkan/main.cpp
+2 −2 examples/example_win32_directx10/example_win32_directx10.vcxproj.filters
+8 −9 examples/example_win32_directx10/main.cpp
+2 −2 examples/example_win32_directx11/example_win32_directx11.vcxproj.filters
+8 −9 examples/example_win32_directx11/main.cpp
+0 −3 examples/example_win32_directx12/example_win32_directx12.vcxproj
+1 −6 examples/example_win32_directx12/example_win32_directx12.vcxproj.filters
+8 −9 examples/example_win32_directx12/main.cpp
+2 −2 examples/example_win32_directx9/example_win32_directx9.vcxproj.filters
+8 −9 examples/example_win32_directx9/main.cpp
+12 −25 examples/imgui_examples.sln
+5 −0 imconfig.h
+1,035 −1,869 imgui.cpp
+134 −185 imgui.h
+348 −392 imgui_demo.cpp
+57 −65 imgui_draw.cpp
+92 −308 imgui_internal.h
+15 −36 imgui_tables.cpp
+232 −201 imgui_widgets.cpp
+1 −2 misc/freetype/imgui_freetype.cpp
2 changes: 1 addition & 1 deletion AGE/vendor/spdlog
Submodule spdlog updated 53 files
+0 −81 .github/workflows/ci.yml
+155 −0 .travis.yml
+4 −22 CMakeLists.txt
+9 −6 README.md
+3 −14 appveyor.yml
+1 −1 bench/bench.cpp
+11 −0 bench/latency.cpp
+9 −12 example/example.cpp
+1 −1 include/spdlog/common-inl.h
+3 −54 include/spdlog/common.h
+0 −6 include/spdlog/details/backtracer-inl.h
+0 −1 include/spdlog/details/backtracer.h
+5 −0 include/spdlog/details/fmt_helper.h
+2 −24 include/spdlog/details/mpmc_blocking_q.h
+1 −2 include/spdlog/details/periodic_worker.h
+0 −1 include/spdlog/details/tcp_client.h
+5 −1 include/spdlog/details/thread_pool-inl.h
+4 −4 include/spdlog/fmt/bundled/args.h
+61 −63 include/spdlog/fmt/bundled/chrono.h
+100 −113 include/spdlog/fmt/bundled/color.h
+71 −40 include/spdlog/fmt/bundled/compile.h
+394 −481 include/spdlog/fmt/bundled/core.h
+1,727 −807 include/spdlog/fmt/bundled/format-inl.h
+422 −1,535 include/spdlog/fmt/bundled/format.h
+75 −26 include/spdlog/fmt/bundled/os.h
+22 −124 include/spdlog/fmt/bundled/ostream.h
+24 −7 include/spdlog/fmt/bundled/printf.h
+362 −291 include/spdlog/fmt/bundled/ranges.h
+0 −171 include/spdlog/fmt/bundled/std.h
+33 −26 include/spdlog/fmt/bundled/xchar.h
+0 −4 include/spdlog/fmt/fmt.h
+0 −23 include/spdlog/fmt/std.h
+1 −1 include/spdlog/logger-inl.h
+29 −3 include/spdlog/logger.h
+1 −4 include/spdlog/sinks/daily_file_sink.h
+7 −7 include/spdlog/sinks/dist_sink.h
+3 −5 include/spdlog/sinks/dup_filter_sink.h
+9 −17 include/spdlog/sinks/mongo_sink.h
+1 −11 include/spdlog/sinks/msvc_sink.h
+4 −4 include/spdlog/sinks/win_eventlog_sink.h
+1 −6 include/spdlog/spdlog.h
+2 −8 include/spdlog/tweakme.h
+1 −1 include/spdlog/version.h
+0 −12 scripts/ci_setup_clang.sh
+0 −52 src/bundled_fmtlib_format.cpp
+110 −0 src/fmt.cpp
+1 −10 tests/includes.h
+1 −10 tests/main.cpp
+0 −13 tests/test_backtrace.cpp
+1 −1 tests/test_fmt_helper.cpp
+2 −22 tests/test_mpmc_q.cpp
+1 −1 tests/test_pattern_formatter.cpp
+1 −1 tests/utils.cpp
4 changes: 3 additions & 1 deletion Sandbox/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.7)
project(AGE)
set(CMAKE_CXX_STANDARD 23)

add_executable(Sandbox src/main.cpp src/Sandbox.cpp src/Sandbox.h src/Sandbox2DLayer.cpp src/Sandbox2DLayer.h src/SandboxLayer.cpp src/SandboxLayer.h)
file(GLOB _SANDBOX_SRCS src/*.cpp src/*.h)

add_executable(Sandbox ${_SANDBOX_SRCS})
target_link_libraries(Sandbox PUBLIC age_core)

file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/assets)
Expand Down
70 changes: 70 additions & 0 deletions Sandbox/src/Particle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Created by alex on 1/25/23.
//

#include "Particle.h"

void ParticleSystem::OnUpdate(AGE::Timestep ts) {
for (auto& particle: m_Particles) {
if (!particle.Active)
continue;

if (particle.LifeRemaining <= 0.0f) {
particle.Active = false;
continue;
}

particle.LifeRemaining -= ts;
particle.Position += particle.Velocity * ts.Seconds();
particle.RotationDeg += particle.RotationSpeedDeg * ts.Seconds();
}
}

void ParticleSystem::Render() {
for (auto& particle: m_Particles) {
if(!particle.Active)
continue;

float life = particle.LifeRemaining / particle.LifeTime;
glm::vec4 color = particle.ColorBegin * (1.0f - life) + particle.ColorEnd * (life);
float size = particle.SizeBegin * (1.0f + life) + particle.SizeEnd * (life);

AGE::Renderer2D::DrawRotatedQuad(particle.Position, glm::vec2{size}, particle.RotationDeg, color);
}
}

void ParticleSystem::Emit(const ParticleProps& props) {
if (m_NextPoolInsert == m_Particles.end())
m_NextPoolInsert = m_Particles.begin();

glm::vec2 velocity = {props.Velocity.x + props.VelocityVariations * AGE::Random::Float(-1.0f, 1.0f),
props.Velocity.y + props.VelocityVariations * AGE::Random::Float(-1.0f, 1.0f)};
float rotationSpeed = props.RotationSpeedDeg + props.RotationSpeedVariationDeg * AGE::Random::Float(-1.0f, 1.0f);

float sizeBegin = props.SizeBegin + props.SizeVariations * AGE::Random::Float(-1.0f, 1.0f);
float sizeEnd = props.SizeEnd + props.SizeVariations * AGE::Random::Float(-1.0f, 1.0f);

glm::vec4 colorBegin = {props.ColorBegin.r + props.ColorVariations * AGE::Random::Float(-1.0f, 1.0f),
props.ColorBegin.g + props.ColorVariations * AGE::Random::Float(-1.0f, 1.0f),
props.ColorBegin.b + props.ColorVariations * AGE::Random::Float(-1.0f, 1.0f),
props.ColorBegin.a + props.ColorVariations * AGE::Random::Float(-1.0f, 1.0f)};
glm::vec4 colorEnd = {props.ColorEnd.r + props.ColorVariations * AGE::Random::Float(-1.0f, 1.0f),
props.ColorEnd.g + props.ColorVariations * AGE::Random::Float(-1.0f, 1.0f),
props.ColorEnd.b + props.ColorVariations * AGE::Random::Float(-1.0f, 1.0f),
props.ColorEnd.a + props.ColorVariations * AGE::Random::Float(-1.0f, 1.0f)};

m_NextPoolInsert->Active = true;
m_NextPoolInsert->LifeRemaining = m_NextPoolInsert->LifeTime = props.LifeTime;

m_NextPoolInsert->ColorEnd = colorBegin;
m_NextPoolInsert->ColorBegin = colorEnd;

m_NextPoolInsert->RotationSpeedDeg = rotationSpeed;
m_NextPoolInsert->RotationDeg = 0;
m_NextPoolInsert->Position = props.Position;
m_NextPoolInsert->SizeBegin = sizeBegin;
m_NextPoolInsert->SizeEnd = sizeEnd;
m_NextPoolInsert->Velocity = velocity;

m_NextPoolInsert++;
}
59 changes: 59 additions & 0 deletions Sandbox/src/Particle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Created by alex on 1/25/23.
//

#ifndef AGE_PARTICLE_H
#define AGE_PARTICLE_H

#include <vector>

#include "glm/glm.hpp"

#include "Age/Age.h"

struct ParticleProps {
glm::vec2 Position;
glm::vec2 Velocity;
float VelocityVariations;
float RotationSpeedDeg;
float RotationSpeedVariationDeg;

glm::vec4 ColorBegin, ColorEnd;
float ColorVariations;

float SizeBegin, SizeEnd;
float SizeVariations;

float LifeTime{1.0f};
float Rotation;
};

class ParticleSystem {
public:
ParticleSystem(uint32_t poolSize) : m_Particles(poolSize), m_NextPoolInsert(m_Particles.begin()) {}

void OnUpdate(AGE::Timestep ts);
void Render();
void Emit(const ParticleProps& props);

private:
struct Particle {
glm::vec2 Position;
float RotationDeg;
float RotationSpeedDeg;
glm::vec2 Velocity;
glm::vec4 ColorBegin, ColorEnd;
float SizeBegin, SizeEnd;

float LifeTime{1.0f};
float LifeRemaining{0.0f};

bool Active{false};
};

std::vector<Particle> m_Particles;
std::vector<Particle>::iterator m_NextPoolInsert;
};


#endif //AGE_PARTICLE_H
2 changes: 1 addition & 1 deletion Sandbox/src/Sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AGE::Application* AGE::CreateApplication() {

Sandbox::Sandbox() : AGE::Application() {
PushLayer(new Sandbox2DLayer);
PushOverlay(new Sandbox2DUI);
// PushOverlay(new Sandbox2DUI);
}

Sandbox::~Sandbox() {}
Expand Down
Loading

0 comments on commit f4466d2

Please sign in to comment.