incrementaltp: respect physics overrides
[waspsaliva.git] / src / tileanimation.cpp
blob930fd94731013089bda7a8dde8c243f4c92eba77
1 /*
2 Minetest
3 Copyright (C) 2016 sfan5 <sfan5@live.de>
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.
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.
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.
19 #include "tileanimation.h"
20 #include "util/serialize.h"
22 void TileAnimationParams::serialize(std::ostream &os, u8 tiledef_version) const
24 writeU8(os, type);
25 if (type == TAT_VERTICAL_FRAMES) {
26 writeU16(os, vertical_frames.aspect_w);
27 writeU16(os, vertical_frames.aspect_h);
28 writeF32(os, vertical_frames.length);
29 } else if (type == TAT_SHEET_2D) {
30 writeU8(os, sheet_2d.frames_w);
31 writeU8(os, sheet_2d.frames_h);
32 writeF32(os, sheet_2d.frame_length);
36 void TileAnimationParams::deSerialize(std::istream &is, u8 tiledef_version)
38 type = (TileAnimationType) readU8(is);
40 if (type == TAT_VERTICAL_FRAMES) {
41 vertical_frames.aspect_w = readU16(is);
42 vertical_frames.aspect_h = readU16(is);
43 vertical_frames.length = readF32(is);
44 } else if (type == TAT_SHEET_2D) {
45 sheet_2d.frames_w = readU8(is);
46 sheet_2d.frames_h = readU8(is);
47 sheet_2d.frame_length = readF32(is);
51 void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count,
52 int *frame_length_ms, v2u32 *frame_size) const
54 if (type == TAT_VERTICAL_FRAMES) {
55 int frame_height = (float)texture_size.X /
56 (float)vertical_frames.aspect_w *
57 (float)vertical_frames.aspect_h;
58 int _frame_count = texture_size.Y / frame_height;
59 if (frame_count)
60 *frame_count = _frame_count;
61 if (frame_length_ms)
62 *frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
63 if (frame_size)
64 *frame_size = v2u32(texture_size.X, frame_height);
65 } else if (type == TAT_SHEET_2D) {
66 if (frame_count)
67 *frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
68 if (frame_length_ms)
69 *frame_length_ms = 1000 * sheet_2d.frame_length;
70 if (frame_size)
71 *frame_size = v2u32(texture_size.X / sheet_2d.frames_w, texture_size.Y / sheet_2d.frames_h);
73 // caller should check for TAT_NONE
76 void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
78 if (type == TAT_NONE)
79 return;
80 if (type == TAT_VERTICAL_FRAMES) {
81 int frame_count;
82 determineParams(texture_size, &frame_count, NULL, NULL);
83 os << "^[verticalframe:" << frame_count << ":" << frame;
84 } else if (type == TAT_SHEET_2D) {
85 int q, r;
86 q = frame / sheet_2d.frames_w;
87 r = frame % sheet_2d.frames_w;
88 os << "^[sheet:" << sheet_2d.frames_w << "x" << sheet_2d.frames_h
89 << ":" << r << "," << q;
93 v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const
95 v2u32 ret(0, 0);
96 if (type == TAT_VERTICAL_FRAMES) {
97 int frame_height = (float)texture_size.X /
98 (float)vertical_frames.aspect_w *
99 (float)vertical_frames.aspect_h;
100 ret = v2u32(0, frame_height * frame);
101 } else if (type == TAT_SHEET_2D) {
102 v2u32 frame_size;
103 determineParams(texture_size, NULL, NULL, &frame_size);
104 int q, r;
105 q = frame / sheet_2d.frames_w;
106 r = frame % sheet_2d.frames_w;
107 ret = v2u32(r * frame_size.X, q * frame_size.Y);
109 return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y);