wrap_Canvas.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Copyright (c) 2006-2012 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. canvas->startGrab();
  45. lua_settop(L, 2); // make sure the function is on top of the stack
  46. lua_call(L, 0, 0);
  47. canvas->stopGrab();
  48. return 0;
  49. }
  50. int w_Canvas_getImageData(lua_State *L)
  51. {
  52. Canvas *canvas = luax_checkcanvas(L, 1);
  53. love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  54. love::image::ImageData *img = canvas->getImageData(image);
  55. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)img);
  56. return 1;
  57. }
  58. int w_Canvas_getPixel(lua_State * L)
  59. {
  60. Canvas * canvas = luax_checkcanvas(L, 1);
  61. int x = luaL_checkint(L, 2);
  62. int y = luaL_checkint(L, 3);
  63. unsigned char c[4];
  64. try
  65. {
  66. canvas->getPixel(c, x, y);
  67. }
  68. catch (love::Exception & e)
  69. {
  70. return luaL_error(L, "%s", e.what());
  71. }
  72. lua_pushnumber(L, c[0]);
  73. lua_pushnumber(L, c[1]);
  74. lua_pushnumber(L, c[2]);
  75. lua_pushnumber(L, c[3]);
  76. return 4;
  77. }
  78. int w_Canvas_setFilter(lua_State *L)
  79. {
  80. Canvas *canvas = luax_checkcanvas(L, 1);
  81. Image::Filter f;
  82. const char *minstr = luaL_checkstring(L, 2);
  83. const char *magstr = luaL_optstring(L, 3, minstr);
  84. if (!Image::getConstant(minstr, f.min))
  85. return luaL_error(L, "Invalid filter mode: %s", minstr);
  86. if (!Image::getConstant(magstr, f.mag))
  87. return luaL_error(L, "Invalid filter mode: %s", magstr);
  88. canvas->setFilter(f);
  89. return 0;
  90. }
  91. int w_Canvas_getFilter(lua_State *L)
  92. {
  93. Canvas *canvas = luax_checkcanvas(L, 1);
  94. const Image::Filter f = canvas->getFilter();
  95. const char *minstr;
  96. const char *magstr;
  97. Image::getConstant(f.min, minstr);
  98. Image::getConstant(f.mag, magstr);
  99. lua_pushstring(L, minstr);
  100. lua_pushstring(L, magstr);
  101. return 2;
  102. }
  103. int w_Canvas_setWrap(lua_State *L)
  104. {
  105. Canvas *canvas = luax_checkcanvas(L, 1);
  106. Image::Wrap w;
  107. const char *sstr = luaL_checkstring(L, 2);
  108. const char *tstr = luaL_optstring(L, 3, sstr);
  109. if (!Image::getConstant(sstr, w.s))
  110. return luaL_error(L, "Invalid wrap mode: %s", sstr);
  111. if (!Image::getConstant(tstr, w.t))
  112. return luaL_error(L, "Invalid wrap mode, %s", tstr);
  113. canvas->setWrap(w);
  114. return 0;
  115. }
  116. int w_Canvas_getWrap(lua_State *L)
  117. {
  118. Canvas *canvas = luax_checkcanvas(L, 1);
  119. const Image::Wrap w = canvas->getWrap();
  120. const char *wrap_s;
  121. const char *wrap_t;
  122. Image::getConstant(w.s, wrap_s);
  123. Image::getConstant(w.t, wrap_t);
  124. lua_pushstring(L, wrap_s);
  125. lua_pushstring(L, wrap_t);
  126. return 2;
  127. }
  128. int w_Canvas_clear(lua_State *L)
  129. {
  130. Canvas *canvas = luax_checkcanvas(L, 1);
  131. Color c;
  132. if (lua_isnoneornil(L, 2))
  133. {
  134. c.r = 0;
  135. c.g = 0;
  136. c.b = 0;
  137. c.a = 0;
  138. }
  139. else if (lua_istable(L, 2))
  140. {
  141. lua_pushinteger(L, 1);
  142. lua_gettable(L, 2);
  143. c.r = (unsigned char)luaL_checkint(L, -1);
  144. lua_pushinteger(L, 2);
  145. lua_gettable(L, 2);
  146. c.g = (unsigned char)luaL_checkint(L, -1);
  147. lua_pushinteger(L, 3);
  148. lua_gettable(L, 2);
  149. c.b = (unsigned char)luaL_checkint(L, -1);
  150. lua_pushinteger(L, 4);
  151. lua_gettable(L, 2);
  152. c.g = (unsigned char)luaL_optint(L, -1, 255);
  153. lua_pop(L, 4);
  154. }
  155. else
  156. {
  157. c.r = (unsigned char)luaL_checkint(L, 2);
  158. c.g = (unsigned char)luaL_checkint(L, 3);
  159. c.b = (unsigned char)luaL_checkint(L, 4);
  160. c.a = (unsigned char)luaL_optint(L, 5, 255);
  161. }
  162. canvas->clear(c);
  163. return 0;
  164. }
  165. int w_Canvas_getWidth(lua_State *L)
  166. {
  167. Canvas *canvas = luax_checkcanvas(L, 1);
  168. lua_pushnumber(L, canvas->getWidth());
  169. return 1;
  170. }
  171. int w_Canvas_getHeight(lua_State *L)
  172. {
  173. Canvas *canvas = luax_checkcanvas(L, 1);
  174. lua_pushnumber(L, canvas->getHeight());
  175. return 1;
  176. }
  177. int w_Canvas_getType(lua_State *L)
  178. {
  179. Canvas *canvas = luax_checkcanvas(L, 1);
  180. Canvas::TextureType type = canvas->getTextureType();
  181. const char *str;
  182. Canvas::getConstant(type, str);
  183. lua_pushstring(L, str);
  184. return 1;
  185. }
  186. static const luaL_Reg functions[] =
  187. {
  188. { "renderTo", w_Canvas_renderTo },
  189. { "getImageData", w_Canvas_getImageData },
  190. { "getPixel", w_Canvas_getPixel },
  191. { "setFilter", w_Canvas_setFilter },
  192. { "getFilter", w_Canvas_getFilter },
  193. { "setWrap", w_Canvas_setWrap },
  194. { "getWrap", w_Canvas_getWrap },
  195. { "clear", w_Canvas_clear },
  196. { "getWidth", w_Canvas_getWidth },
  197. { "getHeight", w_Canvas_getHeight },
  198. { "getType", w_Canvas_getType },
  199. { 0, 0 }
  200. };
  201. extern "C" int luaopen_canvas(lua_State *L)
  202. {
  203. return luax_register_type(L, "Canvas", functions);
  204. }
  205. } // opengl
  206. } // graphics
  207. } // love