Reformat most

This commit is contained in:
Patricia Aas 2020-11-27 17:16:42 +01:00
parent e572f2a93a
commit c34a2b730d
10 changed files with 132 additions and 84 deletions

16
.clang-format Normal file
View file

@ -0,0 +1,16 @@
---
BasedOnStyle: Mozilla
AllowAllConstructorInitializersOnNextLine: false
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
BreakBeforeBraces: Custom
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: '0'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
NamespaceIndentation: All
PenaltyReturnTypeOnItsOwnLine: '10000'
PointerAlignment: Middle
...

View file

@ -1,7 +1,7 @@
from conans import ConanFile, CMake from conans import ConanFile, CMake
class ConanDependencies(ConanFile):
class ConanDependencies(ConanFile):
settings = "os", "compiler", "build_type", "arch" settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package" generators = "cmake", "cmake_find_package"
default_options = { default_options = {

View file

@ -67,7 +67,8 @@ bool Board::isWalkable(Position point, float_t position_delta, Direction directi
case Direction::DOWN: case Direction::DOWN:
return board_state[int(point.y) + 1][int(point.x)] != 0; return board_state[int(point.y) + 1][int(point.x)] != 0;
case Direction::NONE: case Direction::NONE:
default: return true; default:
return true;
} }
} }

View file

@ -25,7 +25,7 @@ public:
std::vector<SDL_Point> superPelletPositions(); std::vector<SDL_Point> superPelletPositions();
private: private:
uint8_t board_state[ROWS][COLUMNS]; uint8_t board_state[ROWS][COLUMNS]{};
const SDL_Rect super_pellet = {0 * 32, 9 * 32, 32, 32}; const SDL_Rect super_pellet = {0 * 32, 9 * 32, 32, 32};
const SDL_Rect pellet = {1 * 32, 9 * 32, 32, 32}; const SDL_Rect pellet = {1 * 32, 9 * 32, 32, 32};

View file

@ -9,6 +9,7 @@
class Game { class Game {
public: public:
Game(); Game();
void run(); void run();
private: private:
@ -17,7 +18,9 @@ private:
Board board; Board board;
static void processEvents(InputState & inputState); static void processEvents(InputState & inputState);
static void keyToggle(const SDL_Event & event, InputState & inputState, bool on); static void keyToggle(const SDL_Event & event, InputState & inputState, bool on);
[[nodiscard]] static auto now(); [[nodiscard]] static auto now();
}; };

View file

@ -125,12 +125,14 @@ void GameWindow::setDrawColor(SDL_Renderer * sdl_renderer) {
exitFailure("Failed to set renderer color"); exitFailure("Failed to set renderer color");
} }
std::unique_ptr<SDL_Texture, SDL_Texture_Deleter> GameWindow::loadTexture(SDL_Renderer * sdl_renderer, const std::string& path) { std::unique_ptr<SDL_Texture, SDL_Texture_Deleter>
GameWindow::loadTexture(SDL_Renderer * sdl_renderer, const std::string & path) {
auto surface = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(IMG_Load(path.c_str())); auto surface = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(IMG_Load(path.c_str()));
if (!surface) if (!surface)
exitImgFailure("Failed to load image"); exitImgFailure("Failed to load image");
auto texture = std::unique_ptr<SDL_Texture, SDL_Texture_Deleter>(SDL_CreateTextureFromSurface(sdl_renderer, surface.get())); auto texture = std::unique_ptr<SDL_Texture, SDL_Texture_Deleter>(
SDL_CreateTextureFromSurface(sdl_renderer, surface.get()));
if (!texture) if (!texture)
exitFailure("Failed to create texture from surface"); exitFailure("Failed to create texture from surface");
return texture; return texture;

View file

@ -36,6 +36,7 @@ class PacMan;
class GameWindow { class GameWindow {
public: public:
explicit GameWindow(int width, int height); explicit GameWindow(int width, int height);
void update(const PacMan & pacMan, Board board); void update(const PacMan & pacMan, Board board);
private: private:
@ -45,21 +46,38 @@ private:
std::unique_ptr<SDL_Surface, SDL_Surface_Deleter> window_surface; std::unique_ptr<SDL_Surface, SDL_Surface_Deleter> window_surface;
std::unique_ptr<SDL_Texture, SDL_Texture_Deleter> maze_texture; std::unique_ptr<SDL_Texture, SDL_Texture_Deleter> maze_texture;
std::unique_ptr<SDL_Texture, SDL_Texture_Deleter> sprite_texture; std::unique_ptr<SDL_Texture, SDL_Texture_Deleter> sprite_texture;
SDL_Window * createWindow(int width, int height); SDL_Window * createWindow(int width, int height);
SDL_Renderer * createRenderer(SDL_Window * window); SDL_Renderer * createRenderer(SDL_Window * window);
void createWindowSurface(SDL_Window * sdl_window); void createWindowSurface(SDL_Window * sdl_window);
static void initSDL(); static void initSDL();
static void initSDLImage(); static void initSDLImage();
static void setDrawColor(SDL_Renderer * sdl_renderer); static void setDrawColor(SDL_Renderer * sdl_renderer);
static void exitFailure(const std::string & message); static void exitFailure(const std::string & message);
static void exitImgFailure(const std::string & message); static void exitImgFailure(const std::string & message);
static std::unique_ptr<SDL_Texture, SDL_Texture_Deleter> loadTexture(SDL_Renderer * sdl_renderer, const std::string& path);
static std::unique_ptr<SDL_Texture, SDL_Texture_Deleter>
loadTexture(SDL_Renderer * sdl_renderer, const std::string & path);
void renderMaze() const; void renderMaze() const;
void renderPacMan(const PacMan & pac_man) const; void renderPacMan(const PacMan & pac_man) const;
void renderBoard(Board board); void renderBoard(Board board);
void renderPellets(Board & board) const; void renderPellets(Board & board) const;
void renderSuperPellets(Board & board) const; void renderSuperPellets(Board & board) const;
static SDL_Rect targetRect(const Position & position, int pixel_increase); static SDL_Rect targetRect(const Position & position, int pixel_increase);
void renderTexture(SDL_Texture * texture, SDL_Rect * texture_rect, SDL_Rect * target_rect) const; void renderTexture(SDL_Texture * texture, SDL_Rect * texture_rect, SDL_Rect * target_rect) const;
}; };

View file

@ -9,11 +9,16 @@ PacMan::PacMan() :
SDL_Rect PacMan::currentSprite() const { SDL_Rect PacMan::currentSprite() const {
switch (direction) { switch (direction) {
case Direction::NONE: return closed; case Direction::NONE:
case Direction::LEFT: return left_animation[animation_position]; return closed;
case Direction::RIGHT: return right_animation[animation_position]; case Direction::LEFT:
case Direction::UP: return up_animation[animation_position]; return left_animation[animation_position];
case Direction::DOWN: return down_animation[animation_position]; case Direction::RIGHT:
return right_animation[animation_position];
case Direction::UP:
return up_animation[animation_position];
case Direction::DOWN:
return down_animation[animation_position];
} }
} }
@ -73,5 +78,4 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar
break; break;
} }
} }
} }

View file

@ -12,7 +12,9 @@
class PacMan { class PacMan {
public: public:
PacMan(); PacMan();
[[nodiscard]] SDL_Rect currentSprite() const; [[nodiscard]] SDL_Rect currentSprite() const;
[[nodiscard]] Position currentPosition() const; [[nodiscard]] Position currentPosition() const;
void update(std::chrono::milliseconds time_delta, InputState state, const Board & board); void update(std::chrono::milliseconds time_delta, InputState state, const Board & board);
@ -39,7 +41,9 @@ private:
float_t animation_position_delta = 0.0; float_t animation_position_delta = 0.0;
void setDirection(const InputState & state); void setDirection(const InputState & state);
void updateAnimationPosition(std::chrono::milliseconds time_delta); void updateAnimationPosition(std::chrono::milliseconds time_delta);
void updateMazePosition(std::chrono::milliseconds time_delta, const Board & board); void updateMazePosition(std::chrono::milliseconds time_delta, const Board & board);
}; };