wrap_Graphics.cpp 34 KB

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