-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpriteBatch.cpp
52 lines (47 loc) · 1.31 KB
/
SpriteBatch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "SpriteBatch.hpp"
#include "Common.hpp"
#include <SFML/Graphics/PrimitiveType.hpp>
#include <SFML/System/Vector2.hpp>
namespace swift
{
SpriteBatch::SpriteBatch(const sf::Texture& tex, unsigned int s)
: vertices(s * 6),
texture(tex),
spriteNum(0)
{
sf::Vector2f texSize = static_cast<sf::Vector2f>(texture.getSize());
for(auto i = 0u; i < vertices.size(); i += 6)
{
std::array<sf::Vector2f, 6> triangles = getTrianglesFromVector(texSize);
for( int j = 0; j < 6; j++ ) {
vertices[i+j].texCoords = triangles[j];
}
}
}
const std::vector<sf::Vertex>& SpriteBatch::getVertices() const
{
return vertices;
}
std::array<sf::Vertex*, 6> SpriteBatch::addSprite()
{
if((spriteNum + 1) * 6 <= vertices.size())
{
unsigned int s = spriteNum * 6;
spriteNum++;
return {&vertices[s], &vertices[s + 1], &vertices[s + 2], &vertices[s + 3], &vertices[s+4], &vertices[s+5]};
}
else
{
return {nullptr};
}
}
sf::Vector2u SpriteBatch::getTextureSize() const
{
return texture.getSize();
}
void SpriteBatch::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.texture = &texture;
target.draw(vertices.data(), spriteNum * 6, sf::Triangles, states);
}
}