the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "Texture.h"
3#include "..\Minecraft.World\StringHelpers.h"
4#include "ClockTexture.h"
5#include "CompassTexture.h"
6#include "StitchedTexture.h"
7#include "TextureManager.h"
8
9StitchedTexture *StitchedTexture::create(const wstring &name)
10{
11 // TODO: Generalize?
12 if (name.compare(L"clock") == 0)
13 {
14 return new ClockTexture();
15 }
16 else if (name.compare(L"compass") == 0)
17 {
18 return new CompassTexture();
19 }
20 else
21 {
22 return new StitchedTexture(name,name);
23 }
24}
25
26StitchedTexture::StitchedTexture(const wstring &name, const wstring &filename) : name(name)
27{
28 // 4J Initialisers
29 source = NULL;
30 rotated = false;
31 x = 0;
32 y = 0;
33 width = 0;
34 height = 0;
35 u0 = 0.0f;
36 u1 = 0.0f;
37 v0 = 0.0f;
38 v1 = 0.0f;
39 widthTranslation = 0.0f;
40 heightTranslation = 0.0f;
41 frame = 0;
42 subFrame = 0;
43 frameOverride = NULL;
44 flags = 0;
45 frames = NULL;
46 m_fileName = filename;
47}
48
49void StitchedTexture::freeFrameTextures()
50{
51 if( frames )
52 {
53 for(AUTO_VAR(it, frames->begin()); it != frames->end(); ++it)
54 {
55 TextureManager::getInstance()->unregisterTexture(L"", *it);
56 delete *it;
57 }
58 delete frames;
59 }
60}
61
62StitchedTexture::~StitchedTexture()
63{
64 for(AUTO_VAR(it, frames->begin()); it != frames->end(); ++it)
65 {
66 delete *it;
67 }
68 delete frames;
69}
70
71void StitchedTexture::initUVs(float U0, float V0, float U1, float V1)
72{
73 u0 = U0;
74 u1 = U1;
75 v0 = V0;
76 v1 = V1;
77}
78
79void StitchedTexture::init(Texture *source, vector<Texture *> *frames, int x, int y, int width, int height, bool rotated)
80{
81 this->source = source;
82 this->frames = frames;
83 frame = -1; // Force an update of animated textures
84 this->x = x;
85 this->y = y;
86 this->width = width;
87 this->height = height;
88 this->rotated = rotated;
89
90 float marginX = 0.0f; //0.01f / source->getWidth();
91 float marginY = 0.0f; //0.01f / source->getHeight();
92
93 this->u0 = x / (float) source->getWidth() + marginX;
94 this->u1 = (x + width) / (float) source->getWidth() - marginX;
95 this->v0 = y / (float) source->getHeight() + marginY;
96 this->v1 = (y + height) / (float) source->getHeight() - marginY;
97
98#ifndef _CONTENT_PACKAGE
99 bool addBreakpoint = false;
100 if(addBreakpoint)
101 {
102 printf("\nTreeTop\n");
103 printf("u0 = %f\n", u0);
104 printf("u1 = %f\n", u1);
105 printf("v0 = %f\n", v0);
106 printf("v1 = %f\n", v1);
107 printf("\n\n");
108 }
109#endif
110
111 this->widthTranslation = width / (float) SharedConstants::WORLD_RESOLUTION;
112 this->heightTranslation = height / (float) SharedConstants::WORLD_RESOLUTION;
113}
114
115void StitchedTexture::replaceWith(StitchedTexture *texture)
116{
117 init(texture->source, texture->frames, texture->x, texture->y, texture->width, texture->height, texture->rotated);
118}
119
120int StitchedTexture::getX() const
121{
122 return x;
123}
124
125int StitchedTexture::getY() const
126{
127 return y;
128}
129
130int StitchedTexture::getWidth() const
131{
132 return width;
133}
134
135int StitchedTexture::getHeight() const
136{
137 return height;
138}
139
140static const float UVAdjust = (1.0f/16.0f)/256.0f;
141
142float StitchedTexture::getU0(bool adjust/*=false*/) const
143{
144 return adjust ? ( u0 + UVAdjust ) : u0;
145}
146
147float StitchedTexture::getU1(bool adjust/*=false*/) const
148{
149 return adjust ? ( u1 - UVAdjust ) : u1;
150}
151
152float StitchedTexture::getU(double offset, bool adjust/*=false*/) const
153{
154 float diff = getU1(adjust) - getU0(adjust);
155 return getU0(adjust) + (diff * ((float) offset / SharedConstants::WORLD_RESOLUTION));
156}
157
158float StitchedTexture::getV0(bool adjust/*=false*/) const
159{
160 return adjust ? ( v0 + UVAdjust ) : v0;
161}
162
163float StitchedTexture::getV1(bool adjust/*=false*/) const
164{
165 return adjust ? ( v1 - UVAdjust ) : v1;
166}
167
168float StitchedTexture::getV(double offset, bool adjust/*=false*/) const
169{
170 float diff = getV1(adjust) - getV0(adjust);
171 return getV0(adjust) + (diff * ((float) offset / SharedConstants::WORLD_RESOLUTION));
172}
173
174wstring StitchedTexture::getName() const
175{
176 return name;
177}
178
179int StitchedTexture::getSourceWidth() const
180{
181 return source->getWidth();
182}
183
184int StitchedTexture::getSourceHeight() const
185{
186 return source->getHeight();
187}
188
189void StitchedTexture::cycleFrames()
190{
191 if (frameOverride != NULL)
192 {
193 pair<int, int> current = frameOverride->at(frame);
194 subFrame++;
195 if (subFrame >= current.second)
196 {
197 int oldFrame = current.first;
198 frame = (frame + 1) % frameOverride->size();
199 subFrame = 0;
200
201 current = frameOverride->at(frame);
202 int newFrame = current.first;
203 if (oldFrame != newFrame && newFrame >= 0 && newFrame < frames->size())
204 {
205 source->blit(x, y, frames->at(newFrame), rotated);
206 }
207 }
208
209 }
210 else
211 {
212 int oldFrame = frame;
213 frame = (frame + 1) % frames->size();
214
215 if (oldFrame != frame)
216 {
217 source->blit(x, y, frames->at(this->frame), rotated);
218 }
219 }
220}
221
222Texture *StitchedTexture::getSource()
223{
224 return source;
225}
226
227Texture *StitchedTexture::getFrame(int i)
228{
229 return frames->at(0);
230}
231
232int StitchedTexture::getFrames()
233{
234 return frames?frames->size():0;
235}
236
237/**
238* Loads animation frames from a file with the syntax, <code>
239* 0,1,2,3,
240* 4*10,5*10,
241* 4*10,3,2,1,
242* 0
243* </code> or similar
244*
245* @param bufferedReader
246*/
247void StitchedTexture::loadAnimationFrames(BufferedReader *bufferedReader)
248{
249 if(frameOverride != NULL)
250 {
251 delete frameOverride;
252 frameOverride = NULL;
253 }
254 frame = 0;
255 subFrame = 0;
256
257 intPairVector *results = new intPairVector();
258
259 //try {
260 wstring line = bufferedReader->readLine();
261 while (!line.empty())
262 {
263 line = trimString(line);
264 if (line.length() > 0)
265 {
266 std::vector<std::wstring> tokens = stringSplit(line, L',');
267 //for (String token : tokens)
268 for(AUTO_VAR(it, tokens.begin()); it != tokens.end(); ++it)
269 {
270 wstring token = *it;
271 int multiPos = token.find_first_of('*');
272 if (multiPos > 0)
273 {
274 int frame = _fromString<int>(token.substr(0, multiPos));
275 int count = _fromString<int>(token.substr(multiPos + 1));
276 results->push_back( intPairVector::value_type(frame, count));
277 }
278 else
279 {
280 int tokenVal = _fromString<int>(token);
281 results->push_back( intPairVector::value_type(tokenVal, 1));
282 }
283 }
284 }
285 line = bufferedReader->readLine();
286 }
287 //} catch (Exception e) {
288 // System.err.println("Failed to read animation info for " + name + ": " + e.getMessage());
289 //}
290
291 if (!results->empty() && results->size() < (SharedConstants::TICKS_PER_SECOND * 30))
292 {
293 frameOverride = results;
294 }
295 else
296 {
297 delete results;
298 }
299}
300
301void StitchedTexture::loadAnimationFrames(const wstring &string)
302{
303 if(frameOverride != NULL)
304 {
305 delete frameOverride;
306 frameOverride = NULL;
307 }
308 frame = 0;
309 subFrame = 0;
310
311 intPairVector *results = new intPairVector();
312
313 std::vector<std::wstring> tokens = stringSplit(trimString(string), L',');
314 //for (String token : tokens)
315 for(AUTO_VAR(it, tokens.begin()); it != tokens.end(); ++it)
316 {
317 wstring token = trimString(*it);
318 int multiPos = token.find_first_of('*');
319 if (multiPos > 0)
320 {
321 int frame = _fromString<int>(token.substr(0, multiPos));
322 int count = _fromString<int>(token.substr(multiPos + 1));
323 results->push_back( intPairVector::value_type(frame, count));
324 }
325 else if(!token.empty())
326 {
327 int tokenVal = _fromString<int>(token);
328 results->push_back( intPairVector::value_type(tokenVal, 1));
329 }
330 }
331
332 if (!results->empty() && results->size() < (SharedConstants::TICKS_PER_SECOND * 30))
333 {
334 frameOverride = results;
335 }
336 else
337 {
338 delete results;
339 }
340}
341
342void StitchedTexture::setFlags(int flags)
343{
344 this->flags = flags;
345}
346
347int StitchedTexture::getFlags() const
348{
349 return this->flags;
350}
351
352bool StitchedTexture::hasOwnData()
353{
354 return true;
355}