Преглед изворни кода

Merge branch 'master' into shapes

* master:
  Install clang-format

# Conflicts:
#	math/include/game/math/math_fwd.hpp
#	math/include/game/math/shape.hpp
#	math/src/common.cpp
#	math/src/shape.cpp
Sam Jaffe пре 6 година
родитељ
комит
720707bb5c

+ 108 - 0
.clang-format

@@ -0,0 +1,108 @@
+---
+Language:        Cpp
+# BasedOnStyle:  LLVM
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Right
+AlignOperands:   true
+AlignTrailingComments: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: true
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: true
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:   
+  AfterClass:      false
+  AfterControlStatement: false
+  AfterEnum:       false
+  AfterFunction:   false
+  AfterNamespace:  false
+  AfterObjCDeclaration: false
+  AfterStruct:     false
+  AfterUnion:      false
+  BeforeCatch:     false
+  BeforeElse:      false
+  IndentBraces:    false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Attach
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeColon
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: true
+ColumnLimit:     80
+CommentPragmas:  '^ IWYU pragma:'
+CompactNamespaces: true
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false
+DisableFormat:   false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+ForEachMacros:   
+  - foreach
+  - Q_FOREACH
+  - BOOST_FOREACH
+IncludeCategories: 
+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority:        2
+  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
+    Priority:        3
+  - Regex:           '.*'
+    Priority:        1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentWidth:     2
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: All
+ObjCBlockIndentWidth: 2
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 60
+PointerAlignment: Middle
+ReflowComments:  true
+SortIncludes:    true
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeParens: ControlStatements
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard:        Cpp11
+TabWidth:        8
+UseTab:          Never
+...
+

+ 3 - 3
engine/include/game/engine/engine_fwd.hpp

@@ -14,16 +14,16 @@ namespace engine {
   using raw_key_t = int;
   using key_enum_t = int;
   using scene_id_t = std::string;
-  
+
   using key_binding = std::unordered_map<raw_key_t, key_enum_t>;
 
   class game_dispatch;
   class scene;
-  
+
   namespace event {
     struct key_event;
     struct mouse_event;
   }
-  
+
   struct tick;
 }

+ 10 - 10
engine/include/game/engine/events.hpp

@@ -25,24 +25,24 @@ namespace engine {
       QUIT
     };
   }
-  
+
   namespace event {
     enum event_type {
-      PRESSED_MASK   = 0x1,
-      RELEASED_MASK  = 0x2,
-      KEY_MASK       = 0x4,
-      MOUSE_MASK     = 0x8,
-      KEY_PRESSED    = KEY_MASK   | PRESSED_MASK ,
-      KEY_RELEASED   = KEY_MASK   | RELEASED_MASK,
-      MOUSE_PRESSED  = MOUSE_MASK | PRESSED_MASK ,
+      PRESSED_MASK = 0x1,
+      RELEASED_MASK = 0x2,
+      KEY_MASK = 0x4,
+      MOUSE_MASK = 0x8,
+      KEY_PRESSED = KEY_MASK | PRESSED_MASK,
+      KEY_RELEASED = KEY_MASK | RELEASED_MASK,
+      MOUSE_PRESSED = MOUSE_MASK | PRESSED_MASK,
       MOUSE_RELEASED = MOUSE_MASK | RELEASED_MASK
     };
-    
+
     struct key_event {
       key_enum_t key;
       event_type type;
     };
-    
+
     struct mouse_event {
       math::vec2 current_mouse_position;
       event_type type;

+ 19 - 18
engine/include/game/engine/game_dispatch.hpp

@@ -14,42 +14,43 @@
 #include "game/math/math_fwd.hpp"
 #include "vector/vector.hpp"
 
-#include "time.hpp"
 #include "engine_fwd.hpp"
 #include "events.hpp"
+#include "time.hpp"
 
 namespace engine {
   class game_dispatch {
   public:
-    game_dispatch( );
-    
-    void process_key_event( raw_key_t key, bool press );
-    void process_mouse_event( math::vec2 click, bool press );
-    
-    [[noreturn]] void gameloop( );
-    void quit( );
+    game_dispatch();
+
+    void process_key_event(raw_key_t key, bool press);
+    void process_mouse_event(math::vec2 click, bool press);
+
+    [[noreturn]] void gameloop();
+    void quit();
+
   private:
-    void single_frame( tick t ) { }
-    tick get_tick( );
-    tick next_frame( );
-    void cleanup_resources( ) { } // TODO ???
+    void single_frame(tick t) {}
+    tick get_tick();
+    tick next_frame();
+    void cleanup_resources() {} // TODO ???
   private:
     math::vec2 screen_size;
     duration minimum_frame_duration;
 
     using scene_t = std::unique_ptr<scene>;
     std::unordered_map<scene_id_t, scene_t> scenes;
-    
+
     bool running = true;
     timestamp current_timestamp;
     struct current_scene_info {
-      current_scene_info( );
-      current_scene_info( scene * );
-      
+      current_scene_info();
+      current_scene_info(scene *);
+
       scene * ptr; // weak
-      
+
       std::string current_scene_id; // metadata
-      math::vec2 local_size; // metadata
+      math::vec2 local_size;        // metadata
       bool is_mouse_event;
       bool is_keyboard_event;
     } curr_scene;

+ 14 - 12
engine/include/game/engine/scene.hpp

@@ -10,26 +10,28 @@
 #include <memory>
 
 #include "game/math/math_fwd.hpp"
-#include "vector/vector.hpp"
 #include "util/identity.hpp"
+#include "vector/vector.hpp"
 
 #include "engine_fwd.hpp"
 
-namespace engine {  
+namespace engine {
   class scene : public identity<scene, std::string> {
   public:
     using identity<scene, std::string>::identity;
-    virtual ~scene( );
-    
-    virtual void update( tick );
-    virtual void render( );
-    virtual void handle_key_event( event::key_event evt );
-    virtual void handle_mouse_event( event::mouse_event evt );
-    
-    math::vec2 get_size( ) const;
-    key_binding const & get_binding( ) const;
+    virtual ~scene();
+
+    virtual void update(tick);
+    virtual void render();
+    virtual void handle_key_event(event::key_event evt);
+    virtual void handle_mouse_event(event::mouse_event evt);
+
+    math::vec2 get_size() const;
+    key_binding const & get_binding() const;
+
   protected:
-    void change_scene( std::string const & scene_id );
+    void change_scene(std::string const & scene_id);
+
   private:
     math::vec2 local_scene_dimension;
     key_binding keys;

+ 3 - 3
engine/include/game/engine/time.hpp

@@ -13,7 +13,7 @@ namespace engine {
   using clock = std::chrono::steady_clock;
   using timestamp = std::chrono::time_point<clock>;
   using duration = clock::duration;
-  
+
   struct fps {
     static duration const v24;
     static duration const v30;
@@ -21,9 +21,9 @@ namespace engine {
     static duration const v120;
     static duration const v144;
   };
-  
+
   struct tick {
     timestamp now;
     duration since;
-  };  
+  };
 }

+ 52 - 65
engine/src/game_dispatch.cpp

@@ -14,80 +14,67 @@
 
 namespace engine {
   namespace {
-    event::event_type mask( event::event_type t, bool pressed ) {
-      return static_cast<event::event_type>( t | ( pressed ? event::PRESSED_MASK : event::RELEASED_MASK ) );
+    event::event_type mask(event::event_type t, bool pressed) {
+      return static_cast<event::event_type>(
+          t | (pressed ? event::PRESSED_MASK : event::RELEASED_MASK));
     }
   }
-  
-  game_dispatch::game_dispatch( )
-  : screen_size( { 1920.f, 1080.f } )
-  , minimum_frame_duration( engine::fps::v60 )
-  , scenes( )
-  , current_timestamp( clock::now( ) )
-  , curr_scene( ) {
-    
-  }
-  
-  game_dispatch::current_scene_info::current_scene_info( ) {
-    
-  }
-  
-  game_dispatch::current_scene_info::current_scene_info( scene * curr )
-  : ptr( curr )
-  , current_scene_id( ptr->id )
-  , local_size( ptr->get_size( ) ) {
-    
-  }
-  
-  void game_dispatch::process_key_event( raw_key_t key, bool press ) {
-    if ( ! curr_scene.is_keyboard_event ) return;
-    auto const & binding = curr_scene.ptr->get_binding( );
-    auto it = binding.find( key );
-    if ( it == binding.end( ) ) return;
-
-    curr_scene.ptr->handle_key_event( {
-      it->second,
-      mask( event::KEY_MASK, press )
-    } );
+
+  game_dispatch::game_dispatch()
+      : screen_size({1920.f, 1080.f}), minimum_frame_duration(engine::fps::v60),
+        scenes(), current_timestamp(clock::now()), curr_scene() {}
+
+  game_dispatch::current_scene_info::current_scene_info() {}
+
+  game_dispatch::current_scene_info::current_scene_info(scene * curr)
+      : ptr(curr), current_scene_id(ptr->id), local_size(ptr->get_size()) {}
+
+  void game_dispatch::process_key_event(raw_key_t key, bool press) {
+    if (!curr_scene.is_keyboard_event) return;
+    auto const & binding = curr_scene.ptr->get_binding();
+    auto it = binding.find(key);
+    if (it == binding.end()) return;
+
+    curr_scene.ptr->handle_key_event(
+        {it->second, mask(event::KEY_MASK, press)});
   }
-  
-  void game_dispatch::process_mouse_event( math::vec2 mouse_pos, bool press ) {
-    if ( ! curr_scene.is_mouse_event ) return;
-    math::vec2 local_scene_position = mouse_pos * curr_scene.local_size / screen_size;
-
-    curr_scene.ptr->handle_mouse_event( {
-      local_scene_position,
-      mask( event::MOUSE_MASK, press )
-    } );
+
+  void game_dispatch::process_mouse_event(math::vec2 mouse_pos, bool press) {
+    if (!curr_scene.is_mouse_event) return;
+    math::vec2 local_scene_position =
+        mouse_pos * curr_scene.local_size / screen_size;
+
+    curr_scene.ptr->handle_mouse_event(
+        {local_scene_position, mask(event::MOUSE_MASK, press)});
   }
-  
-  tick game_dispatch::get_tick( ) {
-    timestamp now = clock::now( );
-    return { now, now - current_timestamp };
+
+  tick game_dispatch::get_tick() {
+    timestamp now = clock::now();
+    return {now, now - current_timestamp};
   }
-  
-  tick game_dispatch::next_frame( ) {
-    tick t = get_tick( );
-    
-    while ( t.since < minimum_frame_duration ) {
-      std::this_thread::sleep_for( minimum_frame_duration - t.since );
-      t = get_tick( );
+
+  tick game_dispatch::next_frame() {
+    tick t = get_tick();
+
+    while (t.since < minimum_frame_duration) {
+      std::this_thread::sleep_for(minimum_frame_duration - t.since);
+      t = get_tick();
     }
-    
+
     current_timestamp = t.now;
     return t;
   }
-  
-  void game_dispatch::gameloop( ) try {
-    while ( running ) { single_frame( next_frame( ) ); }
-    cleanup_resources( );
-    exit( EXIT_SUCCESS );
-  } catch ( std::exception const & ex ) {
+
+  void game_dispatch::gameloop() try {
+    while (running) {
+      single_frame(next_frame());
+    }
+    cleanup_resources();
+    exit(EXIT_SUCCESS);
+  } catch (std::exception const & ex) {
     // todo: logging
-    exit( EXIT_FAILURE );
-  }
-  
-  void game_dispatch::quit( ) {
-    running = false;
+    exit(EXIT_FAILURE);
   }
+
+  void game_dispatch::quit() { running = false; }
 }

+ 16 - 28
engine/src/scene.cpp

@@ -11,34 +11,22 @@
 #include "game/engine/game_dispatch.hpp"
 
 namespace engine {
-  
-  
-  scene::~scene( ) { }
-  
-  void scene::update( tick ) {
-    
-  }
-  
-  void scene::render( ) {
-    
-  }
-  
-  void scene::handle_key_event( event::key_event evt ) {
-    if ( evt.type & event::PRESSED_MASK &&
-        evt.key == key::QUIT ) {
-      dispatch.lock()->quit( );
+
+  scene::~scene() {}
+
+  void scene::update(tick) {}
+
+  void scene::render() {}
+
+  void scene::handle_key_event(event::key_event evt) {
+    if (evt.type & event::PRESSED_MASK && evt.key == key::QUIT) {
+      dispatch.lock()->quit();
     }
   }
-  
-  void scene::handle_mouse_event( event::mouse_event evt ) {
-    
-  }
-  
-  math::vec2 scene::get_size( ) const {
-    return local_scene_dimension;
-  }
-  
-  key_binding const & scene::get_binding( ) const {
-    return keys;
-  }
+
+  void scene::handle_mouse_event(event::mouse_event evt) {}
+
+  math::vec2 scene::get_size() const { return local_scene_dimension; }
+
+  key_binding const & scene::get_binding() const { return keys; }
 }

+ 6 - 6
engine/src/time.cpp

@@ -10,10 +10,10 @@
 namespace engine {
   using std::chrono::duration_cast;
   using std::chrono::nanoseconds;
-  
-  duration const fps::v24 { nanoseconds{ 41666666 } };
-  duration const fps::v30 { nanoseconds{ 33333333 } };
-  duration const fps::v60 { nanoseconds{ 16666666 } };
-  duration const fps::v120{ nanoseconds{  8333333 } };
-  duration const fps::v144{ nanoseconds{  6944444 } };
+
+  duration const fps::v24{nanoseconds{41666666}};
+  duration const fps::v30{nanoseconds{33333333}};
+  duration const fps::v60{nanoseconds{16666666}};
+  duration const fps::v120{nanoseconds{8333333}};
+  duration const fps::v144{nanoseconds{6944444}};
 }

+ 1 - 0
graphics/include/game/graphics/material.hpp

@@ -15,6 +15,7 @@ namespace graphics {
   class material : public identity<material> {
   public:
     program const shaders;
+
   private:
     material(unsigned int, program);
   };

+ 1 - 1
graphics/include/game/graphics/object.hpp

@@ -18,7 +18,7 @@ namespace graphics {
     math::vec2 corner;
     math::vec2 size;
   };
-  
+
   struct object {
     bound location;
     math::quad points;

+ 6 - 5
graphics/include/game/graphics/texture.hpp

@@ -8,21 +8,22 @@
 #pragma once
 
 #include "game/math/math_fwd.hpp"
-#include "vector/vector.hpp"
 #include "util/identity.hpp"
+#include "vector/vector.hpp"
 
 namespace graphics {
   class texture : public identity<texture> {
   public:
     static texture create(std::string const & imagefile);
-    
+
     static texture const WHITE;
     static texture const DARK_YELLOW;
     static texture const LIGHT_BLUE;
-    
+
     math::vec2i const size;
+
   private:
-    static texture create( unsigned char *, math::vec2i );
-    texture( unsigned int, math::vec2i = math::vec2i{{0, 0}} );
+    static texture create(unsigned char *, math::vec2i);
+    texture(unsigned int, math::vec2i = math::vec2i{{0, 0}});
   };
 }

+ 22 - 24
graphics/src/texture.cpp

@@ -9,8 +9,8 @@
 
 #include "scope_guard/scope_guard.hpp"
 
-#include <unordered_map>
 #include <string>
+#include <unordered_map>
 
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wcomma"
@@ -18,56 +18,54 @@
 #include "stb/stb_image.h"
 #pragma clang diagnostic pop
 
-unsigned char * stbi_load( char const *, int *, int *, int *, int );
-void stbi_image_free( void * );
+unsigned char * stbi_load(char const *, int *, int *, int *, int);
+void stbi_image_free(void *);
 
-template <typename T, typename Key = std::string>
-class private_factory {
+template <typename T, typename Key = std::string> class private_factory {
 public:
   using key_t = Key;
-  
-  template <typename... Args>
-  static T create(key_t const & key) {
+
+  template <typename... Args> static T create(key_t const & key) {
     auto found = instances.find(key);
     if (found == instances.end()) {
       found = instances.emplace(key, T::create(key)).first;
     }
     return found->second;
   }
+
 private:
   static std::unordered_map<key_t, T> instances;
 };
 
 namespace graphics { namespace detail { namespace texture {
   struct format {};
-      
+
   std::unordered_map<std::string, ::graphics::texture> g_textures;
-    
+
   unsigned int init(format = {}, math::vec2i = {}, unsigned char * = {}) {
     throw; // TODO implement
   }
-} } }
+}}}
 
 namespace graphics {
-  texture texture::create( std::string const & imagefile ) {
+  texture texture::create(std::string const & imagefile) {
     using detail::texture::g_textures;
     auto found = g_textures.find(imagefile);
     if (found != g_textures.end()) { return found->second; }
-    
+
     int components = 0;
     math::vec2i size;
-    unsigned char * data = stbi_load(imagefile.c_str(), &size.x(), &size.y(),
-                                     &components, 0);
-    scope(exit) { stbi_image_free( data ); };
-    texture tex{ detail::texture::init({}, size, data), size };
+    unsigned char * data =
+        stbi_load(imagefile.c_str(), &size.x(), &size.y(), &components, 0);
+    scope(exit) { stbi_image_free(data); };
+    texture tex{detail::texture::init({}, size, data), size};
     return g_textures.emplace(imagefile, std::move(tex)).first->second;
   }
-  
-  texture texture::create( unsigned char * data, math::vec2i size ) {
-    return { detail::texture::init({}, size, data), size };
-  }
-  
-  texture::texture( unsigned int id, math::vec2i sz )
-  : identity<graphics::texture>(id), size(sz) {
+
+  texture texture::create(unsigned char * data, math::vec2i size) {
+    return {detail::texture::init({}, size, data), size};
   }
+
+  texture::texture(unsigned int id, math::vec2i sz)
+      : identity<graphics::texture>(id), size(sz) {}
 }

+ 2 - 2
math/include/game/math/angle.hpp

@@ -12,14 +12,14 @@ namespace math {
     degree(double v);
     double value;
   };
-  
+
   struct radian {
     radian(double v);
     radian(degree d);
     operator degree() const;
     double value;
   };
-  
+
   double sin(radian r);
   double cos(radian r);
 }

+ 3 - 3
math/include/game/math/common.hpp

@@ -12,11 +12,11 @@
 namespace math {
   vec2 rotate(vec2 const & center, vec2 const & point, radian r);
   dim2::quad rotate(vec2 const & center, dim2::quad const & q, radian r);
-  
+
   bool contains(dim2::line const & shape, dim2::point const & pt);
   bool contains(dim2::circle const & shape, dim2::point const & pt);
   bool contains(dim2::quad const & shape, dim2::point const & pt);
-  
+
   bool intersects(dim2::line const & lhs, dim2::line const & rhs);
   bool intersects(dim2::line const & lhs, dim2::circle const & rhs);
   bool intersects(dim2::line const & lhs, dim2::quad const & rhs);
@@ -28,7 +28,7 @@ namespace math {
   bool intersects(dim2::circle const & lhs, dim2::circle const & rhs);
 }
 
-namespace math {  
+namespace math {
   inline bool intersects(dim2::quad const & lhs, dim2::line const & rhs) {
     return intersects(rhs, lhs);
   }

+ 11 - 18
math/include/game/math/compare.hpp

@@ -4,44 +4,37 @@
 #include <cmath>
 
 namespace math {
-  template <typename T>
-  T safe_div(T num, T denom) {
+  template <typename T> T safe_div(T num, T denom) {
     return (num == denom && denom == 0) ? 0 : num / denom;
   }
-  
-  template <typename T>
-  bool between_exclusive(T val, T min, T max) {
+
+  template <typename T> bool between_exclusive(T val, T min, T max) {
     return val >= min && val < max;
   }
 
-  template <typename T>
-  bool between(T val, T min, T max) {
+  template <typename T> bool between(T val, T min, T max) {
     return val >= min && val <= max;
   }
 
-  template <typename T>
-  bool approx_equal(T lhs, T rhs, T eps) {
+  template <typename T> bool approx_equal(T lhs, T rhs, T eps) {
     T const a = std::abs(lhs);
     T const b = std::abs(rhs);
     return std::abs(lhs - rhs) <= (std::max(a, b) * eps);
   }
-  
-  template <typename T>
-  bool essentially_equal(T lhs, T rhs, T eps) {
+
+  template <typename T> bool essentially_equal(T lhs, T rhs, T eps) {
     T const a = std::abs(lhs);
     T const b = std::abs(rhs);
     return std::abs(lhs - rhs) <= (std::min(a, b) * eps);
   }
-  
-  template <typename T>
-  bool definitely_greater(T lhs, T rhs, T eps) {
+
+  template <typename T> bool definitely_greater(T lhs, T rhs, T eps) {
     T const a = std::abs(lhs);
     T const b = std::abs(rhs);
     return (lhs - rhs) > (std::max(a, b) * eps);
   }
-  
-  template <typename T>
-  bool definitely_less(T lhs, T rhs, T eps) {
+
+  template <typename T> bool definitely_less(T lhs, T rhs, T eps) {
     T const a = std::abs(lhs);
     T const b = std::abs(rhs);
     return (rhs - lhs) > (std::max(a, b) * eps);

+ 7 - 8
math/include/game/math/math_fwd.hpp

@@ -16,18 +16,17 @@ namespace math {
   }
   namespace matrix {
     template <typename, size_t, size_t> class matrix;
-    
-    template <typename T, size_t N>
-    using square_matrix = matrix<T, N, N>;
+
+    template <typename T, size_t N> using square_matrix = matrix<T, N, N>;
   }
 }
 
 namespace math {
   using vec2i = vector::vector<int, 2>;
-  using vec2  = vector::vector<float, 2>;
-  using vec3  = vector::vector<float, 3>;
-  using rgba  = vector::vector<uint8_t, 4>;
-  
+  using vec2 = vector::vector<float, 2>;
+  using vec3 = vector::vector<float, 3>;
+  using rgba = vector::vector<uint8_t, 4>;
+
   using matr4 = matrix::matrix<float, 4, 4>;
 
   namespace dim2 {
@@ -38,7 +37,7 @@ namespace math {
     struct rectangle;
     struct square;
   }
-  
+
   struct degree;
   struct radian;
 }

+ 8 - 8
math/include/game/math/shape.hpp

@@ -13,41 +13,41 @@
 
 namespace math { namespace dim2 {
   using point = vec2;
-  
+
   struct line {
     float length() const;
     float slope() const;
     point first, second;
   };
-  
+
   struct circle {
     point center;
     float radius;
   };
-  
+
   struct quad {
     point ll, lr, ur, ul;
   };
-  
+
   struct rectangle {
     operator quad() const;
     point origin, size;
   };
-  
+
   struct square {
     operator rectangle() const;
     operator quad() const;
     point origin;
     float size;
   };
-} }
+}}
 
 namespace math { namespace shapes {
   std::vector<dim2::line> segments(dim2::quad const & shape);
-} }
+}}
 
 namespace math { namespace lines {
   bool parallel(dim2::line const & lhs, dim2::line const & rhs);
   dim2::point intersection(dim2::line const & lhs, dim2::line const & rhs);
   dim2::line orthogonal(dim2::line const & from, dim2::point const & to);
-} }
+}}

+ 3 - 3
math/src/angle.cpp

@@ -11,11 +11,11 @@
 
 namespace math {
   degree::degree(double v) : value(v) {}
-  
+
   radian::radian(double v) : value(v) {}
   radian::radian(degree d) : value(d.value / M_2_PI) {}
-  radian::operator degree() const { return { value * M_2_PI }; }
-  
+  radian::operator degree() const { return {value * M_2_PI}; }
+
   double sin(radian r) { return std::sin(r.value); }
   double cos(radian r) { return std::cos(r.value); }
 }

+ 29 - 41
math/src/common.cpp

@@ -18,51 +18,41 @@ namespace math {
     vec2 trans = p - c;
     vec2 vcos = trans * static_cast<float>(cos(r));
     vec2 vsin = trans * static_cast<float>(sin(r));
-    return {{
-      vcos[0] - vsin[1] + c[0],
-      vsin[0] - vcos[1] + c[1]
-    }};
+    return {{vcos[0] - vsin[1] + c[0], vsin[0] - vcos[1] + c[1]}};
   }
-  
-  dim2::quad rotate(vec2 const & c, dim2::quad const & q, radian r) {
-    return {
-      rotate(c, q.ll, r),
-      rotate(c, q.lr, r),
-      rotate(c, q.ur, r),
-      rotate(c, q.ul, r)
-    };
+
+  quad rotate(vec2 const & c, quad const & q, radian r) {
+    return {rotate(c, q.ll, r), rotate(c, q.lr, r), rotate(c, q.ur, r),
+            rotate(c, q.ul, r)};
   }
-  
-  enum orientation {
-    colinear = 0, clockwise = 1, anticlockwise = 2
-  };
-  
+
+  enum orientation { colinear = 0, clockwise = 1, anticlockwise = 2 };
+
   orientation orient(dim2::line const & ln, dim2::point const & pt) {
     auto val = (ln.second[1] - ln.first[1]) * (pt[0] - ln.second[0]) -
-        (ln.second[0] - ln.first[0]) * (pt[1] - ln.second[1]);
-    
+               (ln.second[0] - ln.first[0]) * (pt[1] - ln.second[1]);
+
     if (val == 0) return colinear;
     return (val > 0) ? clockwise : anticlockwise;
   }
-  
+
   bool contains(dim2::line const & ln, dim2::point const & pt) {
     auto xs = std::minmax(ln.first[0], ln.second[0]);
     auto ys = std::minmax(ln.first[1], ln.second[1]);
-    return orient(ln, pt) == colinear &&
-        between(pt[0], xs.first, xs.second) &&
-        between(pt[1], ys.first, ys.second);
+    return orient(ln, pt) == colinear && between(pt[0], xs.first, xs.second) &&
+           between(pt[1], ys.first, ys.second);
   }
-  
+
   bool contains(dim2::circle const & shape, dim2::point const & pt) {
     vec2 const delta = pt - shape.center;
     return delta.dot(delta) <= std::pow(shape.radius, 2);
   }
-  
+
   static dim2::line ray_x(dim2::point const & pt, dim2::line const & l) {
     auto x_inf = std::max({l.first[0], l.second[0], pt[0]}) + 1;
     return {pt, {{x_inf, pt[1]}}};
   }
-  
+
   static bool contains(std::vector<dim2::line> const & segments,
                        dim2::point const & pt) {
     int hits = 0;
@@ -73,11 +63,11 @@ namespace math {
     }
     return (hits & 1) == 1;
   }
-  
+
   bool contains(dim2::quad const & shape, dim2::point const & pt) {
     return contains(shapes::segments(shape), pt);
   }
-  
+
   bool intersects(dim2::line const & lhs, dim2::line const & rhs) {
     // https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
     // Find the four orientations needed for general and special cases
@@ -85,43 +75,41 @@ namespace math {
     orientation o2 = orient(lhs, rhs.second);
     orientation o3 = orient(rhs, lhs.first);
     orientation o4 = orient(rhs, lhs.second);
-    
+
     // General Case: Lines cross through each other
-    if (o1 != o2 && o3 != o4)
-      return true;
-    
+    if (o1 != o2 && o3 != o4) return true;
+
     // Special Cases: one of the points exists on the other line
     return contains(lhs, rhs.first) || contains(lhs, rhs.second) ||
-        contains(rhs, lhs.first) || contains(rhs, lhs.second);
+           contains(rhs, lhs.first) || contains(rhs, lhs.second);
   }
-  
+
   bool intersects(dim2::line const & lhs, dim2::circle const & rhs) {
     dim2::line const orth = lines::orthogonal(lhs, rhs.center);
     vec2 const delta = orth.second - orth.first;
     return delta.dot(delta) <= std::pow(rhs.radius, 2);
   }
-  
+
   bool intersects(dim2::line const & lhs, dim2::quad const & rhs) {
     std::vector<dim2::line> segments = shapes::segments(rhs);
     auto lhs_intersects = [&lhs](dim2::line const & ln) {
       return intersects(lhs, ln);
     };
     return std::any_of(segments.begin(), segments.end(), lhs_intersects) ||
-        contains(segments, lhs.first) ||
-        contains(segments, lhs.second);
+           contains(segments, lhs.first) || contains(segments, lhs.second);
   }
-  
+
   bool intersects(dim2::quad const & lhs, dim2::circle const & rhs) {
     std::vector<dim2::line> segments = shapes::segments(lhs);
     auto rhs_intersects = [&rhs](dim2::line const & ln) {
       return intersects(ln, rhs);
     };
     return std::any_of(segments.begin(), segments.end(), rhs_intersects) ||
-        contains(lhs, rhs.center);
+           contains(lhs, rhs.center);
   }
-  
+
   bool intersects(dim2::quad const & lhs, dim2::quad const & rhs);
-  
+
   bool intersects(dim2::circle const & lhs, dim2::circle const & rhs) {
     vec2 const delta = rhs.center - lhs.center;
     return delta.dot(delta) <= std::pow(lhs.radius + rhs.radius, 2);

+ 19 - 30
math/src/shape.cpp

@@ -7,57 +7,46 @@
 
 #include "game/math/shape.hpp"
 
-#include <iostream>
 #include <vector>
 
 #include "game/math/compare.hpp"
 
 namespace math { namespace dim2 {
-  float line::length() const {
-    return (second - first).magnitude();
-  }
-    
+  float line::length() const { return (second - first).magnitude(); }
+
   float line::slope() const {
     return safe_div(second[1] - first[1], second[0] - first[0]);
   }
-  
+
   rectangle::operator quad() const {
-    return {
-      origin,
-      origin + point{{size.x(), 0.0}},
-      origin + size,
-      origin + point{{0.0, size.y()}}
-    };
+    return {origin, origin + vec2{{size.x(), 0.0}}, origin + size,
+            origin + vec2{{0.0, size.y()}}};
   }
 
   square::operator rectangle() const {
-    return { origin, origin + vec2{{size, size}} };
+    return {origin, origin + vec2{{size, size}}};
   }
-  
+
   square::operator quad() const {
-    return {
-      origin,
-      origin + point{{size, 0.0 }},
-      origin + point{{size, size}},
-      origin + point{{ 0.0, size}}
-    };
+    return {origin, origin + vec2{{size, 0.0}}, origin + vec2{{size, size}},
+            origin + vec2{{0.0, size}}};
   }
-} }
+}}
 
 namespace math { namespace shapes {
   std::vector<dim2::line> segments(dim2::quad const & shape) {
-    return {
-      {shape.ll, shape.lr}, {shape.lr, shape.ur},
-      {shape.ur, shape.ul}, {shape.ul, shape.ll}
-    };
+    return {{shape.ll, shape.lr},
+            {shape.lr, shape.ur},
+            {shape.ur, shape.ul},
+            {shape.ul, shape.ll}};
   }
-} }
+}}
 
 namespace math { namespace lines {
   bool parallel(dim2::line const & lhs, dim2::line const & rhs) {
     return lhs.slope() == rhs.slope();
   }
-  
+
   inline dim2::point intersection(float b1, float s1, float b2, float s2) {
     float const x = safe_div(b1 + b2, s1 - s2);
     return {{x, b1 + x * s1}};
@@ -70,7 +59,7 @@ namespace math { namespace lines {
     float y2 = p2[0] == 0 ? 0 : s2 * p2[0];
     return intersection(p1[1] - y1, s1, p2[1] - y2, s2);
   }
-  
+
   dim2::point intersection(dim2::line const & lhs, dim2::line const & rhs) {
     return intersection(lhs.first, lhs.slope(), rhs.first, rhs.slope());
   }
@@ -78,6 +67,6 @@ namespace math { namespace lines {
   dim2::line orthogonal(dim2::line const & from, dim2::point const & to) {
     float const slope = from.slope();
     if (slope == 0 || isinf(slope)) { return {to, {{to[0], from.first[1]}}}; }
-    return {to, intersection(from.first, slope, to, -1/slope)};
+    return {to, intersection(from.first, slope, to, -1 / slope)};
   }
-} }
+}}

+ 10 - 10
math/test/common_test.cxx

@@ -48,12 +48,12 @@ TEST_P(LineTest, ColinearOutsideDoesNotContain) {
   EXPECT_FALSE(math::contains(ln, pt));
 }
 
-std::vector<point> const line_ends{
-  {{1, 1}}, {{0, 1}}, {{1, 0}}, {{-1, -1}}, {{0, -1}}, {{-1, 0}}
-};
+std::vector<point> const line_ends{{{1, 1}},   {{0, 1}},  {{1, 0}},
+                                   {{-1, -1}}, {{0, -1}}, {{-1, 0}}};
 
 INSTANTIATE_TEST_CASE_P(Contains, LineTest,
-    Combine(ValuesIn(line_ends), ValuesIn(generate(0, 100, 10))));
+                        Combine(ValuesIn(line_ends),
+                                ValuesIn(generate(0, 100, 10))));
 
 struct UnitCircleTest : TestWithParam<point> {};
 
@@ -72,7 +72,7 @@ point unit_circle_angle(math::degree degs) {
 }
 
 INSTANTIATE_TEST_CASE_P(Contains, UnitCircleTest,
-    ValuesIn(generate(0.0, 360.0, 5.0, unit_circle_angle)));
+                        ValuesIn(generate(0.0, 360.0, 5.0, unit_circle_angle)));
 
 struct UnitSquareTest : TestWithParam<std::tuple<float, float>> {};
 
@@ -84,12 +84,12 @@ TEST_P(UnitSquareTest, PointInSquare) {
 
 TEST_F(UnitSquareTest, PointOutsideSquare) {
   square unit{{{0, 0}}, 1};
-  EXPECT_FALSE(math::contains(unit, {{ 0.f,  1.1f}}));
-  EXPECT_FALSE(math::contains(unit, {{ 0.f, -0.1f}}));
+  EXPECT_FALSE(math::contains(unit, {{0.f, 1.1f}}));
+  EXPECT_FALSE(math::contains(unit, {{0.f, -0.1f}}));
   EXPECT_FALSE(math::contains(unit, {{-0.1f, 0.0f}}));
-  EXPECT_FALSE(math::contains(unit, {{ 1.1f, 0.0f}}));
+  EXPECT_FALSE(math::contains(unit, {{1.1f, 0.0f}}));
 }
 
 INSTANTIATE_TEST_CASE_P(Contains, UnitSquareTest,
-    Combine(ValuesIn(generate(0.f, 1.f, 0.25f)),
-            ValuesIn(generate(0.f, 1.f, 0.25f))));
+                        Combine(ValuesIn(generate(0.f, 1.f, 0.25f)),
+                                ValuesIn(generate(0.f, 1.f, 0.25f))));

+ 30 - 37
math/test/shape_test.cxx

@@ -18,26 +18,25 @@ using namespace testing;
 struct FromOriginTest : TestWithParam<line> {};
 
 TEST_P(FromOriginTest, IntersectsAtOrigin) {
-  line l1 = { GetParam().first, {{0, 0}} };
-  line l2 = { {{0, 0}}, GetParam().second };
-  
-  EXPECT_THAT(math::lines::intersection(l1, l2),
-              Eq(point{{0, 0}}));
+  line l1 = {GetParam().first, {{0, 0}}};
+  line l2 = {{{0, 0}}, GetParam().second};
+
+  EXPECT_THAT(math::lines::intersection(l1, l2), Eq(point{{0, 0}}));
 }
 
 std::vector<line> const point_pairs{
-  {{{1, 1}}, {{0, 0}}},   // 0 length
-  {{{1, 1}}, {{1, 0}}},   // -45deg
-  {{{1, 1}}, {{0, 1}}},   // +45deg
-  {{{1, 1}}, {{1, 1}}},   // +0deg
-  {{{1, 1}}, {{2, 1}}},   // -18deg (approx)
-  {{{1, 1}}, {{1, 2}}},   // +18deg (approx)
-  {{{2, 3}}, {{1, 2}}},   //
-  {{{1, 1}}, {{-1,  0}}}, // +135deg
-  {{{1, 1}}, {{ 0, -1}}}, // -135deg
-  {{{1, 1}}, {{-1, -1}}}, // +180deg
-  {{{1, 1}}, {{ 1, -1}}}, // +90deg
-  {{{1, 1}}, {{-1,  1}}}, // -90deg
+    {{{1, 1}}, {{0, 0}}},   // 0 length
+    {{{1, 1}}, {{1, 0}}},   // -45deg
+    {{{1, 1}}, {{0, 1}}},   // +45deg
+    {{{1, 1}}, {{1, 1}}},   // +0deg
+    {{{1, 1}}, {{2, 1}}},   // -18deg (approx)
+    {{{1, 1}}, {{1, 2}}},   // +18deg (approx)
+    {{{2, 3}}, {{1, 2}}},   //
+    {{{1, 1}}, {{-1, 0}}},  // +135deg
+    {{{1, 1}}, {{0, -1}}},  // -135deg
+    {{{1, 1}}, {{-1, -1}}}, // +180deg
+    {{{1, 1}}, {{1, -1}}},  // +90deg
+    {{{1, 1}}, {{-1, 1}}},  // -90deg
 };
 
 INSTANTIATE_TEST_CASE_P(LineIntersection, FromOriginTest,
@@ -49,23 +48,20 @@ TEST_P(UnitLineTest, OrthoOnIntersection) {
   line const ln{{{0, 0}}, {{1, 0}}};
   point const pt = GetParam();
   line const expected{pt, {{pt[0], 0}}};
-  EXPECT_THAT(math::lines::orthogonal(ln, pt),
-              Eq(expected));
+  EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
 }
 
 TEST_P(UnitLineTest, OrthoOnIntersectionY) {
   line const ln{{{0, 0}}, {{0, 1}}};
   point const pt = GetParam();
   line const expected{pt, {{pt[0], 0}}};
-  EXPECT_THAT(math::lines::orthogonal(ln, pt),
-              Eq(expected));
+  EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
 }
 
 std::vector<point> x_orthos{
-  {{0, 1}}, {{1, 1}}, {{1, 0}}, {{-1, 0}}, {{0, -1}}, {{-1, -1}},
-  {{0, 2}}, {{2, 2}}, {{2, 0}}, {{-2, 0}}, {{0, -2}}, {{-2, -2}},
-  {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}
-};
+    {{0, 1}}, {{1, 1}}, {{1, 0}},  {{-1, 0}}, {{0, -1}}, {{-1, -1}},
+    {{0, 2}}, {{2, 2}}, {{2, 0}},  {{-2, 0}}, {{0, -2}}, {{-2, -2}},
+    {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}};
 
 INSTANTIATE_TEST_CASE_P(LineOrthogonal, UnitLineTest, ValuesIn(x_orthos));
 
@@ -75,18 +71,15 @@ TEST_P(DiagonalTest, OrthoOnIntersection) {
   line const ln{{{0, 0}}, {{1, 1}}};
   point const pt = GetParam().first;
   line const expected{pt, {{GetParam().second, GetParam().second}}};
-  EXPECT_THAT(math::lines::orthogonal(ln, pt),
-              Eq(expected));
+  EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
 }
 
 std::vector<std::pair<point, float>> diag_orthos{
-  {{{0, 1}}, 0.5f}, {{{1, 1}}, 1.f}, {{{1, 0}}, 0.5f},
-  {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f},
-  {{{0, 2}}, 1.f}, {{{2, 2}}, 2.f}, {{{2, 0}}, 1.f},
-  {{{-2, 0}}, -1.f}, {{{0, -2}}, -1.f}, {{{-2, -2}}, -2.f},
-  {{{2, 1}}, 1.5f}, {{{1, 2}}, 1.5f}, {{{-2, 1}}, -0.5f},
-  {{{-1, 2}}, 0.5f}, {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}
-};
-
-INSTANTIATE_TEST_CASE_P(LineOrthogonal, DiagonalTest,
-                        ValuesIn(diag_orthos));
+    {{{0, 1}}, 0.5f},   {{{1, 1}}, 1.f},    {{{1, 0}}, 0.5f},
+    {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f},
+    {{{0, 2}}, 1.f},    {{{2, 2}}, 2.f},    {{{2, 0}}, 1.f},
+    {{{-2, 0}}, -1.f},  {{{0, -2}}, -1.f},  {{{-2, -2}}, -2.f},
+    {{{2, 1}}, 1.5f},   {{{1, 2}}, 1.5f},   {{{-2, 1}}, -0.5f},
+    {{{-1, 2}}, 0.5f},  {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}};
+
+INSTANTIATE_TEST_CASE_P(LineOrthogonal, DiagonalTest, ValuesIn(diag_orthos));

+ 2 - 2
math/test/test_printers.h

@@ -16,7 +16,7 @@ namespace math { namespace vector {
   inline std::ostream & operator<<(std::ostream & os, math::vec2 const & p) {
     return os << '[' << p[0] << ',' << p[1] << ']';
   }
-} }
+}}
 namespace math { namespace dim2 {
   inline bool operator==(line const & lhs, line const & rhs) {
     return lhs.first == rhs.first && lhs.second == rhs.second;
@@ -24,6 +24,6 @@ namespace math { namespace dim2 {
   inline std::ostream & operator<<(std::ostream & os, line const & l) {
     return os << '[' << l.first << ',' << l.second << ']';
   }
-} }
+}}
 
 #endif /* test_printers_h */

+ 4 - 4
util/identity.hpp

@@ -9,12 +9,12 @@
 
 #include <type_traits>
 
-template <typename T, typename ID = unsigned int>
-class identity {
+template <typename T, typename ID = unsigned int> class identity {
 public:
   ID const id;
+
 private:
   friend T;
-  identity( ID _id ) : id( std::move( _id ) ) {}
-  identity( ) = delete;
+  identity(ID _id) : id(std::move(_id)) {}
+  identity() = delete;
 };