2016-12-23 13:48:32 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2016 sfan5 <sfan5@live.de>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#include "tileanimation.h"
|
|
|
|
#include "util/serialize.h"
|
|
|
|
|
2022-09-04 16:22:12 +02:00
|
|
|
void TileAnimationParams::serialize(std::ostream &os, u16 protocol_ver) const
|
2016-12-23 13:48:32 +01:00
|
|
|
{
|
2022-09-04 16:22:12 +02:00
|
|
|
// The particle code overloads the length parameter so that negative numbers
|
|
|
|
// indicate an extra feature which old clients don't understand (crash).
|
|
|
|
// In hindsight it would have been better to use an extra parameter for this
|
|
|
|
// but we're stuck with this now.
|
|
|
|
const bool need_abs = protocol_ver < 40;
|
|
|
|
|
2016-12-23 13:48:32 +01:00
|
|
|
writeU8(os, type);
|
|
|
|
if (type == TAT_VERTICAL_FRAMES) {
|
|
|
|
writeU16(os, vertical_frames.aspect_w);
|
|
|
|
writeU16(os, vertical_frames.aspect_h);
|
2022-09-04 16:22:12 +02:00
|
|
|
writeF32(os, need_abs ? fabs(vertical_frames.length) : vertical_frames.length);
|
2016-12-23 14:43:56 +01:00
|
|
|
} else if (type == TAT_SHEET_2D) {
|
|
|
|
writeU8(os, sheet_2d.frames_w);
|
|
|
|
writeU8(os, sheet_2d.frames_h);
|
2022-09-04 16:22:12 +02:00
|
|
|
writeF32(os, need_abs ? fabs(sheet_2d.frame_length) : sheet_2d.frame_length);
|
2016-12-23 13:48:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 16:22:12 +02:00
|
|
|
void TileAnimationParams::deSerialize(std::istream &is, u16 protocol_ver)
|
2016-12-23 13:48:32 +01:00
|
|
|
{
|
|
|
|
type = (TileAnimationType) readU8(is);
|
|
|
|
|
2016-12-23 14:43:56 +01:00
|
|
|
if (type == TAT_VERTICAL_FRAMES) {
|
2016-12-23 13:48:32 +01:00
|
|
|
vertical_frames.aspect_w = readU16(is);
|
|
|
|
vertical_frames.aspect_h = readU16(is);
|
2019-01-03 17:04:26 +01:00
|
|
|
vertical_frames.length = readF32(is);
|
2016-12-23 14:43:56 +01:00
|
|
|
} else if (type == TAT_SHEET_2D) {
|
|
|
|
sheet_2d.frames_w = readU8(is);
|
|
|
|
sheet_2d.frames_h = readU8(is);
|
2019-01-03 17:04:26 +01:00
|
|
|
sheet_2d.frame_length = readF32(is);
|
2016-12-23 13:48:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 16:48:49 +01:00
|
|
|
void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count,
|
|
|
|
int *frame_length_ms, v2u32 *frame_size) const
|
2016-12-23 13:48:32 +01:00
|
|
|
{
|
2016-12-23 14:43:56 +01:00
|
|
|
if (type == TAT_VERTICAL_FRAMES) {
|
|
|
|
int frame_height = (float)texture_size.X /
|
|
|
|
(float)vertical_frames.aspect_w *
|
|
|
|
(float)vertical_frames.aspect_h;
|
|
|
|
int _frame_count = texture_size.Y / frame_height;
|
|
|
|
if (frame_count)
|
|
|
|
*frame_count = _frame_count;
|
|
|
|
if (frame_length_ms)
|
|
|
|
*frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
|
2017-01-14 16:48:49 +01:00
|
|
|
if (frame_size)
|
|
|
|
*frame_size = v2u32(texture_size.X, frame_height);
|
2016-12-23 14:43:56 +01:00
|
|
|
} else if (type == TAT_SHEET_2D) {
|
|
|
|
if (frame_count)
|
|
|
|
*frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
|
|
|
|
if (frame_length_ms)
|
|
|
|
*frame_length_ms = 1000 * sheet_2d.frame_length;
|
2017-01-14 16:48:49 +01:00
|
|
|
if (frame_size)
|
|
|
|
*frame_size = v2u32(texture_size.X / sheet_2d.frames_w, texture_size.Y / sheet_2d.frames_h);
|
2016-12-23 13:48:32 +01:00
|
|
|
}
|
2017-01-14 16:48:49 +01:00
|
|
|
// caller should check for TAT_NONE
|
2016-12-23 13:48:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
|
|
|
|
{
|
|
|
|
if (type == TAT_NONE)
|
|
|
|
return;
|
2016-12-23 14:43:56 +01:00
|
|
|
if (type == TAT_VERTICAL_FRAMES) {
|
|
|
|
int frame_count;
|
2017-01-14 16:48:49 +01:00
|
|
|
determineParams(texture_size, &frame_count, NULL, NULL);
|
2016-12-23 14:43:56 +01:00
|
|
|
os << "^[verticalframe:" << frame_count << ":" << frame;
|
|
|
|
} else if (type == TAT_SHEET_2D) {
|
|
|
|
int q, r;
|
|
|
|
q = frame / sheet_2d.frames_w;
|
|
|
|
r = frame % sheet_2d.frames_w;
|
|
|
|
os << "^[sheet:" << sheet_2d.frames_w << "x" << sheet_2d.frames_h
|
|
|
|
<< ":" << r << "," << q;
|
|
|
|
}
|
2016-12-23 13:48:32 +01:00
|
|
|
}
|
2017-01-14 16:48:49 +01:00
|
|
|
|
|
|
|
v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const
|
|
|
|
{
|
|
|
|
v2u32 ret(0, 0);
|
|
|
|
if (type == TAT_VERTICAL_FRAMES) {
|
|
|
|
int frame_height = (float)texture_size.X /
|
|
|
|
(float)vertical_frames.aspect_w *
|
|
|
|
(float)vertical_frames.aspect_h;
|
|
|
|
ret = v2u32(0, frame_height * frame);
|
|
|
|
} else if (type == TAT_SHEET_2D) {
|
|
|
|
v2u32 frame_size;
|
|
|
|
determineParams(texture_size, NULL, NULL, &frame_size);
|
|
|
|
int q, r;
|
|
|
|
q = frame / sheet_2d.frames_w;
|
|
|
|
r = frame % sheet_2d.frames_w;
|
|
|
|
ret = v2u32(r * frame_size.X, q * frame_size.Y);
|
|
|
|
}
|
|
|
|
return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y);
|
|
|
|
}
|