Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into Arms/Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
A-rms committed Feb 8, 2024
2 parents 2f70902 + f3ab96d commit b20ddd2
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 27 deletions.
13 changes: 13 additions & 0 deletions .github/format_check_diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

[[ ! $(git --version) ]] && exit 1

output=$(git diff)

if [[ "$output" != "" ]]; then
echo "One or more source files are not formatted!"
exit 1
fi

echo "All source files are formatted"
exit
11 changes: 11 additions & 0 deletions .github/workflows/format_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: ci
on: [push]
jobs:
format-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: format code
run: tools/format_code.sh
- name: check diff
run: .github/format_check_diff.sh
4 changes: 2 additions & 2 deletions DogTales/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
set(generated_header_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
configure_file(src/build_version.hpp.in "${generated_header_dir}/src/build_version.hpp" @ONLY)
configure_file(dog/build_version.hpp.in "${generated_header_dir}/dog/build_version.hpp" @ONLY)

add_executable(${PROJECT_NAME})

Expand All @@ -13,6 +13,6 @@ target_include_directories(${PROJECT_NAME} PRIVATE
"${generated_header_dir}"
)

file(GLOB_RECURSE sources LIST_DIRECTORIES false CONFIGURE_DEPENDS "src/*.?pp")
file(GLOB_RECURSE sources LIST_DIRECTORIES false CONFIGURE_DEPENDS "dog/*.?pp")

target_sources(${PROJECT_NAME} PRIVATE ${sources})
File renamed without changes.
24 changes: 24 additions & 0 deletions DogTales/dog/components/physics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <dog/components/physics.hpp>

namespace dog::component {

void Physics::tick(bave::Seconds dt) {
static constexpr bave::Seconds ft{0.005}; // 5ms

if (dt.count() > tick_limit_v.count()) {
return;
} // return for unexpected dt values, particularly during the beginning of the state

for (dt += m_residue; dt > ft; dt -= ft) { integrate(ft); }
m_residue = dt;
}

void Physics::integrate(bave::Seconds dt) {
acceleration.y += gravity * dt.count();
velocity = (velocity + (acceleration / mass) * dt.count()) * friction;

velocity = glm::clamp(velocity, -max_velocity_v, max_velocity_v);

position += velocity * dt.count();
}
} // namespace dog::component
32 changes: 32 additions & 0 deletions DogTales/dog/components/physics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include <bave/app.hpp>
#include <bave/graphics/sprite.hpp>

namespace dog::component {
class Physics {

static constexpr glm::vec2 default_friction{0.99f};
static constexpr float default_gravity{-1000.f};
static constexpr float default_mass{1.f};

static constexpr glm::vec2 max_velocity_v{1000.f};
static constexpr bave::Seconds tick_limit_v{100ms};

bave::Seconds m_residue{};

public:
Physics(glm::vec2 friction = default_friction, float gravity = default_gravity, float mass = default_mass)
: friction(friction), gravity(gravity), mass(mass) {}

glm::vec2 position{};
glm::vec2 velocity{};
glm::vec2 acceleration{};

glm::vec2 friction{};
float gravity{};
float mass{};

void tick(bave::Seconds dt);
void integrate(bave::Seconds dt);
};
} // namespace dog::component
4 changes: 3 additions & 1 deletion DogTales/src/dogtales.cpp → DogTales/dog/dogtales.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <src/dogtales.hpp>
#include <dog/dogtales.hpp>

namespace dog {
DogTales::DogTales(bave::App& app) : bave::Driver(app) {}

void DogTales::tick() {
Expand All @@ -23,3 +24,4 @@ void DogTales::on_key(bave::KeyInput const& key_input) {
void DogTales::set_viewport_to_world_space() const {
get_app().get_render_device().render_view.viewport = world_space_v;
}
} // namespace dog
4 changes: 3 additions & 1 deletion DogTales/src/dogtales.hpp → DogTales/dog/dogtales.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include <bave/driver.hpp>
#include <src/player.hpp>
#include <dog/player.hpp>

namespace dog {
class DogTales : public bave::Driver {
static constexpr glm::vec2 world_space_v{1280.0f, 720.0f};

Expand All @@ -17,3 +18,4 @@ class DogTales : public bave::Driver {
public:
explicit DogTales(bave::App& app);
};
} // namespace dog
2 changes: 2 additions & 0 deletions DogTales/src/fatal_error.hpp → DogTales/dog/fatal_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#include <fmt/format.h>
#include <stdexcept>

namespace dog {
class FatalError : public std::runtime_error {
public:
template <typename... Args>
explicit FatalError(fmt::format_string<Args...> fmt, Args&&... args)
: std::runtime_error(fmt::format(fmt, std::forward<Args>(args)...)) {}
};
} // namespace dog
8 changes: 4 additions & 4 deletions DogTales/src/main.cpp → DogTales/dog/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <bave/clap/clap.hpp>
#include <bave/desktop_app.hpp>
#include <src/build_version.hpp>
#include <src/dogtales.hpp>
#include <src/tests/test.hpp>
#include <dog/build_version.hpp>
#include <dog/dogtales.hpp>
#include <dog/tests/test.hpp>
#include <iostream>

namespace {
Expand Down Expand Up @@ -41,7 +41,7 @@ auto run_app(int const argc, char const* const* argv) -> int {

auto app = bave::DesktopApp{create_info};

app.set_bootloader([](bave::App& app) { return std::make_unique<DogTales>(app); });
app.set_bootloader([](bave::App& app) { return std::make_unique<dog::DogTales>(app); });

return static_cast<int>(app.run());
}
Expand Down
21 changes: 14 additions & 7 deletions DogTales/src/player.cpp → DogTales/dog/player.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#include <src/player.hpp>
#include <dog/player.hpp>

namespace dog {
Player::Player(glm::vec2 const world_space) : m_world_space(world_space) { m_sprite.set_size(size_v); }

void Player::tick(bave::Seconds const dt) {
// Update the player's movement based on velocity.
m_sprite.transform.position += m_vel * dt.count();

m_physics.tick(dt);
m_sprite.transform.position = m_physics.position;

handle_wall_collision();
m_physics.position = m_sprite.transform.position;
}

void Player::draw(bave::Shader& shader) const { m_sprite.draw(shader); }

void Player::update_movement(glm::vec2 const& direction) {
// Normalize the direction vector
glm::vec2 normalized_direction = glm::normalize(direction);
// glm::vec2 normalized_direction = glm::normalize(direction);

// Apply speed to the normalized direction vector
m_vel = normalized_direction * speed_v;
}

void Player::handle_input(bave::KeyInput const& key_input) {
Expand All @@ -41,10 +43,15 @@ void Player::handle_wall_collision() {
auto const bounce_rect = bave::Rect<>::from_size(m_world_space - m_sprite.get_size(), glm::vec2{0.0f});

// if the sprite's position exceeds the play area, the corresponding velocity component needs to flip.
if (position.x < bounce_rect.top_left().x || position.x > bounce_rect.bottom_right().x) { m_vel.x *= -1.0f; }
if (position.y > bounce_rect.top_left().y || position.y < bounce_rect.bottom_right().y) { m_vel.y *= -1.0f; }
if (position.x < bounce_rect.top_left().x || position.x > bounce_rect.bottom_right().x) {
m_physics.velocity.x *= -0.9f;
}
if (position.y > bounce_rect.top_left().y || position.y < bounce_rect.bottom_right().y) {
m_physics.velocity.y *= -0.9f;
}

// clamp the position to the play area.
// bottom_left() gives us the minimum x and y whereas top_right() gives us the maximum.
position = glm::clamp(position, bounce_rect.bottom_left(), bounce_rect.top_right());
}
} // namespace dog
6 changes: 4 additions & 2 deletions DogTales/src/player.hpp → DogTales/dog/player.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once
#include <bave/app.hpp>
#include <bave/graphics/sprite.hpp>
#include <unordered_set>
#include "components/physics.hpp"

namespace dog {
class Player {
static constexpr glm::vec2 speed_v{500.0f, 500.0f};
static constexpr glm::vec2 size_v{50.0f, 90.0f};
Expand All @@ -11,7 +12,7 @@ class Player {

bave::Sprite m_sprite{};

glm::vec2 m_vel{};
component::Physics m_physics{};

void handle_wall_collision();

Expand All @@ -24,3 +25,4 @@ class Player {
void draw(bave::Shader& shader) const;
void update_movement(glm::vec2 const& direction);
};
} // namespace dog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include <bave/core/polymorphic.hpp>

namespace dog {
/// \brief Base class for all services.
class IService : public bave::Polymorphic {};
} // namespace dog
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pragma once
#include <bave/core/not_null.hpp>
#include <bave/core/ptr.hpp>
#include <src/fatal_error.hpp>
#include <src/services/service.hpp>
#include <dog/fatal_error.hpp>
#include <dog/services/service.hpp>
#include <memory>
#include <mutex>
#include <typeindex>
#include <unordered_map>

namespace dog {
/// \brief Concept constraining Type to a subclass of IService.
template <typename Type>
concept ServiceT = std::derived_from<Type, IService>;
Expand All @@ -27,10 +28,10 @@ class Services {
/// \pre service must not be null, From must not already be bound.
template <ServiceT From, std::derived_from<From> To>
void bind(std::unique_ptr<To> service) {
if (!service) { throw FatalError{"Attempt to bind null service"}; }
if (!service) { throw dog::FatalError{"Attempt to bind null service"}; }
static auto const index = std::type_index{typeid(From)};
auto lock = std::scoped_lock{m_mutex};
if (m_services.contains(index)) { throw FatalError{"Attempt to bind duplicate service"}; }
if (m_services.contains(index)) { throw dog::FatalError{"Attempt to bind duplicate service"}; }
m_services.insert_or_assign(index, std::move(service));
}

Expand Down Expand Up @@ -73,7 +74,7 @@ class Services {
template <ServiceT Type>
[[nodiscard]] auto get() const -> Type& {
auto ret = find<Type>();
if (!ret) { throw FatalError{"Service not found"}; }
if (!ret) { throw dog::FatalError{"Service not found"}; }
return *ret;
}

Expand All @@ -88,3 +89,4 @@ class Services {
std::unordered_map<std::type_index, std::unique_ptr<IService>> m_services{};
mutable std::mutex m_mutex{};
};
} // namespace dog
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <fmt/format.h>
#include <src/tests/test.hpp>
#include <dog/tests/test.hpp>
#include <filesystem>
#include <iostream>
#include <vector>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <bave/core/pinned.hpp>
#include <src/services/services.hpp>
#include <src/tests/test.hpp>
#include <dog/services/services.hpp>
#include <dog/tests/test.hpp>

namespace dog {
namespace {
namespace one {
struct Foo : IService {
Expand Down Expand Up @@ -37,7 +38,7 @@ ADD_TEST(Services_GetException) {
auto thrown = false;
try {
[[maybe_unused]] auto& foo = services.get<one::Foo>();
} catch (FatalError const&) { thrown = true; }
} catch (dog::FatalError const&) { thrown = true; }

EXPECT(thrown);
}
Expand All @@ -50,7 +51,7 @@ ADD_TEST(Services_DuplicateException) {
auto thrown = false;
try {
services.bind<one::Foo>(std::make_unique<one::Foo>());
} catch (FatalError const&) { thrown = true; }
} catch (dog::FatalError const&) { thrown = true; }

EXPECT(thrown);
}
Expand All @@ -72,3 +73,4 @@ ADD_TEST(Services_BindSubclass) {
EXPECT(services.get<Interface>().get_value() == 42);
}
} // namespace
} // namespace dog
23 changes: 23 additions & 0 deletions tools/format_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

[[ ! $(clang-format --version) ]] && exit 1

script_path=${0%/*}
tools_root=${script_path%/*}
project_root=$tools_root/..

if [[ "$0" != "$project_root" ]] && [[ "$project_root" != "" ]]; then
cd "$project_root" || exit 1
echo "-- Changed pwd to $(pwd)"
fi

files=$(find DogTales -name "*.?pp")
if [[ "$files" == "" ]]; then
echo "-- No source files found"
exit
fi

clang-format -i $files
echo -e "-- Formatted Files:\n$files\n"

exit

0 comments on commit b20ddd2

Please sign in to comment.