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.
 
 
 

241 line
5.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdlib>
  14. #include <cstdio>
  15. #include <cmath>
  16. #include <cstring>
  17. #if defined WIN32 && !defined _XBOX
  18. # define WIN32_LEAN_AND_MEAN
  19. # include <windows.h>
  20. #endif
  21. #include "core.h"
  22. #include "lolgl.h"
  23. using namespace std;
  24. namespace lol
  25. {
  26. /*
  27. * TileSet implementation class
  28. */
  29. class TileSetData
  30. {
  31. friend class TileSet;
  32. private:
  33. char *name, *path;
  34. int *tiles, ntiles;
  35. ivec2 size, isize, count;
  36. float dilate, tx, ty;
  37. Image *img;
  38. GLuint texture;
  39. };
  40. /*
  41. * Public TileSet class
  42. */
  43. TileSet::TileSet(char const *path, ivec2 size, ivec2 count, float dilate)
  44. : data(new TileSetData())
  45. {
  46. data->name = (char *)malloc(10 + strlen(path) + 1);
  47. data->path = data->name + 10;
  48. sprintf(data->name, "<tileset> %s", path);
  49. data->tiles = NULL;
  50. data->texture = 0;
  51. data->img = new Image(path);
  52. data->isize = data->img->GetSize();
  53. if (count.i > 0 && count.j > 0)
  54. {
  55. data->count = count;
  56. data->size = data->isize / count;
  57. }
  58. else
  59. {
  60. if (size.x <= 0 || size.y <= 0)
  61. size = 32;
  62. data->count.i = data->isize.x > size.i ? data->isize.x / size.i : 1;
  63. data->count.j = data->isize.y > size.j ? data->isize.y / size.j : 1;
  64. data->size = size;
  65. }
  66. data->tx = (float)data->size.x / PotUp(data->isize.x);
  67. data->ty = (float)data->size.y / PotUp(data->isize.y);
  68. data->dilate = dilate;
  69. data->ntiles = data->count.i * data->count.j;
  70. drawgroup = DRAWGROUP_BEFORE;
  71. }
  72. TileSet::~TileSet()
  73. {
  74. free(data->tiles);
  75. free(data->name);
  76. delete data;
  77. }
  78. void TileSet::TickDraw(float deltams)
  79. {
  80. Entity::TickDraw(deltams);
  81. if (IsDestroying())
  82. {
  83. if (data->img)
  84. delete data->img;
  85. else
  86. glDeleteTextures(1, &data->texture);
  87. }
  88. else if (data->img)
  89. {
  90. GLuint format;
  91. int planes;
  92. switch (data->img->GetFormat())
  93. {
  94. case Image::FORMAT_RGB:
  95. format = GL_RGB;
  96. planes = 3;
  97. break;
  98. case Image::FORMAT_RGBA:
  99. default:
  100. format = GL_RGBA;
  101. planes = 4;
  102. break;
  103. }
  104. int w = PotUp(data->isize.x);
  105. int h = PotUp(data->isize.y);
  106. uint8_t *pixels = (uint8_t *)data->img->GetData();
  107. if (w != data->isize.x || h != data->isize.y)
  108. {
  109. uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
  110. for (int line = 0; line < data->isize.y; line++)
  111. memcpy(tmp + planes * w * line,
  112. pixels + planes * data->isize.x * line,
  113. planes * data->isize.x);
  114. pixels = tmp;
  115. }
  116. glGenTextures(1, &data->texture);
  117. glEnable(GL_TEXTURE_2D);
  118. glBindTexture(GL_TEXTURE_2D, data->texture);
  119. glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
  120. format, GL_UNSIGNED_BYTE, pixels);
  121. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  122. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  123. if (pixels != data->img->GetData())
  124. free(pixels);
  125. delete data->img;
  126. data->img = NULL;
  127. }
  128. }
  129. char const *TileSet::GetName()
  130. {
  131. return data->name;
  132. }
  133. ivec2 TileSet::GetCount() const
  134. {
  135. return data->count;
  136. }
  137. ivec2 TileSet::GetSize(int tileid) const
  138. {
  139. return data->size;
  140. }
  141. void TileSet::Bind()
  142. {
  143. if (!data->img && data->texture)
  144. {
  145. glActiveTexture(GL_TEXTURE0);
  146. glBindTexture(GL_TEXTURE_2D, data->texture);
  147. }
  148. }
  149. void TileSet::BlitTile(uint32_t id, vec3 pos, int o,
  150. float *vertex, float *texture)
  151. {
  152. float tx = data->tx * ((id & 0xffff) % data->count.i);
  153. float ty = data->ty * ((id & 0xffff) / data->count.i);
  154. float dilate = data->dilate;
  155. int dx = data->size.x;
  156. int dy = o ? 0 : data->size.y;
  157. int dz = o ? data->size.y : 0;
  158. if (!data->img && data->texture)
  159. {
  160. float tmp[10];
  161. *vertex++ = tmp[0] = pos.x;
  162. *vertex++ = tmp[1] = dilate * (pos.y + dy);
  163. *vertex++ = tmp[2] = dilate * (pos.z + dz);
  164. *texture++ = tmp[3] = tx;
  165. *texture++ = tmp[4] = ty;
  166. *vertex++ = pos.x + dx;
  167. *vertex++ = dilate * (pos.y + dy);
  168. *vertex++ = dilate * (pos.z + dz);
  169. *texture++ = tx + data->tx;
  170. *texture++ = ty;
  171. *vertex++ = tmp[5] = pos.x + dx;
  172. *vertex++ = tmp[6] = dilate * pos.y;
  173. *vertex++ = tmp[7] = dilate * pos.z;
  174. *texture++ = tmp[8] = tx + data->tx;
  175. *texture++ = tmp[9] = ty + data->ty;
  176. *vertex++ = tmp[0];
  177. *vertex++ = tmp[1];
  178. *vertex++ = tmp[2];
  179. *texture++ = tmp[3];
  180. *texture++ = tmp[4];
  181. *vertex++ = tmp[5];
  182. *vertex++ = tmp[6];
  183. *vertex++ = tmp[7];
  184. *texture++ = tmp[8];
  185. *texture++ = tmp[9];
  186. *vertex++ = pos.x;
  187. *vertex++ = dilate * pos.y;
  188. *vertex++ = dilate * pos.z;
  189. *texture++ = tx;
  190. *texture++ = ty + data->ty;
  191. }
  192. else
  193. {
  194. memset(vertex, 0, 3 * sizeof(float));
  195. memset(texture, 0, 2 * sizeof(float));
  196. }
  197. }
  198. } /* namespace lol */