wrap_Graphics.cpp 34 KB

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