From 9ccf3248f2d9cd2cb9bd5a4826e8d0409f98f3d1 Mon Sep 17 00:00:00 2001 From: Stephen Enders Date: Tue, 5 Jan 2021 00:30:53 -0500 Subject: Initial commit - Rendering --- helper.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 helper.cpp (limited to 'helper.cpp') diff --git a/helper.cpp b/helper.cpp new file mode 100644 index 0000000..c16cb3a --- /dev/null +++ b/helper.cpp @@ -0,0 +1,106 @@ +#include "helper.h" +#include + +std::shared_ptr> +loadTextures(const char* path) +{ + int sprite_width = SPRITE_SIZE, sprite_height = SPRITE_SIZE; + sf::Image textureAtlas; + if (!textureAtlas.loadFromFile(path)) { + std::cerr << "Unable to load textures from file: " << path << std::endl; + throw std::runtime_error("Unable to load spritesheet"); + } + + textureAtlas.createMaskFromColor(GLOBAL_MASK); + auto imageSize = textureAtlas.getSize(); + + auto textures = std::make_shared>(); + + for (int y = 0; y < imageSize.y; y += sprite_height) { + for (int x = 0; x < imageSize.x; x += sprite_width) { + sf::Texture t; + t.loadFromImage(textureAtlas, + sf::IntRect(x, y, sprite_width, sprite_height)); + textures->push_back(t); + } + } + return textures; +} + +sf::Font loadFont() +{ + sf::Font font; + if (!font.loadFromFile("./res/DejaVuSansMono.ttf")) + { + std::cerr << "Unable to load font" << std::endl; + throw std::runtime_error("Unable to load font"); + } + return font; +} + +std::shared_ptr +createPiece(int id, sf::Texture texture) +{ + std::shared_ptr s = std::make_shared(); + s->setTexture(texture); + + auto p = std::make_shared(); + p->id = id; + p->sprite = s; + + return p; +} + +std::shared_ptr +createPlayer(sf::Texture texture) +{ + std::shared_ptr player = std::make_shared(); + player->score = 0; + player->pieces = + std::make_shared>>(); + for (int i = 0; i < NUM_PIECES; i++) { + player->pieces->push_back(createPiece(i + 1, texture)); + } + + return player; +} + +bool +clickedPiece(sf::Vector2i mousePosition, struct piece_t* piece) +{ + return piece->sprite->getGlobalBounds().contains(mousePosition.x, + mousePosition.y); +} + +bool +canMovePiece( + struct piece_t* piece, + int roll, + std::shared_ptr>> myPieces, + std::shared_ptr>> enemyPieces) +{ + int next = piece->position + roll; + + // rolled passed the exit + if (next > EXIT_SPACE) { + return false; + } + + // colliding with another piece + for (std::shared_ptr p : (*myPieces)) { + // cannot move onto your own piece + if (p->id != piece->id && p->position == next) { + return false; + } + } + + // can't attack in safe square + for (std::shared_ptr p : (*enemyPieces)) { + // cannot move onto a protected enemy piece + if (next == SAFE_SPACE && p->position == SAFE_SPACE) { + return false; + } + } + + return true; +} -- cgit v1.2.3-54-g00ecf