Line data Source code
1 : #include <client.hpp> 2 : #include <iostream> 3 : 4 : #define FIRST_OFFSET_SCALE (float(185) / float(1600)) // Offset of the first priority card 5 : #define PRIORITY_CARD_OFFSET (float(249) / float(1600)) // Offset between each card 6 : #define PRIORITY_SCALE (float(140) / float(900)) 7 : #define ACTION_SCALE (float(200) / float(1600)) 8 : #define FIRST_OFFSET_ACTION_SCALE (float(200) / float(900)) 9 : #define ACTION_CARD_OFFSET (float(140) / float(900)) // Offset between each action cards 10 : 11 : using namespace client; 12 : 13 : /*! 14 : * @brief Constructor 15 : * 16 : * Constructor of TextureDisplayer class 17 : * 18 : * @param filename name of the png path 19 : */ 20 0 : TextureDisplayer::TextureDisplayer(const std::string& filename) 21 : { 22 0 : texture = std::make_shared<sf::Texture>(); 23 : 24 0 : if (!texture->loadFromFile(filename)) 25 0 : throw std::runtime_error("Holder::load - Failed to load " + filename); 26 : 27 0 : mutexTexture = std::make_unique<std::mutex>(); 28 0 : } 29 : 30 0 : void TextureDisplayer::setImageType(HudTextureType imageType) 31 : { 32 0 : this->typeOfImageToLoad = imageType; 33 0 : } 34 : 35 : /*! 36 : * @brief Add a Sprite with the texture to the TextureDisplayer 37 : */ 38 0 : void TextureDisplayer::addSprite() 39 : { 40 0 : std::lock_guard<std::mutex> lock(*mutexTexture); 41 0 : std::unique_ptr<sf::Sprite> sprite = std::make_unique<sf::Sprite>(); 42 0 : sprite->setTexture(*texture); 43 0 : sprites.push_back(std::move(sprite)); 44 0 : } 45 : 46 : /*! 47 : * @brief Move the sprite Position 48 : * @param xOffset is the X offset of the map in the screen 49 : * @param yOffset is the Y offset of the map in the screen 50 : */ 51 0 : void TextureDisplayer::moveSpritePosition(int xOffset, int yOffset) 52 : { 53 0 : std::lock_guard<std::mutex> lock(*mutexTexture); 54 0 : for (unsigned i = 0; i < this->sprites.size(); i++) 55 : { 56 0 : sf::Vector2f pos = getSprite(i).getPosition(); 57 0 : getSprite(i).setPosition(pos.x + xOffset, pos.y + yOffset); 58 : } 59 0 : } 60 : 61 : /*! 62 : * @brief Delete all sprites 63 : */ 64 0 : void TextureDisplayer::clearSprites() 65 : { 66 0 : std::lock_guard<std::mutex> lock(*mutexTexture); 67 0 : this->sprites.clear(); 68 0 : } 69 : 70 : /*! 71 : * @brief Set a particular Sprite Position 72 : * @param index is the index position of the sprite 73 : * @param x is the X position of the sprite in the Map 74 : * @param y is the Y position of the sprite in the Map 75 : * @param xOffset is the X offset of the map in the screen 76 : * @param yOffset is the Y offset of the map in the screen 77 : * @param hexSize is the size of an hexagon, it is used to place element on the Map 78 : */ 79 0 : void TextureDisplayer::setSpritePosition(int index, int x, int y, int xOffset, int yOffset, std::array<int, 2> hexSize) 80 : { 81 : 82 0 : std::lock_guard<std::mutex> lock(*mutexTexture); 83 0 : xOffset += hexSize[0] != 0 ? (hexSize[0] - getSprite(index).getLocalBounds().width) / 2 : 0; 84 0 : yOffset += hexSize[1] != 0 ? (hexSize[1] - getSprite(index).getLocalBounds().height) / 2 : 0; 85 : 86 0 : int xHexSize = hexSize[0] != 0 ? hexSize[0] : getSprite(index).getLocalBounds().width; 87 0 : int yHexSize = hexSize[1] != 0 ? hexSize[1] : getSprite(index).getLocalBounds().height; 88 : 89 0 : if (y % 2 == 0) 90 : { 91 : // divide by 2 to have the good offset & -1 because width count the size from 1 92 0 : x = xOffset + (int)(xHexSize) / 2 + x * (xHexSize - 1); 93 : 94 : // multiply by 3/4 to not count the supperposition of hexagon & -1 because height count the size from 1 95 0 : y = yOffset + y + y * (yHexSize - 1) * 3 / 4; 96 : } 97 : else 98 : { 99 0 : x = xOffset + x * (xHexSize - 1); 100 0 : y = yOffset + y + y * (yHexSize - 1) * 3 / 4; 101 : } 102 0 : getSprite(index).setPosition(sf::Vector2f(x, y)); 103 0 : } 104 : 105 : /*! 106 : * @brief Set the position of all the HUD (barbareWheel, ladder, techWheel, priorityCard, actionCard) considering the type of the HUD 107 : * @param scale scale to set the size of the hud image(1 by default if the windowLength is 1600) 108 : * @param windowLength window length (1600 by default) 109 : * @param windowWidth window width (900 by default) 110 : * @param rotation rotation of the image (0 by default), usefull for the techWheel 111 : * @param index index of the priorityCard to set the x position of each priorityCard 112 : */ 113 0 : void TextureDisplayer::setHudSpritePosition(float scale, int windowLength, int windowWidth, int rotation, int index) 114 : { 115 0 : int xPos = 0; 116 0 : int yPos = 0; 117 : 118 0 : switch (this->typeOfImageToLoad) 119 : { 120 : 121 0 : case HudTextureType::barbareWheel0: 122 : case HudTextureType::barbareWheel1: 123 : case HudTextureType::barbareWheel2: 124 : case HudTextureType::barbareWheel3: 125 : case HudTextureType::barbareWheel4: 126 : { 127 0 : yPos = (windowWidth - getWidth() * scale); 128 0 : break; 129 : } 130 : 131 0 : case HudTextureType::ladder: 132 : { 133 0 : xPos = (windowLength - getWidth() * scale) / 2; 134 0 : yPos = (windowWidth - getHeight() * scale); 135 0 : break; 136 : } 137 : 138 0 : case HudTextureType::chat: 139 : { 140 0 : xPos = 20; 141 0 : yPos = 585; 142 0 : break; 143 : } 144 : 145 0 : case HudTextureType::moveMap: 146 : { 147 0 : xPos = 20; 148 0 : yPos = 653; 149 0 : break; 150 : } 151 : 152 0 : case HudTextureType::leave: 153 : { 154 0 : xPos = 10; 155 0 : yPos = 10; 156 0 : break; 157 : } 158 : 159 0 : case HudTextureType::techWheel: 160 : { 161 0 : xPos = windowLength; 162 0 : yPos = windowWidth; 163 0 : sprites[0]->setOrigin(getWidth() / 2, getHeight() / 2); 164 0 : sprites[0]->rotate(rotation); 165 0 : break; 166 : } 167 : 168 0 : case HudTextureType::arrow: 169 : { 170 0 : xPos = windowLength - getWidth() * scale; 171 0 : yPos = windowWidth - getHeight() * scale; 172 0 : break; 173 : } 174 : 175 0 : case HudTextureType::priorityCardEconomy: 176 : case HudTextureType::priorityCardArmy: 177 : case HudTextureType::priorityCardScience: 178 : case HudTextureType::priorityCardCulture: 179 : case HudTextureType::priorityCardIndustry: 180 : { 181 0 : xPos = PRIORITY_CARD_OFFSET * windowLength * index + FIRST_OFFSET_SCALE * windowLength; 182 0 : yPos = windowWidth - getHeight() * scale + PRIORITY_SCALE * windowWidth; 183 0 : break; 184 : } 185 : 186 0 : case HudTextureType::bruxelles: 187 : case HudTextureType::buenosAires: 188 : case HudTextureType::carthage: 189 : case HudTextureType::geneva: 190 : case HudTextureType::kaboul: 191 : case HudTextureType::kumasi: 192 : case HudTextureType::seoul: 193 : case HudTextureType::mohenjoDaro: 194 : case HudTextureType::actionCardPlayer1: 195 : case HudTextureType::actionCardPlayer2: 196 : case HudTextureType::actionCardPlayer3: 197 : case HudTextureType::actionCardPlayer4: 198 : { 199 0 : xPos = windowLength - getWidth() * scale; 200 0 : yPos = FIRST_OFFSET_ACTION_SCALE * windowWidth + index * ACTION_CARD_OFFSET * windowWidth; 201 0 : break; 202 : } 203 : 204 0 : case HudTextureType::empty: 205 0 : break; 206 : 207 0 : default: 208 0 : std::cerr << "Error loading Image Type" << std::endl; 209 0 : break; 210 : } 211 : 212 0 : getSprite().setScale(scale, scale); 213 0 : getSprite().setPosition(xPos, yPos); 214 0 : } 215 : 216 : /*! 217 : * @brief Get the number of sprite in a TextureDisplayer 218 : */ 219 0 : unsigned TextureDisplayer::getSize() 220 : { 221 0 : return sprites.size(); 222 : } 223 : 224 : /*! 225 : * @brief Get a particular sprite 226 : * 227 : * @param index is the position of the sprite in the textureDisplayer list of Sprite 228 : */ 229 0 : sf::Sprite &TextureDisplayer::getSprite(unsigned index) 230 : { 231 0 : return *sprites[index]; 232 : } 233 : 234 : /*! 235 : * @brief Get the Width of the texture 236 : */ 237 0 : int TextureDisplayer::getWidth() 238 : { 239 0 : return getSize() > 0 ? getSprite().getLocalBounds().width : 0; 240 : } 241 : 242 : /*! 243 : * @brief Get the Height of the texture 244 : */ 245 0 : int TextureDisplayer::getHeight() 246 : { 247 0 : return getSize() > 0 ? getSprite().getLocalBounds().height : 0; 248 : } 249 : 250 : /*! 251 : * @brief draw all the sprites of a TextureDisplayer 252 : * @param window window where the sprites are displayed 253 : */ 254 0 : void TextureDisplayer::drawTextureDisplayerSprite(std::shared_ptr<sf::RenderWindow> window) 255 : { 256 0 : for (unsigned j = 0; j < getSize(); j++) 257 : { 258 0 : std::lock_guard<std::mutex> lock(*mutexTexture); 259 0 : window->draw(getSprite(j)); 260 0 : } 261 0 : }