From 705ea717e1325d6c92336b33aa1b636b56371f21 Mon Sep 17 00:00:00 2001 From: Patricia Aas Date: Wed, 7 Jul 2021 11:09:53 +0200 Subject: [PATCH] Introduce a scale function and clean up --- lib/Canvas.cpp | 45 +++++++++++++++++----------------------- lib/include/Canvas.hpp | 5 +++++ lib/include/Position.hpp | 4 ++++ 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/lib/Canvas.cpp b/lib/Canvas.cpp index 2180e46..20d0346 100644 --- a/lib/Canvas.cpp +++ b/lib/Canvas.cpp @@ -6,8 +6,6 @@ namespace pacman { -static const double SCALE_FACTOR = 0.5; - Canvas::Canvas() : window(sf::VideoMode(windowDimensions().width, windowDimensions().height), "Pacman", @@ -26,10 +24,7 @@ void Canvas::update(const Game & game) { renderPellets(game.pellets); renderSuperPellets(game.superPellets); - std::apply([&](const auto &... ghost) { - (renderGhost(ghost), ...); - }, - game.ghosts); + std::apply([&](const auto &... ghost) { (renderGhost(ghost), ...); }, game.ghosts); renderScore(game.score.points); renderLives(game.score.lives); @@ -62,8 +57,8 @@ void Canvas::renderMaze() { 0, DEFAULT_MAZE_WIDTH, DEFAULT_MAZE_HEIGHT }); - maze.setScale(DEFAULT_MAZE_SCALE_UP * SCALE_FACTOR, DEFAULT_MAZE_SCALE_UP * SCALE_FACTOR); - maze.setPosition(LEFT_MARGIN * SCALE_FACTOR, TOP_MARGIN * SCALE_FACTOR); + maze.setScale(scale(DEFAULT_MAZE_SCALE_UP), scale(DEFAULT_MAZE_SCALE_UP)); + maze.setPosition(scale(LEFT_MARGIN), scale(TOP_MARGIN)); window.draw(maze); } @@ -71,7 +66,7 @@ void Canvas::renderPellets(const Pellets & pellets) { Sprite pellet = getSprite(pellets.currentSprite()); std::vector pelletPositions = pellets.currentPositions(); for (const auto & pos : pelletPositions) { - renderSprite(pellet, { double(pos.x), double(pos.y) }); + renderSprite(pellet, gridPositionToPosition(pos)); } } @@ -79,7 +74,7 @@ void Canvas::renderSuperPellets(const SuperPellets & superPellets) { Sprite pellet = getSprite(superPellets.currentSprite()); std::vector superPelletPositions = superPellets.currentPositions(); for (const auto & pos : superPelletPositions) { - renderSprite(pellet, { double(pos.x), double(pos.y) }); + renderSprite(pellet, gridPositionToPosition(pos)); } } @@ -96,36 +91,30 @@ void Canvas::renderGhost(const Ghost & ghost) { } void Canvas::renderScore(int score) { - const int x = (LEFT_MARGIN + DEFAULT_TARGET_MAZE_WIDTH + LEFT_MARGIN) * SCALE_FACTOR; - const int y = (TOP_MARGIN * 2) * SCALE_FACTOR; - sf::Text text; - text.setPosition(x, y); + text.setPosition(scale(RIGHT_PANEL_X), scale(TOP_MARGIN * 2)); text.setFont(game_font); text.setString(fmt::format("SCORE\n{}", score)); - text.setCharacterSize(40 * SCALE_FACTOR); + text.setCharacterSize(int(scale(40))); text.setFillColor(sf::Color::White); window.draw(text); } void Canvas::renderLives(int lives) { constexpr GridPosition liveSprite = Atlas::pacman_left_narrow; - const size_t x = (LEFT_MARGIN + DEFAULT_TARGET_MAZE_WIDTH + LEFT_MARGIN) * SCALE_FACTOR; - const size_t y = DEFAULT_TARGET_MAZE_HEIGHT * SCALE_FACTOR; + const auto x = scale(RIGHT_PANEL_X); + const auto y = scale(DEFAULT_TARGET_MAZE_HEIGHT); Sprite pacmanSprite = getSprite(liveSprite); for (int i = 0; i < lives - 1; i++) { - size_t life_position = i * (DEFAULT_SPRITE_WIDTH * SCALE_FACTOR); - GridPosition pos{ x + life_position, y }; - pacmanSprite.setPosition(pos.x, pos.y); + auto life_position = scale(i * DEFAULT_SPRITE_WIDTH); + pacmanSprite.setPosition(x + life_position, y); window.draw(pacmanSprite); } } Rect Canvas::windowDimensions() { - const double width = (LEFT_MARGIN + DEFAULT_TARGET_MAZE_WIDTH + SCORE_WIDTH) * SCALE_FACTOR; - const double height = (TOP_MARGIN + DEFAULT_TARGET_MAZE_HEIGHT + BOTTOM_MARGIN) * SCALE_FACTOR; - return { 0, 0, int(width), int(height) }; + return { 0, 0, int(scale(WINDOW_WIDTH)), int(scale(WINDOW_HEIGHT)) }; } Sprite Canvas::getSprite(GridPosition coordinate) const { @@ -140,9 +129,9 @@ Sprite Canvas::getSprite(GridPosition coordinate) const { } void Canvas::renderSprite(Sprite sprite, Position pos) { - pos.x = (LEFT_MARGIN * SCALE_FACTOR) + (pos.x * (DEFAULT_SPRITE_WIDTH * SCALE_FACTOR)); - pos.y = (TOP_MARGIN * SCALE_FACTOR) + (pos.y * (DEFAULT_SPRITE_HEIGHT * SCALE_FACTOR)); - sprite.setPosition(pos.x, pos.y); + const auto x = scale(LEFT_MARGIN + int(pos.x * DEFAULT_SPRITE_WIDTH)); + const auto y = scale(TOP_MARGIN + int(pos.y * DEFAULT_SPRITE_HEIGHT)); + sprite.setPosition(x, y); window.draw(sprite); } @@ -167,4 +156,8 @@ sf::Font Canvas::loadFont(std::string_view path) { return font; } +float_t Canvas::scale(int value) { + return float_t(value) * SCALE_FACTOR; +} + } // namespace pacman diff --git a/lib/include/Canvas.hpp b/lib/include/Canvas.hpp index 1bbdacf..329cead 100644 --- a/lib/include/Canvas.hpp +++ b/lib/include/Canvas.hpp @@ -19,6 +19,7 @@ public: std::optional pollEvent(); private: + static constexpr float_t SCALE_FACTOR = 0.5; static constexpr uint16_t LEFT_MARGIN = 40 * 2; static constexpr uint16_t TOP_MARGIN = 40 * 2; static constexpr uint16_t BOTTOM_MARGIN = 40 * 2; @@ -30,6 +31,9 @@ private: static constexpr uint16_t SCORE_WIDTH = 200 * 2; static constexpr uint16_t DEFAULT_SPRITE_WIDTH = 32; static constexpr uint16_t DEFAULT_SPRITE_HEIGHT = 32; + static constexpr uint16_t RIGHT_PANEL_X = LEFT_MARGIN + DEFAULT_TARGET_MAZE_WIDTH + LEFT_MARGIN; + static constexpr uint16_t WINDOW_WIDTH = LEFT_MARGIN + DEFAULT_TARGET_MAZE_WIDTH + SCORE_WIDTH; + static constexpr uint16_t WINDOW_HEIGHT = TOP_MARGIN + DEFAULT_TARGET_MAZE_HEIGHT + BOTTOM_MARGIN; void clear(); void render(); @@ -46,6 +50,7 @@ private: static Rect windowDimensions(); static sf::Texture loadTexture(std::string_view path); static sf::Font loadFont(std::string_view path); + static float_t scale(int value); Sprite getSprite(GridPosition rect) const; diff --git a/lib/include/Position.hpp b/lib/include/Position.hpp index cc72ce7..ecd93a7 100644 --- a/lib/include/Position.hpp +++ b/lib/include/Position.hpp @@ -24,6 +24,10 @@ inline GridPosition positionToGridPosition(Position pos) { return { size_t(std::round(pos.x)), size_t(std::round(pos.y)) }; } +inline Position gridPositionToPosition(GridPosition pos) { + return { double(pos.x), double(pos.y) }; +} + constexpr bool operator==(const GridPosition & a, const GridPosition & b) { return a.x == b.x && a.y == b.y; }