]> git.lizzy.rs Git - minetest.git/blob - src/tileanimation.cpp
Snake case for screen options in minetest.conf (#5792)
[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) {
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,
73                 int *frame_length_ms, v2u32 *frame_size) const
74 {
75         if (type == TAT_VERTICAL_FRAMES) {
76                 int frame_height = (float)texture_size.X /
77                                 (float)vertical_frames.aspect_w *
78                                 (float)vertical_frames.aspect_h;
79                 int _frame_count = texture_size.Y / frame_height;
80                 if (frame_count)
81                         *frame_count = _frame_count;
82                 if (frame_length_ms)
83                         *frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
84                 if (frame_size)
85                         *frame_size = v2u32(texture_size.X, frame_height);
86         } else if (type == TAT_SHEET_2D) {
87                 if (frame_count)
88                         *frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
89                 if (frame_length_ms)
90                         *frame_length_ms = 1000 * sheet_2d.frame_length;
91                 if (frame_size)
92                         *frame_size = v2u32(texture_size.X / sheet_2d.frames_w, texture_size.Y / sheet_2d.frames_h);
93         }
94         // caller should check for TAT_NONE
95 }
96
97 void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
98 {
99         if (type == TAT_NONE)
100                 return;
101         if (type == TAT_VERTICAL_FRAMES) {
102                 int frame_count;
103                 determineParams(texture_size, &frame_count, NULL, NULL);
104                 os << "^[verticalframe:" << frame_count << ":" << frame;
105         } else if (type == TAT_SHEET_2D) {
106                 int q, r;
107                 q = frame / sheet_2d.frames_w;
108                 r = frame % sheet_2d.frames_w;
109                 os << "^[sheet:" << sheet_2d.frames_w << "x" << sheet_2d.frames_h
110                         << ":" << r << "," << q;
111         }
112 }
113
114 v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const
115 {
116         v2u32 ret(0, 0);
117         if (type == TAT_VERTICAL_FRAMES) {
118                 int frame_height = (float)texture_size.X /
119                                 (float)vertical_frames.aspect_w *
120                                 (float)vertical_frames.aspect_h;
121                 ret = v2u32(0, frame_height * frame);
122         } else if (type == TAT_SHEET_2D) {
123                 v2u32 frame_size;
124                 determineParams(texture_size, NULL, NULL, &frame_size);
125                 int q, r;
126                 q = frame / sheet_2d.frames_w;
127                 r = frame % sheet_2d.frames_w;
128                 ret = v2u32(r * frame_size.X, q * frame_size.Y);
129         }
130         return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y);
131 }