wrap_Canvas.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include "Graphics.h"
  21. #include "wrap_Canvas.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. namespace opengl
  27. {
  28. Canvas *luax_checkcanvas(lua_State *L, int idx)
  29. {
  30. return luax_checktype<Canvas>(L, idx, "Canvas", GRAPHICS_CANVAS_T);
  31. }
  32. int w_Canvas_renderTo(lua_State *L)
  33. {
  34. // As startGrab() clears the framebuffer, better not allow
  35. // grabbing inside another grabbing
  36. if (Canvas::current != NULL)
  37. {
  38. Canvas::bindDefaultCanvas();
  39. return luaL_error(L, "Current render target not the default canvas!");
  40. }
  41. Canvas *canvas = luax_checkcanvas(L, 1);
  42. if (!lua_isfunction(L, 2))
  43. return luaL_error(L, "Need a function to render to canvas.");
  44. static std::vector<Canvas *> attachments;
  45. try
  46. {
  47. canvas->startGrab(attachments);
  48. }
  49. catch (love::Exception &e)
  50. {
  51. return luaL_error(L, "%s", e.what());
  52. }
  53. lua_settop(L, 2); // make sure the function is on top of the stack
  54. lua_call(L, 0, 0);
  55. canvas->stopGrab();
  56. return 0;
  57. }
  58. int w_Canvas_getImageData(lua_State *L)
  59. {
  60. Canvas *canvas = luax_checkcanvas(L, 1);
  61. love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  62. love::image::ImageData *img = canvas->getImageData(image);
  63. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)img);
  64. return 1;
  65. }
  66. int w_Canvas_getPixel(lua_State * L)
  67. {
  68. Canvas * canvas = luax_checkcanvas(L, 1);
  69. int x = luaL_checkint(L, 2);
  70. int y = luaL_checkint(L, 3);
  71. unsigned char c[4];
  72. try
  73. {
  74. canvas->getPixel(c, x, y);
  75. }
  76. catch (love::Exception & e)
  77. {
  78. return luaL_error(L, "%s", e.what());
  79. }
  80. lua_pushnumber(L, c[0]);
  81. lua_pushnumber(L, c[1]);
  82. lua_pushnumber(L, c[2]);
  83. lua_pushnumber(L, c[3]);
  84. return 4;
  85. }
  86. int w_Canvas_setFilter(lua_State *L)
  87. {
  88. Canvas *canvas = luax_checkcanvas(L, 1);
  89. Image::Filter f;
  90. const char *minstr = luaL_checkstring(L, 2);
  91. const char *magstr = luaL_optstring(L, 3, minstr);
  92. if (!Image::getConstant(minstr, f.min))
  93. return luaL_error(L, "Invalid filter mode: %s", minstr);
  94. if (!Image::getConstant(magstr, f.mag))
  95. return luaL_error(L, "Invalid filter mode: %s", magstr);
  96. f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
  97. canvas->setFilter(f);
  98. return 0;
  99. }
  100. int w_Canvas_getFilter(lua_State *L)
  101. {
  102. Canvas *canvas = luax_checkcanvas(L, 1);
  103. const Image::Filter f = canvas->getFilter();
  104. const char *minstr;
  105. const char *magstr;
  106. Image::getConstant(f.min, minstr);
  107. Image::getConstant(f.mag, magstr);
  108. lua_pushstring(L, minstr);
  109. lua_pushstring(L, magstr);
  110. lua_pushnumber(L, f.anisotropy);
  111. return 3;
  112. }
  113. int w_Canvas_setWrap(lua_State *L)
  114. {
  115. Canvas *canvas = luax_checkcanvas(L, 1);
  116. Image::Wrap w;
  117. const char *sstr = luaL_checkstring(L, 2);
  118. const char *tstr = luaL_optstring(L, 3, sstr);
  119. if (!Image::getConstant(sstr, w.s))
  120. return luaL_error(L, "Invalid wrap mode: %s", sstr);
  121. if (!Image::getConstant(tstr, w.t))
  122. return luaL_error(L, "Invalid wrap mode, %s", tstr);
  123. canvas->setWrap(w);
  124. return 0;
  125. }
  126. int w_Canvas_getWrap(lua_State *L)
  127. {
  128. Canvas *canvas = luax_checkcanvas(L, 1);
  129. const Image::Wrap w = canvas->getWrap();
  130. const char *wrap_s;
  131. const char *wrap_t;
  132. Image::getConstant(w.s, wrap_s);
  133. Image::getConstant(w.t, wrap_t);
  134. lua_pushstring(L, wrap_s);
  135. lua_pushstring(L, wrap_t);
  136. return 2;
  137. }
  138. int w_Canvas_clear(lua_State *L)
  139. {
  140. Canvas *canvas = luax_checkcanvas(L, 1);
  141. Color c;
  142. if (lua_isnoneornil(L, 2))
  143. {
  144. c.r = 0;
  145. c.g = 0;
  146. c.b = 0;
  147. c.a = 0;
  148. }
  149. else if (lua_istable(L, 2))
  150. {
  151. lua_pushinteger(L, 1);
  152. lua_gettable(L, 2);
  153. c.r = (unsigned char)luaL_checkint(L, -1);
  154. lua_pushinteger(L, 2);
  155. lua_gettable(L, 2);
  156. c.g = (unsigned char)luaL_checkint(L, -1);
  157. lua_pushinteger(L, 3);
  158. lua_gettable(L, 2);
  159. c.b = (unsigned char)luaL_checkint(L, -1);
  160. lua_pushinteger(L, 4);
  161. lua_gettable(L, 2);
  162. c.g = (unsigned char)luaL_optint(L, -1, 255);
  163. lua_pop(L, 4);
  164. }
  165. else
  166. {
  167. c.r = (unsigned char)luaL_checkint(L, 2);
  168. c.g = (unsigned char)luaL_checkint(L, 3);
  169. c.b = (unsigned char)luaL_checkint(L, 4);
  170. c.a = (unsigned char)luaL_optint(L, 5, 255);
  171. }
  172. canvas->clear(c);
  173. return 0;
  174. }
  175. int w_Canvas_getWidth(lua_State *L)
  176. {
  177. Canvas *canvas = luax_checkcanvas(L, 1);
  178. lua_pushnumber(L, canvas->getWidth());
  179. return 1;
  180. }
  181. int w_Canvas_getHeight(lua_State *L)
  182. {
  183. Canvas *canvas = luax_checkcanvas(L, 1);
  184. lua_pushnumber(L, canvas->getHeight());
  185. return 1;
  186. }
  187. int w_Canvas_getDimensions(lua_State *L)
  188. {
  189. Canvas *canvas = luax_checkcanvas(L, 1);
  190. lua_pushnumber(L, canvas->getWidth());
  191. lua_pushnumber(L, canvas->getHeight());
  192. return 2;
  193. }
  194. int w_Canvas_getType(lua_State *L)
  195. {
  196. Canvas *canvas = luax_checkcanvas(L, 1);
  197. Canvas::TextureType type = canvas->getTextureType();
  198. const char *str;
  199. Canvas::getConstant(type, str);
  200. lua_pushstring(L, str);
  201. return 1;
  202. }
  203. static const luaL_Reg functions[] =
  204. {
  205. { "renderTo", w_Canvas_renderTo },
  206. { "getImageData", w_Canvas_getImageData },
  207. { "getPixel", w_Canvas_getPixel },
  208. { "setFilter", w_Canvas_setFilter },
  209. { "getFilter", w_Canvas_getFilter },
  210. { "setWrap", w_Canvas_setWrap },
  211. { "getWrap", w_Canvas_getWrap },
  212. { "clear", w_Canvas_clear },
  213. { "getWidth", w_Canvas_getWidth },
  214. { "getHeight", w_Canvas_getHeight },
  215. { "getDimensions", w_Canvas_getDimensions },
  216. { "getType", w_Canvas_getType },
  217. { 0, 0 }
  218. };
  219. extern "C" int luaopen_canvas(lua_State *L)
  220. {
  221. return luax_register_type(L, "Canvas", functions);
  222. }
  223. } // opengl
  224. } // graphics
  225. } // love