aboutsummaryrefslogtreecommitdiff
path: root/src/gui/guiAnimatedImage.cpp
blob: b1447c45f6732ac90b5bceece30760b8c8f22374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "guiAnimatedImage.h"

#include "client/guiscalingfilter.h"
#include "client/tile.h" // ITextureSource
#include "log.h"
#include "porting.h"
#include "util/string.h"
#include <string>
#include <vector>

GUIAnimatedImage::GUIAnimatedImage(gui::IGUIEnvironment *env, gui::IGUIElement *parent,
	s32 id, const core::rect<s32> &rectangle, const std::string &texture_name,
	s32 frame_count, s32 frame_duration, ISimpleTextureSource *tsrc) :
	gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle), m_tsrc(tsrc)
{
	m_texture = m_tsrc->getTexture(texture_name);

	m_frame_count    = std::max(frame_count,    1);
	m_frame_duration = std::max(frame_duration, 0);

	if (m_texture != nullptr) {
		core::dimension2d<u32> size = m_texture->getOriginalSize();
		if (size.Height < (u64)m_frame_count)
			m_frame_count = size.Height;
	} else {
		// No need to step an animation if we have nothing to draw
		m_frame_count = 1;
	}
}

void GUIAnimatedImage::draw()
{
	// Render the current frame
	if (m_texture != nullptr) {
		video::IVideoDriver *driver = Environment->getVideoDriver();

		const video::SColor color(255, 255, 255, 255);
		const video::SColor colors[] = {color, color, color, color};

		core::dimension2d<u32> size = m_texture->getOriginalSize();
		size.Height /= m_frame_count;

		draw2DImageFilterScaled(driver, m_texture, AbsoluteRect,
			core::rect<s32>(core::position2d<s32>(0, size.Height * m_frame_idx), size),
			NoClip ? nullptr : &AbsoluteClippingRect, colors, true);
	}

	// Step the animation
	if (m_frame_count > 1 && m_frame_duration > 0) {
		// Determine the delta time to step
		u64 new_global_time = porting::getTimeMs();
		if (m_global_time > 0)
			m_frame_time += new_global_time - m_global_time;

		m_global_time = new_global_time;

		// Advance by the number of elapsed frames, looping if necessary
		m_frame_idx += u32(m_frame_time / m_frame_duration);
		m_frame_idx %= m_frame_count;

		// If 1 or more frames have elapsed, reset the frame time counter with
		// the remainder
		m_frame_time %= m_frame_duration;
	}
}


void GUIAnimatedImage::setFrameIndex(s32 frame)
{
	s32 idx = std::max(frame, 0);
	if (idx > 0 && idx < m_frame_count)
		m_frame_idx = idx;
}
Function(const Proto* f, const TString* p, DumpState* D); static void DumpConstants(const Proto* f, DumpState* D) { int i,n=f->sizek; DumpInt(n,D); for (i=0; i<n; i++) { const TValue* o=&f->k[i]; DumpChar(ttype(o),D); switch (ttype(o)) { case LUA_TNIL: break; case LUA_TBOOLEAN: DumpChar(bvalue(o),D); break; case LUA_TNUMBER: DumpNumber(nvalue(o),D); break; case LUA_TSTRING: DumpString(rawtsvalue(o),D); break; default: lua_assert(0); /* cannot happen */ break; } } n=f->sizep; DumpInt(n,D); for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); } static void DumpDebug(const Proto* f, DumpState* D) { int i,n; n= (D->strip) ? 0 : f->sizelineinfo; DumpVector(f->lineinfo,n,sizeof(int),D); n= (D->strip) ? 0 : f->sizelocvars; DumpInt(n,D); for (i=0; i<n; i++) { DumpString(f->locvars[i].varname,D); DumpInt(f->locvars[i].startpc,D); DumpInt(f->locvars[i].endpc,D); } n= (D->strip) ? 0 : f->sizeupvalues; DumpInt(n,D); for (i=0; i<n; i++) DumpString(f->upvalues[i],D); } static void DumpFunction(const Proto* f, const TString* p, DumpState* D) { DumpString((f->source==p || D->strip) ? NULL : f->source,D); DumpInt(f->linedefined,D); DumpInt(f->lastlinedefined,D); DumpChar(f->nups,D); DumpChar(f->numparams,D); DumpChar(f->is_vararg,D); DumpChar(f->maxstacksize,D); DumpCode(f,D); DumpConstants(f,D); DumpDebug(f,D); } static void DumpHeader(DumpState* D) { char h[LUAC_HEADERSIZE]; luaU_header(h); DumpBlock(h,LUAC_HEADERSIZE,D); } /* ** dump Lua function as precompiled chunk */ int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) { DumpState D; D.L=L; D.writer=w; D.data=data; D.strip=strip; D.status=0; DumpHeader(&D); DumpFunction(f,NULL,&D); return D.status; }