wrap_Framebuffer.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "wrap_Framebuffer.h"
  2. namespace love
  3. {
  4. namespace graphics
  5. {
  6. namespace opengl
  7. {
  8. Framebuffer * luax_checkfbo(lua_State * L, int idx)
  9. {
  10. return luax_checktype<Framebuffer>(L, idx, "Framebuffer", GRAPHICS_FRAMEBUFFER_T);
  11. }
  12. int w_Framebuffer_renderTo(lua_State * L)
  13. {
  14. // As startGrab() clears the framebuffer, better not allow
  15. // grabbing inside another grabbing
  16. if (Framebuffer::current != NULL) {
  17. Framebuffer::bindDefaultBuffer();
  18. return luaL_error(L, "Current render target not the default framebuffer!");
  19. }
  20. Framebuffer * fbo = luax_checkfbo(L, 1);
  21. if (!lua_isfunction(L, 2))
  22. return luaL_error(L, "Need a function to render to fbo");
  23. fbo->startGrab();
  24. lua_settop(L, 2); // make sure the function is on top of the stack
  25. lua_call(L, 0, 0);
  26. fbo->stopGrab();
  27. return 0;
  28. }
  29. int w_Framebuffer_getImageData(lua_State * L)
  30. {
  31. Framebuffer * fbo = luax_checkfbo(L, 1);
  32. love::image::Image * image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  33. love::image::ImageData * img = fbo->getImageData( image );
  34. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)img);
  35. return 1;
  36. }
  37. int w_Framebuffer_setFilter(lua_State * L)
  38. {
  39. Framebuffer * fbo = luax_checkfbo(L, 1);
  40. const char * minstr = luaL_checkstring(L, 2);
  41. const char * magstr = luaL_checkstring(L, 3);
  42. Image::Filter f;
  43. if (!Image::getConstant(minstr, f.min))
  44. return luaL_error(L, "Invalid min filter mode: %s", minstr);
  45. if (!Image::getConstant(magstr, f.mag))
  46. return luaL_error(L, "Invalid max filter mode: %s", minstr);
  47. fbo->setFilter(f);
  48. return 0;
  49. }
  50. int w_Framebuffer_getFilter(lua_State * L)
  51. {
  52. Framebuffer * fbo = luax_checkfbo(L, 1);
  53. Image::Filter f = fbo->getFilter();
  54. const char * minstr;
  55. const char * magstr;
  56. Image::getConstant(f.min, minstr);
  57. Image::getConstant(f.mag, magstr);
  58. lua_pushstring(L, minstr);
  59. lua_pushstring(L, magstr);
  60. return 2;
  61. }
  62. static const luaL_Reg functions[] = {
  63. { "renderTo", w_Framebuffer_renderTo },
  64. { "getImageData", w_Framebuffer_getImageData },
  65. { "setFilter", w_Framebuffer_setFilter },
  66. { "getFilter", w_Framebuffer_getFilter },
  67. { 0, 0 }
  68. };
  69. int luaopen_framebuffer(lua_State * L)
  70. {
  71. return luax_register_type(L, "Framebuffer", functions);
  72. }
  73. } // opengl
  74. } // graphics
  75. } // love