FIFE  2008.0
visual.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "util/log/logger.h"
31 #include "util/base/exception.h"
32 
33 #include "model/structures/instance.h"
34 #include "model/metamodel/object.h"
35 #include "model/metamodel/action.h"
36 
37 #include "visual.h"
38 
39 
40 namespace FIFE {
41 
42  static Logger _log(LM_VIEW);
43 
44  Visual2DGfx::Visual2DGfx(): m_transparency(0), m_visible(true) {
45  }
46 
48  }
49 
50  ObjectVisual::ObjectVisual() {
51  }
52 
54  if (object->getVisual<ObjectVisual>()) {
55  throw Duplicate("Object already contains visualization");
56  }
57  ObjectVisual* v = new ObjectVisual();
58  object->adoptVisual(v);
59  return v;
60  }
61 
63  }
64 
65  void ObjectVisual::addStaticImage(uint32_t angle, int32_t image_index) {
66  m_angle2img[angle % 360] = image_index;
67  }
68 
70  int32_t closestMatch = 0;
71  return getIndexByAngle(angle, m_angle2img, closestMatch);
72  }
73 
74  int32_t ObjectVisual::getClosestMatchingAngle(int32_t angle) {
75  int32_t closestMatch = 0;
76  getIndexByAngle(angle, m_angle2img, closestMatch);
77  return closestMatch;
78  }
79 
80  void ObjectVisual::getStaticImageAngles(std::vector<int32_t>& angles) {
81  angles.clear();
82  type_angle2id::const_iterator i(m_angle2img.begin());
83  while (i != m_angle2img.end()) {
84  angles.push_back(i->first);
85  ++i;
86  }
87  }
88 
89  InstanceVisual::InstanceVisual():
90  m_stackposition(0) {
91  }
92 
94  if (instance->getVisual<InstanceVisual>()) {
95  throw Duplicate("Instance already contains visualization");
96  }
97  InstanceVisual* v = new InstanceVisual();
98  instance->setVisual(v);
99  return v;
100  }
101 
103  }
104 
105  ActionVisual::ActionVisual(): m_animation_map(), m_map() {
106  }
107 
109  if (action->getVisual<ActionVisual>()) {
110  throw Duplicate("Action already contains visualization");
111  }
112  ActionVisual* v = new ActionVisual();
113  action->adoptVisual(v);
114  return v;
115  }
116 
118  }
119 
120  void ActionVisual::addAnimation(uint32_t angle, AnimationPtr animationptr) {
121  m_animation_map[angle % 360] = animationptr;
122  m_map[angle % 360] = angle % 360;
123  }
124 
126  int32_t closestMatch = 0;
127  return m_animation_map[getIndexByAngle(angle, m_map, closestMatch)];
128  }
129 
130  void ActionVisual::getActionImageAngles(std::vector<int32_t>& angles) {
131  angles.clear();
132  AngleAnimationMap::const_iterator i(m_animation_map.begin());
133  while (i != m_animation_map.end()) {
134  angles.push_back(i->first);
135  ++i;
136  }
137  }
138 }
static InstanceVisual * create(Instance *instance)
Definition: visual.cpp:93
int32_t getClosestMatchingAngle(int32_t angle)
Definition: visual.cpp:74
virtual ~Visual2DGfx()
Definition: visual.cpp:47
static ActionVisual * create(Action *action)
Definition: visual.cpp:108
T * getVisual() const
Definition: instance.h:291
void setVisual(IVisual *visual)
Definition: instance.h:287
int32_t getStaticImageIndexByAngle(int32_t angle)
Definition: visual.cpp:69
void getActionImageAngles(std::vector< int32_t > &angles)
Definition: visual.cpp:130
T * getVisual() const
Definition: object.h:117
void getStaticImageAngles(std::vector< int32_t > &angles)
Definition: visual.cpp:80
void addAnimation(uint32_t angle, AnimationPtr animationptr)
Definition: visual.cpp:120
virtual ~ActionVisual()
Definition: visual.cpp:117
static ObjectVisual * create(Object *object)
Definition: visual.cpp:53
void addStaticImage(uint32_t angle, int32_t image_index)
Definition: visual.cpp:65
AnimationPtr getAnimationByAngle(int32_t angle)
Definition: visual.cpp:125
int32_t getIndexByAngle(int32_t angle, const type_angle2id &angle2id, int32_t &closestMatchingAngle)
Definition: angles.cpp:34
virtual ~ObjectVisual()
Definition: visual.cpp:62
virtual ~InstanceVisual()
Definition: visual.cpp:102
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
Definition: soundclip.cpp:39