wrap_Graphics.cpp 32 KB

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