wrap_Texture.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_Texture.h"
  21. namespace love
  22. {
  23. namespace graphics
  24. {
  25. Texture *luax_checktexture(lua_State *L, int idx)
  26. {
  27. return luax_checktype<Texture>(L, idx);
  28. }
  29. int w_Texture_getWidth(lua_State *L)
  30. {
  31. Texture *t = luax_checktexture(L, 1);
  32. lua_pushnumber(L, t->getWidth());
  33. return 1;
  34. }
  35. int w_Texture_getHeight(lua_State *L)
  36. {
  37. Texture *t = luax_checktexture(L, 1);
  38. lua_pushnumber(L, t->getHeight());
  39. return 1;
  40. }
  41. int w_Texture_getDimensions(lua_State *L)
  42. {
  43. Texture *t = luax_checktexture(L, 1);
  44. lua_pushnumber(L, t->getWidth());
  45. lua_pushnumber(L, t->getHeight());
  46. return 2;
  47. }
  48. int w_Texture_setFilter(lua_State *L)
  49. {
  50. Texture *t = luax_checktexture(L, 1);
  51. Texture::Filter f = t->getFilter();
  52. const char *minstr = luaL_checkstring(L, 2);
  53. const char *magstr = luaL_optstring(L, 3, minstr);
  54. if (!Texture::getConstant(minstr, f.min))
  55. return luaL_error(L, "Invalid filter mode: %s", minstr);
  56. if (!Texture::getConstant(magstr, f.mag))
  57. return luaL_error(L, "Invalid filter mode: %s", magstr);
  58. f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
  59. luax_catchexcept(L, [&](){ t->setFilter(f); });
  60. return 0;
  61. }
  62. int w_Texture_getFilter(lua_State *L)
  63. {
  64. Texture *t = luax_checktexture(L, 1);
  65. const Texture::Filter f = t->getFilter();
  66. const char *minstr = nullptr;
  67. const char *magstr = nullptr;
  68. if (!Texture::getConstant(f.min, minstr))
  69. return luaL_error(L, "Unknown filter mode.");
  70. if (!Texture::getConstant(f.mag, magstr))
  71. return luaL_error(L, "Unknown filter mode.");
  72. lua_pushstring(L, minstr);
  73. lua_pushstring(L, magstr);
  74. lua_pushnumber(L, f.anisotropy);
  75. return 3;
  76. }
  77. int w_Texture_setWrap(lua_State *L)
  78. {
  79. Texture *t = luax_checktexture(L, 1);
  80. Texture::Wrap w;
  81. const char *sstr = luaL_checkstring(L, 2);
  82. const char *tstr = luaL_optstring(L, 3, sstr);
  83. if (!Texture::getConstant(sstr, w.s))
  84. return luaL_error(L, "Invalid wrap mode: %s", sstr);
  85. if (!Texture::getConstant(tstr, w.t))
  86. return luaL_error(L, "Invalid wrap mode, %s", tstr);
  87. luax_pushboolean(L, t->setWrap(w));
  88. return 1;
  89. }
  90. int w_Texture_getWrap(lua_State *L)
  91. {
  92. Texture *t = luax_checktexture(L, 1);
  93. const Texture::Wrap w = t->getWrap();
  94. const char *sstr = nullptr;
  95. const char *tstr = nullptr;
  96. if (!Texture::getConstant(w.s, sstr))
  97. return luaL_error(L, "Unknown wrap mode.");
  98. if (!Texture::getConstant(w.t, tstr))
  99. return luaL_error(L, "Unknown wrap mode.");
  100. lua_pushstring(L, sstr);
  101. lua_pushstring(L, tstr);
  102. return 2;
  103. }
  104. const luaL_Reg w_Texture_functions[] =
  105. {
  106. { "getWidth", w_Texture_getWidth },
  107. { "getHeight", w_Texture_getHeight },
  108. { "getDimensions", w_Texture_getDimensions },
  109. { "setFilter", w_Texture_setFilter },
  110. { "getFilter", w_Texture_getFilter },
  111. { "setWrap", w_Texture_setWrap },
  112. { "getWrap", w_Texture_getWrap },
  113. { 0, 0 }
  114. };
  115. extern "C" int luaopen_texture(lua_State *L)
  116. {
  117. return luax_register_type(L, Texture::type, "Texture", w_Texture_functions, nullptr);
  118. }
  119. } // graphics
  120. } // love