]> git.lizzy.rs Git - minetest.git/blob - src/tileanimation.cpp
Remove old rollback migration code (#13082)
[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_ver) const
23 {
24         // The particle code overloads the length parameter so that negative numbers
25         // indicate an extra feature which old clients don't understand (crash).
26         // In hindsight it would have been better to use an extra parameter for this
27         // but we're stuck with this now.
28         const bool need_abs = protocol_ver < 40;
29
30         writeU8(os, type);
31         if (type == TAT_VERTICAL_FRAMES) {
32                 writeU16(os, vertical_frames.aspect_w);
33                 writeU16(os, vertical_frames.aspect_h);
34                 writeF32(os, need_abs ? fabs(vertical_frames.length) : vertical_frames.length);
35         } else if (type == TAT_SHEET_2D) {
36                 writeU8(os, sheet_2d.frames_w);
37                 writeU8(os, sheet_2d.frames_h);
38                 writeF32(os, need_abs ? fabs(sheet_2d.frame_length) : sheet_2d.frame_length);
39         }
40 }
41
42 void TileAnimationParams::deSerialize(std::istream &is, u16 protocol_ver)
43 {
44         type = (TileAnimationType) readU8(is);
45
46         if (type == TAT_VERTICAL_FRAMES) {
47                 vertical_frames.aspect_w = readU16(is);
48                 vertical_frames.aspect_h = readU16(is);
49                 vertical_frames.length = readF32(is);
50         } else if (type == TAT_SHEET_2D) {
51                 sheet_2d.frames_w = readU8(is);
52                 sheet_2d.frames_h = readU8(is);
53                 sheet_2d.frame_length = readF32(is);
54         }
55 }
56
57 void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count,
58                 int *frame_length_ms, v2u32 *frame_size) const
59 {
60         if (type == TAT_VERTICAL_FRAMES) {
61                 int frame_height = (float)texture_size.X /
62                                 (float)vertical_frames.aspect_w *
63                                 (float)vertical_frames.aspect_h;
64                 int _frame_count = texture_size.Y / frame_height;
65                 if (frame_count)
66                         *frame_count = _frame_count;
67                 if (frame_length_ms)
68                         *frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
69                 if (frame_size)
70                         *frame_size = v2u32(texture_size.X, frame_height);
71         } else if (type == TAT_SHEET_2D) {
72                 if (frame_count)
73                         *frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
74                 if (frame_length_ms)
75                         *frame_length_ms = 1000 * sheet_2d.frame_length;
76                 if (frame_size)
77                         *frame_size = v2u32(texture_size.X / sheet_2d.frames_w, texture_size.Y / sheet_2d.frames_h);
78         }
79         // caller should check for TAT_NONE
80 }
81
82 void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
83 {
84         if (type == TAT_NONE)
85                 return;
86         if (type == TAT_VERTICAL_FRAMES) {
87                 int frame_count;
88                 determineParams(texture_size, &frame_count, NULL, NULL);
89                 os << "^[verticalframe:" << frame_count << ":" << frame;
90         } else if (type == TAT_SHEET_2D) {
91                 int q, r;
92                 q = frame / sheet_2d.frames_w;
93                 r = frame % sheet_2d.frames_w;
94                 os << "^[sheet:" << sheet_2d.frames_w << "x" << sheet_2d.frames_h
95                         << ":" << r << "," << q;
96         }
97 }
98
99 v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const
100 {
101         v2u32 ret(0, 0);
102         if (type == TAT_VERTICAL_FRAMES) {
103                 int frame_height = (float)texture_size.X /
104                                 (float)vertical_frames.aspect_w *
105                                 (float)vertical_frames.aspect_h;
106                 ret = v2u32(0, frame_height * frame);
107         } else if (type == TAT_SHEET_2D) {
108                 v2u32 frame_size;
109                 determineParams(texture_size, NULL, NULL, &frame_size);
110                 int q, r;
111                 q = frame / sheet_2d.frames_w;
112                 r = frame % sheet_2d.frames_w;
113                 ret = v2u32(r * frame_size.X, q * frame_size.Y);
114         }
115         return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y);
116 }