Skip to content

Commit a07b032

Browse files
committedJan 2, 2017
Add 2D sheet animation for nodes
1 parent 7057c19 commit a07b032

File tree

8 files changed

+88
-27
lines changed

8 files changed

+88
-27
lines changed
 

Diff for: ‎doc/lua_api.txt

+20-1
Original file line numberDiff line numberDiff line change
@@ -3702,7 +3702,26 @@ Definition tables
37023702
* `image` (name)
37033703

37043704
### Tile animation definition
3705-
* `{type="vertical_frames", aspect_w=16, aspect_h=16, length=3.0}`
3705+
3706+
{
3707+
type = "vertical_frames",
3708+
aspect_w = 16,
3709+
-- ^ specify width of a frame in pixels
3710+
aspect_h = 16,
3711+
-- ^ specify height of a frame in pixels
3712+
length = 3.0,
3713+
-- ^ specify full loop length
3714+
}
3715+
3716+
{
3717+
type = "sheet_2d",
3718+
frames_w = 5,
3719+
-- ^ specify width in number of frames
3720+
frames_h = 3,
3721+
-- ^ specify height in number of frames
3722+
frame_length = 0.5,
3723+
-- ^ specify length of a single frame
3724+
}
37063725

37073726
### Node definition (`register_node`)
37083727

Diff for: ‎games/minimal/mods/default/init.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,11 @@ minetest.register_node("default:lava_source", {
10441044
inventory_image = minetest.inventorycube("default_lava.png"),
10451045
drawtype = "liquid",
10461046
--tiles ={"default_lava.png"},
1047-
tiles ={
1048-
{name="default_lava_source_animated.png", animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=3.0}}
1047+
tiles = {
1048+
{
1049+
name = "default_lava_source_animated.png",
1050+
animation = {type="sheet_2d", frames_w=3, frames_h=2, frame_length=0.5}
1051+
}
10491052
},
10501053
special_tiles = {
10511054
-- New-style lava source material (mostly unused)
Loading

Diff for: ‎src/nodedef.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
528528
tile->material_flags = 0;
529529
if (backface_culling)
530530
tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
531-
if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
531+
if (tiledef->animation.type != TAT_NONE)
532532
tile->material_flags |= MATERIAL_FLAG_ANIMATION;
533533
if (tiledef->tileable_horizontal)
534534
tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL;

Diff for: ‎src/particles.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ void ParticleManager::addNodeParticle(IGameDef* gamedef, scene::ISceneManager* s
570570
video::ITexture *texture;
571571

572572
// Only use first frame of animated texture
573-
if(tiles[texid].material_flags & MATERIAL_FLAG_ANIMATION)
573+
if (tiles[texid].material_flags & MATERIAL_FLAG_ANIMATION)
574574
texture = tiles[texid].frames[0].texture;
575575
else
576576
texture = tiles[texid].texture;

Diff for: ‎src/script/common/c_content.cpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct EnumString es_TileAnimationType[] =
3939
{
4040
{TAT_NONE, "none"},
4141
{TAT_VERTICAL_FRAMES, "vertical_frames"},
42+
{TAT_SHEET_2D, "sheet_2d"},
4243
{0, NULL},
4344
};
4445

@@ -334,16 +335,26 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
334335
// animation = {}
335336
lua_getfield(L, index, "animation");
336337
if(lua_istable(L, -1)){
337-
// {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}
338338
tiledef.animation.type = (TileAnimationType)
339339
getenumfield(L, -1, "type", es_TileAnimationType,
340340
TAT_NONE);
341-
tiledef.animation.vertical_frames.aspect_w =
342-
getintfield_default(L, -1, "aspect_w", 16);
343-
tiledef.animation.vertical_frames.aspect_h =
344-
getintfield_default(L, -1, "aspect_h", 16);
345-
tiledef.animation.vertical_frames.length =
346-
getfloatfield_default(L, -1, "length", 1.0);
341+
if (tiledef.animation.type == TAT_VERTICAL_FRAMES) {
342+
// {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}
343+
tiledef.animation.vertical_frames.aspect_w =
344+
getintfield_default(L, -1, "aspect_w", 16);
345+
tiledef.animation.vertical_frames.aspect_h =
346+
getintfield_default(L, -1, "aspect_h", 16);
347+
tiledef.animation.vertical_frames.length =
348+
getfloatfield_default(L, -1, "length", 1.0);
349+
} else if (tiledef.animation.type == TAT_SHEET_2D) {
350+
// {type="sheet_2d", frames_w=5, frames_h=3, frame_length=0.5}
351+
getintfield(L, -1, "frames_w",
352+
tiledef.animation.sheet_2d.frames_w);
353+
getintfield(L, -1, "frames_h",
354+
tiledef.animation.sheet_2d.frames_h);
355+
getfloatfield(L, -1, "frame_length",
356+
tiledef.animation.sheet_2d.frame_length);
357+
}
347358
}
348359
lua_pop(L, 1);
349360
}

Diff for: ‎src/tileanimation.cpp

+37-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121

2222
void TileAnimationParams::serialize(std::ostream &os, u16 protocol_version) const
2323
{
24-
if(protocol_version < 29 /* TODO bump */) {
24+
if (protocol_version < 29) {
2525
if (type == TAT_VERTICAL_FRAMES) {
2626
writeU8(os, type);
2727
writeU16(os, vertical_frames.aspect_w);
@@ -41,47 +41,69 @@ void TileAnimationParams::serialize(std::ostream &os, u16 protocol_version) cons
4141
writeU16(os, vertical_frames.aspect_w);
4242
writeU16(os, vertical_frames.aspect_h);
4343
writeF1000(os, vertical_frames.length);
44+
} else if (type == TAT_SHEET_2D) {
45+
writeU8(os, sheet_2d.frames_w);
46+
writeU8(os, sheet_2d.frames_h);
47+
writeF1000(os, sheet_2d.frame_length);
4448
}
4549
}
4650

4751
void TileAnimationParams::deSerialize(std::istream &is, u16 protocol_version)
4852
{
4953
type = (TileAnimationType) readU8(is);
50-
if(protocol_version < 29 /* TODO bump */) {
54+
if (protocol_version < 29) {
5155
vertical_frames.aspect_w = readU16(is);
5256
vertical_frames.aspect_h = readU16(is);
5357
vertical_frames.length = readF1000(is);
5458
return;
5559
}
5660

57-
if(type == TAT_VERTICAL_FRAMES) {
61+
if (type == TAT_VERTICAL_FRAMES) {
5862
vertical_frames.aspect_w = readU16(is);
5963
vertical_frames.aspect_h = readU16(is);
6064
vertical_frames.length = readF1000(is);
65+
} else if (type == TAT_SHEET_2D) {
66+
sheet_2d.frames_w = readU8(is);
67+
sheet_2d.frames_h = readU8(is);
68+
sheet_2d.frame_length = readF1000(is);
6169
}
6270
}
6371

6472
void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count, int *frame_length_ms) const
6573
{
66-
if (type == TAT_NONE) {
74+
if (type == TAT_VERTICAL_FRAMES) {
75+
int frame_height = (float)texture_size.X /
76+
(float)vertical_frames.aspect_w *
77+
(float)vertical_frames.aspect_h;
78+
int _frame_count = texture_size.Y / frame_height;
79+
if (frame_count)
80+
*frame_count = _frame_count;
81+
if (frame_length_ms)
82+
*frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
83+
} else if (type == TAT_SHEET_2D) {
84+
if (frame_count)
85+
*frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
86+
if (frame_length_ms)
87+
*frame_length_ms = 1000 * sheet_2d.frame_length;
88+
} else { // TAT_NONE
6789
*frame_count = 1;
6890
*frame_length_ms = 1000;
69-
return;
7091
}
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);
7892
}
7993

8094
void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
8195
{
8296
if (type == TAT_NONE)
8397
return;
84-
int frame_count;
85-
determineParams(texture_size, &frame_count, NULL);
86-
os << "^[verticalframe:" << frame_count << ":" << frame;
98+
if (type == TAT_VERTICAL_FRAMES) {
99+
int frame_count;
100+
determineParams(texture_size, &frame_count, NULL);
101+
os << "^[verticalframe:" << frame_count << ":" << frame;
102+
} else if (type == TAT_SHEET_2D) {
103+
int q, r;
104+
q = frame / sheet_2d.frames_w;
105+
r = frame % sheet_2d.frames_w;
106+
os << "^[sheet:" << sheet_2d.frames_w << "x" << sheet_2d.frames_h
107+
<< ":" << r << "," << q;
108+
}
87109
}

Diff for: ‎src/tileanimation.h

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
enum TileAnimationType {
2727
TAT_NONE = 0,
2828
TAT_VERTICAL_FRAMES = 1,
29+
TAT_SHEET_2D = 2,
2930
};
3031

3132
struct TileAnimationParams {
@@ -38,6 +39,11 @@ struct TileAnimationParams {
3839
int aspect_h; // height for aspect ratio
3940
float length; // seconds
4041
} vertical_frames;
42+
struct {
43+
int frames_w; // number of frames left-to-right
44+
int frames_h; // number of frames top-to-bottom
45+
float frame_length; // seconds
46+
} sheet_2d;
4147
};
4248

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

0 commit comments

Comments
 (0)
Please sign in to comment.