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, u16 protocol_ver) const
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;
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);
42 void TileAnimationParams::deSerialize(std::istream &is, u16 protocol_ver)
44 type = (TileAnimationType) readU8(is);
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);
57 void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count,
58 int *frame_length_ms, v2u32 *frame_size) const
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;
66 *frame_count = _frame_count;
68 *frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
70 *frame_size = v2u32(texture_size.X, frame_height);
71 } else if (type == TAT_SHEET_2D) {
73 *frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
75 *frame_length_ms = 1000 * sheet_2d.frame_length;
77 *frame_size = v2u32(texture_size.X / sheet_2d.frames_w, texture_size.Y / sheet_2d.frames_h);
79 // caller should check for TAT_NONE
82 void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
86 if (type == TAT_VERTICAL_FRAMES) {
88 determineParams(texture_size, &frame_count, NULL, NULL);
89 os << "^[verticalframe:" << frame_count << ":" << frame;
90 } else if (type == TAT_SHEET_2D) {
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;
99 v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const
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) {
109 determineParams(texture_size, NULL, NULL, &frame_size);
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);
115 return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y);