wrap_Image.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // LOVE
  21. #include "wrap_Image.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. namespace opengl
  27. {
  28. Image *luax_checkimage(lua_State *L, int idx)
  29. {
  30. return luax_checktype<Image>(L, idx);
  31. }
  32. int w_Image_setMipmapFilter(lua_State *L)
  33. {
  34. Image *t = luax_checkimage(L, 1);
  35. Texture::Filter f = t->getFilter();
  36. if (lua_isnoneornil(L, 2))
  37. f.mipmap = Texture::FILTER_NONE; // mipmapping is disabled if no argument is given
  38. else
  39. {
  40. const char *mipmapstr = luaL_checkstring(L, 2);
  41. if (!Texture::getConstant(mipmapstr, f.mipmap))
  42. return luaL_error(L, "Invalid filter mode: %s", mipmapstr);
  43. }
  44. luax_catchexcept(L, [&](){ t->setFilter(f); });
  45. t->setMipmapSharpness((float) luaL_optnumber(L, 3, 0.0));
  46. return 0;
  47. }
  48. int w_Image_getMipmapFilter(lua_State *L)
  49. {
  50. Image *t = luax_checkimage(L, 1);
  51. const Texture::Filter &f = t->getFilter();
  52. const char *mipmapstr;
  53. if (Texture::getConstant(f.mipmap, mipmapstr))
  54. lua_pushstring(L, mipmapstr);
  55. else
  56. lua_pushnil(L); // only return a mipmap filter if mipmapping is enabled
  57. lua_pushnumber(L, t->getMipmapSharpness());
  58. return 2;
  59. }
  60. int w_Image_isCompressed(lua_State *L)
  61. {
  62. Image *i = luax_checkimage(L, 1);
  63. luax_pushboolean(L, i->isCompressed());
  64. return 1;
  65. }
  66. int w_Image_refresh(lua_State *L)
  67. {
  68. Image *i = luax_checkimage(L, 1);
  69. int xoffset = (int) luaL_optnumber(L, 2, 0);
  70. int yoffset = (int) luaL_optnumber(L, 3, 0);
  71. int w = (int) luaL_optnumber(L, 4, i->getWidth());
  72. int h = (int) luaL_optnumber(L, 5, i->getHeight());
  73. luax_catchexcept(L, [&](){ i->refresh(xoffset, yoffset, w, h); });
  74. return 0;
  75. }
  76. int w_Image_getData(lua_State *L)
  77. {
  78. Image *i = luax_checkimage(L, 1);
  79. int n = 0;
  80. if (i->isCompressed())
  81. {
  82. for (const auto &cdata : i->getCompressedData())
  83. {
  84. luax_pushtype(L, cdata.get());
  85. n++;
  86. }
  87. }
  88. else
  89. {
  90. for (const auto &data : i->getImageData())
  91. {
  92. luax_pushtype(L, data.get());
  93. n++;
  94. }
  95. }
  96. return n;
  97. }
  98. const char *luax_imageSettingName(Image::SettingType settingtype)
  99. {
  100. const char *name = nullptr;
  101. Image::getConstant(settingtype, name);
  102. return name;
  103. }
  104. int w_Image_getFlags(lua_State *L)
  105. {
  106. Image *i = luax_checkimage(L, 1);
  107. Image::Settings settings = i->getFlags();
  108. lua_createtable(L, 0, 2);
  109. lua_pushboolean(L, settings.mipmaps);
  110. lua_setfield(L, -2, luax_imageSettingName(Image::SETTING_MIPMAPS));
  111. lua_pushboolean(L, settings.linear);
  112. lua_setfield(L, -2, luax_imageSettingName(Image::SETTING_LINEAR));
  113. lua_pushnumber(L, settings.pixeldensity);
  114. lua_setfield(L, -2, luax_imageSettingName(Image::SETTING_PIXELDENSITY));
  115. return 1;
  116. }
  117. static const luaL_Reg w_Image_functions[] =
  118. {
  119. { "setMipmapFilter", w_Image_setMipmapFilter },
  120. { "getMipmapFilter", w_Image_getMipmapFilter },
  121. { "isCompressed", w_Image_isCompressed },
  122. { "refresh", w_Image_refresh },
  123. { "getData", w_Image_getData },
  124. { "getFlags", w_Image_getFlags },
  125. { 0, 0 }
  126. };
  127. extern "C" int luaopen_image(lua_State *L)
  128. {
  129. return luax_register_type(L, &Image::type, w_Texture_functions, w_Image_functions, nullptr);
  130. }
  131. } // opengl
  132. } // graphics
  133. } // love