wrap_Graphics.cpp 36 KB

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