wrap_Graphics.cpp 35 KB

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