tmp_ts_file.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "tmp_ts_file.h"
  17. #include "image_file.h"
  18. void Ctmp_ts_file::get_rect(int& x, int& y, int& cx, int& cy) const
  19. {
  20. x = INT_MAX;
  21. y = INT_MAX;
  22. cx = INT_MIN;
  23. cy = INT_MIN;
  24. int bigy = INT_MIN;
  25. int bigyval = 0;
  26. for (int i = 0; i < get_c_tiles(); i++)
  27. {
  28. if (get_index()[i])
  29. {
  30. int height = get_height() - get_height(i);
  31. int x_t = get_x(i);
  32. int y_t = get_y(i) + (height * (get_cy() / 2));
  33. int x2_t = x_t + get_cx();
  34. int y2_t = y_t + get_cy();
  35. if (has_extra_graphics(i))
  36. {
  37. int y_t_extra = get_y_extra(i) + (height * (get_cy() / 2));
  38. int y2_t_extra = y_t_extra + get_cy_extra(i);
  39. if (y_t_extra < y)
  40. y = y_t_extra;
  41. if (y2_t_extra > cy)
  42. cy = y2_t_extra;
  43. }
  44. if (x_t < x)
  45. x = x_t;
  46. if (x2_t > cx)
  47. cx = x2_t;
  48. if (y_t < y)
  49. y = y_t;
  50. if (y2_t > cy)
  51. cy = y2_t;
  52. if (bigy < get_y(i))
  53. {
  54. bigy = get_y(i);
  55. bigyval = get_y(i) + get_cy() + (get_height(i) * (get_cy() / 2));
  56. if (has_extra_graphics(i))
  57. bigyval -= get_y_extra(i);
  58. }
  59. }
  60. }
  61. cx -= x;
  62. cy -= y;
  63. if (cy < bigyval)
  64. cy = bigyval;
  65. }
  66. void Ctmp_ts_file::draw(byte* d) const
  67. {
  68. int global_x, global_y, global_cx, global_cy;
  69. get_rect(global_x, global_y, global_cx, global_cy);
  70. memset(d, 0, global_cx * global_cy);
  71. for (int i = 0; i < get_c_tiles(); i++)
  72. {
  73. if (get_index()[i])
  74. {
  75. int height = get_height() - get_height(i);
  76. const byte* r = get_image(i);
  77. byte* w_line = d + get_x(i) - global_x + global_cx * (get_y(i) - global_y + (height * (get_cy() / 2)));
  78. int x = get_cx() / 2;
  79. int cx = 0;
  80. int y = 0;
  81. for (; y < get_cy() / 2; y++)
  82. {
  83. cx += 4;
  84. x -= 2;
  85. memcpy(w_line + x, r, cx);
  86. r += cx;
  87. w_line += global_cx;
  88. }
  89. for (; y < get_cy(); y++)
  90. {
  91. cx -= 4;
  92. x += 2;
  93. memcpy(w_line + x, r, cx);
  94. r += cx;
  95. w_line += global_cx;
  96. }
  97. if (has_extra_graphics(i))
  98. {
  99. r += get_cx() * get_cy() / 2;
  100. w_line = d + get_x_extra(i) - global_x + global_cx * (get_y_extra(i) - global_y + (height * (get_cy() / 2)));
  101. int cx = get_cx_extra(i);
  102. int cy = get_cy_extra(i);
  103. for (y = 0; y < cy; y++)
  104. {
  105. byte* w = w_line;
  106. for (int i = 0; i < cx; i++)
  107. {
  108. int v = *r++;
  109. if (v)
  110. *w = v;
  111. w++;
  112. }
  113. w_line += global_cx;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. int decode_tile(const byte* s, byte* d, int cx_d)
  120. {
  121. int cy = cx_d >> 1;
  122. memset(d, 0, cx_d * cy);
  123. const byte* r = s;
  124. byte* w = d;
  125. int x = cx_d / 2;
  126. int cx = 0;
  127. int y = 0;
  128. for (; y < cy / 2; y++)
  129. {
  130. cx += 4;
  131. x -= 2;
  132. memcpy(w + x, r, cx);
  133. r += cx;
  134. w += cx_d;
  135. }
  136. for (; y < cy; y++)
  137. {
  138. cx -= 4;
  139. x += 2;
  140. memcpy(w + x, r, cx);
  141. r += cx;
  142. w += cx_d;
  143. }
  144. return w - d;
  145. }
  146. int encode_tile(const byte* s, byte* d, int cx_s)
  147. {
  148. int cy = cx_s >> 1;
  149. const byte* r = s;
  150. byte* w = d;
  151. int x = cx_s / 2;
  152. int cx = 0;
  153. int y = 0;
  154. for (; y < cy / 2; y++)
  155. {
  156. cx += 4;
  157. x -= 2;
  158. memcpy(w, r + x, cx);
  159. r += cx_s;
  160. w += cx;
  161. }
  162. for (; y < cy; y++)
  163. {
  164. cx -= 4;
  165. x += 2;
  166. memcpy(w, r + x, cx);
  167. r += cx_s;
  168. w += cx;
  169. }
  170. return w - d;
  171. }
  172. void Ctmp_ts_file::decode(void* d) const
  173. {
  174. draw(reinterpret_cast<byte*>(d));
  175. }