2
0

wrap_Graphics.cpp 34 KB

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