tileanimation.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Minetest
  3. Copyright (C) 2016 sfan5 <sfan5@live.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "tileanimation.h"
  17. #include "util/serialize.h"
  18. void TileAnimationParams::serialize(std::ostream &os, u16 protocol_ver) const
  19. {
  20. // The particle code overloads the length parameter so that negative numbers
  21. // indicate an extra feature which old clients don't understand (crash).
  22. // In hindsight it would have been better to use an extra parameter for this
  23. // but we're stuck with this now.
  24. const bool need_abs = protocol_ver < 40;
  25. writeU8(os, type);
  26. if (type == TAT_VERTICAL_FRAMES) {
  27. writeU16(os, vertical_frames.aspect_w);
  28. writeU16(os, vertical_frames.aspect_h);
  29. writeF32(os, need_abs ? fabs(vertical_frames.length) : vertical_frames.length);
  30. } else if (type == TAT_SHEET_2D) {
  31. writeU8(os, sheet_2d.frames_w);
  32. writeU8(os, sheet_2d.frames_h);
  33. writeF32(os, need_abs ? fabs(sheet_2d.frame_length) : sheet_2d.frame_length);
  34. }
  35. }
  36. void TileAnimationParams::deSerialize(std::istream &is, u16 protocol_ver)
  37. {
  38. type = (TileAnimationType) readU8(is);
  39. if (type == TAT_VERTICAL_FRAMES) {
  40. vertical_frames.aspect_w = readU16(is);
  41. vertical_frames.aspect_h = readU16(is);
  42. vertical_frames.length = readF32(is);
  43. } else if (type == TAT_SHEET_2D) {
  44. sheet_2d.frames_w = readU8(is);
  45. sheet_2d.frames_h = readU8(is);
  46. sheet_2d.frame_length = readF32(is);
  47. }
  48. }
  49. void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count,
  50. int *frame_length_ms, v2u32 *frame_size) const
  51. {
  52. if (type == TAT_VERTICAL_FRAMES) {
  53. int frame_height = (float)texture_size.X /
  54. (float)vertical_frames.aspect_w *
  55. (float)vertical_frames.aspect_h;
  56. int _frame_count = texture_size.Y / frame_height;
  57. if (frame_count)
  58. *frame_count = _frame_count;
  59. if (frame_length_ms)
  60. *frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
  61. if (frame_size)
  62. *frame_size = v2u32(texture_size.X, frame_height);
  63. } else if (type == TAT_SHEET_2D) {
  64. if (frame_count)
  65. *frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
  66. if (frame_length_ms)
  67. *frame_length_ms = 1000 * sheet_2d.frame_length;
  68. if (frame_size)
  69. *frame_size = v2u32(texture_size.X / sheet_2d.frames_w, texture_size.Y / sheet_2d.frames_h);
  70. }
  71. // caller should check for TAT_NONE
  72. }
  73. void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
  74. {
  75. if (type == TAT_NONE)
  76. return;
  77. if (type == TAT_VERTICAL_FRAMES) {
  78. int frame_count;
  79. determineParams(texture_size, &frame_count, NULL, NULL);
  80. os << "^[verticalframe:" << frame_count << ":" << frame;
  81. } else if (type == TAT_SHEET_2D) {
  82. int q, r;
  83. q = frame / sheet_2d.frames_w;
  84. r = frame % sheet_2d.frames_w;
  85. os << "^[sheet:" << sheet_2d.frames_w << "x" << sheet_2d.frames_h
  86. << ":" << r << "," << q;
  87. }
  88. }
  89. v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const
  90. {
  91. v2u32 ret(0, 0);
  92. if (type == TAT_VERTICAL_FRAMES) {
  93. int frame_height = (float)texture_size.X /
  94. (float)vertical_frames.aspect_w *
  95. (float)vertical_frames.aspect_h;
  96. ret = v2u32(0, frame_height * frame);
  97. } else if (type == TAT_SHEET_2D) {
  98. v2u32 frame_size;
  99. determineParams(texture_size, NULL, NULL, &frame_size);
  100. int q, r;
  101. q = frame / sheet_2d.frames_w;
  102. r = frame % sheet_2d.frames_w;
  103. ret = v2u32(r * frame_size.X, q * frame_size.Y);
  104. }
  105. return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y);
  106. }