Skip to content

Commit

Permalink
Introduce clang-format (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mityu authored Sep 17, 2024
1 parent 4ca49d9 commit 32e0157
Show file tree
Hide file tree
Showing 21 changed files with 704 additions and 679 deletions.
32 changes: 32 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
BasedOnStyle: Chromium
IndentWidth: 4
TabWidth: 4
ColumnLimit: 95
UseTab: Never
IndentAccessModifiers: false
IndentCaseLabels: false
AccessModifierOffset: -4
BreakStringLiterals: false
PointerAlignment: Right
ReferenceAlignment: Left
BreakBeforeBraces: Attach
AlwaysBreakAfterReturnType: None
AlwaysBreakAfterDefinitionReturnType: None
# BreakAfterReturnType: Automatic
BreakConstructorInitializers: AfterColon
AlignAfterOpenBracket: AlwaysBreak
Cpp11BracedListStyle: true

IncludeCategories:
- Regex: '^<windowsx?.h>'
Priority: 1
- Regex: '^<ext/.*\.h>'
Priority: 3
- Regex: '^"auto/.*\.h"'
Priority: 5
- Regex: '^<.*\.h>'
Priority: 2
- Regex: '^<.*'
Priority: 3
- Regex: '.*'
Priority: 4
19 changes: 19 additions & 0 deletions .github/workflows/test-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Code format check
on: [push, pull_request]

jobs:
clang-format:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- name: Setup homebrew
uses: Homebrew/actions/setup-homebrew@master
- name: Prepare newer clang-format
run: |
brew install clang-format
brew link clang-format --force
- name: Show clang-format version
run: clang-format --version
- name: Check code format
run: make fmt-check
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ all: clean $(TARGET);
$(OBJDIR) auto/ tool/:
mkdir -p $@

fmt: FMT_OPTS := -i
fmt: clang-format

fmt-check: FMT_OPTS := --dry-run --Werror
fmt-check: clang-format

clang-format:
clang-format $(FMT_OPTS) $(wildcard *.cpp) $(wildcard *.mm) $(wildcard *.hpp)

# Make distribution package
PKGNAME:=yoMMD-$(PKGNAME_PLATFORM)-$(shell date '+%Y%m%d%H%M').zip
package: may-create-release-build
Expand Down Expand Up @@ -190,6 +199,8 @@ help:
@echo "debug Debug build"
@echo "run Build and run binary"
@echo "clean Clean build related files"
@echo "fmt Format source code by clang-format"
@echo "fmt-check Check if source code is formatted"
@echo "app Make application bundle (Only available on macOS)"
@echo "package Make distribution package without any MMD models/motions"
@echo "package-huge Make distribution package with default config,"
Expand All @@ -199,6 +210,6 @@ help:
@echo "bulid-submodule Build submodule libraries"
@echo "help Show this help"

.PHONY: release debug help run clean package package-huge app
.PHONY: release debug help run clean package package-huge app fmt fmt-check clang-format
.PHONY: may-create-release-build
.PHONY: build-bullet build-saba update-sokol-shdc build-submodule
31 changes: 16 additions & 15 deletions config.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "config.hpp"
#include "util.hpp"
#include "toml.hpp" // IWYU pragma: keep; supress warning from clangd.
#include <filesystem>
#include <string_view>
#include <vector>
#include "toml.hpp" // IWYU pragma: keep; supress warning from clangd.
#include "util.hpp"

namespace {
inline glm::vec2 toVec2(const std::array<float, 2> a) {
Expand All @@ -14,26 +14,27 @@ inline glm::vec3 toVec3(const std::array<float, 3> a) {
return glm::vec3(a[0], a[1], a[2]);
}

}
} // namespace

Config::Config() :
simulationFPS(60.0f), gravity(9.8f), lightDirection(-0.5f, -1.0f, -0.5f),
defaultModelPosition(0.0f, 0.0f), defaultScale(1.0f),
defaultCameraPosition(0, 10, 50), defaultGazePosition(0, 10, 0),
defaultScreenNumber(std::nullopt)
{}
simulationFPS(60.0f),
gravity(9.8f),
lightDirection(-0.5f, -1.0f, -0.5f),
defaultModelPosition(0.0f, 0.0f),
defaultScale(1.0f),
defaultCameraPosition(0, 10, 50),
defaultGazePosition(0, 10, 0),
defaultScreenNumber(std::nullopt) {}

Config Config::Parse(const std::filesystem::path& configFile) {
namespace fs = std::filesystem;

constexpr auto warnUnsupportedKey = [](const toml::value::key_type& k, const toml::value& v) {
constexpr auto warnUnsupportedKey = [](const toml::value::key_type& k,
const toml::value& v) {
// TODO: Error message should point key, not its value
constexpr std::string_view header = "[error]";
const std::string rawmsg = toml::format_error(
"Ignoring unsupported config key.",
v,
"Key is not supported: " + k
);
"Ignoring unsupported config key.", v, "Key is not supported: " + k);
std::string_view errmsg = rawmsg;
if (errmsg.starts_with(header)) {
errmsg.remove_prefix(header.size());
Expand Down Expand Up @@ -94,8 +95,8 @@ Config Config::Parse(const std::filesystem::path& configFile) {
c.weight = v.as_integer();
if (c.weight <= 0) {
const auto errmsg = toml::format_error(
"Invalid value for \"weight\"", v,
"Value must be bigger than or equals to 1.");
"Invalid value for \"weight\"", v,
"Value must be bigger than or equals to 1.");
Err::Log(errmsg);
}
} else if (k == "disabled") {
Expand Down
4 changes: 2 additions & 2 deletions config.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef CONFIG_HPP_
#define CONFIG_HPP_

#include "glm/vec2.hpp" // IWYU pragma: keep; supress warning from clangd.
#include "glm/vec3.hpp" // IWYU pragma: keep; supress warning from clangd.
#include <filesystem>
#include <optional>
#include <vector>
#include "glm/vec2.hpp" // IWYU pragma: keep; supress warning from clangd.
#include "glm/vec3.hpp" // IWYU pragma: keep; supress warning from clangd.

struct Config {
using Path = std::filesystem::path;
Expand Down
2 changes: 1 addition & 1 deletion constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ constexpr int PreferredSampleCount = 4;
constexpr float FPS = 60.0f;
constexpr float VmdFPS = 30.0f;
constexpr std::string_view DefaultLogFilePath = "";
}
} // namespace Constant

#endif // CONSTANT_HPP_
32 changes: 14 additions & 18 deletions image.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "stb_image.h"
#include "image.hpp"
#include "platform.hpp"
#include <cstdio>
#include <string_view>
#include "platform.hpp"
#include "stb_image.h"

#ifdef PLATFORM_WINDOWS
# include <windows.h>
#include <windows.h>
#endif

class File : private NonCopyable {
Expand All @@ -17,13 +17,12 @@ class File : private NonCopyable {
void Close();
operator FILE *();
operator bool() const;

private:
FILE *fp;
};

File::File() :
fp(nullptr)
{}
File::File() : fp(nullptr) {}

File::File(const std::string_view path) {
Open(path);
Expand All @@ -36,11 +35,10 @@ File::~File() {
void File::Open(const std::string_view path) {
#ifdef PLATFORM_WINDOWS
std::wstring wpath;
const int size = MultiByteToWideChar(
CP_UTF8, MB_COMPOSITE, path.data(), -1, nullptr, 0);
wpath.resize(size-1, '\0');
const int status = MultiByteToWideChar(
CP_UTF8, MB_COMPOSITE, path.data(), -1, wpath.data(), size);
const int size = MultiByteToWideChar(CP_UTF8, MB_COMPOSITE, path.data(), -1, nullptr, 0);
wpath.resize(size - 1, '\0');
const int status =
MultiByteToWideChar(CP_UTF8, MB_COMPOSITE, path.data(), -1, wpath.data(), size);
if (!status)
Err::Exit("String conversion failed: from:", path);
fp = _wfopen(wpath.c_str(), L"rb");
Expand All @@ -64,15 +62,13 @@ File::operator bool() const {
return fp != nullptr;
}

Image::Image() :
width(0), height(0), dataSize(0), hasAlpha(false)
{}
Image::Image() : width(0), height(0), dataSize(0), hasAlpha(false) {}

Image::Image(Image&& image) {
*this = std::move(image);
}

Image& Image::operator=(Image &&rhs) {
Image& Image::operator=(Image&& rhs) {
width = rhs.width;
height = rhs.height;
dataSize = rhs.dataSize;
Expand Down Expand Up @@ -104,7 +100,7 @@ bool Image::loadFromFile(const std::string_view path) {
else
hasAlpha = false;

uint8_t * const image = stbi_load_from_file(file, &width, &height, &comp, STBI_rgb_alpha);
uint8_t *const image = stbi_load_from_file(file, &width, &height, &comp, STBI_rgb_alpha);
dataSize = width * height * 4;
pixels.resize(dataSize);
std::copy(image, image + dataSize, pixels.data());
Expand All @@ -129,8 +125,8 @@ bool Image::loadFromMemory(const Resource::View& resource) {
else
hasAlpha = false;

uint8_t * const image =
stbi_load_from_memory(resource.data(), resource.length(), &width, &height, &comp, STBI_rgb_alpha);
uint8_t *const image = stbi_load_from_memory(
resource.data(), resource.length(), &width, &height, &comp, STBI_rgb_alpha);
dataSize = width * height * 4;
pixels.resize(dataSize);
std::copy(image, image + dataSize, pixels.data());
Expand Down
7 changes: 4 additions & 3 deletions image.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef IMAGE_HPP_
#define IMAGE_HPP_

#include "util.hpp"
#include "resources.hpp"
#include <vector>
#include "resources.hpp"
#include "util.hpp"

class Image : private NonCopyable {
public:
Expand All @@ -15,9 +15,10 @@ class Image : private NonCopyable {

Image();
Image(Image&& image);
Image &operator=(Image &&rhs);
Image& operator=(Image&& rhs);
bool loadFromFile(const std::string_view path);
bool loadFromMemory(const Resource::View& resource);

private:
};

Expand Down
2 changes: 1 addition & 1 deletion keyboard.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "keyboard.hpp"
#include <cstddef>
#include <array>
#include <cstddef>

namespace {
std::array<bool, static_cast<std::size_t>(Keycode::Count)> state({});
Expand Down
15 changes: 6 additions & 9 deletions keyboard.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#ifndef KEYBOARD_HPP_
#define KEYBOARD_HPP_

enum class Keycode {
Shift,
Count
};
enum class Keycode { Shift, Count };

namespace Keyboard {
bool IsKeyPressed(Keycode code);
void OnKeyDown(Keycode code);
void OnKeyUp(Keycode code);
void ResetAllState();
}
bool IsKeyPressed(Keycode code);
void OnKeyDown(Keycode code);
void OnKeyUp(Keycode code);
void ResetAllState();
} // namespace Keyboard

#endif // KEYBOARD_HPP_
5 changes: 2 additions & 3 deletions libs.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
#define SOKOL_IMPL

#if defined(PLATFORM_MAC)
# define SOKOL_METAL
#define SOKOL_METAL
#elif defined(PLATFORM_WINDOWS)
# define SOKOL_D3D11
#define SOKOL_D3D11
#endif

#include "sokol_gfx.h"
#include "sokol_time.h"


#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
6 changes: 3 additions & 3 deletions main.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef MAIN_HPP_
#define MAIN_HPP_

#include "glm/vec2.hpp" // IWYU pragma: keep; silence clangd.
#include "sokol_gfx.h"
#include <string_view>
#include "glm/vec2.hpp" // IWYU pragma: keep; silence clangd.
#include "sokol_gfx.h"

namespace Dialog {
void messageBox(std::string_view msg);
Expand All @@ -20,6 +20,6 @@ int getSampleCount();
// window.
glm::vec2 getMousePosition();
bool shouldEmphasizeModel();
}
} // namespace Context

#endif // MAIN_HPP_
Loading

0 comments on commit 32e0157

Please sign in to comment.