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

@ -11,38 +11,38 @@
// Maze in pixels - width: 448 - height - 496 // Maze in pixels - width: 448 - height - 496
static const uint8_t board[ROWS][COLUMNS] = { static const uint8_t board[ROWS][COLUMNS] = {
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // 0 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 0
{0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0}, // 1 {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1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0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, // 29 {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}, // 29
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, // 30 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 30
}; };
Board::Board() { Board::Board() {
@ -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,9 +25,9 @@ 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};
void resetBoardState(); void resetBoardState();
}; };

View file

@ -5,7 +5,7 @@
#include <chrono> #include <chrono>
Game::Game() Game::Game()
: window(448, 496) { : window(448, 496) {
} }
auto Game::now() { auto Game::now() {

View file

@ -9,6 +9,7 @@
class Game { class Game {
public: public:
Game(); Game();
void run(); void run();
private: private:
@ -16,9 +17,11 @@ private:
PacMan pacMan; PacMan pacMan;
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();
}; };
#endif //PACMAN_GAME_H #endif //PACMAN_GAME_H

View file

@ -8,7 +8,7 @@
GameWindow::GameWindow(int width, int height) { GameWindow::GameWindow(int width, int height) {
initSDL(); initSDL();
initSDLImage(); initSDLImage();
auto sdl_window = createWindow(width*SCALE_FACTOR, height*SCALE_FACTOR); auto sdl_window = createWindow(width * SCALE_FACTOR, height * SCALE_FACTOR);
auto sdl_renderer = createRenderer(sdl_window); auto sdl_renderer = createRenderer(sdl_window);
createWindowSurface(sdl_window); createWindowSurface(sdl_window);
setDrawColor(sdl_renderer); setDrawColor(sdl_renderer);
@ -39,7 +39,7 @@ void GameWindow::renderSuperPellets(Board & board) const {
SDL_Rect sprite_rect = board.superPelletSprite(); SDL_Rect sprite_rect = board.superPelletSprite();
std::vector<SDL_Point> superPelletPositions = board.superPelletPositions(); std::vector<SDL_Point> superPelletPositions = board.superPelletPositions();
for (const auto & pos : superPelletPositions) { for (const auto & pos : superPelletPositions) {
SDL_Rect maze_rect = targetRect({ float_t(pos.x), float_t(pos.y) }, 8*SCALE_FACTOR); SDL_Rect maze_rect = targetRect({float_t(pos.x), float_t(pos.y)}, 8 * SCALE_FACTOR);
renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect); renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect);
} }
} }
@ -48,26 +48,26 @@ void GameWindow::renderPellets(Board & board) const {
SDL_Rect sprite_rect = board.pelletSprite(); SDL_Rect sprite_rect = board.pelletSprite();
std::vector<SDL_Point> pelletPositions = board.pelletPositions(); std::vector<SDL_Point> pelletPositions = board.pelletPositions();
for (const auto & pos : pelletPositions) { for (const auto & pos : pelletPositions) {
SDL_Rect maze_rect = targetRect({ float_t(pos.x), float_t(pos.y) }, 8*SCALE_FACTOR); SDL_Rect maze_rect = targetRect({float_t(pos.x), float_t(pos.y)}, 8 * SCALE_FACTOR);
renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect); renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect);
} }
} }
void GameWindow::renderPacMan(const PacMan & pac_man) const { void GameWindow::renderPacMan(const PacMan & pac_man) const {
Position maze_position = pac_man.currentPosition(); Position maze_position = pac_man.currentPosition();
SDL_Rect maze_rect = targetRect(maze_position, 8*SCALE_FACTOR); SDL_Rect maze_rect = targetRect(maze_position, 8 * SCALE_FACTOR);
SDL_Rect sprite_rect = pac_man.currentSprite(); SDL_Rect sprite_rect = pac_man.currentSprite();
renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect); renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect);
} }
SDL_Rect GameWindow::targetRect(const Position & position, int pixel_increase) { SDL_Rect GameWindow::targetRect(const Position & position, int pixel_increase) {
int pixels = 16*SCALE_FACTOR; int pixels = 16 * SCALE_FACTOR;
int displacement = pixel_increase / 2; int displacement = pixel_increase / 2;
return { return {
int(pixels * position.x) - displacement, int(pixels * position.x) - displacement,
int(pixels * position.y) - displacement, int(pixels * position.y) - displacement,
(pixels + pixel_increase), (pixels + pixel_increase),
(pixels + pixel_increase) (pixels + pixel_increase)
}; };
} }
@ -89,12 +89,12 @@ void GameWindow::initSDLImage() {
SDL_Window * GameWindow::createWindow(int width, int height) { SDL_Window * GameWindow::createWindow(int width, int height) {
window = std::unique_ptr<SDL_Window, SDL_Window_Deleter>(SDL_CreateWindow( window = std::unique_ptr<SDL_Window, SDL_Window_Deleter>(SDL_CreateWindow(
"Pacman", "Pacman",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, width,
height, height,
SDL_WINDOW_OPENGL)); SDL_WINDOW_OPENGL));
if (!window) if (!window)
exitFailure("Failed to create window"); exitFailure("Failed to create window");
@ -104,9 +104,9 @@ SDL_Window * GameWindow::createWindow(int width, int height) {
SDL_Renderer * GameWindow::createRenderer(SDL_Window * sdl_window) { SDL_Renderer * GameWindow::createRenderer(SDL_Window * sdl_window) {
renderer = std::unique_ptr<SDL_Renderer, SDL_Renderer_Deleter>(SDL_CreateRenderer( renderer = std::unique_ptr<SDL_Renderer, SDL_Renderer_Deleter>(SDL_CreateRenderer(
sdl_window, sdl_window,
-1, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
if (!renderer) if (!renderer)
exitFailure("Failed to create renderer"); exitFailure("Failed to create renderer");
@ -125,24 +125,26 @@ 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;
} }
void GameWindow::exitFailure(const std::string& message) { void GameWindow::exitFailure(const std::string & message) {
std::cerr << message << "\n"; std::cerr << message << "\n";
std::cerr << "SDL2 Error: " << SDL_GetError() << "\n"; std::cerr << "SDL2 Error: " << SDL_GetError() << "\n";
exit(1); exit(1);
} }
void GameWindow::exitImgFailure(const std::string& message) { void GameWindow::exitImgFailure(const std::string & message) {
std::cerr << message << "\n"; std::cerr << message << "\n";
std::cerr << "SDL2_Image Error: " << IMG_GetError() << "\n"; std::cerr << "SDL2_Image Error: " << IMG_GetError() << "\n";
exit(1); exit(1);

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 exitImgFailure(const std::string& message); static void exitFailure(const std::string & message);
static std::unique_ptr<SDL_Texture, SDL_Texture_Deleter> loadTexture(SDL_Renderer * sdl_renderer, const std::string& path);
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);
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

@ -1,19 +1,24 @@
#include "PacMan.h" #include "PacMan.h"
PacMan::PacMan() : PacMan::PacMan() :
right_animation{right_wide, right_narrow, closed, right_narrow}, right_animation{right_wide, right_narrow, closed, right_narrow},
left_animation{left_wide, left_narrow, closed, left_narrow}, left_animation{left_wide, left_narrow, closed, left_narrow},
up_animation{up_wide, up_narrow, closed, up_narrow}, up_animation{up_wide, up_narrow, closed, up_narrow},
down_animation{down_wide, down_narrow, closed, down_narrow} { down_animation{down_wide, down_narrow, closed, down_narrow} {
} }
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);
@ -22,15 +24,15 @@ private:
Direction direction = Direction::NONE; Direction direction = Direction::NONE;
Direction desired_direction = Direction::NONE; Direction desired_direction = Direction::NONE;
Position pos = {14, 23}; Position pos = {14, 23};
const SDL_Rect right_wide = {0*32, 0, 32, 32}; const SDL_Rect right_wide = {0 * 32, 0, 32, 32};
const SDL_Rect right_narrow = {1*32, 0, 32, 32}; const SDL_Rect right_narrow = {1 * 32, 0, 32, 32};
const SDL_Rect closed = {2*32, 0, 32, 32}; const SDL_Rect closed = {2 * 32, 0, 32, 32};
const SDL_Rect left_narrow = {3*32, 0, 32, 32}; const SDL_Rect left_narrow = {3 * 32, 0, 32, 32};
const SDL_Rect left_wide = {4*32, 0, 32, 32}; const SDL_Rect left_wide = {4 * 32, 0, 32, 32};
const SDL_Rect up_wide = {5*32, 0, 32, 32}; const SDL_Rect up_wide = {5 * 32, 0, 32, 32};
const SDL_Rect up_narrow = {6*32, 0, 32, 32}; const SDL_Rect up_narrow = {6 * 32, 0, 32, 32};
const SDL_Rect down_wide = {7*32, 0, 32, 32}; const SDL_Rect down_wide = {7 * 32, 0, 32, 32};
const SDL_Rect down_narrow = {8*32, 0, 32, 32}; const SDL_Rect down_narrow = {8 * 32, 0, 32, 32};
const SDL_Rect right_animation[4]; const SDL_Rect right_animation[4];
const SDL_Rect left_animation[4]; const SDL_Rect left_animation[4];
const SDL_Rect up_animation[4]; const SDL_Rect up_animation[4];
@ -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);
}; };