wrap_Window.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**
  2. * Copyright (c) 2006-2014 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_Window.h"
  21. #include "sdl/Window.h"
  22. namespace love
  23. {
  24. namespace window
  25. {
  26. static Window *instance = nullptr;
  27. int w_getDisplayCount(lua_State *L)
  28. {
  29. lua_pushinteger(L, instance->getDisplayCount());
  30. return 1;
  31. }
  32. static const char *settingName(Window::Setting setting)
  33. {
  34. const char *name = nullptr;
  35. Window::getConstant(setting, name);
  36. return name;
  37. }
  38. int w_setMode(lua_State *L)
  39. {
  40. int w = luaL_checkint(L, 1);
  41. int h = luaL_checkint(L, 2);
  42. if (lua_isnoneornil(L, 3))
  43. {
  44. luax_pushboolean(L, instance->setWindow(w, h, 0));
  45. return 1;
  46. }
  47. luaL_checktype(L, 3, LUA_TTABLE);
  48. // We want to error for invalid / misspelled window attributes.
  49. lua_pushnil(L);
  50. while (lua_next(L, 3))
  51. {
  52. if (lua_type(L, -2) != LUA_TSTRING)
  53. return luax_typerror(L, -2, "string");
  54. const char *key = luaL_checkstring(L, -2);
  55. Window::Setting setting;
  56. if (!Window::getConstant(key, setting))
  57. return luaL_error(L, "Invalid window setting: %s", key);
  58. lua_pop(L, 1);
  59. }
  60. WindowSettings settings;
  61. lua_getfield(L, 3, settingName(Window::SETTING_FULLSCREEN_TYPE));
  62. if (!lua_isnoneornil(L, -1))
  63. {
  64. const char *typestr = luaL_checkstring(L, -1);
  65. if (!Window::getConstant(typestr, settings.fstype))
  66. return luaL_error(L, "Invalid fullscreen type: %s", typestr);
  67. }
  68. else
  69. {
  70. // Default to "normal" fullscreen.
  71. settings.fstype = Window::FULLSCREEN_TYPE_NORMAL;
  72. }
  73. lua_pop(L, 1);
  74. settings.fullscreen = luax_boolflag(L, 3, settingName(Window::SETTING_FULLSCREEN), false);
  75. settings.vsync = luax_boolflag(L, 3, settingName(Window::SETTING_VSYNC), true);
  76. settings.fsaa = luax_intflag(L, 3, settingName(Window::SETTING_FSAA), 0);
  77. settings.resizable = luax_boolflag(L, 3, settingName(Window::SETTING_RESIZABLE), false);
  78. settings.minwidth = luax_intflag(L, 3, settingName(Window::SETTING_MIN_WIDTH), 1);
  79. settings.minheight = luax_intflag(L, 3, settingName(Window::SETTING_MIN_HEIGHT), 1);
  80. settings.borderless = luax_boolflag(L, 3, settingName(Window::SETTING_BORDERLESS), false);
  81. settings.centered = luax_boolflag(L, 3, settingName(Window::SETTING_CENTERED), true);
  82. settings.display = luax_intflag(L, 3, settingName(Window::SETTING_DISPLAY), 1);
  83. settings.highdpi = luax_boolflag(L, 3, settingName(Window::SETTING_HIGHDPI), false);
  84. settings.sRGB = luax_boolflag(L, 3, settingName(Window::SETTING_SRGB), false);
  85. // Display index is 1-based in Lua and 0-based internally.
  86. settings.display--;
  87. EXCEPT_GUARD(luax_pushboolean(L, instance->setWindow(w, h, &settings));)
  88. return 1;
  89. }
  90. int w_getMode(lua_State *L)
  91. {
  92. int w, h;
  93. WindowSettings settings;
  94. instance->getWindow(w, h, settings);
  95. lua_pushnumber(L, w);
  96. lua_pushnumber(L, h);
  97. lua_newtable(L);
  98. const char *fstypestr = "normal";
  99. Window::getConstant(settings.fstype, fstypestr);
  100. lua_pushstring(L, fstypestr);
  101. lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN_TYPE));
  102. luax_pushboolean(L, settings.fullscreen);
  103. lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN));
  104. luax_pushboolean(L, settings.vsync);
  105. lua_setfield(L, -2, settingName(Window::SETTING_VSYNC));
  106. lua_pushinteger(L, settings.fsaa);
  107. lua_setfield(L, -2, settingName(Window::SETTING_FSAA));
  108. luax_pushboolean(L, settings.resizable);
  109. lua_setfield(L, -2, settingName(Window::SETTING_RESIZABLE));
  110. lua_pushinteger(L, settings.minwidth);
  111. lua_setfield(L, -2, settingName(Window::SETTING_MIN_WIDTH));
  112. lua_pushinteger(L, settings.minheight);
  113. lua_setfield(L, -2, settingName(Window::SETTING_MIN_HEIGHT));
  114. luax_pushboolean(L, settings.borderless);
  115. lua_setfield(L, -2, settingName(Window::SETTING_BORDERLESS));
  116. luax_pushboolean(L, settings.centered);
  117. lua_setfield(L, -2, settingName(Window::SETTING_CENTERED));
  118. // Display index is 0-based internally and 1-based in Lua.
  119. lua_pushinteger(L, settings.display + 1);
  120. lua_setfield(L, -2, settingName(Window::SETTING_DISPLAY));
  121. luax_pushboolean(L, settings.highdpi);
  122. lua_setfield(L, -2, settingName(Window::SETTING_HIGHDPI));
  123. luax_pushboolean(L, settings.sRGB);
  124. lua_setfield(L, -2, settingName(Window::SETTING_SRGB));
  125. return 3;
  126. }
  127. int w_getFullscreenModes(lua_State *L)
  128. {
  129. int displayindex = luaL_optint(L, 1, 1) - 1;
  130. std::vector<Window::WindowSize> modes = instance->getFullscreenSizes(displayindex);
  131. lua_createtable(L, modes.size(), 0);
  132. for (size_t i = 0; i < modes.size(); i++)
  133. {
  134. lua_pushinteger(L, i+1);
  135. lua_createtable(L, 0, 2);
  136. // Inner table attribs.
  137. lua_pushinteger(L, modes[i].width);
  138. lua_setfield(L, -2, "width");
  139. lua_pushinteger(L, modes[i].height);
  140. lua_setfield(L, -2, "height");
  141. // Inner table attribs end.
  142. lua_settable(L, -3);
  143. }
  144. return 1;
  145. }
  146. int w_setFullscreen(lua_State *L)
  147. {
  148. bool fullscreen = luax_toboolean(L, 1);
  149. Window::FullscreenType fstype = Window::FULLSCREEN_TYPE_MAX_ENUM;
  150. const char *typestr = lua_isnoneornil(L, 2) ? 0 : luaL_checkstring(L, 2);
  151. if (typestr && !Window::getConstant(typestr, fstype))
  152. return luaL_error(L, "Invalid fullscreen type: %s", typestr);
  153. bool success = false;
  154. if (fstype == Window::FULLSCREEN_TYPE_MAX_ENUM)
  155. success = instance->setFullscreen(fullscreen);
  156. else
  157. success = instance->setFullscreen(fullscreen, fstype);
  158. luax_pushboolean(L, success);
  159. return 1;
  160. }
  161. int w_getFullscreen(lua_State *L)
  162. {
  163. int w, h;
  164. WindowSettings settings;
  165. instance->getWindow(w, h, settings);
  166. const char *typestr;
  167. if (!Window::getConstant(settings.fstype, typestr))
  168. luaL_error(L, "Unknown fullscreen type.");
  169. luax_pushboolean(L, settings.fullscreen);
  170. lua_pushstring(L, typestr);
  171. return 2;
  172. }
  173. int w_isCreated(lua_State *L)
  174. {
  175. luax_pushboolean(L, instance->isCreated());
  176. return 1;
  177. }
  178. int w_getWidth(lua_State *L)
  179. {
  180. lua_pushinteger(L, instance->getWidth());
  181. return 1;
  182. }
  183. int w_getHeight(lua_State *L)
  184. {
  185. lua_pushinteger(L, instance->getHeight());
  186. return 1;
  187. }
  188. int w_getDimensions(lua_State *L)
  189. {
  190. lua_pushinteger(L, instance->getWidth());
  191. lua_pushinteger(L, instance->getHeight());
  192. return 2;
  193. }
  194. int w_getDesktopDimensions(lua_State *L)
  195. {
  196. int width = 0, height = 0;
  197. int displayindex = luaL_optint(L, 1, 1) - 1;
  198. instance->getDesktopDimensions(displayindex, width, height);
  199. lua_pushinteger(L, width);
  200. lua_pushinteger(L, height);
  201. return 2;
  202. }
  203. int w_setIcon(lua_State *L)
  204. {
  205. image::ImageData *i = luax_checktype<image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
  206. luax_pushboolean(L, instance->setIcon(i));
  207. return 1;
  208. }
  209. int w_getIcon(lua_State *L)
  210. {
  211. image::ImageData *i = instance->getIcon();
  212. if (i)
  213. {
  214. i->retain();
  215. luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, i);
  216. }
  217. else
  218. lua_pushnil(L);
  219. return 1;
  220. }
  221. int w_setTitle(lua_State *L)
  222. {
  223. std::string title = luax_checkstring(L, 1);
  224. instance->setWindowTitle(title);
  225. return 0;
  226. }
  227. int w_getTitle(lua_State *L)
  228. {
  229. luax_pushstring(L, instance->getWindowTitle());
  230. return 1;
  231. }
  232. int w_hasFocus(lua_State *L)
  233. {
  234. luax_pushboolean(L, instance->hasFocus());
  235. return 1;
  236. }
  237. int w_hasMouseFocus(lua_State *L)
  238. {
  239. luax_pushboolean(L, instance->hasMouseFocus());
  240. return 1;
  241. }
  242. int w_isVisible(lua_State *L)
  243. {
  244. luax_pushboolean(L, instance->isVisible());
  245. return 1;
  246. }
  247. int w_getPixelScale(lua_State *L)
  248. {
  249. lua_pushnumber(L, instance->getPixelScale());
  250. return 1;
  251. }
  252. static const luaL_Reg functions[] =
  253. {
  254. { "getDisplayCount", w_getDisplayCount },
  255. { "setMode", w_setMode },
  256. { "getMode", w_getMode },
  257. { "getFullscreenModes", w_getFullscreenModes },
  258. { "setFullscreen", w_setFullscreen },
  259. { "getFullscreen", w_getFullscreen },
  260. { "isCreated", w_isCreated },
  261. { "getWidth", w_getWidth },
  262. { "getHeight", w_getHeight },
  263. { "getDimensions", w_getDimensions },
  264. { "getDesktopDimensions", w_getDesktopDimensions },
  265. { "setIcon", w_setIcon },
  266. { "getIcon", w_getIcon },
  267. { "setTitle", w_setTitle },
  268. { "getTitle", w_getTitle },
  269. { "hasFocus", w_hasFocus },
  270. { "hasMouseFocus", w_hasMouseFocus },
  271. { "isVisible", w_isVisible },
  272. { "getPixelScale", w_getPixelScale },
  273. { 0, 0 }
  274. };
  275. extern "C" int luaopen_love_window(lua_State *L)
  276. {
  277. EXCEPT_GUARD(instance = sdl::Window::createSingleton();)
  278. WrappedModule w;
  279. w.module = instance;
  280. w.name = "window";
  281. w.flags = MODULE_T;
  282. w.functions = functions;
  283. w.types = 0;
  284. return luax_register_module(L, w);
  285. }
  286. } // window
  287. } // love