-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaledSprite.cpp
108 lines (98 loc) · 3.28 KB
/
scaledSprite.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
#include <iostream>
#include <cmath>
#include "SDL/SDL_rotozoom.h"
#include "scaledSprite.h"
#include "gamedata.h"
#include<stdlib.h>
#include<time.h>
ScaledSprite::ScaledSprite(const std::string& name,
SDL_Surface* surface) :
Drawable(name,
Vector2f(0, rand()%380 ),
Vector2f( (rand()%2?1:-1)*getRandom(
Gamedata::getInstance().getXmlInt(name+"/cushion"),
Gamedata::getInstance().getXmlInt(name+"/speed/x")),
(rand()%2?1:-1)*getRandom(
Gamedata::getInstance().getXmlInt(name+"/cushion"),
Gamedata::getInstance().getXmlInt(name+"/speed/y"))
)
),
scale(getRandFloat(Gamedata::getInstance().getXmlFloat(name+"/scale/min"),
Gamedata::getInstance().getXmlFloat(name+"/scale/max"))
),
scaledSurface( rotozoomSurface(surface, 0, scale, SMOOTHING_ON) ),
frame( new Frame(scaledSurface, scaledSurface->w, scaledSurface->h,
Gamedata::getInstance().getXmlInt(name+"/src/x"),
Gamedata::getInstance().getXmlInt(name+"/src/y"))
),
frameWidth(frame->getWidth()),
frameHeight(frame->getHeight()),
worldWidth(Gamedata::getInstance().getXmlInt("world/width")),
worldHeight(Gamedata::getInstance().getXmlInt("world/height"))
{ }
ScaledSprite::ScaledSprite(const ScaledSprite& s) :
Drawable(s.getName(), s.getPosition(), s.getVelocity()),
scale(s.scale),
scaledSurface(s.scaledSurface),
frame(s.frame),
frameWidth(s.frameWidth),
frameHeight(s.frameHeight),
worldWidth(s.worldWidth),
worldHeight(s.worldHeight)
{ }
ScaledSprite& ScaledSprite::operator=(const ScaledSprite& rhs) {
setName( rhs.getName() );
setPosition(rhs.getPosition());
setVelocity(rhs.getVelocity());
scale = rhs.scale;
scaledSurface = rhs.scaledSurface;
frame = rhs.frame;
frameWidth = rhs.frameWidth;
frameHeight = rhs.frameHeight;
worldWidth = rhs.worldWidth;
worldHeight = rhs.worldHeight;
return *this;
}
ScaledSprite::~ScaledSprite() {
SDL_FreeSurface( scaledSurface );
delete frame;
}
void ScaledSprite::draw() const {
Uint32 x = static_cast<Uint32>(X());
Uint32 y = static_cast<Uint32>(Y());
frame->draw(x, y);
}
unsigned ScaledSprite::getPixel(Uint32 i, Uint32 j) const {
Uint32 x = static_cast<Uint32>(X());
Uint32 y = static_cast<Uint32>(Y());
x = i - x;
y = j - y;
Uint32 *pixels = static_cast<Uint32 *>(frame->getSurface()->pixels);
return pixels[ ( y * frame->getWidth() ) + x ];
}
void ScaledSprite::update(Uint32 ticks) {
//Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001;
//setPosition(getPosition() + incr);
//float incrY = velocityY() * static_cast<float>(ticks) * 0.001;
float incrX = velocityX() * static_cast<float>(ticks) * (0.001/4);
// Y( Y() + incrY );
X(X() + incrX);
if ( Y() < 0) {
velocityY( abs( velocityY() ) );
}
if ( Y() > worldHeight-frameHeight) {
Vector2f XPos(((rand()%1280)+1),0);
setPosition( XPos );
velocityY( abs( velocityY() ) );
}
if ( X() < 0) {
Vector2f XPos(0, ((rand()%380)+1));
setPosition( XPos );
velocityY( abs( velocityY() ) );
}
if ( X() > worldWidth-frameWidth) {
Vector2f XPos(0, ((rand()%380)+1));
setPosition( XPos );
velocityY( abs( velocityY() ) );
}
}