From a408a43cd105b89a8d44292e2ef69802a5660497 Mon Sep 17 00:00:00 2001 From: Stephen Enders Date: Sat, 23 Jan 2021 11:52:32 -0500 Subject: Move source files to src folder We'll be making a few source files it'll be useful to have them organized at least somewhat from our non-src files --- src/helper.cpp | 265 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 src/helper.cpp (limited to 'src/helper.cpp') diff --git a/src/helper.cpp b/src/helper.cpp new file mode 100644 index 0000000..8d6f3ce --- /dev/null +++ b/src/helper.cpp @@ -0,0 +1,265 @@ +#include "helper.hpp" +#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; +} + +// increment through the textures +inline int +next(int* p, int max) +{ + int i = *p; + (*p) = (i + 1) % max; + return i; +} + +std::shared_ptr> +createBoard(std::shared_ptr> textures) +{ + auto sprites = std::make_shared>(); + sf::Texture& star_texture = (*textures)[STAR_TILE]; + int blank_idx = 0; + int sp_idx = 0; + // p1 pieces + // p1 star + { + sf::Sprite s; + s.setTexture(star_texture); + s.setPosition(pos(3, 5)); + sprites->push_back(s); + } + // p1 start + for (int i = 0; i < 3; i++) { + sf::Texture& t = (*textures)[P1_BOARD_TILES[next(&sp_idx, 2)]]; + sf::Sprite s; + s.setTexture(t); + s.setPosition(pos(4 + i, 5)); + sprites->push_back(s); + } + // p1 end + { + sf::Sprite goal; + goal.setTexture((*textures)[P1_END]); + goal.setPosition(pos(8, 5)); + sprites->push_back(goal); + + sf::Sprite end_star; + end_star.setTexture(star_texture); + end_star.setPosition(pos(9, 5)); + sprites->push_back(end_star); + + sf::Texture& t = (*textures)[P1_BOARD_TILES[next(&sp_idx, 2)]]; + sf::Sprite s; + s.setTexture(t); + s.setPosition(pos(10, 5)); + sprites->push_back(s); + } + // center pieces + for (int i = 0; i < 8; i++) { + sf::Sprite s; + if (i == 3) { + s.setTexture(star_texture); + } else { + sf::Texture& t = (*textures)[BLANK_TILES[next(&blank_idx, 3)]]; + s.setTexture(t); + } + s.setPosition(pos(3 + i, 4)); + sprites->push_back(s); + } + // p2 pieces + // p2 star + { + sf::Sprite s; + s.setTexture(star_texture); + s.setPosition(pos(3, 3)); + sprites->push_back(s); + } + // p2 start + for (int i = 0; i < 3; i++) { + sf::Texture& t = (*textures)[P2_BOARD_TILES[next(&sp_idx, 2)]]; + sf::Sprite s; + s.setTexture(t); + s.setPosition(pos(4 + i, 3)); + sprites->push_back(s); + } + // p2 end + { + sf::Sprite goal; + goal.setTexture((*textures)[P2_END]); + goal.setPosition(pos(8, 3)); + sprites->push_back(goal); + + sf::Sprite end_star; + end_star.setTexture(star_texture); + end_star.setPosition(pos(9, 3)); + sprites->push_back(end_star); + + sf::Texture& t = (*textures)[P2_BOARD_TILES[next(&sp_idx, 2)]]; + sf::Sprite end_tile; + end_tile.setTexture(t); + end_tile.setPosition(pos(10, 3)); + sprites->push_back(end_tile); + } + + return sprites; +} + +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) +{ + + sf::Sprite s(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; +} + +std::shared_ptr> +createAllDice(sf::Texture& die0Texture, sf::Texture& die1Texture) +{ + auto dice = std::make_shared>(); + + // create dice, even 0 odds 1 + // there are 8 dice results int total + // 4 potential 0s + // 4 potential 1s + // The dice will be rendered in pairs + // [0, 1] will be the left most die + // [2, 3] will be the second + // ... and so on + // Since a die can only have 2 results (0 or 1) + // To simplify how we will be placing them, the results of the roll + // will dictate which value each die gets + for (int i = 0; i < 8; i++) { + if (i % 2 == 0) { + sf::Sprite s; + s.setTexture(die0Texture); + struct dice_t die = { 0, true, s }; + dice->push_back(die); + } else { + sf::Sprite s; + s.setTexture(die1Texture); + struct dice_t die = { 1, false, s }; + dice->push_back(die); + } + } + + return dice; +} + +std::shared_ptr> +createRollSprites(sf::Texture& t1, sf::Texture& t2) +{ + auto sprites = std::make_shared>(); + + sprites->push_back(sf::Sprite(t1)); + sprites->push_back(sf::Sprite(t2)); + + return sprites; +} + +void +makeNum(sf::Sprite* sprite_ptr, + int num, + std::shared_ptr> textures) +{ + sf::Texture& t = (*textures)[NUMS_TILES[num]]; + sprite_ptr->setTexture(t); +}; + +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 (struct piece_t& 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 (struct piece_t& p : (*enemyPieces)) { + // cannot move onto a protected enemy piece + if (next == SAFE_SPACE && p.position == SAFE_SPACE) { + return false; + } + } + + return true; +} + +// This function takes in an row and col we want to put the sprite in +// and translate it to a real position in the view +// This is because we've ZOOMed in an that adjusts the entire view. +sf::Vector2f +pos(float c, float r) +{ + return { c * SPRITE_SIZE, r * SPRITE_SIZE }; +} -- cgit v1.2.3-54-g00ecf