]> git.lizzy.rs Git - dragonfireclient.git/blob - src/tileanimation.cpp
Make nametag removable with set_nametag_attributes (#5021)
[dragonfireclient.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) {
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         } 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);
48         }
49 }
50
51 void TileAnimationParams::deSerialize(std::istream &is, u16 protocol_version)
52 {
53         type = (TileAnimationType) readU8(is);
54         if (protocol_version < 29) {
55                 vertical_frames.aspect_w = readU16(is);
56                 vertical_frames.aspect_h = readU16(is);
57                 vertical_frames.length = readF1000(is);
58                 return;
59         }
60
61         if (type == TAT_VERTICAL_FRAMES) {
62                 vertical_frames.aspect_w = readU16(is);
63                 vertical_frames.aspect_h = readU16(is);
64                 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);
69         }
70 }
71
72 void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count, int *frame_length_ms) const
73 {
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
89                 *frame_count = 1;
90                 *frame_length_ms = 1000;
91         }
92 }
93
94 void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
95 {
96         if (type == TAT_NONE)
97                 return;
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         }
109 }