1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name graphicanimation.cpp - The Graphic Animation class. */
12 //
13 // (c) Copyright 2007-2008 by Francois Beerten
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29
30 //@{
31
32 #include "stratagus.h"
33
34 #include "particle.h"
35
36 #include "map.h"
37 #include "player.h"
38 #include "ui.h"
39 #include "video.h"
40
GraphicAnimation(CGraphic * g,int ticksPerFrame)41 GraphicAnimation::GraphicAnimation(CGraphic *g, int ticksPerFrame) :
42 g(g), ticksPerFrame(ticksPerFrame), currentFrame(0), currTicks(0)
43 {
44 Assert(g);
45 }
46
47
draw(int x,int y)48 void GraphicAnimation::draw(int x, int y)
49 {
50 if (!isFinished()) {
51 g->DrawFrameClip(currentFrame, x - g->Width / 2, y - g->Height / 2);
52 }
53 }
54
update(int ticks)55 void GraphicAnimation::update(int ticks)
56 {
57 currTicks += ticks;
58 while (currTicks > ticksPerFrame) {
59 currTicks -= ticksPerFrame;
60 ++currentFrame;
61 }
62 }
63
isFinished()64 bool GraphicAnimation::isFinished()
65 {
66 return currentFrame >= g->NumFrames;
67 }
68
isVisible(const CViewport & vp,const CPosition & pos)69 bool GraphicAnimation::isVisible(const CViewport &vp, const CPosition &pos)
70 {
71 // invisible graphics always invisible
72 if (!g) {
73 return false;
74 }
75
76 PixelSize graphicSize(g->Width, g->Height);
77 PixelDiff margin(PixelTileSize.x - 1, PixelTileSize.y - 1);
78 PixelPos position(pos.x, pos.y);
79 Vec2i minPos = Map.MapPixelPosToTilePos(position);
80 Vec2i maxPos = Map.MapPixelPosToTilePos(position + graphicSize + margin);
81 Map.Clamp(minPos);
82 Map.Clamp(maxPos);
83
84 if (!vp.AnyMapAreaVisibleInViewport(minPos, maxPos)) {
85 return false;
86 }
87
88 Vec2i p;
89 for (p.x = minPos.x; p.x <= maxPos.x; ++p.x) {
90 for (p.y = minPos.y; p.y <= maxPos.y; ++p.y) {
91 if (ReplayRevealMap || Map.Field(p)->playerInfo.IsTeamVisible(*ThisPlayer)) {
92 return true;
93 }
94 }
95 }
96 return false;
97 }
98
clone()99 GraphicAnimation *GraphicAnimation::clone()
100 {
101 return new GraphicAnimation(g, ticksPerFrame);
102 }
103
104 //@}
105