/
Sprite.cpp
152 lines (123 loc) · 3.59 KB
/
Sprite.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* GDevelop Core
* Copyright 2008-2014 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
#include "GDCore/BuiltinExtensions/SpriteExtension/Polygon2d.h"
#include "GDCore/BuiltinExtensions/SpriteExtension/Sprite.h"
#include "GDCore/PlatformDefinition/ImageManager.h"
#include <SFML/Graphics/Sprite.hpp>
#include <iostream>
using namespace std;
namespace gd
{
Point Sprite::badPoint("");
Sprite::Sprite() :
#if !defined(EMSCRIPTEN)
hasItsOwnImage(false),
#endif
automaticCollisionMask(true),
origine("origine"),
centre("centre"),
automaticCentre(true)
{
}
Sprite::~Sprite()
{
};
void Sprite::AddPoint( const Point & point )
{
if ( !HasPoint(point.GetName()))
points.push_back(point);
}
void Sprite::DelPoint( const std::string & name )
{
for (unsigned int i = 0;i<points.size();++i)
{
if ( name == points[i].GetName() )
points.erase(points.begin() + i);
}
}
bool Sprite::HasPoint( const std::string & name ) const
{
if (name == "Origin") return true;
if (name == "Centre" || name == "Center") return true;
for (unsigned int i = 0;i<points.size();++i)
{
if ( name == points[i].GetName() )
return true;
}
return false;
}
const Point & Sprite::GetPoint( const std::string & name) const
{
if (name == "Origin") return origine;
if (name == "Centre" || name == "Center") return centre;
for (unsigned int i = 0;i<points.size();++i)
{
if ( name == points[i].GetName() )
return points[i];
}
return badPoint;
}
Point & Sprite::GetPoint(const std::string & name)
{
if (name == "Origin") return origine;
if (name == "Centre" || name == "Center") return centre;
for (unsigned int i = 0;i<points.size();++i)
{
if ( name == points[i].GetName() )
return points[i];
}
return badPoint;
}
bool Sprite::SetDefaultCenterPoint(bool enabled)
{
automaticCentre = enabled;
#if !defined(EMSCRIPTEN)
if ( automaticCentre )
centre.SetXY(sfmlSprite.getLocalBounds().width/2, sfmlSprite.getLocalBounds().height/2);
#endif
return true;
}
std::vector<Polygon2d> Sprite::GetCollisionMask() const
{
#if !defined(EMSCRIPTEN)
if ( automaticCollisionMask )
{
std::vector<Polygon2d> mask;
Polygon2d rectangle;
rectangle.vertices.push_back(sf::Vector2f(0, 0));
rectangle.vertices.push_back(sf::Vector2f(sfmlSprite.getLocalBounds().width, 0));
rectangle.vertices.push_back(sf::Vector2f(sfmlSprite.getLocalBounds().width, sfmlSprite.getLocalBounds().height));
rectangle.vertices.push_back(sf::Vector2f(0, sfmlSprite.getLocalBounds().height));
mask.push_back(rectangle);
return mask;
}
#endif
return customCollisionMask;
}
void Sprite::SetCustomCollisionMask(const std::vector<Polygon2d> & collisionMask)
{
customCollisionMask = collisionMask;
}
#if !defined(EMSCRIPTEN)
void Sprite::LoadImage(boost::shared_ptr<SFMLTextureWrapper> image_)
{
sfmlImage = image_;
sfmlSprite.setTexture(sfmlImage->texture, true);
hasItsOwnImage = false;
if ( automaticCentre )
centre.SetXY(sfmlSprite.getLocalBounds().width/2, sfmlSprite.getLocalBounds().height/2);
}
void Sprite::MakeSpriteOwnsItsImage()
{
if ( !hasItsOwnImage || sfmlImage == boost::shared_ptr<SFMLTextureWrapper>() )
{
sfmlImage = boost::shared_ptr<SFMLTextureWrapper>(new SFMLTextureWrapper(sfmlImage->texture)); //Copy the texture.
sfmlSprite.setTexture(sfmlImage->texture);
hasItsOwnImage = true;
}
}
#endif
}