-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.cpp
81 lines (62 loc) · 1.84 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
/*
* Sprite.cpp
*
* Created on: 5 maj 2010
* Author: meros
*/
#include "Sprite.h"
#include <math.h>
#include <iostream>
using namespace std;
Sprite::Sprite() {
}
Sprite::~Sprite() {
// destroy_bitmap(myBMP);
}
bool Sprite::LoadTGA(const char* aTgaFile) {
if (!aTgaFile)
return false;
if (!(myBMP.loadFromFile(aTgaFile)))
return false;
mySprite.setTexture(myBMP);
return true;
}
void Sprite::DrawGroundCenterRelative(sf::RenderTarget& aTarget, float aXCenter,
float aYGround, float aHeight, float aRotation) {
float factor = (float) aHeight / myBMP.getSize().y;
mySprite.setScale(factor, factor);
mySprite.setPosition(aXCenter - mySprite.getTextureRect().width / 2.0,
aYGround - aHeight);
mySprite.setRotation(-aRotation / (2 * 3.1415) * 360);
aTarget.draw(mySprite);
}
void Sprite::DrawTiling(sf::RenderTarget& aTarget, float aXStart, float aYStart,
float aXEnd, float aYEnd) {
float factor = (float) (aYEnd - aYStart) / myBMP.getSize().y;
mySprite.setScale(factor, factor);
int numreps = floor(
0.5 + (aXEnd - aXStart) / mySprite.getGlobalBounds().width);
int reps = 0;
for (float x = aXStart; true; x += mySprite.getGlobalBounds().width) {
//FIXME: uhm.. ugly
reps++;
if (reps > numreps)
break;
mySprite.setPosition(x, aYStart);
aTarget.draw(mySprite);
}
}
void Sprite::DrawStretchRotation(sf::RenderTarget& aTarget, float aXCenter,
float aYCenter, float aNonRotHeight, float aAngleRad) {
float factor = (aNonRotHeight / myBMP.getSize().y);
mySprite.setScale(factor, factor);
mySprite.setOrigin(myBMP.getSize().x / 2.0, myBMP.getSize().y / 2.0);
mySprite.setPosition(aXCenter, aYCenter);
mySprite.setRotation(aAngleRad / (2 * 3.1415) * 360);
aTarget.draw(mySprite);
}
void Sprite::Mirror() {
sf::IntRect rect = mySprite.getTextureRect();
rect.width = -rect.width;
mySprite.setTextureRect(rect);
}