wrap_Graphics.cpp 31 KB

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