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
class ConanDependencies(ConanFile):
class ConanDependencies(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package"
default_options = {

View file

@ -12,37 +12,37 @@
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,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,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, // 2
{0,4,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,4,0}, // 3
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, // 4
{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}, // 5
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0}, // 6
{0,1,0,0,0,0,1,0,0,1,0,0,0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, 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
};
Board::Board() {
@ -67,7 +67,8 @@ bool Board::isWalkable(Position point, float_t position_delta, Direction directi
case Direction::DOWN:
return board_state[int(point.y) + 1][int(point.x)] != 0;
case Direction::NONE:
default: return true;
default:
return true;
}
}

View file

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

View file

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

View file

@ -8,7 +8,7 @@
GameWindow::GameWindow(int width, int height) {
initSDL();
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);
createWindowSurface(sdl_window);
setDrawColor(sdl_renderer);
@ -39,7 +39,7 @@ void GameWindow::renderSuperPellets(Board & board) const {
SDL_Rect sprite_rect = board.superPelletSprite();
std::vector<SDL_Point> superPelletPositions = board.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);
}
}
@ -48,20 +48,20 @@ void GameWindow::renderPellets(Board & board) const {
SDL_Rect sprite_rect = board.pelletSprite();
std::vector<SDL_Point> pelletPositions = board.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);
}
}
void GameWindow::renderPacMan(const PacMan & pac_man) const {
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();
renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect);
}
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;
return {
int(pixels * position.x) - displacement,
@ -125,24 +125,26 @@ void GameWindow::setDrawColor(SDL_Renderer * sdl_renderer) {
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()));
if (!surface)
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)
exitFailure("Failed to create texture from surface");
return texture;
}
void GameWindow::exitFailure(const std::string& message) {
void GameWindow::exitFailure(const std::string & message) {
std::cerr << message << "\n";
std::cerr << "SDL2 Error: " << SDL_GetError() << "\n";
exit(1);
}
void GameWindow::exitImgFailure(const std::string& message) {
void GameWindow::exitImgFailure(const std::string & message) {
std::cerr << message << "\n";
std::cerr << "SDL2_Image Error: " << IMG_GetError() << "\n";
exit(1);

View file

@ -36,6 +36,7 @@ class PacMan;
class GameWindow {
public:
explicit GameWindow(int width, int height);
void update(const PacMan & pacMan, Board board);
private:
@ -45,21 +46,38 @@ private:
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> sprite_texture;
SDL_Window * createWindow(int width, int height);
SDL_Renderer * createRenderer(SDL_Window * window);
void createWindowSurface(SDL_Window * sdl_window);
static void initSDL();
static void initSDLImage();
static void setDrawColor(SDL_Renderer * sdl_renderer);
static void exitFailure(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 void exitFailure(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);
void renderMaze() const;
void renderPacMan(const PacMan & pac_man) const;
void renderBoard(Board board);
void renderPellets(Board & board) const;
void renderSuperPellets(Board & board) const;
static SDL_Rect targetRect(const Position & position, int pixel_increase);
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 {
switch (direction) {
case Direction::NONE: return closed;
case Direction::LEFT: return left_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];
case Direction::NONE:
return closed;
case Direction::LEFT:
return left_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;
}
}
}

View file

@ -12,7 +12,9 @@
class PacMan {
public:
PacMan();
[[nodiscard]] SDL_Rect currentSprite() const;
[[nodiscard]] Position currentPosition() const;
void update(std::chrono::milliseconds time_delta, InputState state, const Board & board);
@ -22,15 +24,15 @@ private:
Direction direction = Direction::NONE;
Direction desired_direction = Direction::NONE;
Position pos = {14, 23};
const SDL_Rect right_wide = {0*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 left_narrow = {3*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_narrow = {6*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 right_wide = {0 * 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 left_narrow = {3 * 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_narrow = {6 * 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 right_animation[4];
const SDL_Rect left_animation[4];
const SDL_Rect up_animation[4];
@ -39,7 +41,9 @@ private:
float_t animation_position_delta = 0.0;
void setDirection(const InputState & state);
void updateAnimationPosition(std::chrono::milliseconds time_delta);
void updateMazePosition(std::chrono::milliseconds time_delta, const Board & board);
};