Skip to content

Commit 523f0e8

Browse files
committedJan 2, 2017
Move TileAnimation code to seperate file
1 parent e2e8da5 commit 523f0e8

File tree

10 files changed

+168
-48
lines changed

10 files changed

+168
-48
lines changed
 

‎src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ set(common_SRCS
459459
staticobject.cpp
460460
subgame.cpp
461461
terminal_chat_console.cpp
462+
tileanimation.cpp
462463
tool.cpp
463464
treegen.cpp
464465
version.cpp

‎src/client/tile.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@ enum MaterialType{
161161
// Should the crack be drawn on transparent pixels (unset) or not (set)?
162162
// Ignored if MATERIAL_FLAG_CRACK is not set.
163163
#define MATERIAL_FLAG_CRACK_OVERLAY 0x04
164-
// Animation made up by splitting the texture to vertical frames, as
165-
// defined by extra parameters
166-
#define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
164+
#define MATERIAL_FLAG_ANIMATION 0x08
167165
#define MATERIAL_FLAG_HIGHLIGHTED 0x10
168166
#define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
169167
#define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40

‎src/mapblock_mesh.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
11311131
&p.tile.texture_id);
11321132
}
11331133
// - Texture animation
1134-
if(p.tile.material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
1135-
{
1134+
if (p.tile.material_flags & MATERIAL_FLAG_ANIMATION) {
11361135
// Add to MapBlockMesh in order to animate these tiles
11371136
m_animation_tiles[i] = p.tile;
11381137
m_animation_frames[i] = 0;

‎src/network/networkprotocol.h

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
140140
CPT2_MESHOPTIONS
141141
PROTOCOL_VERSION 29:
142142
Server doesn't accept TOSERVER_BREATH anymore
143+
serialization of TileAnimation params changed
144+
TAT_SHEET_2D
143145
*/
144146

145147
#define LATEST_PROTOCOL_VERSION 29

‎src/nodedef.cpp

+14-21
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,16 @@ void NodeBox::deSerialize(std::istream &is)
188188

189189
void TileDef::serialize(std::ostream &os, u16 protocol_version) const
190190
{
191-
if (protocol_version >= 26)
191+
if (protocol_version >= 29)
192+
writeU8(os, 3);
193+
else if (protocol_version >= 26)
192194
writeU8(os, 2);
193195
else if (protocol_version >= 17)
194196
writeU8(os, 1);
195197
else
196198
writeU8(os, 0);
197199
os<<serializeString(name);
198-
writeU8(os, animation.type);
199-
writeU16(os, animation.aspect_w);
200-
writeU16(os, animation.aspect_h);
201-
writeF1000(os, animation.length);
200+
animation.serialize(os, protocol_version);
202201
if (protocol_version >= 17)
203202
writeU8(os, backface_culling);
204203
if (protocol_version >= 26) {
@@ -211,10 +210,7 @@ void TileDef::deSerialize(std::istream &is, const u8 contenfeatures_version, con
211210
{
212211
int version = readU8(is);
213212
name = deSerializeString(is);
214-
animation.type = (TileAnimationType)readU8(is);
215-
animation.aspect_w = readU16(is);
216-
animation.aspect_h = readU16(is);
217-
animation.length = readF1000(is);
213+
animation.deSerialize(is, version >= 3 ? 29 : 26);
218214
if (version >= 1)
219215
backface_culling = readU8(is);
220216
if (version >= 2) {
@@ -533,28 +529,24 @@ void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
533529
if (backface_culling)
534530
tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
535531
if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
536-
tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
532+
tile->material_flags |= MATERIAL_FLAG_ANIMATION;
537533
if (tiledef->tileable_horizontal)
538534
tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL;
539535
if (tiledef->tileable_vertical)
540536
tile->material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL;
541537

542538
// Animation parameters
543539
int frame_count = 1;
544-
if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
545-
// Get texture size to determine frame count by aspect ratio
546-
v2u32 size = tile->texture->getOriginalSize();
547-
int frame_height = (float)size.X /
548-
(float)tiledef->animation.aspect_w *
549-
(float)tiledef->animation.aspect_h;
550-
frame_count = size.Y / frame_height;
551-
int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
540+
if (tile->material_flags & MATERIAL_FLAG_ANIMATION) {
541+
int frame_length_ms;
542+
tiledef->animation.determineParams(tile->texture->getOriginalSize(),
543+
&frame_count, &frame_length_ms);
552544
tile->animation_frame_count = frame_count;
553545
tile->animation_frame_length_ms = frame_length_ms;
554546
}
555547

556548
if (frame_count == 1) {
557-
tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
549+
tile->material_flags &= ~MATERIAL_FLAG_ANIMATION;
558550
} else {
559551
std::ostringstream os(std::ios::binary);
560552
tile->frames.resize(frame_count);
@@ -564,8 +556,9 @@ void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
564556
FrameSpec frame;
565557

566558
os.str("");
567-
os << tiledef->name << "^[verticalframe:"
568-
<< frame_count << ":" << i;
559+
os << tiledef->name;
560+
tiledef->animation.getTextureModifer(os,
561+
tile->texture->getOriginalSize(), i);
569562

570563
frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
571564
if (tile->normal_texture)

‎src/nodedef.h

+3-13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3434
#include "itemgroup.h"
3535
#include "sound.h" // SimpleSoundSpec
3636
#include "constants.h" // BS
37+
#include "tileanimation.h"
3738

3839
class INodeDefManager;
3940
class IItemDefManager;
@@ -161,22 +162,14 @@ enum NodeDrawType
161162
/*
162163
Stand-alone definition of a TileSpec (basically a server-side TileSpec)
163164
*/
164-
enum TileAnimationType{
165-
TAT_NONE=0,
166-
TAT_VERTICAL_FRAMES=1,
167-
};
165+
168166
struct TileDef
169167
{
170168
std::string name;
171169
bool backface_culling; // Takes effect only in special cases
172170
bool tileable_horizontal;
173171
bool tileable_vertical;
174-
struct{
175-
enum TileAnimationType type;
176-
int aspect_w; // width for aspect ratio
177-
int aspect_h; // height for aspect ratio
178-
float length; // seconds
179-
} animation;
172+
struct TileAnimationParams animation;
180173

181174
TileDef()
182175
{
@@ -185,9 +178,6 @@ struct TileDef
185178
tileable_horizontal = true;
186179
tileable_vertical = true;
187180
animation.type = TAT_NONE;
188-
animation.aspect_w = 1;
189-
animation.aspect_h = 1;
190-
animation.length = 1.0;
191181
}
192182

193183
void serialize(std::ostream &os, u16 protocol_version) const;

‎src/particles.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -567,19 +567,20 @@ void ParticleManager::addNodeParticle(IGameDef* gamedef, scene::ISceneManager* s
567567
{
568568
// Texture
569569
u8 texid = myrand_range(0, 5);
570-
video::ITexture *texture = tiles[texid].texture;
570+
video::ITexture *texture;
571571

572572
// Only use first frame of animated texture
573-
f32 ymax = 1;
574-
if(tiles[texid].material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
575-
ymax /= tiles[texid].animation_frame_count;
573+
if(tiles[texid].material_flags & MATERIAL_FLAG_ANIMATION)
574+
texture = tiles[texid].frames[0].texture;
575+
else
576+
texture = tiles[texid].texture;
576577

577578
float size = rand() % 64 / 512.;
578579
float visual_size = BS * size;
579-
v2f texsize(size * 2, ymax * size * 2);
580+
v2f texsize(size * 2, size * 2);
580581
v2f texpos;
581582
texpos.X = ((rand() % 64) / 64. - texsize.X);
582-
texpos.Y = ymax * ((rand() % 64) / 64. - texsize.Y);
583+
texpos.Y = ((rand() % 64) / 64. - texsize.Y);
583584

584585
// Physics
585586
v3f velocity((rand() % 100 / 50. - 1) / 1.5,

‎src/script/common/c_content.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,11 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
338338
tiledef.animation.type = (TileAnimationType)
339339
getenumfield(L, -1, "type", es_TileAnimationType,
340340
TAT_NONE);
341-
tiledef.animation.aspect_w =
341+
tiledef.animation.vertical_frames.aspect_w =
342342
getintfield_default(L, -1, "aspect_w", 16);
343-
tiledef.animation.aspect_h =
343+
tiledef.animation.vertical_frames.aspect_h =
344344
getintfield_default(L, -1, "aspect_h", 16);
345-
tiledef.animation.length =
345+
tiledef.animation.vertical_frames.length =
346346
getfloatfield_default(L, -1, "length", 1.0);
347347
}
348348
lua_pop(L, 1);

‎src/tileanimation.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2016 sfan5 <sfan5@live.de>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
#include "tileanimation.h"
20+
#include "util/serialize.h"
21+
22+
void TileAnimationParams::serialize(std::ostream &os, u16 protocol_version) const
23+
{
24+
if(protocol_version < 29 /* TODO bump */) {
25+
if (type == TAT_VERTICAL_FRAMES) {
26+
writeU8(os, type);
27+
writeU16(os, vertical_frames.aspect_w);
28+
writeU16(os, vertical_frames.aspect_h);
29+
writeF1000(os, vertical_frames.length);
30+
} else {
31+
writeU8(os, TAT_NONE);
32+
writeU16(os, 1);
33+
writeU16(os, 1);
34+
writeF1000(os, 1.0);
35+
}
36+
return;
37+
}
38+
39+
writeU8(os, type);
40+
if (type == TAT_VERTICAL_FRAMES) {
41+
writeU16(os, vertical_frames.aspect_w);
42+
writeU16(os, vertical_frames.aspect_h);
43+
writeF1000(os, vertical_frames.length);
44+
}
45+
}
46+
47+
void TileAnimationParams::deSerialize(std::istream &is, u16 protocol_version)
48+
{
49+
type = (TileAnimationType) readU8(is);
50+
if(protocol_version < 29 /* TODO bump */) {
51+
vertical_frames.aspect_w = readU16(is);
52+
vertical_frames.aspect_h = readU16(is);
53+
vertical_frames.length = readF1000(is);
54+
return;
55+
}
56+
57+
if(type == TAT_VERTICAL_FRAMES) {
58+
vertical_frames.aspect_w = readU16(is);
59+
vertical_frames.aspect_h = readU16(is);
60+
vertical_frames.length = readF1000(is);
61+
}
62+
}
63+
64+
void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count, int *frame_length_ms) const
65+
{
66+
if (type == TAT_NONE) {
67+
*frame_count = 1;
68+
*frame_length_ms = 1000;
69+
return;
70+
}
71+
int frame_height = (float)texture_size.X /
72+
(float)vertical_frames.aspect_w *
73+
(float)vertical_frames.aspect_h;
74+
if (frame_count)
75+
*frame_count = texture_size.Y / frame_height;
76+
if (frame_length_ms)
77+
*frame_length_ms = 1000.0 * vertical_frames.length / (texture_size.Y / frame_height);
78+
}
79+
80+
void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
81+
{
82+
if (type == TAT_NONE)
83+
return;
84+
int frame_count;
85+
determineParams(texture_size, &frame_count, NULL);
86+
os << "^[verticalframe:" << frame_count << ":" << frame;
87+
}

‎src/tileanimation.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2016 sfan5 <sfan5@live.de>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#ifndef TILEANIMATION_HEADER
21+
#define TILEANIMATION_HEADER
22+
23+
#include "irrlichttypes_bloated.h"
24+
#include <iostream>
25+
26+
enum TileAnimationType {
27+
TAT_NONE = 0,
28+
TAT_VERTICAL_FRAMES = 1,
29+
};
30+
31+
struct TileAnimationParams {
32+
enum TileAnimationType type;
33+
union {
34+
// struct {
35+
// } none;
36+
struct {
37+
int aspect_w; // width for aspect ratio
38+
int aspect_h; // height for aspect ratio
39+
float length; // seconds
40+
} vertical_frames;
41+
};
42+
43+
void serialize(std::ostream &os, u16 protocol_version) const;
44+
void deSerialize(std::istream &is, u16 protocol_version);
45+
void determineParams(v2u32 texture_size, int *frame_count, int *frame_length_ms) const;
46+
void getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const;
47+
};
48+
49+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.