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_version) const
24 if (protocol_version < 29) {
25 if (type == TAT_VERTICAL_FRAMES) {
27 writeU16(os, vertical_frames.aspect_w);
28 writeU16(os, vertical_frames.aspect_h);
29 writeF1000(os, vertical_frames.length);
31 writeU8(os, TAT_NONE);
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);
51 void TileAnimationParams::deSerialize(std::istream &is, u16 protocol_version)
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);
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);
72 void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count,
73 int *frame_length_ms, v2u32 *frame_size) const
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;
81 *frame_count = _frame_count;
83 *frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
85 *frame_size = v2u32(texture_size.X, frame_height);
86 } else if (type == TAT_SHEET_2D) {
88 *frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
90 *frame_length_ms = 1000 * sheet_2d.frame_length;
92 *frame_size = v2u32(texture_size.X / sheet_2d.frames_w, texture_size.Y / sheet_2d.frames_h);
94 // caller should check for TAT_NONE
97 void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
101 if (type == TAT_VERTICAL_FRAMES) {
103 determineParams(texture_size, &frame_count, NULL, NULL);
104 os << "^[verticalframe:" << frame_count << ":" << frame;
105 } else if (type == TAT_SHEET_2D) {
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;
114 v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const
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) {
124 determineParams(texture_size, NULL, NULL, &frame_size);
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);
130 return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y);