Introduce a scale function and clean up

This commit is contained in:
Patricia Aas 2021-07-07 11:09:53 +02:00 committed by Patricia Aas
parent ce7dd70332
commit 705ea717e1
3 changed files with 28 additions and 26 deletions

View File

@ -6,8 +6,6 @@
namespace pacman { namespace pacman {
static const double SCALE_FACTOR = 0.5;
Canvas::Canvas() Canvas::Canvas()
: window(sf::VideoMode(windowDimensions().width, windowDimensions().height), : window(sf::VideoMode(windowDimensions().width, windowDimensions().height),
"Pacman", "Pacman",
@ -26,10 +24,7 @@ void Canvas::update(const Game & game) {
renderPellets(game.pellets); renderPellets(game.pellets);
renderSuperPellets(game.superPellets); renderSuperPellets(game.superPellets);
std::apply([&](const auto &... ghost) { std::apply([&](const auto &... ghost) { (renderGhost(ghost), ...); }, game.ghosts);
(renderGhost(ghost), ...);
},
game.ghosts);
renderScore(game.score.points); renderScore(game.score.points);
renderLives(game.score.lives); renderLives(game.score.lives);
@ -62,8 +57,8 @@ void Canvas::renderMaze() {
0, 0,
DEFAULT_MAZE_WIDTH, DEFAULT_MAZE_WIDTH,
DEFAULT_MAZE_HEIGHT }); DEFAULT_MAZE_HEIGHT });
maze.setScale(DEFAULT_MAZE_SCALE_UP * SCALE_FACTOR, DEFAULT_MAZE_SCALE_UP * SCALE_FACTOR); maze.setScale(scale(DEFAULT_MAZE_SCALE_UP), scale(DEFAULT_MAZE_SCALE_UP));
maze.setPosition(LEFT_MARGIN * SCALE_FACTOR, TOP_MARGIN * SCALE_FACTOR); maze.setPosition(scale(LEFT_MARGIN), scale(TOP_MARGIN));
window.draw(maze); window.draw(maze);
} }
@ -71,7 +66,7 @@ void Canvas::renderPellets(const Pellets & pellets) {
Sprite pellet = getSprite(pellets.currentSprite()); Sprite pellet = getSprite(pellets.currentSprite());
std::vector<GridPosition> pelletPositions = pellets.currentPositions(); std::vector<GridPosition> pelletPositions = pellets.currentPositions();
for (const auto & pos : pelletPositions) { 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()); Sprite pellet = getSprite(superPellets.currentSprite());
std::vector<GridPosition> superPelletPositions = superPellets.currentPositions(); std::vector<GridPosition> superPelletPositions = superPellets.currentPositions();
for (const auto & pos : superPelletPositions) { 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) { 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; sf::Text text;
text.setPosition(x, y); text.setPosition(scale(RIGHT_PANEL_X), scale(TOP_MARGIN * 2));
text.setFont(game_font); text.setFont(game_font);
text.setString(fmt::format("SCORE\n{}", score)); text.setString(fmt::format("SCORE\n{}", score));
text.setCharacterSize(40 * SCALE_FACTOR); text.setCharacterSize(int(scale(40)));
text.setFillColor(sf::Color::White); text.setFillColor(sf::Color::White);
window.draw(text); window.draw(text);
} }
void Canvas::renderLives(int lives) { void Canvas::renderLives(int lives) {
constexpr GridPosition liveSprite = Atlas::pacman_left_narrow; constexpr GridPosition liveSprite = Atlas::pacman_left_narrow;
const size_t x = (LEFT_MARGIN + DEFAULT_TARGET_MAZE_WIDTH + LEFT_MARGIN) * SCALE_FACTOR; const auto x = scale(RIGHT_PANEL_X);
const size_t y = DEFAULT_TARGET_MAZE_HEIGHT * SCALE_FACTOR; const auto y = scale(DEFAULT_TARGET_MAZE_HEIGHT);
Sprite pacmanSprite = getSprite(liveSprite); Sprite pacmanSprite = getSprite(liveSprite);
for (int i = 0; i < lives - 1; i++) { for (int i = 0; i < lives - 1; i++) {
size_t life_position = i * (DEFAULT_SPRITE_WIDTH * SCALE_FACTOR); auto life_position = scale(i * DEFAULT_SPRITE_WIDTH);
GridPosition pos{ x + life_position, y }; pacmanSprite.setPosition(x + life_position, y);
pacmanSprite.setPosition(pos.x, pos.y);
window.draw(pacmanSprite); window.draw(pacmanSprite);
} }
} }
Rect Canvas::windowDimensions() { Rect Canvas::windowDimensions() {
const double width = (LEFT_MARGIN + DEFAULT_TARGET_MAZE_WIDTH + SCORE_WIDTH) * SCALE_FACTOR; return { 0, 0, int(scale(WINDOW_WIDTH)), int(scale(WINDOW_HEIGHT)) };
const double height = (TOP_MARGIN + DEFAULT_TARGET_MAZE_HEIGHT + BOTTOM_MARGIN) * SCALE_FACTOR;
return { 0, 0, int(width), int(height) };
} }
Sprite Canvas::getSprite(GridPosition coordinate) const { Sprite Canvas::getSprite(GridPosition coordinate) const {
@ -140,9 +129,9 @@ Sprite Canvas::getSprite(GridPosition coordinate) const {
} }
void Canvas::renderSprite(Sprite sprite, Position pos) { void Canvas::renderSprite(Sprite sprite, Position pos) {
pos.x = (LEFT_MARGIN * SCALE_FACTOR) + (pos.x * (DEFAULT_SPRITE_WIDTH * SCALE_FACTOR)); const auto x = scale(LEFT_MARGIN + int(pos.x * DEFAULT_SPRITE_WIDTH));
pos.y = (TOP_MARGIN * SCALE_FACTOR) + (pos.y * (DEFAULT_SPRITE_HEIGHT * SCALE_FACTOR)); const auto y = scale(TOP_MARGIN + int(pos.y * DEFAULT_SPRITE_HEIGHT));
sprite.setPosition(pos.x, pos.y); sprite.setPosition(x, y);
window.draw(sprite); window.draw(sprite);
} }
@ -167,4 +156,8 @@ sf::Font Canvas::loadFont(std::string_view path) {
return font; return font;
} }
float_t Canvas::scale(int value) {
return float_t(value) * SCALE_FACTOR;
}
} // namespace pacman } // namespace pacman

View File

@ -19,6 +19,7 @@ public:
std::optional<sf::Event> pollEvent(); std::optional<sf::Event> pollEvent();
private: private:
static constexpr float_t SCALE_FACTOR = 0.5;
static constexpr uint16_t LEFT_MARGIN = 40 * 2; static constexpr uint16_t LEFT_MARGIN = 40 * 2;
static constexpr uint16_t TOP_MARGIN = 40 * 2; static constexpr uint16_t TOP_MARGIN = 40 * 2;
static constexpr uint16_t BOTTOM_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 SCORE_WIDTH = 200 * 2;
static constexpr uint16_t DEFAULT_SPRITE_WIDTH = 32; static constexpr uint16_t DEFAULT_SPRITE_WIDTH = 32;
static constexpr uint16_t DEFAULT_SPRITE_HEIGHT = 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 clear();
void render(); void render();
@ -46,6 +50,7 @@ private:
static Rect windowDimensions(); static Rect windowDimensions();
static sf::Texture loadTexture(std::string_view path); static sf::Texture loadTexture(std::string_view path);
static sf::Font loadFont(std::string_view path); static sf::Font loadFont(std::string_view path);
static float_t scale(int value);
Sprite getSprite(GridPosition rect) const; Sprite getSprite(GridPosition rect) const;

View File

@ -24,6 +24,10 @@ inline GridPosition positionToGridPosition(Position pos) {
return { size_t(std::round(pos.x)), size_t(std::round(pos.y)) }; 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) { constexpr bool operator==(const GridPosition & a, const GridPosition & b) {
return a.x == b.x && a.y == b.y; return a.x == b.x && a.y == b.y;
} }