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.
 
 
 

269 lines
5.7 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. char *name, *path;
  43. int *tiles, ntiles;
  44. ivec2 size, isize, count;
  45. vec2 scale;
  46. float tx, ty;
  47. Image *img;
  48. Texture *m_texture;
  49. };
  50. /*
  51. * Public TileSet class
  52. */
  53. TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
  54. : data(new TileSetData())
  55. {
  56. data->name = (char *)malloc(10 + strlen(path) + 1);
  57. data->path = data->name + 10;
  58. sprintf(data->name, "<tileset> %s", path);
  59. data->tiles = NULL;
  60. data->m_texture = 0;
  61. data->img = new Image(path);
  62. data->isize = data->img->GetSize();
  63. if (count.x > 0 && count.y > 0)
  64. {
  65. data->count = count;
  66. data->size = data->isize / count;
  67. }
  68. else
  69. {
  70. if (size.x <= 0 || size.y <= 0)
  71. size = ivec2(32, 32);
  72. data->count.x = data->isize.x > size.x ? data->isize.x / size.x : 1;
  73. data->count.y = data->isize.y > size.y ? data->isize.y / size.y : 1;
  74. data->size = size;
  75. }
  76. data->tx = (float)data->size.x / PotUp(data->isize.x);
  77. data->ty = (float)data->size.y / PotUp(data->isize.y);
  78. data->ntiles = data->count.x * data->count.y;
  79. m_drawgroup = DRAWGROUP_BEFORE;
  80. }
  81. TileSet::~TileSet()
  82. {
  83. free(data->tiles);
  84. free(data->name);
  85. delete data;
  86. }
  87. void TileSet::TickDraw(float seconds)
  88. {
  89. Entity::TickDraw(seconds);
  90. if (IsDestroying())
  91. {
  92. if (data->img)
  93. delete data->img;
  94. else
  95. delete data->m_texture;
  96. }
  97. else if (data->img)
  98. {
  99. int planes;
  100. PixelFormat format = PixelFormat::Unknown;
  101. switch (data->img->GetFormat())
  102. {
  103. case Image::FORMAT_RGB:
  104. format = PixelFormat::RGB_8;
  105. planes = 3;
  106. break;
  107. case Image::FORMAT_RGBA:
  108. default:
  109. format = PixelFormat::ARGB_8;
  110. planes = 4;
  111. break;
  112. }
  113. int w = PotUp(data->isize.x);
  114. int h = PotUp(data->isize.y);
  115. uint8_t *pixels = (uint8_t *)data->img->GetData();
  116. if (w != data->isize.x || h != data->isize.y)
  117. {
  118. uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
  119. for (int line = 0; line < data->isize.y; line++)
  120. memcpy(tmp + planes * w * line,
  121. pixels + planes * data->isize.x * line,
  122. planes * data->isize.x);
  123. pixels = tmp;
  124. }
  125. data->m_texture = new Texture(ivec2(w, h), format);
  126. data->m_texture->SetData(pixels);
  127. if (pixels != data->img->GetData())
  128. free(pixels);
  129. delete data->img;
  130. data->img = NULL;
  131. }
  132. }
  133. char const *TileSet::GetName()
  134. {
  135. return data->name;
  136. }
  137. ivec2 TileSet::GetCount() const
  138. {
  139. return data->count;
  140. }
  141. ivec2 TileSet::GetSize(int tileid) const
  142. {
  143. (void)tileid;
  144. return data->size;
  145. }
  146. vec2 TileSet::GetTileSize() const
  147. {
  148. return vec2(data->tx, data->ty);
  149. }
  150. ShaderTexture TileSet::GetTexture() const
  151. {
  152. return data->m_texture->GetTexture();
  153. }
  154. void TileSet::Bind()
  155. {
  156. if (!data->img && data->m_texture)
  157. data->m_texture->Bind();
  158. }
  159. void TileSet::Unbind()
  160. {
  161. ;
  162. }
  163. void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale,
  164. float *vertex, float *texture)
  165. {
  166. float dtx = data->tx;
  167. float dty = data->ty;
  168. float tx = dtx * ((id & 0xffff) % data->count.x);
  169. float ty = dty * ((id & 0xffff) / data->count.x);
  170. int dx = data->size.x * scale.x;
  171. int dy = o ? 0 : data->size.y * scale.y;
  172. int dz = o ? data->size.y * scale.y : 0;
  173. /* If scaling is negative, switch triangle winding */
  174. if (scale.x * scale.y < 0.0f)
  175. {
  176. pos.x += dx;
  177. dx = -dx;
  178. tx += dtx;
  179. dtx = -dtx;
  180. }
  181. if (!data->img && data->m_texture)
  182. {
  183. float tmp[10];
  184. *vertex++ = pos.x + dx;
  185. *vertex++ = pos.y + dy;
  186. *vertex++ = pos.z + dz;
  187. *texture++ = tx + dtx;
  188. *texture++ = ty;
  189. *vertex++ = tmp[0] = pos.x;
  190. *vertex++ = tmp[1] = pos.y + dy;
  191. *vertex++ = tmp[2] = pos.z + dz;
  192. *texture++ = tmp[3] = tx;
  193. *texture++ = tmp[4] = ty;
  194. *vertex++ = tmp[5] = pos.x + dx;
  195. *vertex++ = tmp[6] = pos.y;
  196. *vertex++ = tmp[7] = pos.z;
  197. *texture++ = tmp[8] = tx + dtx;
  198. *texture++ = tmp[9] = ty + dty;
  199. *vertex++ = tmp[5];
  200. *vertex++ = tmp[6];
  201. *vertex++ = tmp[7];
  202. *texture++ = tmp[8];
  203. *texture++ = tmp[9];
  204. *vertex++ = tmp[0];
  205. *vertex++ = tmp[1];
  206. *vertex++ = tmp[2];
  207. *texture++ = tmp[3];
  208. *texture++ = tmp[4];
  209. *vertex++ = pos.x;
  210. *vertex++ = pos.y;
  211. *vertex++ = pos.z;
  212. *texture++ = tx;
  213. *texture++ = ty + dty;
  214. }
  215. else
  216. {
  217. memset(vertex, 0, 3 * sizeof(float));
  218. memset(texture, 0, 2 * sizeof(float));
  219. }
  220. }
  221. } /* namespace lol */