Coverage report for gui


src/
File: src/render/sprites/Sprite.hpp
Date: 2024-06-25 10:57:02
Lines:
0/28
0.0%
Functions:
0/3
0.0%
Branches:
0/195
0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** sprite
6 */
7
8 #ifndef SPRITE_HPP_
9 #define SPRITE_HPP_
10
11 #include <SFML/Graphics.hpp>
12 #include <iostream>
13
14
15 class Sprite {
16 public:
17 Sprite(const std::string &path, int frameCount = 1, float frameTime = 0.1f) : _frame(0), _frameCount(frameCount), _frameTime(frameTime)
18 {
19 _path = path;
20 if (!_texture.loadFromFile(path))
21 throw std::runtime_error("Cannot load texture");
22 _sprite.setTexture(_texture);
23 _frameSize = sf::Vector2u(_texture.getSize().x / _frameCount, _texture.getSize().y);
24 _sprite.setOrigin(_frameSize.x / 2, _frameSize.y / 2);
25 _sprite.setTextureRect(sf::IntRect(0, 0, _frameSize.x, _frameSize.y));
26 }
27 ~Sprite() {};
28
29 void setPosition(sf::Vector2f pos) { _sprite.setPosition(pos); }
30 sf::Vector2f getPosition() { return _sprite.getPosition(); }
31 void setSize(sf::Vector2f size) {
32 _sprite.setScale(size);
33 }
34 void draw(sf::RenderWindow &window) { window.draw(_sprite); }
35 int update(float fElapsedTime);
36 void disableLooping() { _looping = false; }
37 void setFrame(int frame) {
38 if (frame >= _frameCount)
39 _frame = 0;
40 else
41 _frame = frame;
42 _sprite.setTextureRect(sf::IntRect(_frame * _frameSize.x, 0, _frameSize.x, _frameSize.y));
43 }
44 bool mouseOver(sf::RenderWindow &window) {
45 sf::Vector2i mousePos = sf::Mouse::getPosition(window);
46 sf::Vector2f worldPos = window.mapPixelToCoords(mousePos);
47 sf::FloatRect bounds = _sprite.getGlobalBounds();
48 return bounds.contains(worldPos);
49 }
50 void resetOrigin() {
51 _sprite.setOrigin(0, 0);
52 }
53 void setScale(float scale) {
54 _sprite.setScale(scale, scale);
55 }
56 void setColor(sf::Color color) {
57 _sprite.setColor(color);
58 }
59 void setRotation(float angle) {
60 _sprite.setRotation(angle);
61 }
62 bool isFinished() {
63 if (_frame == _frameCount - 1)
64 return true;
65 return false;
66 }
67
68 sf::Sprite _sprite;
69 private:
70 sf::Texture _texture;
71 sf::Vector2u _frameSize;
72 bool _looping = true;
73
74 int _frame = 0;
75 int _frameCount = 1;
76 float _frameTime = 0.1f;
77 float _time = 0;
78 std::string _path = "";
79 };
80
81 #endif /* !SPRITE_HPP_ */
82