Image.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * Copyright (c) 2006-2011 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Image.h"
  21. // STD
  22. #include <cstring> // For memcpy
  23. namespace love
  24. {
  25. namespace graphics
  26. {
  27. namespace opengl
  28. {
  29. Image::Image(love::image::ImageData * data)
  30. : width((float)(data->getWidth())), height((float)(data->getHeight())), texture(0)
  31. {
  32. data->retain();
  33. this->data = data;
  34. memset(vertices, 255, sizeof(vertex)*4);
  35. vertices[0].x = 0; vertices[0].y = 0;
  36. vertices[1].x = 0; vertices[1].y = height;
  37. vertices[2].x = width; vertices[2].y = height;
  38. vertices[3].x = width; vertices[3].y = 0;
  39. vertices[0].s = 0; vertices[0].t = 0;
  40. vertices[1].s = 0; vertices[1].t = 1;
  41. vertices[2].s = 1; vertices[2].t = 1;
  42. vertices[3].s = 1; vertices[3].t = 0;
  43. }
  44. Image::~Image()
  45. {
  46. if(data != 0)
  47. data->release();
  48. unload();
  49. }
  50. float Image::getWidth() const
  51. {
  52. return width;
  53. }
  54. float Image::getHeight() const
  55. {
  56. return height;
  57. }
  58. const vertex * Image::getVertices() const
  59. {
  60. return vertices;
  61. }
  62. love::image::ImageData * Image::getData() const
  63. {
  64. return data;
  65. }
  66. void Image::getRectangleVertices(int x, int y, int w, int h, vertex * vertices) const
  67. {
  68. // Check upper.
  69. x = (x+w > (int)width) ? (int)width-w : x;
  70. y = (y+h > (int)height) ? (int)height-h : y;
  71. // Check lower.
  72. x = (x < 0) ? 0 : x;
  73. y = (y < 0) ? 0 : y;
  74. vertices[0].x = 0; vertices[0].y = 0;
  75. vertices[1].x = 0; vertices[1].y = (float)h;
  76. vertices[2].x = (float)w; vertices[2].y = (float)h;
  77. vertices[3].x = (float)w; vertices[3].y = 0;
  78. float tx = (float)x/width;
  79. float ty = (float)y/height;
  80. float tw = (float)w/width;
  81. float th = (float)h/height;
  82. vertices[0].s = tx; vertices[0].t = ty;
  83. vertices[1].s = tx; vertices[1].t = ty+th;
  84. vertices[2].s = tx+tw; vertices[2].t = ty+th;
  85. vertices[3].s = tx+tw; vertices[3].t = ty;
  86. }
  87. void Image::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const
  88. {
  89. static Matrix t;
  90. t.setTransformation(x, y, angle, sx, sy, ox, oy);
  91. drawv(t, vertices);
  92. }
  93. void Image::drawq(Quad * quad, float x, float y, float angle, float sx, float sy, float ox, float oy) const
  94. {
  95. static Matrix t;
  96. const vertex * v = quad->getVertices();
  97. t.setTransformation(x, y, angle, sx, sy, ox, oy);
  98. drawv(t, v);
  99. }
  100. void Image::setFilter(Image::Filter f)
  101. {
  102. GLint gmin, gmag;
  103. gmin = gmag = 0; // so that they're not used uninitialized
  104. switch(f.min)
  105. {
  106. case FILTER_LINEAR:
  107. gmin = GL_LINEAR;
  108. break;
  109. case FILTER_NEAREST:
  110. gmin = GL_NEAREST;
  111. break;
  112. default:
  113. break;
  114. }
  115. switch(f.mag)
  116. {
  117. case FILTER_LINEAR:
  118. gmag = GL_LINEAR;
  119. break;
  120. case FILTER_NEAREST:
  121. gmag = GL_NEAREST;
  122. break;
  123. default:
  124. break;
  125. }
  126. bind();
  127. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gmin);
  128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag);
  129. }
  130. Image::Filter Image::getFilter() const
  131. {
  132. bind();
  133. GLint gmin, gmag;
  134. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, &gmin);
  135. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, &gmag);
  136. Image::Filter f;
  137. switch(gmin)
  138. {
  139. case GL_NEAREST:
  140. f.min = FILTER_NEAREST;
  141. break;
  142. case GL_LINEAR:
  143. default:
  144. f.min = FILTER_LINEAR;
  145. break;
  146. }
  147. switch(gmin)
  148. {
  149. case GL_NEAREST:
  150. f.mag = FILTER_NEAREST;
  151. break;
  152. case GL_LINEAR:
  153. default:
  154. f.mag = FILTER_LINEAR;
  155. break;
  156. }
  157. return f;
  158. }
  159. void Image::setWrap(Image::Wrap w)
  160. {
  161. GLint gs, gt;
  162. switch(w.s)
  163. {
  164. case WRAP_CLAMP:
  165. gs = GL_CLAMP_TO_EDGE;
  166. break;
  167. case WRAP_REPEAT:
  168. default:
  169. gs = GL_REPEAT;
  170. break;
  171. }
  172. switch(w.t)
  173. {
  174. case WRAP_CLAMP:
  175. gt = GL_CLAMP_TO_EDGE;
  176. break;
  177. case WRAP_REPEAT:
  178. default:
  179. gt = GL_REPEAT;
  180. break;
  181. }
  182. bind();
  183. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, gs);
  184. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, gt);
  185. }
  186. Image::Wrap Image::getWrap() const
  187. {
  188. bind();
  189. GLint gs, gt;
  190. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, &gs);
  191. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, &gt);
  192. Wrap w;
  193. switch(gs)
  194. {
  195. case GL_CLAMP_TO_EDGE:
  196. w.s = WRAP_CLAMP;
  197. break;
  198. case GL_REPEAT:
  199. default:
  200. w.s = WRAP_REPEAT;
  201. break;
  202. }
  203. switch(gt)
  204. {
  205. case GL_CLAMP_TO_EDGE:
  206. w.t = WRAP_CLAMP;
  207. break;
  208. case GL_REPEAT:
  209. default:
  210. w.t = WRAP_REPEAT;
  211. break;
  212. }
  213. return w;
  214. }
  215. void Image::bind() const
  216. {
  217. if(texture != 0)
  218. glBindTexture(GL_TEXTURE_2D,texture);
  219. }
  220. bool Image::load()
  221. {
  222. return loadVolatile();
  223. }
  224. void Image::unload()
  225. {
  226. return unloadVolatile();
  227. }
  228. bool Image::loadVolatile()
  229. {
  230. glGenTextures(1,(GLuint*)&texture);
  231. glBindTexture(GL_TEXTURE_2D, texture);
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  233. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  234. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  235. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  236. float p2width = next_p2(width);
  237. float p2height = next_p2(height);
  238. float t = width/p2width;
  239. float s = height/p2height;
  240. vertices[1].t = t;
  241. vertices[2].t = t;
  242. vertices[2].s = s;
  243. vertices[3].s = s;
  244. glTexImage2D(GL_TEXTURE_2D,
  245. 0,
  246. GL_RGBA8,
  247. (GLsizei)p2width,
  248. (GLsizei)p2height,
  249. 0,
  250. GL_RGBA,
  251. GL_UNSIGNED_BYTE,
  252. 0);
  253. glTexSubImage2D(GL_TEXTURE_2D,
  254. 0,
  255. 0,
  256. 0,
  257. (GLsizei)width,
  258. (GLsizei)height,
  259. GL_RGBA,
  260. GL_UNSIGNED_BYTE,
  261. data->getData());
  262. setFilter(settings.filter);
  263. setWrap(settings.wrap);
  264. return true;
  265. }
  266. void Image::unloadVolatile()
  267. {
  268. settings.filter = getFilter();
  269. settings.wrap = getWrap();
  270. // Delete the hardware texture.
  271. if(texture != 0)
  272. {
  273. glDeleteTextures(1, (GLuint*)&texture);
  274. texture = 0;
  275. }
  276. }
  277. void Image::drawv(const Matrix & t, const vertex * v) const
  278. {
  279. bind();
  280. glPushMatrix();
  281. glMultMatrixf((const GLfloat*)t.getElements());
  282. glEnableClientState(GL_VERTEX_ARRAY);
  283. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  284. glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid*)&v[0].x);
  285. glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid*)&v[0].s);
  286. glDrawArrays(GL_QUADS, 0, 4);
  287. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  288. glDisableClientState(GL_VERTEX_ARRAY);
  289. glPopMatrix();
  290. }
  291. } // opengl
  292. } // graphics
  293. } // love