wrap_Image.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * Copyright (c) 2006-2013 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. // LOVE
  21. #include "wrap_Image.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. namespace opengl
  27. {
  28. Image *luax_checkimage(lua_State *L, int idx)
  29. {
  30. return luax_checktype<Image>(L, idx, "Image", GRAPHICS_IMAGE_T);
  31. }
  32. int w_Image_getWidth(lua_State *L)
  33. {
  34. Image *t = luax_checkimage(L, 1);
  35. lua_pushnumber(L, t->getWidth());
  36. return 1;
  37. }
  38. int w_Image_getHeight(lua_State *L)
  39. {
  40. Image *t = luax_checkimage(L, 1);
  41. lua_pushnumber(L, t->getHeight());
  42. return 1;
  43. }
  44. int w_Image_getDimensions(lua_State *L)
  45. {
  46. Image *t = luax_checkimage(L, 1);
  47. lua_pushnumber(L, t->getWidth());
  48. lua_pushnumber(L, t->getHeight());
  49. return 2;
  50. }
  51. int w_Image_setFilter(lua_State *L)
  52. {
  53. Image *t = luax_checkimage(L, 1);
  54. Texture::Filter f = t->getFilter();
  55. const char *minstr = luaL_checkstring(L, 2);
  56. const char *magstr = luaL_optstring(L, 3, minstr);
  57. if (!Texture::getConstant(minstr, f.min))
  58. return luaL_error(L, "Invalid filter mode: %s", minstr);
  59. if (!Texture::getConstant(magstr, f.mag))
  60. return luaL_error(L, "Invalid filter mode: %s", magstr);
  61. f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
  62. EXCEPT_GUARD(t->setFilter(f);)
  63. return 0;
  64. }
  65. int w_Image_getFilter(lua_State *L)
  66. {
  67. Image *t = luax_checkimage(L, 1);
  68. const Texture::Filter f = t->getFilter();
  69. const char *minstr;
  70. const char *magstr;
  71. Texture::getConstant(f.min, minstr);
  72. Texture::getConstant(f.mag, magstr);
  73. lua_pushstring(L, minstr);
  74. lua_pushstring(L, magstr);
  75. lua_pushnumber(L, f.anisotropy);
  76. return 3;
  77. }
  78. int w_Image_setMipmapFilter(lua_State *L)
  79. {
  80. Image *t = luax_checkimage(L, 1);
  81. Texture::Filter f = t->getFilter();
  82. if (lua_isnoneornil(L, 2))
  83. f.mipmap = Texture::FILTER_NONE; // mipmapping is disabled if no argument is given
  84. else
  85. {
  86. const char *mipmapstr = luaL_checkstring(L, 2);
  87. if (!Texture::getConstant(mipmapstr, f.mipmap))
  88. return luaL_error(L, "Invalid filter mode: %s", mipmapstr);
  89. }
  90. EXCEPT_GUARD(t->setFilter(f);)
  91. float sharpness = (float) luaL_optnumber(L, 3, 0);
  92. t->setMipmapSharpness(sharpness);
  93. return 0;
  94. }
  95. int w_Image_getMipmapFilter(lua_State *L)
  96. {
  97. Image *t = luax_checkimage(L, 1);
  98. const Texture::Filter &f = t->getFilter();
  99. const char *mipmapstr;
  100. if (Texture::getConstant(f.mipmap, mipmapstr))
  101. lua_pushstring(L, mipmapstr);
  102. else
  103. lua_pushnil(L); // only return a mipmap filter if mipmapping is enabled
  104. lua_pushnumber(L, t->getMipmapSharpness());
  105. return 2;
  106. }
  107. int w_Image_setWrap(lua_State *L)
  108. {
  109. Image *i = luax_checkimage(L, 1);
  110. Texture::Wrap w;
  111. const char *sstr = luaL_checkstring(L, 2);
  112. const char *tstr = luaL_optstring(L, 3, sstr);
  113. if (!Texture::getConstant(sstr, w.s))
  114. return luaL_error(L, "Invalid wrap mode: %s", sstr);
  115. if (!Texture::getConstant(tstr, w.t))
  116. return luaL_error(L, "Invalid wrap mode, %s", tstr);
  117. i->setWrap(w);
  118. return 0;
  119. }
  120. int w_Image_getWrap(lua_State *L)
  121. {
  122. Image *i = luax_checkimage(L, 1);
  123. const Texture::Wrap w = i->getWrap();
  124. const char *sstr;
  125. const char *tstr;
  126. Texture::getConstant(w.s, sstr);
  127. Texture::getConstant(w.t, tstr);
  128. lua_pushstring(L, sstr);
  129. lua_pushstring(L, tstr);
  130. return 2;
  131. }
  132. int w_Image_isCompressed(lua_State *L)
  133. {
  134. Image *i = luax_checkimage(L, 1);
  135. luax_pushboolean(L, i->isCompressed());
  136. return 1;
  137. }
  138. int w_Image_refresh(lua_State *L)
  139. {
  140. Image *i = luax_checkimage(L, 1);
  141. EXCEPT_GUARD(i->refresh();)
  142. return 0;
  143. }
  144. int w_Image_getData(lua_State *L)
  145. {
  146. Image *i = luax_checkimage(L, 1);
  147. if (i->isCompressed())
  148. {
  149. love::image::CompressedData *t = i->getCompressedData();
  150. if (t)
  151. {
  152. t->retain();
  153. luax_pushtype(L, "CompressedData", IMAGE_COMPRESSED_DATA_T, t);
  154. }
  155. else
  156. lua_pushnil(L);
  157. }
  158. else
  159. {
  160. love::image::ImageData *t = i->getImageData();
  161. if (t)
  162. {
  163. t->retain();
  164. luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, t);
  165. }
  166. else
  167. lua_pushnil(L);
  168. }
  169. return 1;
  170. }
  171. static const luaL_Reg functions[] =
  172. {
  173. { "getWidth", w_Image_getWidth },
  174. { "getHeight", w_Image_getHeight },
  175. { "getDimensions", w_Image_getDimensions },
  176. { "setFilter", w_Image_setFilter },
  177. { "getFilter", w_Image_getFilter },
  178. { "setWrap", w_Image_setWrap },
  179. { "getWrap", w_Image_getWrap },
  180. { "setMipmapFilter", w_Image_setMipmapFilter },
  181. { "getMipmapFilter", w_Image_getMipmapFilter },
  182. { "isCompressed", w_Image_isCompressed },
  183. { "refresh", w_Image_refresh },
  184. { "getData", w_Image_getData },
  185. { 0, 0 }
  186. };
  187. extern "C" int luaopen_image(lua_State *L)
  188. {
  189. return luax_register_type(L, "Image", functions);
  190. }
  191. } // opengl
  192. } // graphics
  193. } // love