wrap_Graphics.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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 luax_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. lua_Number size = luaL_optnumber(L, 2, 1000);
  332. ParticleSystem *t = 0;
  333. if (size < 1.0 || size > LOVE_UINT32_MAX)
  334. return luaL_error(L, "Invalid ParticleSystem size");
  335. try
  336. {
  337. t = instance->newParticleSystem(image, size);
  338. }
  339. catch (love::Exception &e)
  340. {
  341. return luaL_error(L, "%s", e.what());
  342. }
  343. luax_newtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, (void *) t);
  344. return 1;
  345. }
  346. int w_newCanvas(lua_State *L)
  347. {
  348. // check if width and height are given. else default to screen dimensions.
  349. int width = luaL_optint(L, 1, instance->getWidth());
  350. int height = luaL_optint(L, 2, instance->getHeight());
  351. const char *str = luaL_optstring(L, 3, "normal");
  352. Canvas::TextureType texture_type;
  353. if (!Canvas::getConstant(str, texture_type))
  354. return luaL_error(L, "Invalid canvas type: %s", str);
  355. Canvas *canvas = NULL;
  356. try
  357. {
  358. canvas = instance->newCanvas(width, height, texture_type);
  359. }
  360. catch(Exception &e)
  361. {
  362. return luaL_error(L, e.what());
  363. }
  364. if (NULL == canvas)
  365. return luaL_error(L, "Canvas not created, but no error thrown. I don't even...");
  366. luax_newtype(L, "Canvas", GRAPHICS_CANVAS_T, (void *)canvas);
  367. return 1;
  368. }
  369. int w_newShader(lua_State *L)
  370. {
  371. if (!Shader::isSupported())
  372. return luaL_error(L, "Sorry, your graphics card does not support shaders.");
  373. // clamp stack to 2 elements
  374. lua_settop(L, 2);
  375. // read any filepath arguments
  376. for (int i = 1; i <= 2; i++)
  377. {
  378. if (!lua_isstring(L, i))
  379. continue;
  380. // call love.filesystem.isFile(arg_i)
  381. luax_getfunction(L, "filesystem", "isFile");
  382. lua_pushvalue(L, i);
  383. lua_call(L, 1, 1);
  384. bool isFile = luax_toboolean(L, -1);
  385. lua_pop(L, 1);
  386. if (isFile)
  387. {
  388. luax_getfunction(L, "filesystem", "read");
  389. lua_pushvalue(L, i);
  390. lua_call(L, 1, 1);
  391. lua_replace(L, i);
  392. }
  393. }
  394. bool has_arg1 = lua_isstring(L, 1);
  395. bool has_arg2 = lua_isstring(L, 2);
  396. // require at least one string argument
  397. if (!(has_arg1 || has_arg2))
  398. luaL_checkstring(L, 1);
  399. luax_getfunction(L, "graphics", "_shaderCodeToGLSL");
  400. // push vertexcode and pixelcode strings to the top of the stack
  401. lua_pushvalue(L, 1);
  402. lua_pushvalue(L, 2);
  403. // call effectCodeToGLSL, returned values will be at the top of the stack
  404. if (lua_pcall(L, 2, 2, 0) != 0)
  405. return luaL_error(L, "%s", lua_tostring(L, -1));
  406. Shader::ShaderSources sources;
  407. // vertex shader code
  408. if (lua_isstring(L, -2))
  409. {
  410. std::string vertexcode(luaL_checkstring(L, -2));
  411. sources[Shader::TYPE_VERTEX] = vertexcode;
  412. }
  413. else if (has_arg1 && has_arg2)
  414. return luaL_error(L, "Could not parse vertex shader code (missing 'position' function?)");
  415. // pixel shader code
  416. if (lua_isstring(L, -1))
  417. {
  418. std::string pixelcode(luaL_checkstring(L, -1));
  419. sources[Shader::TYPE_PIXEL] = pixelcode;
  420. }
  421. else if (has_arg1 && has_arg2)
  422. return luaL_error(L, "Could not parse pixel shader code (missing 'effect' function?)");
  423. if (sources.empty())
  424. {
  425. // Original args had source code, but effectCodeToGLSL couldn't translate it
  426. for (int i = 1; i <= 2; i++)
  427. {
  428. if (lua_isstring(L, i))
  429. return luaL_argerror(L, i, "missing 'position' or 'effect' function?");
  430. }
  431. }
  432. try
  433. {
  434. Shader *shader = instance->newShader(sources);
  435. luax_newtype(L, "Shader", GRAPHICS_SHADER_T, (void *)shader);
  436. }
  437. catch (const love::Exception &e)
  438. {
  439. // memory is freed in Graphics::newShader
  440. luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
  441. lua_pushstring(L, e.what());
  442. lua_pcall(L, 1, 1, 0);
  443. const char *err = lua_tostring(L, -1);
  444. return luaL_error(L, "%s", err);
  445. }
  446. return 1;
  447. }
  448. int w_setColor(lua_State *L)
  449. {
  450. Color c;
  451. if (lua_istable(L, 1))
  452. {
  453. for (int i = 1; i <= 4; i++)
  454. lua_rawgeti(L, 1, i);
  455. c.r = (unsigned char)luaL_checkint(L, -4);
  456. c.g = (unsigned char)luaL_checkint(L, -3);
  457. c.b = (unsigned char)luaL_checkint(L, -2);
  458. c.a = (unsigned char)luaL_optint(L, -1, 255);
  459. lua_pop(L, 4);
  460. }
  461. else
  462. {
  463. c.r = (unsigned char)luaL_checkint(L, 1);
  464. c.g = (unsigned char)luaL_checkint(L, 2);
  465. c.b = (unsigned char)luaL_checkint(L, 3);
  466. c.a = (unsigned char)luaL_optint(L, 4, 255);
  467. }
  468. instance->setColor(c);
  469. return 0;
  470. }
  471. int w_getColor(lua_State *L)
  472. {
  473. Color c = instance->getColor();
  474. lua_pushinteger(L, c.r);
  475. lua_pushinteger(L, c.g);
  476. lua_pushinteger(L, c.b);
  477. lua_pushinteger(L, c.a);
  478. return 4;
  479. }
  480. int w_setBackgroundColor(lua_State *L)
  481. {
  482. Color c;
  483. if (lua_istable(L, 1))
  484. {
  485. for (int i = 1; i <= 4; i++)
  486. lua_rawgeti(L, 1, i);
  487. c.r = (unsigned char)luaL_checkint(L, -4);
  488. c.g = (unsigned char)luaL_checkint(L, -3);
  489. c.b = (unsigned char)luaL_checkint(L, -2);
  490. c.a = (unsigned char)luaL_optint(L, -1, 255);
  491. lua_pop(L, 4);
  492. }
  493. else
  494. {
  495. c.r = (unsigned char)luaL_checkint(L, 1);
  496. c.g = (unsigned char)luaL_checkint(L, 2);
  497. c.b = (unsigned char)luaL_checkint(L, 3);
  498. c.a = (unsigned char)luaL_optint(L, 4, 255);
  499. }
  500. instance->setBackgroundColor(c);
  501. return 0;
  502. }
  503. int w_getBackgroundColor(lua_State *L)
  504. {
  505. Color c = instance->getBackgroundColor();
  506. lua_pushinteger(L, c.r);
  507. lua_pushinteger(L, c.g);
  508. lua_pushinteger(L, c.b);
  509. lua_pushinteger(L, c.a);
  510. return 4;
  511. }
  512. int w_setFont(lua_State *L)
  513. {
  514. Font *font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
  515. instance->setFont(font);
  516. return 0;
  517. }
  518. int w_getFont(lua_State *L)
  519. {
  520. Font *f = instance->getFont();
  521. if (f == 0)
  522. return 0;
  523. f->retain();
  524. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void *)f);
  525. return 1;
  526. }
  527. int w_setColorMask(lua_State *L)
  528. {
  529. bool mask[4];
  530. if (lua_gettop(L) == 0)
  531. {
  532. // Enable all color components if no argument is given.
  533. mask[0] = mask[1] = mask[2] = mask[3] = true;
  534. }
  535. else
  536. {
  537. for (int i = 0; i < 4; i++)
  538. mask[i] = luax_toboolean(L, i + 1);
  539. }
  540. // r, g, b, a
  541. instance->setColorMask(mask[0], mask[1], mask[2], mask[3]);
  542. return 0;
  543. }
  544. int w_getColorMask(lua_State *L)
  545. {
  546. const bool *mask = instance->getColorMask();
  547. for (int i = 0; i < 4; i++)
  548. luax_pushboolean(L, mask[i]);
  549. return 4;
  550. }
  551. int w_setBlendMode(lua_State *L)
  552. {
  553. Graphics::BlendMode mode;
  554. const char *str = luaL_checkstring(L, 1);
  555. if (!Graphics::getConstant(str, mode))
  556. return luaL_error(L, "Invalid blend mode: %s", str);
  557. try
  558. {
  559. instance->setBlendMode(mode);
  560. }
  561. catch(love::Exception &e)
  562. {
  563. return luaL_error(L, e.what());
  564. }
  565. return 0;
  566. }
  567. int w_getBlendMode(lua_State *L)
  568. {
  569. try
  570. {
  571. Graphics::BlendMode mode = instance->getBlendMode();
  572. const char *str;
  573. if (!Graphics::getConstant(mode, str))
  574. return luaL_error(L, "Unknown blend mode");
  575. lua_pushstring(L, str);
  576. return 1;
  577. }
  578. catch (love::Exception &e)
  579. {
  580. return luaL_error(L, "%s", e.what());
  581. }
  582. }
  583. int w_setDefaultFilter(lua_State *L)
  584. {
  585. Image::FilterMode min;
  586. Image::FilterMode mag;
  587. const char *minstr = luaL_checkstring(L, 1);
  588. const char *magstr = luaL_optstring(L, 2, minstr);
  589. if (!Image::getConstant(minstr, min))
  590. return luaL_error(L, "Invalid filter mode: %s", minstr);
  591. if (!Image::getConstant(magstr, mag))
  592. return luaL_error(L, "Invalid filter mode: %s", magstr);
  593. float anisotropy = (float) luaL_optnumber(L, 3, 1.0);
  594. Image::Filter f;
  595. f.min = min;
  596. f.mag = mag;
  597. f.anisotropy = anisotropy;
  598. instance->setDefaultFilter(f);
  599. return 0;
  600. }
  601. int w_getDefaultFilter(lua_State *L)
  602. {
  603. const Image::Filter &f = instance->getDefaultFilter();
  604. const char *minstr;
  605. const char *magstr;
  606. if (!Image::getConstant(f.min, minstr))
  607. return luaL_error(L, "Unknown minification filter mode");
  608. if (!Image::getConstant(f.mag, magstr))
  609. return luaL_error(L, "Unknown magnification filter mode");
  610. lua_pushstring(L, minstr);
  611. lua_pushstring(L, magstr);
  612. lua_pushnumber(L, f.anisotropy);
  613. return 3;
  614. }
  615. int w_setDefaultMipmapFilter(lua_State *L)
  616. {
  617. Image::FilterMode filter = Image::FILTER_NONE;
  618. if (!lua_isnoneornil(L, 1))
  619. {
  620. const char *str = luaL_checkstring(L, 1);
  621. if (!Image::getConstant(str, filter))
  622. return luaL_error(L, "Invalid filter mode: %s", str);
  623. }
  624. float sharpness = luaL_optnumber(L, 2, 0);
  625. instance->setDefaultMipmapFilter(filter, sharpness);
  626. return 0;
  627. }
  628. int w_getDefaultMipmapFilter(lua_State *L)
  629. {
  630. Image::FilterMode filter;
  631. float sharpness;
  632. instance->getDefaultMipmapFilter(&filter, &sharpness);
  633. const char *str;
  634. if (Image::getConstant(filter, str))
  635. lua_pushstring(L, str);
  636. else
  637. lua_pushnil(L);
  638. lua_pushnumber(L, sharpness);
  639. return 2;
  640. }
  641. int w_setLineWidth(lua_State *L)
  642. {
  643. float width = (float)luaL_checknumber(L, 1);
  644. instance->setLineWidth(width);
  645. return 0;
  646. }
  647. int w_setLineStyle(lua_State *L)
  648. {
  649. Graphics::LineStyle style;
  650. const char *str = luaL_checkstring(L, 1);
  651. if (!Graphics::getConstant(str, style))
  652. return luaL_error(L, "Invalid line style: %s", str);
  653. instance->setLineStyle(style);
  654. return 0;
  655. }
  656. int w_getLineWidth(lua_State *L)
  657. {
  658. lua_pushnumber(L, instance->getLineWidth());
  659. return 1;
  660. }
  661. int w_getLineStyle(lua_State *L)
  662. {
  663. Graphics::LineStyle style = instance->getLineStyle();
  664. const char *str;
  665. if (!Graphics::getConstant(style, str))
  666. return luaL_error(L, "Unknown line style");
  667. lua_pushstring(L, str);
  668. return 1;
  669. }
  670. int w_setPointSize(lua_State *L)
  671. {
  672. float size = (float)luaL_checknumber(L, 1);
  673. instance->setPointSize(size);
  674. return 0;
  675. }
  676. int w_setPointStyle(lua_State *L)
  677. {
  678. Graphics::PointStyle style;
  679. const char *str = luaL_checkstring(L, 1);
  680. if (!Graphics::getConstant(str, style))
  681. return luaL_error(L, "Invalid point style: %s", str);
  682. instance->setPointStyle(style);
  683. return 0;
  684. }
  685. int w_getPointSize(lua_State *L)
  686. {
  687. lua_pushnumber(L, instance->getPointSize());
  688. return 1;
  689. }
  690. int w_getPointStyle(lua_State *L)
  691. {
  692. Graphics::PointStyle style = instance->getPointStyle();
  693. const char *str;
  694. if (!Graphics::getConstant(style, str))
  695. return luaL_error(L, "Unknown point style");
  696. lua_pushstring(L, str);
  697. return 1;
  698. }
  699. int w_getMaxPointSize(lua_State *L)
  700. {
  701. lua_pushnumber(L, instance->getMaxPointSize());
  702. return 1;
  703. }
  704. int w_newScreenshot(lua_State *L)
  705. {
  706. love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  707. bool copyAlpha = luax_optboolean(L, 1, false);
  708. love::image::ImageData *i = 0;
  709. try
  710. {
  711. i = instance->newScreenshot(image, copyAlpha);
  712. }
  713. catch (love::Exception &e)
  714. {
  715. return luaL_error(L, "%s", e.what());
  716. }
  717. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)i);
  718. return 1;
  719. }
  720. int w_setCanvas(lua_State *L)
  721. {
  722. // discard stencil testing
  723. instance->discardStencil();
  724. // called with none -> reset to default buffer
  725. // nil is an error, to help people with typoes
  726. if (lua_isnone(L,1))
  727. {
  728. Canvas::bindDefaultCanvas();
  729. return 0;
  730. }
  731. Canvas *canvas = luax_checkcanvas(L, 1);
  732. try
  733. {
  734. // this unbinds the previous fbo
  735. canvas->startGrab();
  736. }
  737. catch (love::Exception &e)
  738. {
  739. return luaL_error(L, "%s", e.what());
  740. }
  741. return 0;
  742. }
  743. int w_setCanvases(lua_State *L)
  744. {
  745. // discard stencil testing
  746. instance->discardStencil();
  747. // called with none -> reset to default buffer
  748. // nil is an error, to help people with typoes
  749. if (lua_isnone(L,1))
  750. {
  751. Canvas::bindDefaultCanvas();
  752. return 0;
  753. }
  754. bool is_table = lua_istable(L, 1);
  755. std::vector<Canvas *> attachments;
  756. Canvas *canvas = 0;
  757. if (is_table)
  758. {
  759. // grab the first canvas in the array and attach the rest
  760. lua_rawgeti(L, 1, 1);
  761. canvas = luax_checkcanvas(L, -1);
  762. lua_pop(L, 1);
  763. for (size_t i = 2; i <= lua_objlen(L, 1); i++)
  764. {
  765. lua_rawgeti(L, 1, i);
  766. attachments.push_back(luax_checkcanvas(L, -1));
  767. lua_pop(L, 1);
  768. }
  769. }
  770. else
  771. {
  772. canvas = luax_checkcanvas(L, 1);
  773. for (int i = 2; i <= lua_gettop(L); i++)
  774. attachments.push_back(luax_checkcanvas(L, i));
  775. }
  776. try
  777. {
  778. canvas->startGrab(attachments);
  779. }
  780. catch (love::Exception &e)
  781. {
  782. return luaL_error(L, "%s", e.what());
  783. }
  784. return 0;
  785. }
  786. int w_getCanvas(lua_State *L)
  787. {
  788. Canvas *canvas = Canvas::current;
  789. int n = 1;
  790. if (canvas)
  791. {
  792. canvas->retain();
  793. luax_newtype(L, "Canvas", GRAPHICS_CANVAS_T, (void *) canvas);
  794. const std::vector<Canvas *> &attachments = canvas->getAttachedCanvases();
  795. for (size_t i = 0; i < attachments.size(); i++)
  796. {
  797. attachments[i]->retain();
  798. luax_newtype(L, "Canvas", GRAPHICS_CANVAS_T, (void *) attachments[i]);
  799. n++;
  800. }
  801. }
  802. else
  803. lua_pushnil(L);
  804. return n;
  805. }
  806. int w_setShader(lua_State *L)
  807. {
  808. if (lua_isnoneornil(L,1))
  809. {
  810. Shader::detach();
  811. return 0;
  812. }
  813. Shader *shader = luax_checkshader(L, 1);
  814. shader->attach();
  815. return 0;
  816. }
  817. int w_getShader(lua_State *L)
  818. {
  819. Shader *shader = Shader::current;
  820. if (shader)
  821. {
  822. shader->retain();
  823. luax_newtype(L, "Shader", GRAPHICS_SHADER_T, (void *) shader);
  824. }
  825. else
  826. lua_pushnil(L);
  827. return 1;
  828. }
  829. int w_isSupported(lua_State *L)
  830. {
  831. bool supported = true;
  832. size_t len = lua_gettop(L);
  833. Graphics::Support support;
  834. for (unsigned int i = 1; i <= len; i++)
  835. {
  836. const char *str = luaL_checkstring(L, i);
  837. if (!Graphics::getConstant(str, support))
  838. {
  839. supported = false;
  840. break;
  841. }
  842. switch (support)
  843. {
  844. case Graphics::SUPPORT_CANVAS:
  845. if (!Canvas::isSupported())
  846. supported = false;
  847. break;
  848. case Graphics::SUPPORT_HDR_CANVAS:
  849. if (!Canvas::isHDRSupported())
  850. supported = false;
  851. break;
  852. case Graphics::SUPPORT_MULTI_CANVAS:
  853. if (!Canvas::isMultiCanvasSupported())
  854. supported = false;
  855. break;
  856. case Graphics::SUPPORT_SHADER:
  857. if (!Shader::isSupported())
  858. supported = false;
  859. break;
  860. case Graphics::SUPPORT_NPOT:
  861. if (!Image::hasNpot())
  862. supported = false;
  863. break;
  864. case Graphics::SUPPORT_SUBTRACTIVE:
  865. if (!((GLEE_VERSION_1_4 || GLEE_ARB_imaging) || (GLEE_EXT_blend_minmax && GLEE_EXT_blend_subtract)))
  866. supported = false;
  867. break;
  868. case Graphics::SUPPORT_MIPMAP:
  869. if (!Image::hasMipmapSupport())
  870. supported = false;
  871. break;
  872. case Graphics::SUPPORT_DXT:
  873. if (!Image::hasCompressedTextureSupport(image::CompressedData::TYPE_DXT5))
  874. supported = false;
  875. break;
  876. case Graphics::SUPPORT_BC5:
  877. if (!Image::hasCompressedTextureSupport(image::CompressedData::TYPE_BC5))
  878. supported = false;
  879. break;
  880. default:
  881. supported = false;
  882. }
  883. if (!supported)
  884. break;
  885. }
  886. lua_pushboolean(L, supported);
  887. return 1;
  888. }
  889. int w_getRendererInfo(lua_State *L)
  890. {
  891. const char *str = luaL_checkstring(L, 1);
  892. Graphics::RendererInfo infotype;
  893. if (!Graphics::getConstant(str, infotype))
  894. return luaL_error(L, "Invalid renderer info type: %s", str);
  895. try
  896. {
  897. luax_pushstring(L, instance->getRendererInfo(infotype));
  898. }
  899. catch (love::Exception &e)
  900. {
  901. return luaL_error(L, "%s", e.what());
  902. }
  903. return 1;
  904. }
  905. /**
  906. * Draws an Image at the specified coordinates, with rotation and
  907. * scaling along both axes.
  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_draw(lua_State *L)
  919. {
  920. Drawable *drawable = luax_checktype<Drawable>(L, 1, "Drawable", GRAPHICS_DRAWABLE_T);
  921. float x = (float)luaL_optnumber(L, 2, 0.0f);
  922. float y = (float)luaL_optnumber(L, 3, 0.0f);
  923. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  924. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  925. float sy = (float)luaL_optnumber(L, 6, sx);
  926. float ox = (float)luaL_optnumber(L, 7, 0);
  927. float oy = (float)luaL_optnumber(L, 8, 0);
  928. float kx = (float)luaL_optnumber(L, 9, 0);
  929. float ky = (float)luaL_optnumber(L, 10, 0);
  930. drawable->draw(x, y, angle, sx, sy, ox, oy, kx, ky);
  931. return 0;
  932. }
  933. /**
  934. * Draws a portion of a DrawGable at the specified coordinates,
  935. * with rotation and scaling along both axes.
  936. *
  937. * @param q The Quad to draw.
  938. * @param x The x-coordinate.
  939. * @param y The y-coordinate.
  940. * @param angle The amount of rotation.
  941. * @param sx The scale factor along the x-axis. (1 = normal).
  942. * @param sy The scale factor along the y-axis. (1 = normal).
  943. * @param ox The offset along the x-axis.
  944. * @param oy The offset along the y-axis.
  945. * @param kx Shear along the x-axis.
  946. * @param ky Shear along the y-axis.
  947. **/
  948. int w_drawg(lua_State *L)
  949. {
  950. DrawGable *dq = luax_checktype<DrawGable>(L, 1, "DrawGable", GRAPHICS_DRAWGABLE_T);
  951. Geometry *geom = luax_checkgeometry(L, 2);
  952. float x = (float)luaL_optnumber(L, 3, 0.0f);
  953. float y = (float)luaL_optnumber(L, 4, 0.0f);
  954. float angle = (float)luaL_optnumber(L, 5, 0);
  955. float sx = (float)luaL_optnumber(L, 6, 1);
  956. float sy = (float)luaL_optnumber(L, 7, sx);
  957. float ox = (float)luaL_optnumber(L, 8, 0);
  958. float oy = (float)luaL_optnumber(L, 9, 0);
  959. float kx = (float)luaL_optnumber(L, 10, 0);
  960. float ky = (float)luaL_optnumber(L, 11, 0);
  961. dq->drawg(geom, x, y, angle, sx, sy, ox, oy, kx, ky);
  962. return 0;
  963. }
  964. int w_print(lua_State *L)
  965. {
  966. std::string str = luax_checkstring(L, 1);
  967. float x = (float)luaL_checknumber(L, 2);
  968. float y = (float)luaL_checknumber(L, 3);
  969. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  970. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  971. float sy = (float)luaL_optnumber(L, 6, sx);
  972. float ox = (float)luaL_optnumber(L, 7, 0.0f);
  973. float oy = (float)luaL_optnumber(L, 8, 0.0f);
  974. float kx = (float)luaL_optnumber(L, 9, 0.0f);
  975. float ky = (float)luaL_optnumber(L, 10, 0.0f);
  976. try
  977. {
  978. instance->print(str, x, y, angle, sx, sy, ox, oy, kx,ky);
  979. }
  980. catch(love::Exception &e)
  981. {
  982. return luaL_error(L, "%s", e.what());
  983. }
  984. return 0;
  985. }
  986. int w_printf(lua_State *L)
  987. {
  988. std::string str = luax_checkstring(L, 1);
  989. float x = (float)luaL_checknumber(L, 2);
  990. float y = (float)luaL_checknumber(L, 3);
  991. float wrap = (float)luaL_checknumber(L, 4);
  992. float angle = 0.0f;
  993. float sx = 1.0f, sy = 1.0f;
  994. float ox = 0.0f, oy = 0.0f;
  995. float kx = 0.0f, ky = 0.0f;
  996. Graphics::AlignMode align = Graphics::ALIGN_LEFT;
  997. if (lua_gettop(L) >= 5)
  998. {
  999. if (!lua_isnil(L, 5))
  1000. {
  1001. const char *str = luaL_checkstring(L, 5);
  1002. if (!Graphics::getConstant(str, align))
  1003. return luaL_error(L, "Incorrect alignment: %s", str);
  1004. }
  1005. angle = (float) luaL_optnumber(L, 6, 0.0f);
  1006. sx = (float) luaL_optnumber(L, 7, 1.0f);
  1007. sy = (float) luaL_optnumber(L, 8, sx);
  1008. ox = (float) luaL_optnumber(L, 9, 0.0f);
  1009. oy = (float) luaL_optnumber(L, 10, 0.0f);
  1010. kx = (float) luaL_optnumber(L, 11, 0.0f);
  1011. ky = (float) luaL_optnumber(L, 12, 0.0f);
  1012. }
  1013. try
  1014. {
  1015. instance->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky);
  1016. }
  1017. catch(love::Exception &e)
  1018. {
  1019. return luaL_error(L, "%s", e.what());
  1020. }
  1021. return 0;
  1022. }
  1023. int w_point(lua_State *L)
  1024. {
  1025. float x = (float)luaL_checknumber(L, 1);
  1026. float y = (float)luaL_checknumber(L, 2);
  1027. instance->point(x, y);
  1028. return 0;
  1029. }
  1030. int w_line(lua_State *L)
  1031. {
  1032. int args = lua_gettop(L);
  1033. bool is_table = false;
  1034. if (args == 1 && lua_istable(L, 1))
  1035. {
  1036. args = lua_objlen(L, 1);
  1037. is_table = true;
  1038. }
  1039. if (args % 2 != 0)
  1040. return luaL_error(L, "Number of vertices must be a multiple of two");
  1041. else if (args < 4)
  1042. return luaL_error(L, "Need at least two vertices to draw a line");
  1043. float *coords = new float[args];
  1044. if (is_table)
  1045. {
  1046. for (int i = 0; i < args; ++i)
  1047. {
  1048. lua_rawgeti(L, 1, i + 1);
  1049. coords[i] = luax_tofloat(L, -1);
  1050. lua_pop(L, 1);
  1051. }
  1052. }
  1053. else
  1054. {
  1055. for (int i = 0; i < args; ++i)
  1056. coords[i] = luax_tofloat(L, i + 1);
  1057. }
  1058. instance->polyline(coords, args);
  1059. delete[] coords;
  1060. return 0;
  1061. }
  1062. int w_rectangle(lua_State *L)
  1063. {
  1064. Graphics::DrawMode mode;
  1065. const char *str = luaL_checkstring(L, 1);
  1066. if (!Graphics::getConstant(str, mode))
  1067. return luaL_error(L, "Incorrect draw mode %s", str);
  1068. float x = (float)luaL_checknumber(L, 2);
  1069. float y = (float)luaL_checknumber(L, 3);
  1070. float w = (float)luaL_checknumber(L, 4);
  1071. float h = (float)luaL_checknumber(L, 5);
  1072. instance->rectangle(mode, x, y, w, h);
  1073. return 0;
  1074. }
  1075. int w_circle(lua_State *L)
  1076. {
  1077. Graphics::DrawMode mode;
  1078. const char *str = luaL_checkstring(L, 1);
  1079. if (!Graphics::getConstant(str, mode))
  1080. return luaL_error(L, "Incorrect draw mode %s", str);
  1081. float x = (float)luaL_checknumber(L, 2);
  1082. float y = (float)luaL_checknumber(L, 3);
  1083. float radius = (float)luaL_checknumber(L, 4);
  1084. int points;
  1085. if (lua_isnoneornil(L, 5))
  1086. points = radius > 10 ? (int)(radius) : 10;
  1087. else
  1088. points = luaL_checkint(L, 5);
  1089. instance->circle(mode, x, y, radius, points);
  1090. return 0;
  1091. }
  1092. int w_arc(lua_State *L)
  1093. {
  1094. Graphics::DrawMode mode;
  1095. const char *str = luaL_checkstring(L, 1);
  1096. if (!Graphics::getConstant(str, mode))
  1097. return luaL_error(L, "Incorrect draw mode %s", str);
  1098. float x = (float)luaL_checknumber(L, 2);
  1099. float y = (float)luaL_checknumber(L, 3);
  1100. float radius = (float)luaL_checknumber(L, 4);
  1101. float angle1 = (float)luaL_checknumber(L, 5);
  1102. float angle2 = (float)luaL_checknumber(L, 6);
  1103. int points;
  1104. if (lua_isnoneornil(L, 7))
  1105. points = radius > 10 ? (int)(radius) : 10;
  1106. else
  1107. points = luaL_checkint(L, 7);
  1108. instance->arc(mode, x, y, radius, angle1, angle2, points);
  1109. return 0;
  1110. }
  1111. int w_polygon(lua_State *L)
  1112. {
  1113. int args = lua_gettop(L) - 1;
  1114. Graphics::DrawMode mode;
  1115. const char *str = luaL_checkstring(L, 1);
  1116. if (!Graphics::getConstant(str, mode))
  1117. return luaL_error(L, "Invalid draw mode: %s", str);
  1118. bool is_table = false;
  1119. float *coords;
  1120. if (args == 1 && lua_istable(L, 2))
  1121. {
  1122. args = lua_objlen(L, 2);
  1123. is_table = true;
  1124. }
  1125. if (args % 2 != 0)
  1126. return luaL_error(L, "Number of vertices must be a multiple of two");
  1127. else if (args < 6)
  1128. return luaL_error(L, "Need at least three vertices to draw a polygon");
  1129. // fetch coords
  1130. coords = new float[args + 2];
  1131. if (is_table)
  1132. {
  1133. for (int i = 0; i < args; ++i)
  1134. {
  1135. lua_rawgeti(L, 2, i + 1);
  1136. coords[i] = luax_tofloat(L, -1);
  1137. lua_pop(L, 1);
  1138. }
  1139. }
  1140. else
  1141. {
  1142. for (int i = 0; i < args; ++i)
  1143. coords[i] = luax_tofloat(L, i + 2);
  1144. }
  1145. // make a closed loop
  1146. coords[args] = coords[0];
  1147. coords[args+1] = coords[1];
  1148. instance->polygon(mode, coords, args+2);
  1149. delete[] coords;
  1150. return 0;
  1151. }
  1152. int w_push(lua_State *L)
  1153. {
  1154. try
  1155. {
  1156. instance->push();
  1157. }
  1158. catch(love::Exception e)
  1159. {
  1160. return luaL_error(L, e.what());
  1161. }
  1162. return 0;
  1163. }
  1164. int w_pop(lua_State *L)
  1165. {
  1166. try
  1167. {
  1168. instance->pop();
  1169. }
  1170. catch(love::Exception e)
  1171. {
  1172. return luaL_error(L, e.what());
  1173. }
  1174. return 0;
  1175. }
  1176. int w_rotate(lua_State *L)
  1177. {
  1178. float angle = (float)luaL_checknumber(L, 1);
  1179. instance->rotate(angle);
  1180. return 0;
  1181. }
  1182. int w_scale(lua_State *L)
  1183. {
  1184. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  1185. float sy = (float)luaL_optnumber(L, 2, sx);
  1186. instance->scale(sx, sy);
  1187. return 0;
  1188. }
  1189. int w_translate(lua_State *L)
  1190. {
  1191. float x = (float)luaL_checknumber(L, 1);
  1192. float y = (float)luaL_checknumber(L, 2);
  1193. instance->translate(x, y);
  1194. return 0;
  1195. }
  1196. int w_shear(lua_State *L)
  1197. {
  1198. float kx = (float)luaL_checknumber(L, 1);
  1199. float ky = (float)luaL_checknumber(L, 2);
  1200. instance->shear(kx, ky);
  1201. return 0;
  1202. }
  1203. int w_origin(lua_State * /*L*/)
  1204. {
  1205. instance->origin();
  1206. return 0;
  1207. }
  1208. // List of functions to wrap.
  1209. static const luaL_Reg functions[] =
  1210. {
  1211. { "reset", w_reset },
  1212. { "clear", w_clear },
  1213. { "present", w_present },
  1214. { "newImage", w_newImage },
  1215. { "newQuad", w_newQuad },
  1216. { "newGeometry", w_newGeometry },
  1217. { "newFont", w_newFont },
  1218. { "newImageFont", w_newImageFont },
  1219. { "newSpriteBatch", w_newSpriteBatch },
  1220. { "newParticleSystem", w_newParticleSystem },
  1221. { "newCanvas", w_newCanvas },
  1222. { "newShader", w_newShader },
  1223. { "setColor", w_setColor },
  1224. { "getColor", w_getColor },
  1225. { "setBackgroundColor", w_setBackgroundColor },
  1226. { "getBackgroundColor", w_getBackgroundColor },
  1227. { "setFont", w_setFont },
  1228. { "getFont", w_getFont },
  1229. { "setColorMask", w_setColorMask },
  1230. { "getColorMask", w_getColorMask },
  1231. { "setBlendMode", w_setBlendMode },
  1232. { "getBlendMode", w_getBlendMode },
  1233. { "setDefaultFilter", w_setDefaultFilter },
  1234. { "getDefaultFilter", w_getDefaultFilter },
  1235. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  1236. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  1237. { "setLineWidth", w_setLineWidth },
  1238. { "setLineStyle", w_setLineStyle },
  1239. { "getLineWidth", w_getLineWidth },
  1240. { "getLineStyle", w_getLineStyle },
  1241. { "setPointSize", w_setPointSize },
  1242. { "setPointStyle", w_setPointStyle },
  1243. { "getPointSize", w_getPointSize },
  1244. { "getPointStyle", w_getPointStyle },
  1245. { "getMaxPointSize", w_getMaxPointSize },
  1246. { "newScreenshot", w_newScreenshot },
  1247. { "setCanvas", w_setCanvas },
  1248. { "setCanvases", w_setCanvases },
  1249. { "getCanvas", w_getCanvas },
  1250. { "getCanvases", w_getCanvas },
  1251. { "setShader", w_setShader },
  1252. { "getShader", w_getShader },
  1253. { "isSupported", w_isSupported },
  1254. { "getRendererInfo", w_getRendererInfo },
  1255. { "draw", w_draw },
  1256. { "drawq", w_drawg }, // legacy
  1257. { "drawg", w_drawg },
  1258. { "print", w_print },
  1259. { "printf", w_printf },
  1260. { "isCreated", w_isCreated },
  1261. { "getWidth", w_getWidth },
  1262. { "getHeight", w_getHeight },
  1263. { "getDimensions", w_getDimensions },
  1264. { "setScissor", w_setScissor },
  1265. { "getScissor", w_getScissor },
  1266. { "setStencil", w_setStencil },
  1267. { "setInvertedStencil", w_setInvertedStencil },
  1268. { "setAlphaTest", w_setAlphaTest },
  1269. { "getAlphaTest", w_getAlphaTest },
  1270. { "point", w_point },
  1271. { "line", w_line },
  1272. { "rectangle", w_rectangle },
  1273. { "circle", w_circle },
  1274. { "arc", w_arc },
  1275. { "polygon", w_polygon },
  1276. { "push", w_push },
  1277. { "pop", w_pop },
  1278. { "rotate", w_rotate },
  1279. { "scale", w_scale },
  1280. { "translate", w_translate },
  1281. { "shear", w_shear },
  1282. { "origin", w_origin },
  1283. { 0, 0 }
  1284. };
  1285. // Types for this module.
  1286. static const lua_CFunction types[] =
  1287. {
  1288. luaopen_font,
  1289. luaopen_image,
  1290. luaopen_geometry,
  1291. luaopen_spritebatch,
  1292. luaopen_particlesystem,
  1293. luaopen_canvas,
  1294. luaopen_shader,
  1295. 0
  1296. };
  1297. extern "C" int luaopen_love_graphics(lua_State *L)
  1298. {
  1299. if (instance == 0)
  1300. {
  1301. try
  1302. {
  1303. instance = new Graphics();
  1304. }
  1305. catch (love::Exception &e)
  1306. {
  1307. return luaL_error(L, e.what());
  1308. }
  1309. }
  1310. else
  1311. instance->retain();
  1312. WrappedModule w;
  1313. w.module = instance;
  1314. w.name = "graphics";
  1315. w.flags = MODULE_GRAPHICS_T;
  1316. w.functions = functions;
  1317. w.types = types;
  1318. int n = luax_register_module(L, w);
  1319. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "graphics.lua") == 0)
  1320. lua_call(L, 0, 0);
  1321. return n;
  1322. }
  1323. } // opengl
  1324. } // graphics
  1325. } // love