diff options
author | cflip <cflip@cflip.net> | 2022-02-27 10:55:11 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-02-27 10:55:11 -0700 |
commit | bba66a0ccb7281cd6c717fe9784a04a4fcf72700 (patch) | |
tree | e9689454cde4d42837ab09681215101fc28ed4a6 | |
parent | 12909a3ea80639a67edd854d441614abdcd4dc12 (diff) |
Add ClangFormat configuration
-rw-r--r-- | .clang-format | 7 | ||||
-rw-r--r-- | src/level.cpp | 14 | ||||
-rw-r--r-- | src/level.h | 1 | ||||
-rw-r--r-- | src/main.cpp | 29 | ||||
-rw-r--r-- | src/train.cpp | 41 | ||||
-rw-r--r-- | src/train.h | 17 | ||||
-rw-r--r-- | src/window.h | 1 |
7 files changed, 61 insertions, 49 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..dba288f --- /dev/null +++ b/.clang-format @@ -0,0 +1,7 @@ +--- +Language: Cpp +BasedOnStyle: WebKit +UseTab: Always +TabWidth: 4 +PackConstructorInitializers: NextLine +AllowShortIfStatementsOnASingleLine: WithoutElse diff --git a/src/level.cpp b/src/level.cpp index 4df1d5a..b42608b 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -18,7 +18,7 @@ uint8_t Level::get(int x, int y) void Level::set(int x, int y, uint8_t tile) { - if (inBounds(x, y)) + if (inBounds(x, y)) m_tiles[x + y * m_width] = tile; } @@ -36,17 +36,13 @@ RailDirection ChooseDirection(Level& level, int x, int y) } if ((e || w) && !(n || s)) { return EastWest; - } - else if (s && e) { + } else if (s && e) { return SouthEast; - } - else if (s && w) { + } else if (s && w) { return SouthWest; - } - else if (n && w) { + } else if (n && w) { return NorthWest; - } - else if (n && e) { + } else if (n && e) { return NorthEast; } diff --git a/src/level.h b/src/level.h index 0ffdac2..344d35f 100644 --- a/src/level.h +++ b/src/level.h @@ -11,6 +11,7 @@ public: uint8_t get(int x, int y); void set(int x, int y, uint8_t tile); bool inBounds(int x, int y) { return x >= 0 && x < m_width && y >= 0 && y < m_height; } + private: int m_width, m_height; uint8_t* m_tiles; diff --git a/src/main.cpp b/src/main.cpp index e9d7e59..4ac5167 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,10 @@ #define SDL_MAIN_HANDLED #include <cmath> -#include "window.h" #include "bitmap.h" #include "level.h" #include "train.h" +#include "window.h" struct Point2D { float x, y; @@ -46,24 +46,19 @@ void DrawLevel(Bitmap& bitmap, Bitmap& tiles, Level& level, int xo, int yo) if (tile == NorthSouth) { tx = 0; ty = 2; - } - else if (tile == EastWest) { + } else if (tile == EastWest) { tx = 1; ty = 2; - } - else if (tile == SouthEast) { + } else if (tile == SouthEast) { tx = 0; ty = 3; - } - else if (tile == SouthWest) { + } else if (tile == SouthWest) { tx = 3; ty = 3; - } - else if (tile == NorthWest) { + } else if (tile == NorthWest) { tx = 1; ty = 3; - } - else if (tile == NorthEast) { + } else if (tile == NorthEast) { tx = 2; ty = 3; } @@ -73,7 +68,7 @@ void DrawLevel(Bitmap& bitmap, Bitmap& tiles, Level& level, int xo, int yo) } } -int main(int argc, char **argv) +int main(int argc, char** argv) { constexpr int Height = 240; constexpr int Width = Height * 16 / 9; @@ -105,8 +100,10 @@ int main(int argc, char **argv) if (level.inBounds(tilePos.x, tilePos.y)) { auto tile = level.get(tilePos.x, tilePos.y); - if (tile > 0) tile = 0; - else tile = ChooseDirection(level, tilePos.x, tilePos.y); + if (tile > 0) + tile = 0; + else + tile = ChooseDirection(level, tilePos.x, tilePos.y); level.set(tilePos.x, tilePos.y, tile); @@ -123,12 +120,12 @@ int main(int argc, char **argv) float mx = x / Scale + xOffs; float my = y / Scale + yOffs; auto pos = screenToTile({ mx, my }); - car.setPosition(pos.x, pos.y); + car.setPosition(pos.x, pos.y); } }); window.onMouseUp([&](int button, int x, int y) { - if (button == 2 && isDragging) + if (button == 2 && isDragging) isDragging = false; }); diff --git a/src/train.cpp b/src/train.cpp index 632f0f2..d401572 100644 --- a/src/train.cpp +++ b/src/train.cpp @@ -1,7 +1,7 @@ #include "train.h" -#include "level.h" #include "bitmap.h" +#include "level.h" void Train::update(Level& level) { @@ -25,24 +25,21 @@ void Train::update(Level& level) if (n == SouthEast) m_dir = East; if (n == SouthWest) m_dir = West; y--; - } - else if (m_dir == East) { + } else if (m_dir == East) { if (e == NorthWest) m_dir = North; if (e == SouthWest) m_dir = South; x++; - } - else if (m_dir == South) { + } else if (m_dir == South) { if (s == NorthEast) m_dir = East; if (s == NorthWest) m_dir = West; y++; - } - else if (m_dir == West) { + } else if (m_dir == West) { if (w == NorthEast) m_dir = North; if (w == SouthEast) m_dir = South; x--; } - nextTile(); + nextTile(); } void Train::draw(Bitmap& bitmap, int xo, int yo) @@ -56,16 +53,28 @@ void Train::draw(Bitmap& bitmap, int xo, int yo) void Train::setPosition(int tx, int ty) { - x = tx; - y = ty; - m_progress = 0.f; - nextTile(); + x = tx; + y = ty; + m_progress = 0.f; + nextTile(); } void Train::nextTile() { - if (m_dir == North) { m_nextX = x; m_nextY = y - 1; } - if (m_dir == East) { m_nextX = x + 1; m_nextY = y; } - if (m_dir == South) { m_nextX = x; m_nextY = y + 1; } - if (m_dir == West) { m_nextX = x - 1; m_nextY = y; } + if (m_dir == North) { + m_nextX = x; + m_nextY = y - 1; + } + if (m_dir == East) { + m_nextX = x + 1; + m_nextY = y; + } + if (m_dir == South) { + m_nextX = x; + m_nextY = y + 1; + } + if (m_dir == West) { + m_nextX = x - 1; + m_nextY = y; + } }
\ No newline at end of file diff --git a/src/train.h b/src/train.h index 13f278a..43892f9 100644 --- a/src/train.h +++ b/src/train.h @@ -14,21 +14,22 @@ class Level; class Train { public: Train() - : m_sprite("../res/car.png") {} + : m_sprite("../res/car.png") { } void update(Level&); void draw(Bitmap&, int, int); - void setPosition(int x, int y); + void setPosition(int x, int y); + private: - void nextTile(); + void nextTile(); Bitmap m_sprite; - int x{0}, y{0}; - float m_speed{0.05f}; - float m_progress{ 0.f }; + int x { 0 }, y { 0 }; + float m_speed { 0.05f }; + float m_progress { 0.f }; - CarDirection m_dir{ North }; - int m_nextX{0}, m_nextY{0}; + CarDirection m_dir { North }; + int m_nextX { 0 }, m_nextY { 0 }; }; diff --git a/src/window.h b/src/window.h index bdd1e91..14494a6 100644 --- a/src/window.h +++ b/src/window.h @@ -21,6 +21,7 @@ public: void onMouseDown(std::function<void(int, int, int)> callback) { m_mouseDown = callback; } void onMouseUp(std::function<void(int, int, int)> callback) { m_mouseUp = callback; } void onMouseMove(std::function<void(int, int)> callback) { m_mouseMove = callback; } + private: bool m_isRunning; |