tileanimation.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, u8 tiledef_version) const
  19. {
  20. writeU8(os, type);
  21. if (type == TAT_VERTICAL_FRAMES) {
  22. writeU16(os, vertical_frames.aspect_w);
  23. writeU16(os, vertical_frames.aspect_h);
  24. writeF1000(os, vertical_frames.length);
  25. } else if (type == TAT_SHEET_2D) {
  26. writeU8(os, sheet_2d.frames_w);
  27. writeU8(os, sheet_2d.frames_h);
  28. writeF1000(os, sheet_2d.frame_length);
  29. }
  30. }
  31. void TileAnimationParams::deSerialize(std::istream &is, u8 tiledef_version)
  32. {
  33. type = (TileAnimationType) readU8(is);
  34. if (type == TAT_VERTICAL_FRAMES) {
  35. vertical_frames.aspect_w = readU16(is);
  36. vertical_frames.aspect_h = readU16(is);
  37. vertical_frames.length = readF1000(is);
  38. } else if (type == TAT_SHEET_2D) {
  39. sheet_2d.frames_w = readU8(is);
  40. sheet_2d.frames_h = readU8(is);
  41. sheet_2d.frame_length = readF1000(is);
  42. }
  43. }
  44. void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count,
  45. int *frame_length_ms, v2u32 *frame_size) const
  46. {
  47. if (type == TAT_VERTICAL_FRAMES) {
  48. int frame_height = (float)texture_size.X /
  49. (float)vertical_frames.aspect_w *
  50. (float)vertical_frames.aspect_h;
  51. int _frame_count = texture_size.Y / frame_height;
  52. if (frame_count)
  53. *frame_count = _frame_count;
  54. if (frame_length_ms)
  55. *frame_length_ms = 1000.0 * vertical_frames.length / _frame_count;
  56. if (frame_size)
  57. *frame_size = v2u32(texture_size.X, frame_height);
  58. } else if (type == TAT_SHEET_2D) {
  59. if (frame_count)
  60. *frame_count = sheet_2d.frames_w * sheet_2d.frames_h;
  61. if (frame_length_ms)
  62. *frame_length_ms = 1000 * sheet_2d.frame_length;
  63. if (frame_size)
  64. *frame_size = v2u32(texture_size.X / sheet_2d.frames_w, texture_size.Y / sheet_2d.frames_h);
  65. }
  66. // caller should check for TAT_NONE
  67. }
  68. void TileAnimationParams::getTextureModifer(std::ostream &os, v2u32 texture_size, int frame) const
  69. {
  70. if (type == TAT_NONE)
  71. return;
  72. if (type == TAT_VERTICAL_FRAMES) {
  73. int frame_count;
  74. determineParams(texture_size, &frame_count, NULL, NULL);
  75. os << "^[verticalframe:" << frame_count << ":" << frame;
  76. } else if (type == TAT_SHEET_2D) {
  77. int q, r;
  78. q = frame / sheet_2d.frames_w;
  79. r = frame % sheet_2d.frames_w;
  80. os << "^[sheet:" << sheet_2d.frames_w << "x" << sheet_2d.frames_h
  81. << ":" << r << "," << q;
  82. }
  83. }
  84. v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const
  85. {
  86. v2u32 ret(0, 0);
  87. if (type == TAT_VERTICAL_FRAMES) {
  88. int frame_height = (float)texture_size.X /
  89. (float)vertical_frames.aspect_w *
  90. (float)vertical_frames.aspect_h;
  91. ret = v2u32(0, frame_height * frame);
  92. } else if (type == TAT_SHEET_2D) {
  93. v2u32 frame_size;
  94. determineParams(texture_size, NULL, NULL, &frame_size);
  95. int q, r;
  96. q = frame / sheet_2d.frames_w;
  97. r = frame % sheet_2d.frames_w;
  98. ret = v2u32(r * frame_size.X, q * frame_size.Y);
  99. }
  100. return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y);
  101. }