comparison engine/core/view/visual.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
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 modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, 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) {
45 }
46
47 Visual2DGfx::~Visual2DGfx() {
48 }
49
50 ObjectVisual::ObjectVisual() {
51 }
52
53 ObjectVisual* ObjectVisual::create(Object* object) {
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
62 ObjectVisual::~ObjectVisual() {
63 }
64
65 void ObjectVisual::addStaticImage(unsigned int angle, int image_index) {
66 m_angle2img[angle % 360] = image_index;
67 }
68
69 int ObjectVisual::getStaticImageIndexByAngle(int angle) {
70 int closestMatch = 0;
71 return getIndexByAngle(angle, m_angle2img, closestMatch);
72 }
73
74 int ObjectVisual::getClosestMatchingAngle(int angle) {
75 int closestMatch = 0;
76 getIndexByAngle(angle, m_angle2img, closestMatch);
77 return closestMatch;
78 }
79
80 void ObjectVisual::getStaticImageAngles(std::vector<int>& 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 const int STATIC_IMAGE_NOT_INITIALIZED = -2;
90 const int STATIC_IMAGE_NOT_FOUND = -1;
91
92 InstanceVisualCacheItem::InstanceVisualCacheItem():
93 screenpoint(),
94 dimensions(),
95 image(NULL),
96 m_cached_static_img_id(STATIC_IMAGE_NOT_INITIALIZED),
97 m_cached_static_img_angle(0) {
98 }
99
100 int InstanceVisualCacheItem::getStaticImageIndexByAngle(unsigned int angle, Instance* instance) {
101 if (static_cast<int>(angle) != m_cached_static_img_angle) {
102 m_cached_static_img_id = STATIC_IMAGE_NOT_INITIALIZED;
103 }
104 if (m_cached_static_img_id != STATIC_IMAGE_NOT_INITIALIZED) {
105 return m_cached_static_img_id;
106 }
107 m_cached_static_img_id = instance->getObject()->getVisual<ObjectVisual>()->getStaticImageIndexByAngle(angle);
108 m_cached_static_img_angle = angle;
109 return m_cached_static_img_id;
110 }
111
112 InstanceVisual::InstanceVisual():
113 m_stackposition(0) {
114 }
115
116 InstanceVisual* InstanceVisual::create(Instance* instance) {
117 if (instance->getVisual<InstanceVisual>()) {
118 throw Duplicate("Instance already contains visualization");
119 }
120 InstanceVisual* v = new InstanceVisual();
121 instance->setVisual(v);
122 return v;
123 }
124
125 InstanceVisual::~InstanceVisual() {
126 }
127
128 ActionVisual::ActionVisual(): m_animations() {
129 }
130
131 ActionVisual* ActionVisual::create(Action* action) {
132 if (action->getVisual<ActionVisual>()) {
133 throw Duplicate("Action already contains visualization");
134 }
135 ActionVisual* v = new ActionVisual();
136 action->adoptVisual(v);
137 return v;
138 }
139
140 ActionVisual::~ActionVisual() {
141 }
142
143 void ActionVisual::addAnimation(unsigned int angle, int animation_index) {
144 m_animations[angle % 360] = animation_index;
145 }
146
147 int ActionVisual::getAnimationIndexByAngle(int angle) {
148 int closestMatch = 0;
149 return getIndexByAngle(angle, m_animations, closestMatch);
150 }
151 }