wrap_Graphics.cpp 35 KB

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