wrap_Graphics.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. /**
  2. * Copyright (c) 2006-2013 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_Graphics.h"
  21. #include "OpenGL.h"
  22. #include "graphics/DrawQable.h"
  23. #include "image/ImageData.h"
  24. #include "font/Rasterizer.h"
  25. #include "scripts/graphics.lua.h"
  26. #include <cassert>
  27. using love::window::WindowFlags;
  28. namespace love
  29. {
  30. namespace graphics
  31. {
  32. namespace opengl
  33. {
  34. static Graphics *instance = 0;
  35. bool luax_boolflag(lua_State *L, int table_index, const char *key, bool defaultValue)
  36. {
  37. lua_getfield(L, table_index, key);
  38. bool retval;
  39. if (lua_isnoneornil(L, -1))
  40. retval = defaultValue;
  41. else
  42. retval = lua_toboolean(L, -1);
  43. lua_pop(L, 1);
  44. return retval;
  45. }
  46. int luax_intflag(lua_State *L, int table_index, const char *key, int defaultValue)
  47. {
  48. lua_getfield(L, table_index, key);
  49. int retval;
  50. if (!lua_isnumber(L, -1))
  51. retval = defaultValue;
  52. else
  53. retval = lua_tonumber(L, -1);
  54. lua_pop(L, 1);
  55. return retval;
  56. }
  57. int w_checkMode(lua_State *L)
  58. {
  59. int w = luaL_checkint(L, 1);
  60. int h = luaL_checkint(L, 2);
  61. bool fs = luax_toboolean(L, 3);
  62. luax_pushboolean(L, instance->checkMode(w, h, fs));
  63. return 1;
  64. }
  65. int w_setMode(lua_State *L)
  66. {
  67. int w = luaL_checkint(L, 1);
  68. int h = luaL_checkint(L, 2);
  69. if (lua_isnoneornil(L, 3))
  70. {
  71. luax_pushboolean(L, instance->setMode(w, h, 0));
  72. return 1;
  73. }
  74. luaL_checktype(L, 3, LUA_TTABLE);
  75. WindowFlags flags;
  76. flags.fullscreen = luax_boolflag(L, 3, "fullscreen", false);
  77. flags.vsync = luax_boolflag(L, 3, "vsync", true);
  78. flags.fsaa = luax_intflag(L, 3, "fsaa", 0);
  79. flags.resizable = luax_boolflag(L, 3, "resizable", false);
  80. flags.borderless = luax_boolflag(L, 3, "borderless", false);
  81. flags.centered = luax_boolflag(L, 3, "centered", true);
  82. luax_pushboolean(L, instance->setMode(w, h, &flags));
  83. return 1;
  84. }
  85. int w_getMode(lua_State *L)
  86. {
  87. int w, h;
  88. WindowFlags flags;
  89. instance->getMode(w, h, flags);
  90. lua_pushnumber(L, w);
  91. lua_pushnumber(L, h);
  92. lua_newtable(L);
  93. luax_pushboolean(L, flags.fullscreen);
  94. lua_setfield(L, -2, "fullscreen");
  95. luax_pushboolean(L, flags.vsync);
  96. lua_setfield(L, -2, "vsync");
  97. lua_pushnumber(L, flags.fsaa);
  98. lua_setfield(L, -2, "fsaa");
  99. luax_pushboolean(L, flags.resizable);
  100. lua_setfield(L, -2, "resizable");
  101. luax_pushboolean(L, flags.borderless);
  102. lua_setfield(L, -2, "borderless");
  103. luax_pushboolean(L, flags.centered);
  104. lua_setfield(L, -2, "centered");
  105. return 3;
  106. }
  107. int w_toggleFullscreen(lua_State *L)
  108. {
  109. luax_pushboolean(L, instance->toggleFullscreen());
  110. return 1;
  111. }
  112. int w_reset(lua_State *)
  113. {
  114. instance->reset();
  115. return 0;
  116. }
  117. int w_clear(lua_State *)
  118. {
  119. instance->clear();
  120. return 0;
  121. }
  122. int w_present(lua_State *)
  123. {
  124. instance->present();
  125. return 0;
  126. }
  127. int w_setIcon(lua_State *L)
  128. {
  129. Image *image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  130. instance->setIcon(image);
  131. return 0;
  132. }
  133. int w_setCaption(lua_State *L)
  134. {
  135. const char *str = luaL_checkstring(L, 1);
  136. instance->setCaption(str);
  137. return 0;
  138. }
  139. int w_getCaption(lua_State *L)
  140. {
  141. return instance->getCaption(L);
  142. }
  143. int w_getWidth(lua_State *L)
  144. {
  145. lua_pushnumber(L, instance->getWidth());
  146. return 1;
  147. }
  148. int w_getHeight(lua_State *L)
  149. {
  150. lua_pushnumber(L, instance->getHeight());
  151. return 1;
  152. }
  153. int w_getDimensions(lua_State *L)
  154. {
  155. lua_pushnumber(L, instance->getWidth());
  156. lua_pushnumber(L, instance->getHeight());
  157. return 2;
  158. }
  159. int w_isCreated(lua_State *L)
  160. {
  161. luax_pushboolean(L, instance->isCreated());
  162. return 1;
  163. }
  164. int w_getModes(lua_State *L)
  165. {
  166. return instance->getModes(L);
  167. }
  168. int w_setScissor(lua_State *L)
  169. {
  170. if (lua_gettop(L) == 0)
  171. {
  172. instance->setScissor();
  173. return 0;
  174. }
  175. int x = luaL_checkint(L, 1);
  176. int y = luaL_checkint(L, 2);
  177. int w = luaL_checkint(L, 3);
  178. int h = luaL_checkint(L, 4);
  179. if (w < 0 || h < 0)
  180. return luaL_error(L, "Can't set scissor with negative width and/or height.");
  181. instance->setScissor(x, y, w, h);
  182. return 0;
  183. }
  184. int w_getScissor(lua_State *L)
  185. {
  186. return instance->getScissor(L);
  187. }
  188. int w_newStencil(lua_State *L)
  189. {
  190. // just return the function
  191. if (!lua_isfunction(L, 1))
  192. return luaL_typerror(L, 1, "function");
  193. lua_settop(L, 1);
  194. return 1;
  195. }
  196. static int setStencil(lua_State *L, bool invert)
  197. {
  198. // no argument -> clear mask
  199. if (lua_isnoneornil(L, 1))
  200. {
  201. instance->discardStencil();
  202. return 0;
  203. }
  204. if (!lua_isfunction(L, 1))
  205. return luaL_typerror(L, 1, "mask");
  206. instance->defineStencil();
  207. lua_call(L, lua_gettop(L) - 1, 0); // call mask(...)
  208. instance->useStencil(invert);
  209. return 0;
  210. }
  211. int w_setStencil(lua_State *L)
  212. {
  213. return setStencil(L, false);
  214. }
  215. int w_setInvertedStencil(lua_State *L)
  216. {
  217. return setStencil(L, true);
  218. }
  219. int w_newImage(lua_State *L)
  220. {
  221. // Convert to File, if necessary.
  222. if (lua_isstring(L, 1))
  223. luax_convobj(L, 1, "filesystem", "newFile");
  224. // Convert to ImageData, if necessary.
  225. if (luax_istype(L, 1, FILESYSTEM_FILE_T))
  226. luax_convobj(L, 1, "image", "newImageData");
  227. love::image::ImageData *data = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
  228. // Create the image.
  229. Image *image = 0;
  230. try
  231. {
  232. image = instance->newImage(data);
  233. }
  234. catch(love::Exception &e)
  235. {
  236. luaL_error(L, e.what());
  237. }
  238. if (image == 0)
  239. return luaL_error(L, "Could not load image.");
  240. // Push the type.
  241. luax_newtype(L, "Image", GRAPHICS_IMAGE_T, (void *)image);
  242. return 1;
  243. }
  244. int w_newQuad(lua_State *L)
  245. {
  246. float x = (float) luaL_checknumber(L, 1);
  247. float y = (float) luaL_checknumber(L, 2);
  248. float w = (float) luaL_checknumber(L, 3);
  249. float h = (float) luaL_checknumber(L, 4);
  250. float sw = (float) luaL_checknumber(L, 5);
  251. float sh = (float) luaL_checknumber(L, 6);
  252. Quad *quad = instance->newQuad(x, y, w, h, sw, sh);
  253. luax_newtype(L, "Quad", GRAPHICS_QUAD_T, (void *)quad);
  254. return 1;
  255. }
  256. int w_newFont(lua_State *L)
  257. {
  258. Data *font_data = NULL;
  259. // Convert to File, if necessary.
  260. if (lua_isstring(L, 1))
  261. luax_convobj(L, 1, "filesystem", "newFile");
  262. // Convert to Data, if necessary.
  263. if (luax_istype(L, 1, FILESYSTEM_FILE_T))
  264. {
  265. love::filesystem::File *f = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
  266. try
  267. {
  268. font_data = f->read();
  269. }
  270. catch(love::Exception &e)
  271. {
  272. return luaL_error(L, e.what());
  273. }
  274. lua_remove(L, 1); // get rid of the file
  275. luax_newtype(L, "Data", DATA_T, (void *)font_data);
  276. lua_insert(L, 1); // put it at the bottom of the stack
  277. }
  278. // Convert to Rasterizer, if necessary.
  279. if (luax_istype(L, 1, DATA_T))
  280. {
  281. int idxs[] = {1, 2};
  282. int ret = luax_pconvobj(L, idxs, 2, "font", "newRasterizer");
  283. if (ret != 0)
  284. {
  285. if (font_data)
  286. font_data->release();
  287. return lua_error(L);
  288. }
  289. }
  290. if (font_data)
  291. font_data->release();
  292. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, "Rasterizer", FONT_RASTERIZER_T);
  293. Font *font = NULL;
  294. try
  295. {
  296. // Create the font.
  297. font = instance->newFont(rasterizer, instance->getDefaultFilter());
  298. }
  299. catch (love::Exception &e)
  300. {
  301. return luaL_error(L, e.what());
  302. }
  303. if (font == 0)
  304. return luaL_error(L, "Could not load font.");
  305. // Push the type.
  306. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void *)font);
  307. return 1;
  308. }
  309. int w_newImageFont(lua_State *L)
  310. {
  311. // filter for glyphs, defaults to linear/linear
  312. Image::Filter img_filter;
  313. bool setFilter = false;
  314. // For the filter modes..
  315. int startIndex = 2;
  316. // Convert to ImageData if necessary.
  317. if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || (luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, IMAGE_IMAGE_DATA_T)))
  318. luax_convobj(L, 1, "image", "newImageData");
  319. else if (luax_istype(L, 1, GRAPHICS_IMAGE_T))
  320. {
  321. Image *i = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  322. img_filter = i->getFilter();
  323. setFilter = true;
  324. love::image::ImageData *id = i->getData();
  325. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)id, false);
  326. lua_replace(L, 1);
  327. }
  328. // Convert to Rasterizer if necessary.
  329. if (luax_istype(L, 1, IMAGE_IMAGE_DATA_T))
  330. {
  331. int idxs[] = {1, 2};
  332. luax_convobj(L, idxs, 2, "font", "newRasterizer");
  333. startIndex = 3; // There's a glyphs args in there, move up
  334. }
  335. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, "Rasterizer", FONT_RASTERIZER_T);
  336. if (lua_isstring(L, startIndex) && lua_isstring(L, startIndex+1))
  337. {
  338. Image::FilterMode min;
  339. Image::FilterMode mag;
  340. const char *minstr = luaL_checkstring(L, startIndex);
  341. const char *magstr = luaL_checkstring(L, startIndex+1);
  342. if (!Image::getConstant(minstr, min))
  343. return luaL_error(L, "Invalid filter mode: %s", minstr);
  344. if (!Image::getConstant(magstr, mag))
  345. return luaL_error(L, "Invalid filter mode: %s", magstr);
  346. img_filter.min = min;
  347. img_filter.mag = mag;
  348. setFilter = true;
  349. }
  350. if (!setFilter)
  351. img_filter = instance->getDefaultFilter();
  352. // Create the font.
  353. Font *font = instance->newFont(rasterizer, img_filter);
  354. if (font == 0)
  355. return luaL_error(L, "Could not load font.");
  356. // Push the type.
  357. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void *)font);
  358. return 1;
  359. }
  360. int w_newSpriteBatch(lua_State *L)
  361. {
  362. Image *image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  363. int size = luaL_optint(L, 2, 1000);
  364. SpriteBatch::UsageHint usage = SpriteBatch::USAGE_DYNAMIC;
  365. if (lua_gettop(L) > 2)
  366. {
  367. if (!SpriteBatch::getConstant(luaL_checkstring(L, 3), usage))
  368. usage = SpriteBatch::USAGE_DYNAMIC;
  369. }
  370. SpriteBatch *t = NULL;
  371. try
  372. {
  373. t = instance->newSpriteBatch(image, size, usage);
  374. }
  375. catch(love::Exception &e)
  376. {
  377. return luaL_error(L, e.what());
  378. }
  379. luax_newtype(L, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T, (void *)t);
  380. return 1;
  381. }
  382. int w_newParticleSystem(lua_State *L)
  383. {
  384. Image *image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  385. int size = luaL_checkint(L, 2);
  386. ParticleSystem *t = instance->newParticleSystem(image, size);
  387. luax_newtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, (void *)t);
  388. return 1;
  389. }
  390. int w_newCanvas(lua_State *L)
  391. {
  392. // check if width and height are given. else default to screen dimensions.
  393. int width = luaL_optint(L, 1, instance->getWidth());
  394. int height = luaL_optint(L, 2, instance->getHeight());
  395. const char *str = luaL_optstring(L, 3, "normal");
  396. Canvas::TextureType texture_type;
  397. if (!Canvas::getConstant(str, texture_type))
  398. return luaL_error(L, "Invalid canvas type: %s", str);
  399. Canvas *canvas = NULL;
  400. try
  401. {
  402. canvas = instance->newCanvas(width, height, texture_type);
  403. }
  404. catch(Exception &e)
  405. {
  406. return luaL_error(L, e.what());
  407. }
  408. if (NULL == canvas)
  409. return luaL_error(L, "Canvas not created, but no error thrown. I don't even...");
  410. luax_newtype(L, "Canvas", GRAPHICS_CANVAS_T, (void *)canvas);
  411. return 1;
  412. }
  413. int w_newShader(lua_State *L)
  414. {
  415. if (!Shader::isSupported())
  416. return luaL_error(L, "Sorry, your graphics card does not support shaders.");
  417. // clamp stack to 2 elements
  418. lua_settop(L, 2);
  419. // read any filepath arguments
  420. for (int i = 1; i <= 2; i++)
  421. {
  422. if (!lua_isstring(L, i))
  423. continue;
  424. // call love.filesystem.isFile(arg_i)
  425. luax_getfunction(L, "filesystem", "isFile");
  426. lua_pushvalue(L, i);
  427. lua_call(L, 1, 1);
  428. bool isFile = luax_toboolean(L, -1);
  429. lua_pop(L, 1);
  430. if (isFile)
  431. {
  432. luax_getfunction(L, "filesystem", "read");
  433. lua_pushvalue(L, i);
  434. lua_call(L, 1, 1);
  435. lua_replace(L, i);
  436. }
  437. }
  438. bool has_arg1 = lua_isstring(L, 1);
  439. bool has_arg2 = lua_isstring(L, 2);
  440. // require at least one string argument
  441. if (!(has_arg1 || has_arg2))
  442. luaL_checkstring(L, 1);
  443. luax_getfunction(L, "graphics", "_shaderCodeToGLSL");
  444. // push vertexcode and pixelcode strings to the top of the stack
  445. lua_pushvalue(L, 1);
  446. lua_pushvalue(L, 2);
  447. // call effectCodeToGLSL, returned values will be at the top of the stack
  448. if (lua_pcall(L, 2, 2, 0) != 0)
  449. return luaL_error(L, "%s", lua_tostring(L, -1));
  450. Shader::ShaderSources sources;
  451. // vertex shader code
  452. if (lua_isstring(L, -2))
  453. {
  454. std::string vertexcode(luaL_checkstring(L, -2));
  455. sources[Shader::TYPE_VERTEX] = vertexcode;
  456. }
  457. else if (has_arg1 && has_arg2)
  458. return luaL_error(L, "Could not parse vertex shader code (missing 'position' function?)");
  459. // pixel shader code
  460. if (lua_isstring(L, -1))
  461. {
  462. std::string pixelcode(luaL_checkstring(L, -1));
  463. sources[Shader::TYPE_PIXEL] = pixelcode;
  464. }
  465. else if (has_arg1 && has_arg2)
  466. return luaL_error(L, "Could not parse pixel shader code (missing 'effect' function?)");
  467. if (sources.empty())
  468. {
  469. // Original args had source code, but effectCodeToGLSL couldn't translate it
  470. for (int i = 1; i <= 2; i++)
  471. {
  472. if (lua_isstring(L, i))
  473. return luaL_argerror(L, i, "missing 'position' or 'effect' function?");
  474. }
  475. }
  476. try
  477. {
  478. Shader *shader = instance->newShader(sources);
  479. luax_newtype(L, "Shader", GRAPHICS_SHADER_T, (void *)shader);
  480. }
  481. catch (const love::Exception &e)
  482. {
  483. // memory is freed in Graphics::newShader
  484. luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
  485. lua_pushstring(L, e.what());
  486. lua_pcall(L, 1, 1, 0);
  487. const char *err = lua_tostring(L, -1);
  488. return luaL_error(L, "%s", err);
  489. }
  490. return 1;
  491. }
  492. int w_setColor(lua_State *L)
  493. {
  494. Color c;
  495. if (lua_istable(L, 1))
  496. {
  497. for (int i = 1; i <= 4; i++)
  498. lua_rawgeti(L, 1, i);
  499. c.r = (unsigned char)luaL_checkint(L, -4);
  500. c.g = (unsigned char)luaL_checkint(L, -3);
  501. c.b = (unsigned char)luaL_checkint(L, -2);
  502. c.a = (unsigned char)luaL_optint(L, -1, 255);
  503. lua_pop(L, 4);
  504. }
  505. else
  506. {
  507. c.r = (unsigned char)luaL_checkint(L, 1);
  508. c.g = (unsigned char)luaL_checkint(L, 2);
  509. c.b = (unsigned char)luaL_checkint(L, 3);
  510. c.a = (unsigned char)luaL_optint(L, 4, 255);
  511. }
  512. instance->setColor(c);
  513. return 0;
  514. }
  515. int w_getColor(lua_State *L)
  516. {
  517. Color c = instance->getColor();
  518. lua_pushinteger(L, c.r);
  519. lua_pushinteger(L, c.g);
  520. lua_pushinteger(L, c.b);
  521. lua_pushinteger(L, c.a);
  522. return 4;
  523. }
  524. int w_setBackgroundColor(lua_State *L)
  525. {
  526. Color c;
  527. if (lua_istable(L, 1))
  528. {
  529. for (int i = 1; i <= 4; i++)
  530. lua_rawgeti(L, 1, i);
  531. c.r = (unsigned char)luaL_checkint(L, -4);
  532. c.g = (unsigned char)luaL_checkint(L, -3);
  533. c.b = (unsigned char)luaL_checkint(L, -2);
  534. c.a = (unsigned char)luaL_optint(L, -1, 255);
  535. lua_pop(L, 4);
  536. }
  537. else
  538. {
  539. c.r = (unsigned char)luaL_checkint(L, 1);
  540. c.g = (unsigned char)luaL_checkint(L, 2);
  541. c.b = (unsigned char)luaL_checkint(L, 3);
  542. c.a = (unsigned char)luaL_optint(L, 4, 255);
  543. }
  544. instance->setBackgroundColor(c);
  545. return 0;
  546. }
  547. int w_getBackgroundColor(lua_State *L)
  548. {
  549. Color c = instance->getBackgroundColor();
  550. lua_pushinteger(L, c.r);
  551. lua_pushinteger(L, c.g);
  552. lua_pushinteger(L, c.b);
  553. lua_pushinteger(L, c.a);
  554. return 4;
  555. }
  556. int w_setFont(lua_State *L)
  557. {
  558. Font *font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
  559. instance->setFont(font);
  560. return 0;
  561. }
  562. int w_getFont(lua_State *L)
  563. {
  564. Font *f = instance->getFont();
  565. if (f == 0)
  566. return 0;
  567. f->retain();
  568. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void *)f);
  569. return 1;
  570. }
  571. int w_setColorMask(lua_State *L)
  572. {
  573. bool mask[4];
  574. for (int i = 0; i < 4; i++)
  575. mask[i] = luax_toboolean(L, i + 1);
  576. // r, g, b, a
  577. instance->setColorMask(mask[0], mask[1], mask[2], mask[3]);
  578. return 0;
  579. }
  580. int w_getColorMask(lua_State *L)
  581. {
  582. const bool *mask = instance->getColorMask();
  583. for (int i = 0; i < 4; i++)
  584. luax_pushboolean(L, mask[i]);
  585. return 4;
  586. }
  587. int w_setBlendMode(lua_State *L)
  588. {
  589. Graphics::BlendMode mode;
  590. const char *str = luaL_checkstring(L, 1);
  591. if (!Graphics::getConstant(str, mode))
  592. return luaL_error(L, "Invalid blend mode: %s", str);
  593. try
  594. {
  595. instance->setBlendMode(mode);
  596. }
  597. catch(love::Exception &e)
  598. {
  599. return luaL_error(L, e.what());
  600. }
  601. return 0;
  602. }
  603. int w_getBlendMode(lua_State *L)
  604. {
  605. try
  606. {
  607. Graphics::BlendMode mode = instance->getBlendMode();
  608. const char *str;
  609. if (!Graphics::getConstant(mode, str))
  610. return luaL_error(L, "Unknown blend mode");
  611. lua_pushstring(L, str);
  612. return 1;
  613. }
  614. catch (love::Exception &e)
  615. {
  616. return luaL_error(L, "%s", e.what());
  617. }
  618. }
  619. int w_setDefaultFilter(lua_State *L)
  620. {
  621. Image::FilterMode min;
  622. Image::FilterMode mag;
  623. const char *minstr = luaL_checkstring(L, 1);
  624. const char *magstr = luaL_optstring(L, 2, minstr);
  625. if (!Image::getConstant(minstr, min))
  626. return luaL_error(L, "Invalid filter mode: %s", minstr);
  627. if (!Image::getConstant(magstr, mag))
  628. return luaL_error(L, "Invalid filter mode: %s", magstr);
  629. float anisotropy = (float) luaL_optnumber(L, 3, 1.0);
  630. Image::Filter f;
  631. f.min = min;
  632. f.mag = mag;
  633. f.anisotropy = anisotropy;
  634. instance->setDefaultFilter(f);
  635. return 0;
  636. }
  637. int w_getDefaultFilter(lua_State *L)
  638. {
  639. const Image::Filter &f = instance->getDefaultFilter();
  640. const char *minstr;
  641. const char *magstr;
  642. if (!Image::getConstant(f.min, minstr))
  643. return luaL_error(L, "Unknown minification filter mode");
  644. if (!Image::getConstant(f.mag, magstr))
  645. return luaL_error(L, "Unknown magnification filter mode");
  646. lua_pushstring(L, minstr);
  647. lua_pushstring(L, magstr);
  648. lua_pushnumber(L, f.anisotropy);
  649. return 3;
  650. }
  651. int w_setDefaultMipmapFilter(lua_State *L)
  652. {
  653. Image::FilterMode filter = Image::FILTER_NONE;
  654. if (!lua_isnoneornil(L, 1))
  655. {
  656. const char *str = luaL_checkstring(L, 1);
  657. if (!Image::getConstant(str, filter))
  658. return luaL_error(L, "Invalid filter mode: %s", str);
  659. }
  660. float sharpness = luaL_optnumber(L, 2, 0);
  661. instance->setDefaultMipmapFilter(filter, sharpness);
  662. return 0;
  663. }
  664. int w_getDefaultMipmapFilter(lua_State *L)
  665. {
  666. Image::FilterMode filter;
  667. float sharpness;
  668. instance->getDefaultMipmapFilter(&filter, &sharpness);
  669. const char *str;
  670. if (Image::getConstant(filter, str))
  671. lua_pushstring(L, str);
  672. else
  673. lua_pushnil(L);
  674. lua_pushnumber(L, sharpness);
  675. return 2;
  676. }
  677. int w_setLineWidth(lua_State *L)
  678. {
  679. float width = (float)luaL_checknumber(L, 1);
  680. instance->setLineWidth(width);
  681. return 0;
  682. }
  683. int w_setLineStyle(lua_State *L)
  684. {
  685. Graphics::LineStyle style;
  686. const char *str = luaL_checkstring(L, 1);
  687. if (!Graphics::getConstant(str, style))
  688. return luaL_error(L, "Invalid line style: %s", str);
  689. instance->setLineStyle(style);
  690. return 0;
  691. }
  692. int w_setLine(lua_State *L)
  693. {
  694. float width = (float)luaL_checknumber(L, 1);
  695. Graphics::LineStyle style = Graphics::LINE_SMOOTH;
  696. if (lua_gettop(L) >= 2)
  697. {
  698. const char *str = luaL_checkstring(L, 2);
  699. if (!Graphics::getConstant(str, style))
  700. return luaL_error(L, "Invalid line style: %s", str);
  701. }
  702. instance->setLine(width, style);
  703. return 0;
  704. }
  705. int w_getLineWidth(lua_State *L)
  706. {
  707. lua_pushnumber(L, instance->getLineWidth());
  708. return 1;
  709. }
  710. int w_getLineStyle(lua_State *L)
  711. {
  712. Graphics::LineStyle style = instance->getLineStyle();
  713. const char *str;
  714. if (!Graphics::getConstant(style, str))
  715. return luaL_error(L, "Unknown line style");
  716. lua_pushstring(L, str);
  717. return 1;
  718. }
  719. int w_setPointSize(lua_State *L)
  720. {
  721. float size = (float)luaL_checknumber(L, 1);
  722. instance->setPointSize(size);
  723. return 0;
  724. }
  725. int w_setPointStyle(lua_State *L)
  726. {
  727. Graphics::PointStyle style;
  728. const char *str = luaL_checkstring(L, 1);
  729. if (!Graphics::getConstant(str, style))
  730. return luaL_error(L, "Invalid point style: %s", str);
  731. instance->setPointStyle(style);
  732. return 0;
  733. }
  734. int w_setPoint(lua_State *L)
  735. {
  736. float size = (float)luaL_checknumber(L, 1);
  737. Graphics::PointStyle style = Graphics::POINT_SMOOTH;
  738. if (lua_gettop(L) >= 2)
  739. {
  740. const char *str = luaL_checkstring(L, 2);
  741. if (!Graphics::getConstant(str, style))
  742. return luaL_error(L, "Invalid point style: %s", str);
  743. }
  744. instance->setPoint(size, style);
  745. return 0;
  746. }
  747. int w_getPointSize(lua_State *L)
  748. {
  749. lua_pushnumber(L, instance->getPointSize());
  750. return 1;
  751. }
  752. int w_getPointStyle(lua_State *L)
  753. {
  754. Graphics::PointStyle style = instance->getPointStyle();
  755. const char *str;
  756. if (!Graphics::getConstant(style, str))
  757. return luaL_error(L, "Unknown point style");
  758. lua_pushstring(L, str);
  759. return 1;
  760. }
  761. int w_getMaxPointSize(lua_State *L)
  762. {
  763. lua_pushnumber(L, instance->getMaxPointSize());
  764. return 1;
  765. }
  766. int w_newScreenshot(lua_State *L)
  767. {
  768. love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  769. bool copyAlpha = luax_optboolean(L, 1, false);
  770. love::image::ImageData *i = instance->newScreenshot(image, copyAlpha);
  771. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)i);
  772. return 1;
  773. }
  774. int w_setCanvas(lua_State *L)
  775. {
  776. // discard stencil testing
  777. instance->discardStencil();
  778. // called with none -> reset to default buffer
  779. // nil is an error, to help people with typoes
  780. if (lua_isnone(L,1))
  781. {
  782. Canvas::bindDefaultCanvas();
  783. return 0;
  784. }
  785. Canvas *canvas = luax_checkcanvas(L, 1);
  786. try
  787. {
  788. // this unbinds the previous fbo
  789. canvas->startGrab();
  790. }
  791. catch (love::Exception &e)
  792. {
  793. return luaL_error(L, "%s", e.what());
  794. }
  795. return 0;
  796. }
  797. int w_setCanvases(lua_State *L)
  798. {
  799. // discard stencil testing
  800. instance->discardStencil();
  801. // called with none -> reset to default buffer
  802. // nil is an error, to help people with typoes
  803. if (lua_isnone(L,1))
  804. {
  805. Canvas::bindDefaultCanvas();
  806. return 0;
  807. }
  808. bool is_table = lua_istable(L, 1);
  809. std::vector<Canvas *> attachments;
  810. Canvas *canvas = 0;
  811. if (is_table)
  812. {
  813. // grab the first canvas in the array and attach the rest
  814. lua_rawgeti(L, 1, 1);
  815. canvas = luax_checkcanvas(L, -1);
  816. lua_pop(L, 1);
  817. for (int i = 2; i <= lua_objlen(L, 1); i++)
  818. {
  819. lua_rawgeti(L, 1, i);
  820. attachments.push_back(luax_checkcanvas(L, -1));
  821. lua_pop(L, 1);
  822. }
  823. }
  824. else
  825. {
  826. canvas = luax_checkcanvas(L, 1);
  827. for (int i = 2; i <= lua_gettop(L); i++)
  828. attachments.push_back(luax_checkcanvas(L, i));
  829. }
  830. try
  831. {
  832. canvas->startGrab(attachments);
  833. }
  834. catch (love::Exception &e)
  835. {
  836. return luaL_error(L, "%s", e.what());
  837. }
  838. return 0;
  839. }
  840. int w_getCanvas(lua_State *L)
  841. {
  842. Canvas *canvas = Canvas::current;
  843. int n = 1;
  844. if (canvas)
  845. {
  846. canvas->retain();
  847. luax_newtype(L, "Canvas", GRAPHICS_CANVAS_T, (void *) canvas);
  848. const std::vector<Canvas *> &attachments = canvas->getAttachedCanvases();
  849. for (size_t i = 0; i < attachments.size(); i++)
  850. {
  851. attachments[i]->retain();
  852. luax_newtype(L, "Canvas", GRAPHICS_CANVAS_T, (void *) attachments[i]);
  853. n++;
  854. }
  855. }
  856. else
  857. lua_pushnil(L);
  858. return n;
  859. }
  860. int w_setShader(lua_State *L)
  861. {
  862. if (lua_isnoneornil(L,1))
  863. {
  864. Shader::detach();
  865. return 0;
  866. }
  867. Shader *shader = luax_checkshader(L, 1);
  868. shader->attach();
  869. return 0;
  870. }
  871. int w_getShader(lua_State *L)
  872. {
  873. Shader *shader = Shader::current;
  874. if (shader)
  875. {
  876. shader->retain();
  877. luax_newtype(L, "Shader", GRAPHICS_SHADER_T, (void *) shader);
  878. }
  879. else
  880. lua_pushnil(L);
  881. return 1;
  882. }
  883. int w_isSupported(lua_State *L)
  884. {
  885. bool supported = true;
  886. size_t len = lua_gettop(L);
  887. Graphics::Support support;
  888. for (unsigned int i = 1; i <= len; i++)
  889. {
  890. const char *str = luaL_checkstring(L, i);
  891. if (!Graphics::getConstant(str, support))
  892. {
  893. supported = false;
  894. break;
  895. }
  896. switch (support)
  897. {
  898. case Graphics::SUPPORT_CANVAS:
  899. if (!Canvas::isSupported())
  900. supported = false;
  901. break;
  902. case Graphics::SUPPORT_HDR_CANVAS:
  903. if (!Canvas::isHDRSupported())
  904. supported = false;
  905. break;
  906. case Graphics::SUPPORT_MULTI_CANVAS:
  907. if (!Canvas::isMultiCanvasSupported())
  908. supported = false;
  909. break;
  910. case Graphics::SUPPORT_SHADER:
  911. if (!Shader::isSupported())
  912. supported = false;
  913. break;
  914. case Graphics::SUPPORT_NPOT:
  915. if (!Image::hasNpot())
  916. supported = false;
  917. break;
  918. case Graphics::SUPPORT_SUBTRACTIVE:
  919. if (!((GLEE_VERSION_1_4 || GLEE_ARB_imaging) || (GLEE_EXT_blend_minmax && GLEE_EXT_blend_subtract)))
  920. supported = false;
  921. break;
  922. case Graphics::SUPPORT_MIPMAP:
  923. if (!Image::hasMipmapSupport())
  924. supported = false;
  925. break;
  926. default:
  927. supported = false;
  928. }
  929. if (!supported)
  930. break;
  931. }
  932. lua_pushboolean(L, supported);
  933. return 1;
  934. }
  935. /**
  936. * Draws an Image at the specified coordinates, with rotation and
  937. * scaling along both axes.
  938. * @param x The x-coordinate.
  939. * @param y The y-coordinate.
  940. * @param angle The amount of rotation.
  941. * @param sx The scale factor along the x-axis. (1 = normal).
  942. * @param sy The scale factor along the y-axis. (1 = normal).
  943. * @param ox The offset along the x-axis.
  944. * @param oy The offset along the y-axis.
  945. * @param kx Shear along the x-axis.
  946. * @param ky Shear along the y-axis.
  947. **/
  948. int w_draw(lua_State *L)
  949. {
  950. Drawable *drawable = luax_checktype<Drawable>(L, 1, "Drawable", GRAPHICS_DRAWABLE_T);
  951. float x = (float)luaL_optnumber(L, 2, 0.0f);
  952. float y = (float)luaL_optnumber(L, 3, 0.0f);
  953. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  954. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  955. float sy = (float)luaL_optnumber(L, 6, sx);
  956. float ox = (float)luaL_optnumber(L, 7, 0);
  957. float oy = (float)luaL_optnumber(L, 8, 0);
  958. float kx = (float)luaL_optnumber(L, 9, 0);
  959. float ky = (float)luaL_optnumber(L, 10, 0);
  960. drawable->draw(x, y, angle, sx, sy, ox, oy, kx, ky);
  961. return 0;
  962. }
  963. /**
  964. * Draws an Quad of a DrawQable at the specified coordinates,
  965. * with rotation and scaling along both axes.
  966. *
  967. * @param q The Quad to draw.
  968. * @param x The x-coordinate.
  969. * @param y The y-coordinate.
  970. * @param angle The amount of rotation.
  971. * @param sx The scale factor along the x-axis. (1 = normal).
  972. * @param sy The scale factor along the y-axis. (1 = normal).
  973. * @param ox The offset along the x-axis.
  974. * @param oy The offset along the y-axis.
  975. * @param kx Shear along the x-axis.
  976. * @param ky Shear along the y-axis.
  977. **/
  978. int w_drawq(lua_State *L)
  979. {
  980. DrawQable *dq = luax_checktype<DrawQable>(L, 1, "DrawQable", GRAPHICS_DRAWQABLE_T);
  981. Quad *q = luax_checkquad(L, 2);
  982. float x = (float)luaL_checknumber(L, 3);
  983. float y = (float)luaL_checknumber(L, 4);
  984. float angle = (float)luaL_optnumber(L, 5, 0);
  985. float sx = (float)luaL_optnumber(L, 6, 1);
  986. float sy = (float)luaL_optnumber(L, 7, sx);
  987. float ox = (float)luaL_optnumber(L, 8, 0);
  988. float oy = (float)luaL_optnumber(L, 9, 0);
  989. float kx = (float)luaL_optnumber(L, 10, 0);
  990. float ky = (float)luaL_optnumber(L, 11, 0);
  991. dq->drawq(q, x, y, angle, sx, sy, ox, oy, kx, ky);
  992. return 0;
  993. }
  994. int w_print(lua_State *L)
  995. {
  996. const char *str = luaL_checkstring(L, 1);
  997. float x = (float)luaL_checknumber(L, 2);
  998. float y = (float)luaL_checknumber(L, 3);
  999. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  1000. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  1001. float sy = (float)luaL_optnumber(L, 6, sx);
  1002. float ox = (float)luaL_optnumber(L, 7, 0.0f);
  1003. float oy = (float)luaL_optnumber(L, 8, 0.0f);
  1004. float kx = (float)luaL_optnumber(L, 9, 0.0f);
  1005. float ky = (float)luaL_optnumber(L, 10, 0.0f);
  1006. try
  1007. {
  1008. instance->print(str, x, y, angle, sx, sy, ox, oy, kx,ky);
  1009. }
  1010. catch(love::Exception e)
  1011. {
  1012. return luaL_error(L, "Decoding error: %s", e.what());
  1013. }
  1014. return 0;
  1015. }
  1016. int w_printf(lua_State *L)
  1017. {
  1018. const char *str = luaL_checkstring(L, 1);
  1019. float x = (float)luaL_checknumber(L, 2);
  1020. float y = (float)luaL_checknumber(L, 3);
  1021. float wrap = (float)luaL_checknumber(L, 4);
  1022. float angle = 0.0f;
  1023. float sx = 1.0f, sy = 1.0f;
  1024. float ox = 0.0f, oy = 0.0f;
  1025. float kx = 0.0f, ky = 0.0f;
  1026. Graphics::AlignMode align = Graphics::ALIGN_LEFT;
  1027. if (lua_gettop(L) >= 5)
  1028. {
  1029. if (!lua_isnil(L, 5))
  1030. {
  1031. const char *str = luaL_checkstring(L, 5);
  1032. if (!Graphics::getConstant(str, align))
  1033. return luaL_error(L, "Incorrect alignment: %s", str);
  1034. }
  1035. angle = (float) luaL_optnumber(L, 6, 0.0f);
  1036. sx = (float) luaL_optnumber(L, 7, 1.0f);
  1037. sy = (float) luaL_optnumber(L, 8, sx);
  1038. ox = (float) luaL_optnumber(L, 9, 0.0f);
  1039. oy = (float) luaL_optnumber(L, 10, 0.0f);
  1040. kx = (float) luaL_optnumber(L, 11, 0.0f);
  1041. ky = (float) luaL_optnumber(L, 12, 0.0f);
  1042. }
  1043. try
  1044. {
  1045. instance->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky);
  1046. }
  1047. catch(love::Exception e)
  1048. {
  1049. return luaL_error(L, "Decoding error: %s", e.what());
  1050. }
  1051. return 0;
  1052. }
  1053. int w_point(lua_State *L)
  1054. {
  1055. float x = (float)luaL_checknumber(L, 1);
  1056. float y = (float)luaL_checknumber(L, 2);
  1057. instance->point(x, y);
  1058. return 0;
  1059. }
  1060. int w_line(lua_State *L)
  1061. {
  1062. int args = lua_gettop(L);
  1063. bool is_table = false;
  1064. if (args == 1 && lua_istable(L, 1))
  1065. {
  1066. args = lua_objlen(L, 1);
  1067. is_table = true;
  1068. }
  1069. if (args % 2 != 0)
  1070. return luaL_error(L, "Number of vertices must be a multiple of two");
  1071. else if (args < 4)
  1072. return luaL_error(L, "Need at least two vertices to draw a line");
  1073. float *coords = new float[args];
  1074. if (is_table)
  1075. {
  1076. for (int i = 0; i < args; ++i)
  1077. {
  1078. lua_rawgeti(L, 1, i + 1);
  1079. coords[i] = luax_tofloat(L, -1);
  1080. lua_pop(L, 1);
  1081. }
  1082. }
  1083. else
  1084. {
  1085. for (int i = 0; i < args; ++i)
  1086. coords[i] = luax_tofloat(L, i + 1);
  1087. }
  1088. instance->polyline(coords, args);
  1089. delete[] coords;
  1090. return 0;
  1091. }
  1092. int w_rectangle(lua_State *L)
  1093. {
  1094. Graphics::DrawMode mode;
  1095. const char *str = luaL_checkstring(L, 1);
  1096. if (!Graphics::getConstant(str, mode))
  1097. return luaL_error(L, "Incorrect draw mode %s", str);
  1098. float x = (float)luaL_checknumber(L, 2);
  1099. float y = (float)luaL_checknumber(L, 3);
  1100. float w = (float)luaL_checknumber(L, 4);
  1101. float h = (float)luaL_checknumber(L, 5);
  1102. instance->rectangle(mode, x, y, w, h);
  1103. return 0;
  1104. }
  1105. int w_circle(lua_State *L)
  1106. {
  1107. Graphics::DrawMode mode;
  1108. const char *str = luaL_checkstring(L, 1);
  1109. if (!Graphics::getConstant(str, mode))
  1110. return luaL_error(L, "Incorrect draw mode %s", str);
  1111. float x = (float)luaL_checknumber(L, 2);
  1112. float y = (float)luaL_checknumber(L, 3);
  1113. float radius = (float)luaL_checknumber(L, 4);
  1114. int points;
  1115. if (lua_isnoneornil(L, 5))
  1116. points = radius > 10 ? (int)(radius) : 10;
  1117. else
  1118. points = luaL_checkint(L, 5);
  1119. instance->circle(mode, x, y, radius, points);
  1120. return 0;
  1121. }
  1122. int w_arc(lua_State *L)
  1123. {
  1124. Graphics::DrawMode mode;
  1125. const char *str = luaL_checkstring(L, 1);
  1126. if (!Graphics::getConstant(str, mode))
  1127. return luaL_error(L, "Incorrect draw mode %s", str);
  1128. float x = (float)luaL_checknumber(L, 2);
  1129. float y = (float)luaL_checknumber(L, 3);
  1130. float radius = (float)luaL_checknumber(L, 4);
  1131. float angle1 = (float)luaL_checknumber(L, 5);
  1132. float angle2 = (float)luaL_checknumber(L, 6);
  1133. int points;
  1134. if (lua_isnoneornil(L, 7))
  1135. points = radius > 10 ? (int)(radius) : 10;
  1136. else
  1137. points = luaL_checkint(L, 7);
  1138. instance->arc(mode, x, y, radius, angle1, angle2, points);
  1139. return 0;
  1140. }
  1141. int w_polygon(lua_State *L)
  1142. {
  1143. int args = lua_gettop(L) - 1;
  1144. Graphics::DrawMode mode;
  1145. const char *str = luaL_checkstring(L, 1);
  1146. if (!Graphics::getConstant(str, mode))
  1147. return luaL_error(L, "Invalid draw mode: %s", str);
  1148. bool is_table = false;
  1149. float *coords;
  1150. if (args == 1 && lua_istable(L, 2))
  1151. {
  1152. args = lua_objlen(L, 2);
  1153. is_table = true;
  1154. }
  1155. if (args % 2 != 0)
  1156. return luaL_error(L, "Number of vertices must be a multiple of two");
  1157. else if (args < 6)
  1158. return luaL_error(L, "Need at least three vertices to draw a polygon");
  1159. // fetch coords
  1160. coords = new float[args + 2];
  1161. if (is_table)
  1162. {
  1163. for (int i = 0; i < args; ++i)
  1164. {
  1165. lua_rawgeti(L, 2, i + 1);
  1166. coords[i] = luax_tofloat(L, -1);
  1167. lua_pop(L, 1);
  1168. }
  1169. }
  1170. else
  1171. {
  1172. for (int i = 0; i < args; ++i)
  1173. coords[i] = luax_tofloat(L, i + 2);
  1174. }
  1175. // make a closed loop
  1176. coords[args] = coords[0];
  1177. coords[args+1] = coords[1];
  1178. instance->polygon(mode, coords, args+2);
  1179. delete[] coords;
  1180. return 0;
  1181. }
  1182. int w_push(lua_State *L)
  1183. {
  1184. try
  1185. {
  1186. instance->push();
  1187. }
  1188. catch(love::Exception e)
  1189. {
  1190. return luaL_error(L, e.what());
  1191. }
  1192. return 0;
  1193. }
  1194. int w_pop(lua_State *L)
  1195. {
  1196. try
  1197. {
  1198. instance->pop();
  1199. }
  1200. catch(love::Exception e)
  1201. {
  1202. return luaL_error(L, e.what());
  1203. }
  1204. return 0;
  1205. }
  1206. int w_rotate(lua_State *L)
  1207. {
  1208. float deg = (float)luaL_checknumber(L, 1);
  1209. instance->rotate(deg);
  1210. return 0;
  1211. }
  1212. int w_scale(lua_State *L)
  1213. {
  1214. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  1215. float sy = (float)luaL_optnumber(L, 2, sx);
  1216. instance->scale(sx, sy);
  1217. return 0;
  1218. }
  1219. int w_translate(lua_State *L)
  1220. {
  1221. float x = (float)luaL_checknumber(L, 1);
  1222. float y = (float)luaL_checknumber(L, 2);
  1223. instance->translate(x, y);
  1224. return 0;
  1225. }
  1226. int w_shear(lua_State *L)
  1227. {
  1228. float kx = (float)luaL_checknumber(L, 1);
  1229. float ky = (float)luaL_checknumber(L, 2);
  1230. instance->shear(kx, ky);
  1231. return 0;
  1232. }
  1233. int w_hasFocus(lua_State *L)
  1234. {
  1235. luax_pushboolean(L, instance->hasFocus());
  1236. return 1;
  1237. }
  1238. // List of functions to wrap.
  1239. static const luaL_Reg functions[] =
  1240. {
  1241. { "checkMode", w_checkMode },
  1242. { "setMode", w_setMode },
  1243. { "getMode", w_getMode },
  1244. { "toggleFullscreen", w_toggleFullscreen },
  1245. { "reset", w_reset },
  1246. { "clear", w_clear },
  1247. { "present", w_present },
  1248. { "newImage", w_newImage },
  1249. { "newQuad", w_newQuad },
  1250. { "newFont", w_newFont },
  1251. { "newImageFont", w_newImageFont },
  1252. { "newSpriteBatch", w_newSpriteBatch },
  1253. { "newParticleSystem", w_newParticleSystem },
  1254. { "newCanvas", w_newCanvas },
  1255. { "newShader", w_newShader },
  1256. { "setColor", w_setColor },
  1257. { "getColor", w_getColor },
  1258. { "setBackgroundColor", w_setBackgroundColor },
  1259. { "getBackgroundColor", w_getBackgroundColor },
  1260. { "setFont", w_setFont },
  1261. { "getFont", w_getFont },
  1262. { "setColorMask", w_setColorMask },
  1263. { "getColorMask", w_getColorMask },
  1264. { "setBlendMode", w_setBlendMode },
  1265. { "getBlendMode", w_getBlendMode },
  1266. { "setDefaultFilter", w_setDefaultFilter },
  1267. { "getDefaultFilter", w_getDefaultFilter },
  1268. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  1269. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  1270. { "setLineWidth", w_setLineWidth },
  1271. { "setLineStyle", w_setLineStyle },
  1272. { "setLine", w_setLine },
  1273. { "getLineWidth", w_getLineWidth },
  1274. { "getLineStyle", w_getLineStyle },
  1275. { "setPointSize", w_setPointSize },
  1276. { "setPointStyle", w_setPointStyle },
  1277. { "setPoint", w_setPoint },
  1278. { "getPointSize", w_getPointSize },
  1279. { "getPointStyle", w_getPointStyle },
  1280. { "getMaxPointSize", w_getMaxPointSize },
  1281. { "newScreenshot", w_newScreenshot },
  1282. { "setCanvas", w_setCanvas },
  1283. { "setCanvases", w_setCanvases },
  1284. { "getCanvas", w_getCanvas },
  1285. { "getCanvases", w_getCanvas },
  1286. { "setShader", w_setShader },
  1287. { "getShader", w_getShader },
  1288. { "isSupported", w_isSupported },
  1289. { "draw", w_draw },
  1290. { "drawq", w_drawq },
  1291. { "print", w_print },
  1292. { "printf", w_printf },
  1293. { "setCaption", w_setCaption },
  1294. { "getCaption", w_getCaption },
  1295. { "setIcon", w_setIcon },
  1296. { "getWidth", w_getWidth },
  1297. { "getHeight", w_getHeight },
  1298. { "getDimensions", w_getDimensions },
  1299. { "isCreated", w_isCreated },
  1300. { "getModes", w_getModes },
  1301. { "setScissor", w_setScissor },
  1302. { "getScissor", w_getScissor },
  1303. { "newStencil", w_newStencil },
  1304. { "setStencil", w_setStencil },
  1305. { "setInvertedStencil", w_setInvertedStencil },
  1306. { "point", w_point },
  1307. { "line", w_line },
  1308. { "rectangle", w_rectangle },
  1309. { "circle", w_circle },
  1310. { "arc", w_arc },
  1311. { "polygon", w_polygon },
  1312. { "push", w_push },
  1313. { "pop", w_pop },
  1314. { "rotate", w_rotate },
  1315. { "scale", w_scale },
  1316. { "translate", w_translate },
  1317. { "shear", w_shear },
  1318. { "hasFocus", w_hasFocus },
  1319. { 0, 0 }
  1320. };
  1321. // Types for this module.
  1322. static const lua_CFunction types[] =
  1323. {
  1324. luaopen_font,
  1325. luaopen_image,
  1326. luaopen_quad,
  1327. luaopen_spritebatch,
  1328. luaopen_particlesystem,
  1329. luaopen_canvas,
  1330. luaopen_shader,
  1331. 0
  1332. };
  1333. extern "C" int luaopen_love_graphics(lua_State *L)
  1334. {
  1335. if (instance == 0)
  1336. {
  1337. try
  1338. {
  1339. instance = new Graphics();
  1340. }
  1341. catch (love::Exception &e)
  1342. {
  1343. return luaL_error(L, e.what());
  1344. }
  1345. }
  1346. else
  1347. instance->retain();
  1348. WrappedModule w;
  1349. w.module = instance;
  1350. w.name = "graphics";
  1351. w.flags = MODULE_T;
  1352. w.functions = functions;
  1353. w.types = types;
  1354. int n = luax_register_module(L, w);
  1355. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "graphics.lua") == 0)
  1356. lua_call(L, 0, 0);
  1357. return n;
  1358. }
  1359. } // opengl
  1360. } // graphics
  1361. } // love