wrap_Canvas.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_setFilter(lua_State *L)
  59. {
  60. Canvas *canvas = luax_checkcanvas(L, 1);
  61. Image::Filter f;
  62. const char *minstr = luaL_checkstring(L, 2);
  63. const char *magstr = luaL_optstring(L, 3, minstr);
  64. if (!Image::getConstant(minstr, f.min))
  65. return luaL_error(L, "Invalid filter mode: %s", minstr);
  66. if (!Image::getConstant(magstr, f.mag))
  67. return luaL_error(L, "Invalid filter mode: %s", magstr);
  68. canvas->setFilter(f);
  69. return 0;
  70. }
  71. int w_Canvas_getFilter(lua_State *L)
  72. {
  73. Canvas *canvas = luax_checkcanvas(L, 1);
  74. const Image::Filter f = canvas->getFilter();
  75. const char *minstr;
  76. const char *magstr;
  77. Image::getConstant(f.min, minstr);
  78. Image::getConstant(f.mag, magstr);
  79. lua_pushstring(L, minstr);
  80. lua_pushstring(L, magstr);
  81. return 2;
  82. }
  83. int w_Canvas_setWrap(lua_State *L)
  84. {
  85. Canvas *canvas = luax_checkcanvas(L, 1);
  86. Image::Wrap w;
  87. const char *sstr = luaL_checkstring(L, 2);
  88. const char *tstr = luaL_optstring(L, 3, sstr);
  89. if (!Image::getConstant(sstr, w.s))
  90. return luaL_error(L, "Invalid wrap mode: %s", sstr);
  91. if (!Image::getConstant(tstr, w.t))
  92. return luaL_error(L, "Invalid wrap mode, %s", tstr);
  93. canvas->setWrap(w);
  94. return 0;
  95. }
  96. int w_Canvas_getWrap(lua_State *L)
  97. {
  98. Canvas *canvas = luax_checkcanvas(L, 1);
  99. const Image::Wrap w = canvas->getWrap();
  100. const char *wrap_s;
  101. const char *wrap_t;
  102. Image::getConstant(w.s, wrap_s);
  103. Image::getConstant(w.t, wrap_t);
  104. lua_pushstring(L, wrap_s);
  105. lua_pushstring(L, wrap_t);
  106. return 2;
  107. }
  108. int w_Canvas_clear(lua_State *L)
  109. {
  110. Canvas *canvas = luax_checkcanvas(L, 1);
  111. Color c;
  112. if (lua_isnoneornil(L, 2))
  113. {
  114. c.r = 0;
  115. c.g = 0;
  116. c.b = 0;
  117. c.a = 0;
  118. }
  119. else if (lua_istable(L, 2))
  120. {
  121. lua_pushinteger(L, 1);
  122. lua_gettable(L, 2);
  123. c.r = (unsigned char)luaL_checkint(L, -1);
  124. lua_pushinteger(L, 2);
  125. lua_gettable(L, 2);
  126. c.g = (unsigned char)luaL_checkint(L, -1);
  127. lua_pushinteger(L, 3);
  128. lua_gettable(L, 2);
  129. c.b = (unsigned char)luaL_checkint(L, -1);
  130. lua_pushinteger(L, 4);
  131. lua_gettable(L, 2);
  132. c.g = (unsigned char)luaL_optint(L, -1, 255);
  133. lua_pop(L, 4);
  134. }
  135. else
  136. {
  137. c.r = (unsigned char)luaL_checkint(L, 2);
  138. c.g = (unsigned char)luaL_checkint(L, 3);
  139. c.b = (unsigned char)luaL_checkint(L, 4);
  140. c.a = (unsigned char)luaL_optint(L, 5, 255);
  141. }
  142. canvas->clear(c);
  143. return 0;
  144. }
  145. int w_Canvas_getWidth(lua_State *L)
  146. {
  147. Canvas *canvas = luax_checkcanvas(L, 1);
  148. lua_pushnumber(L, canvas->getWidth());
  149. return 1;
  150. }
  151. int w_Canvas_getHeight(lua_State *L)
  152. {
  153. Canvas *canvas = luax_checkcanvas(L, 1);
  154. lua_pushnumber(L, canvas->getHeight());
  155. return 1;
  156. }
  157. int w_Canvas_getType(lua_State *L)
  158. {
  159. Canvas *canvas = luax_checkcanvas(L, 1);
  160. Canvas::TextureType type = canvas->getTextureType();
  161. const char *str;
  162. Canvas::getConstant(type, str);
  163. lua_pushstring(L, str);
  164. return 1;
  165. }
  166. static const luaL_Reg functions[] =
  167. {
  168. { "renderTo", w_Canvas_renderTo },
  169. { "getImageData", w_Canvas_getImageData },
  170. { "setFilter", w_Canvas_setFilter },
  171. { "getFilter", w_Canvas_getFilter },
  172. { "setWrap", w_Canvas_setWrap },
  173. { "getWrap", w_Canvas_getWrap },
  174. { "clear", w_Canvas_clear },
  175. { "getWidth", w_Canvas_getWidth },
  176. { "getHeight", w_Canvas_getHeight },
  177. { "getType", w_Canvas_getType },
  178. { 0, 0 }
  179. };
  180. extern "C" int luaopen_canvas(lua_State *L)
  181. {
  182. return luax_register_type(L, "Canvas", functions);
  183. }
  184. } // opengl
  185. } // graphics
  186. } // love