Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code format check as GitHub workflow. #31

Merged
merged 3 commits into from
Feb 2, 2024
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
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: 3 additions & 1 deletion DogTales/src/components/physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace 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
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;
Expand Down
4 changes: 2 additions & 2 deletions DogTales/src/components/physics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Physics {
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) {}
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{};
Expand Down
8 changes: 6 additions & 2 deletions DogTales/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ 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_physics.velocity.x *= -0.9f; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes I really prefer this compact code style. Not sure if there's a way to allow for it in the formatting check, or perhaps you disagree with using the compact style, in which case, I'm curious to know why!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah me too, and it's already set to format that way! Unless the line would be too long, which here exceeds the 120 column limit.

if (position.y > bounce_rect.top_left().y || position.y < bounce_rect.bottom_right().y) { m_physics.velocity.y *= -0.9f; }
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.
Expand Down
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