You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

300 line
6.6 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdlib>
  14. #include <cstdio>
  15. #include <cstring>
  16. #if defined WIN32 && !defined _XBOX
  17. # define WIN32_LEAN_AND_MEAN
  18. # include <windows.h>
  19. # if defined USE_D3D9
  20. # define FAR
  21. # define NEAR
  22. # include <d3d9.h>
  23. # endif
  24. #endif
  25. #include "core.h"
  26. #include "lolgl.h"
  27. using namespace std;
  28. #if defined USE_D3D9
  29. extern IDirect3DDevice9 *g_d3ddevice;
  30. #elif defined _XBOX
  31. extern D3DDevice *g_d3ddevice;
  32. #endif
  33. namespace lol
  34. {
  35. /*
  36. * TileSet implementation class
  37. */
  38. class TileSetData
  39. {
  40. friend class TileSet;
  41. private:
  42. String m_name;
  43. /* Pixels, then texture coordinates */
  44. Array<ibox2, box2> m_tiles;
  45. ivec2 m_tile_size, m_image_size, m_texture_size;
  46. Image *m_image;
  47. Texture *m_texture;
  48. };
  49. /*
  50. * Public TileSet class
  51. */
  52. TileSet::TileSet(char const *path)
  53. : m_data(new TileSetData())
  54. {
  55. Init(path);
  56. }
  57. TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
  58. : m_data(new TileSetData())
  59. {
  60. Init(path);
  61. /* If count is valid, fix size; otherwise, fix count. */
  62. if (count.x > 0 && count.y > 0)
  63. {
  64. size = m_data->m_image_size / count;
  65. }
  66. else
  67. {
  68. if (size.x <= 0 || size.y <= 0)
  69. size = ivec2(32, 32);
  70. count = max(ivec2(1, 1), m_data->m_image_size / size);
  71. }
  72. for (int j = 0; j < count.y; ++j)
  73. for (int i = 0; i < count.x; ++i)
  74. {
  75. AddTile(ibox2(size * ivec2(i, j),
  76. size * ivec2(i + 1, j + 1)));
  77. }
  78. }
  79. void TileSet::Init(char const *path)
  80. {
  81. m_data->m_name = String("<tileset> ") + path;
  82. m_data->m_texture = 0;
  83. m_data->m_image = Image::Create(path);
  84. m_data->m_image_size = m_data->m_image->GetSize();
  85. m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x),
  86. PotUp(m_data->m_image_size.y));
  87. m_drawgroup = DRAWGROUP_BEFORE;
  88. }
  89. int TileSet::AddTile(ibox2 rect)
  90. {
  91. m_data->m_tiles.Push(rect,
  92. box2((vec2)rect.A / (vec2)m_data->m_texture_size,
  93. (vec2)rect.B / (vec2)m_data->m_texture_size));
  94. return m_data->m_tiles.Count() - 1;
  95. }
  96. TileSet::~TileSet()
  97. {
  98. delete m_data;
  99. }
  100. void TileSet::TickDraw(float seconds)
  101. {
  102. Entity::TickDraw(seconds);
  103. if (IsDestroying())
  104. {
  105. if (m_data->m_image)
  106. {
  107. Image::Destroy(m_data->m_image);
  108. m_data->m_image = nullptr;
  109. }
  110. else
  111. {
  112. delete m_data->m_texture;
  113. m_data->m_texture = nullptr;
  114. }
  115. }
  116. else if (m_data->m_image)
  117. {
  118. int planes;
  119. PixelFormat format = m_data->m_image->GetFormat();
  120. switch (format)
  121. {
  122. case PixelFormat::RGB_8:
  123. planes = 3;
  124. break;
  125. case PixelFormat::RGBA_8:
  126. case PixelFormat::ARGB_8:
  127. case PixelFormat::ABGR_8:
  128. default:
  129. planes = 4;
  130. break;
  131. }
  132. int w = m_data->m_texture_size.x;
  133. int h = m_data->m_texture_size.y;
  134. uint8_t *pixels = m_data->m_image->GetData();
  135. if (w != m_data->m_image_size.x || h != m_data->m_image_size.y)
  136. {
  137. uint8_t *tmp = new uint8_t[planes * w * h];
  138. for (int line = 0; line < m_data->m_image_size.y; line++)
  139. memcpy(tmp + planes * w * line,
  140. pixels + planes * m_data->m_image_size.x * line,
  141. planes * m_data->m_image_size.x);
  142. pixels = tmp;
  143. }
  144. m_data->m_texture = new Texture(ivec2(w, h), format);
  145. m_data->m_texture->SetData(pixels);
  146. if (pixels != m_data->m_image->GetData())
  147. delete[] pixels;
  148. Image::Destroy(m_data->m_image);
  149. m_data->m_image = nullptr;
  150. }
  151. }
  152. char const *TileSet::GetName()
  153. {
  154. return m_data->m_name.C();
  155. }
  156. int TileSet::GetTileCount() const
  157. {
  158. return m_data->m_tiles.Count();
  159. }
  160. ivec2 TileSet::GetTileSize(int tileid) const
  161. {
  162. ibox2 const &box = m_data->m_tiles[tileid].m1;
  163. return box.B - box.A;
  164. }
  165. ivec2 TileSet::GetTextureSize() const
  166. {
  167. return m_data->m_texture_size;
  168. }
  169. ShaderTexture TileSet::GetTexture() const
  170. {
  171. return m_data->m_texture->GetTexture();
  172. }
  173. void TileSet::Bind()
  174. {
  175. if (!m_data->m_image && m_data->m_texture)
  176. m_data->m_texture->Bind();
  177. }
  178. void TileSet::Unbind()
  179. {
  180. ;
  181. }
  182. void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale,
  183. float *vertex, float *texture)
  184. {
  185. ibox2 pixels = m_data->m_tiles[id].m1;
  186. box2 texels = m_data->m_tiles[id].m2;
  187. float dtx = texels.B.x - texels.A.x;
  188. float dty = texels.B.y - texels.A.y;
  189. float tx = texels.A.x;
  190. float ty = texels.A.y;
  191. int dx = (pixels.B.x - pixels.A.x) * scale.x;
  192. int dy = o ? 0 : (pixels.B.y - pixels.A.y) * scale.y;
  193. int dz = o ? (pixels.B.y - pixels.A.y) * scale.y : 0;
  194. /* If scaling is negative, switch triangle winding */
  195. if (scale.x * scale.y < 0.0f)
  196. {
  197. pos.x += dx;
  198. dx = -dx;
  199. tx += dtx;
  200. dtx = -dtx;
  201. }
  202. #if 1
  203. /* HACK: tweak UV values */
  204. tx += (1.f / 128.f) * dtx;
  205. ty += (1.f / 128.f) * dty;
  206. dtx *= 126.f / 128.f;
  207. dty *= 126.f / 128.f;
  208. #endif
  209. if (!m_data->m_image && m_data->m_texture)
  210. {
  211. float tmp[10];
  212. *vertex++ = pos.x + dx;
  213. *vertex++ = pos.y + dy;
  214. *vertex++ = pos.z + dz;
  215. *texture++ = tx + dtx;
  216. *texture++ = ty;
  217. *vertex++ = tmp[0] = pos.x;
  218. *vertex++ = tmp[1] = pos.y + dy;
  219. *vertex++ = tmp[2] = pos.z + dz;
  220. *texture++ = tmp[3] = tx;
  221. *texture++ = tmp[4] = ty;
  222. *vertex++ = tmp[5] = pos.x + dx;
  223. *vertex++ = tmp[6] = pos.y;
  224. *vertex++ = tmp[7] = pos.z;
  225. *texture++ = tmp[8] = tx + dtx;
  226. *texture++ = tmp[9] = ty + dty;
  227. *vertex++ = tmp[5];
  228. *vertex++ = tmp[6];
  229. *vertex++ = tmp[7];
  230. *texture++ = tmp[8];
  231. *texture++ = tmp[9];
  232. *vertex++ = tmp[0];
  233. *vertex++ = tmp[1];
  234. *vertex++ = tmp[2];
  235. *texture++ = tmp[3];
  236. *texture++ = tmp[4];
  237. *vertex++ = pos.x;
  238. *vertex++ = pos.y;
  239. *vertex++ = pos.z;
  240. *texture++ = tx;
  241. *texture++ = ty + dty;
  242. }
  243. else
  244. {
  245. memset(vertex, 0, 3 * sizeof(float));
  246. memset(texture, 0, 2 * sizeof(float));
  247. }
  248. }
  249. } /* namespace lol */