Move TileAnimation code to seperate file
[oweals/minetest.git] / src / tileanimation.cpp
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 }