wrap_Canvas.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright (c) 2006-2016 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 "wrap_Canvas.h"
  21. #include "Graphics.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::type);
  31. }
  32. int w_Canvas_renderTo(lua_State *L)
  33. {
  34. Canvas *canvas = luax_checkcanvas(L, 1);
  35. luaL_checktype(L, 2, LUA_TFUNCTION);
  36. auto graphics = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  37. if (graphics)
  38. {
  39. // Save the current Canvas so we can restore it when we're done.
  40. std::vector<Canvas *> oldcanvases = graphics->getCanvas();
  41. for (Canvas *c : oldcanvases)
  42. c->retain();
  43. luax_catchexcept(L, [&](){ graphics->setCanvas(canvas); });
  44. lua_settop(L, 2); // make sure the function is on top of the stack
  45. int status = lua_pcall(L, 0, 0, 0);
  46. graphics->setCanvas(oldcanvases);
  47. for (Canvas *c : oldcanvases)
  48. c->release();
  49. if (status != 0)
  50. return lua_error(L);
  51. }
  52. return 0;
  53. }
  54. int w_Canvas_newImageData(lua_State *L)
  55. {
  56. Canvas *canvas = luax_checkcanvas(L, 1);
  57. love::image::Image *image = luax_getmodule<love::image::Image>(L, love::image::Image::type);
  58. int x = (int) luaL_optnumber(L, 2, 0);
  59. int y = (int) luaL_optnumber(L, 3, 0);
  60. int w = (int) luaL_optnumber(L, 4, canvas->getWidth());
  61. int h = (int) luaL_optnumber(L, 5, canvas->getHeight());
  62. love::image::ImageData *img = nullptr;
  63. luax_catchexcept(L, [&](){ img = canvas->newImageData(image, x, y, w, h); });
  64. luax_pushtype(L, love::image::ImageData::type, img);
  65. img->release();
  66. return 1;
  67. }
  68. int w_Canvas_getFormat(lua_State *L)
  69. {
  70. Canvas *canvas = luax_checkcanvas(L, 1);
  71. Canvas::Format format = canvas->getTextureFormat();
  72. const char *str;
  73. if (!Canvas::getConstant(format, str))
  74. return luaL_error(L, "Unknown Canvas format.");
  75. lua_pushstring(L, str);
  76. return 1;
  77. }
  78. int w_Canvas_getMSAA(lua_State *L)
  79. {
  80. Canvas *canvas = luax_checkcanvas(L, 1);
  81. lua_pushinteger(L, canvas->getMSAA());
  82. return 1;
  83. }
  84. static const luaL_Reg w_Canvas_functions[] =
  85. {
  86. { "renderTo", w_Canvas_renderTo },
  87. { "newImageData", w_Canvas_newImageData },
  88. { "getFormat", w_Canvas_getFormat },
  89. { "getMSAA", w_Canvas_getMSAA },
  90. { 0, 0 }
  91. };
  92. extern "C" int luaopen_canvas(lua_State *L)
  93. {
  94. return luax_register_type(L, Canvas::type, "Canvas", w_Texture_functions, w_Canvas_functions, nullptr);
  95. }
  96. } // opengl
  97. } // graphics
  98. } // love