Mana
Loading...
Searching...
No Matches
sprite.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2010-2012 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include "resources/spritedef.h"
24
25class Animation;
26class Graphics;
27class Image;
28struct Frame;
29
30// Default frame display delay in milliseconds
31const int DEFAULT_FRAME_DELAY = 75;
32
36class Sprite
37{
38 public:
43 Sprite(SpriteDef *sprite);
44
52 static Sprite *load(const std::string &filename, int variant = 0);
53
55
61 bool reset();
62
68 bool play(const std::string &action);
69
76 bool update(int time);
77
82 bool draw(Graphics *graphics, int posX, int posY) const;
83
87 int getWidth() const;
88
92 int getHeight() const;
93
97 int getOffsetX() const;
98
102 int getOffsetY() const;
103
107 const Image *getImage() const;
108
114 bool setDirection(SpriteDirection direction);
115
119 void setAlpha(float alpha) { mAlpha = alpha; }
120
124 float getAlpha() const { return mAlpha; }
125
129 int getDuration() const;
130
131 private:
132 bool updateCurrentAnimation(int dt);
133
134 float mAlpha = 1.0f;
138 int mFrameIndex = 0;
139 int mFrameTime = 0;
142 Action *mAction = nullptr;
144 Frame *mFrame = nullptr;
145};
An action consists of several animations, one for each direction.
Definition action.h:32
An animation consists of several frames, each with their own delay and offset.
Definition animation.h:47
A central point of control for graphics.
Definition graphics.h:78
Defines a class for loading and storing images.
Definition image.h:45
Automatically counting Resource reference.
Definition resource.h:74
Defines a class to load an animation.
Definition spritedef.h:87
Animates a sprite by adding playback state.
Definition sprite.h:37
int getOffsetY() const
Gets the vertical offset that the sprite will be drawn at.
Definition sprite.cpp:198
int getDuration() const
Returns the duration of the current sprite animation in milliseconds.
Definition sprite.cpp:172
int mFrameIndex
The index of the current frame.
Definition sprite.h:138
int mFrameTime
The time since start of frame.
Definition sprite.h:139
float getAlpha() const
Returns the current alpha opacity of the sprite.
Definition sprite.h:124
bool draw(Graphics *graphics, int posX, int posY) const
Draw the current animation frame at the coordinates given in screen pixels.
Definition sprite.cpp:133
Animation * mAnimation
The currently active animation.
Definition sprite.h:143
bool updateCurrentAnimation(int dt)
Definition sprite.cpp:105
int getWidth() const
Gets the width in pixels of the image.
Definition sprite.cpp:179
SpriteDirection mDirection
The sprite direction.
Definition sprite.h:136
bool update(int time)
Inform the animation of the passed time so that it can output the correct animation frame.
Definition sprite.cpp:87
Action * mAction
The currently active action.
Definition sprite.h:142
const Image * getImage() const
Returns a reference to the current image being drawn.
Definition sprite.cpp:203
static Sprite * load(const std::string &filename, int variant=0)
An helper function, which will request the sprite to animate from the resource manager.
Definition sprite.cpp:42
float mAlpha
The alpha opacity used to draw.
Definition sprite.h:134
int getOffsetX() const
Gets the horizontal offset that the sprite will be drawn at.
Definition sprite.cpp:193
bool setDirection(SpriteDirection direction)
Sets the direction.
Definition sprite.cpp:149
ResourceRef< SpriteDef > mSprite
The sprite definition.
Definition sprite.h:141
Frame * mFrame
The currently active frame.
Definition sprite.h:144
void setAlpha(float alpha)
Sets the alpha value of the sprite.
Definition sprite.h:119
bool reset()
Resets the sprite.
Definition sprite.cpp:53
int getHeight() const
Gets the height in pixels of the image.
Definition sprite.cpp:186
bool play(const std::string &action)
Plays an action using the current direction.
Definition sprite.cpp:67
Graphics * graphics
Definition client.cpp:104
const int DEFAULT_FRAME_DELAY
Definition sprite.h:31
SpriteDirection
Definition spritedef.h:74
@ DIRECTION_DOWN
Definition spritedef.h:77
A single frame in an animation, with a delay and an offset.
Definition animation.h:35