wrap_Graphics.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /**
  2. * Copyright (c) 2006-2009 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 <image/ImageData.h>
  22. #include <scripts/graphics.lua.h>
  23. namespace love
  24. {
  25. namespace graphics
  26. {
  27. namespace opengl
  28. {
  29. static Graphics * instance = 0;
  30. int w_checkMode(lua_State * L)
  31. {
  32. int w = luaL_checkint(L, 1);
  33. int h = luaL_checkint(L, 2);
  34. bool fs = luax_toboolean(L, 3);
  35. luax_pushboolean(L, instance->checkMode(w, h, fs));
  36. return 1;
  37. }
  38. int w_setMode(lua_State * L)
  39. {
  40. int w = luaL_checkint(L, 1);
  41. int h = luaL_checkint(L, 2);
  42. bool fs = luax_optboolean(L, 3, false);
  43. bool vsync = luax_optboolean(L, 4, true);
  44. int fsaa = luaL_optint(L, 5, 0);
  45. luax_pushboolean(L, instance->setMode(w, h, fs, vsync, fsaa));
  46. return 1;
  47. }
  48. int w_toggleFullscreen(lua_State * L)
  49. {
  50. luax_pushboolean(L, instance->toggleFullscreen());
  51. return 1;
  52. }
  53. int w_reset(lua_State * L)
  54. {
  55. instance->reset();
  56. return 0;
  57. }
  58. int w_clear(lua_State * L)
  59. {
  60. instance->clear();
  61. return 0;
  62. }
  63. int w_present(lua_State * L)
  64. {
  65. instance->present();
  66. return 0;
  67. }
  68. int w_setCaption(lua_State * L)
  69. {
  70. const char * str = luaL_checkstring(L, 1);
  71. instance->setCaption(str);
  72. return 0;
  73. }
  74. int w_getCaption(lua_State * L)
  75. {
  76. return instance->getCaption(L);
  77. }
  78. int w_getWidth(lua_State * L)
  79. {
  80. lua_pushnumber(L, instance->getWidth());
  81. return 1;
  82. }
  83. int w_getHeight(lua_State * L)
  84. {
  85. lua_pushnumber(L, instance->getHeight());
  86. return 1;
  87. }
  88. int w_isCreated(lua_State * L)
  89. {
  90. luax_pushboolean(L, instance->isCreated());
  91. return 1;
  92. }
  93. int w_getModes(lua_State * L)
  94. {
  95. return instance->getModes(L);
  96. }
  97. int w_setScissor(lua_State * L)
  98. {
  99. if(lua_gettop(L) == 0)
  100. {
  101. instance->setScissor();
  102. return 0;
  103. }
  104. int x = luaL_checkint(L, 1);
  105. int y = luaL_checkint(L, 2);
  106. int w = luaL_checkint(L, 3);
  107. int h = luaL_checkint(L, 4);
  108. instance->setScissor(x, y, w, h);
  109. return 0;
  110. }
  111. int w_getScissor(lua_State * L)
  112. {
  113. return instance->getScissor(L);
  114. }
  115. int w_newImage(lua_State * L)
  116. {
  117. // Convert to File, if necessary.
  118. if(lua_isstring(L, 1))
  119. luax_convobj(L, 1, "filesystem", "newFile");
  120. // Convert to ImageData, if necessary.
  121. if(luax_istype(L, 1, FILESYSTEM_FILE_T))
  122. luax_convobj(L, 1, "image", "newImageData");
  123. love::image::ImageData * data = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
  124. // Create the image.
  125. Image * image = 0;
  126. try {
  127. image = instance->newImage(data);
  128. } catch (love::Exception & e) {
  129. luaL_error(L, e.what());
  130. }
  131. if(image == 0)
  132. return luaL_error(L, "Could not load image.");
  133. // Push the type.
  134. luax_newtype(L, "Image", GRAPHICS_IMAGE_T, (void*)image);
  135. return 1;
  136. }
  137. int w_newGlyph(lua_State * L)
  138. {
  139. love::font::GlyphData * data = luax_checktype<love::font::GlyphData>(L, 1, "GlyphData", FONT_GLYPH_DATA_T);
  140. // Create the image.
  141. Glyph * t = new Glyph(data);
  142. t->load();
  143. // Push the type.
  144. luax_newtype(L, "Glyph", GRAPHICS_GLYPH_T, (void*)t);
  145. return 1;
  146. }
  147. int w_newQuad(lua_State * L)
  148. {
  149. int x = luaL_checkint(L, 1);
  150. int y = luaL_checkint(L, 2);
  151. int w = luaL_checkint(L, 3);
  152. int h = luaL_checkint(L, 4);
  153. int sw = luaL_checkint(L, 5);
  154. int sh = luaL_checkint(L, 6);
  155. Quad * frame = instance->newQuad(x, y, w, h, sw, sh);
  156. if (frame == 0)
  157. return luaL_error(L, "Could not create frame.");
  158. luax_newtype(L, "Quad", GRAPHICS_QUAD_T, (void*)frame);
  159. return 1;
  160. }
  161. int w_newFont1(lua_State * L)
  162. {
  163. Data * d = 0;
  164. // Convert to File, if necessary.
  165. if(lua_isstring(L, 1))
  166. luax_convobj(L, 1, "filesystem", "newFile");
  167. if(luax_istype(L, 1, FILESYSTEM_FILE_T))
  168. {
  169. // Check the value.
  170. love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
  171. d = file->read();
  172. }
  173. else if(luax_istype(L, 1, DATA_T))
  174. {
  175. d = luax_checktype<Data>(L, 1, "Data", DATA_T);
  176. } else { // This is not the type you're looking for.
  177. return luaL_error(L, "love.graphics.newFont() requires a string, File, or font data as argument #1");
  178. }
  179. // Second optional parameter can be a number:
  180. int size = luaL_optint(L, 2, 12);
  181. Font * font;
  182. try {
  183. font = instance->newFont(d, size);
  184. } catch (Exception & e) {
  185. return luaL_error(L, e.what());
  186. }
  187. if(font == 0)
  188. return luaL_error(L, "Could not load the font");
  189. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font);
  190. return 1;
  191. }
  192. int w_newImageFont(lua_State * L)
  193. {
  194. // Convert to File, if necessary.
  195. if(lua_isstring(L, 1))
  196. luax_convobj(L, 1, "filesystem", "newFile");
  197. // Convert to Image, if necessary.
  198. if(luax_istype(L, 1, FILESYSTEM_FILE_T))
  199. luax_convobj(L, 1, "graphics", "newImage");
  200. // Check the value.
  201. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  202. const char * glyphs = luaL_checkstring(L, 2);
  203. Font * font = instance->newImageFont(image, glyphs);
  204. if(font == 0)
  205. return luaL_error(L, "Could not load the font");
  206. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font);
  207. return 1;
  208. }
  209. int w_newSpriteBatch(lua_State * L)
  210. {
  211. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  212. int size = luaL_optint(L, 2, 1000);
  213. int usage = luaL_optint(L, 3, SpriteBatch::USAGE_DYNAMIC);
  214. SpriteBatch * t = instance->newSpriteBatch(image, size, usage);
  215. luax_newtype(L, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T, (void*)t);
  216. return 1;
  217. }
  218. int w_newParticleSystem(lua_State * L)
  219. {
  220. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  221. int size = luaL_checkint(L, 2);
  222. ParticleSystem * t = instance->newParticleSystem(image, size);
  223. luax_newtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, (void*)t);
  224. return 1;
  225. }
  226. int w_setColor(lua_State * L)
  227. {
  228. Color c;
  229. c.r = (unsigned char)luaL_checkint(L, 1);
  230. c.g = (unsigned char)luaL_checkint(L, 2);
  231. c.b = (unsigned char)luaL_checkint(L, 3);
  232. c.a = (unsigned char)luaL_optint(L, 4, 255);
  233. instance->setColor(c);
  234. return 0;
  235. }
  236. int w_getColor(lua_State * L)
  237. {
  238. Color c = instance->getColor();
  239. lua_pushinteger(L, c.r);
  240. lua_pushinteger(L, c.g);
  241. lua_pushinteger(L, c.b);
  242. lua_pushinteger(L, c.a);
  243. return 4;
  244. }
  245. int w_setBackgroundColor(lua_State * L)
  246. {
  247. Color c;
  248. c.r = (unsigned char)luaL_checkint(L, 1);
  249. c.g = (unsigned char)luaL_checkint(L, 2);
  250. c.b = (unsigned char)luaL_checkint(L, 3);
  251. c.a = 255;
  252. instance->setBackgroundColor(c);
  253. return 0;
  254. }
  255. int w_getBackgroundColor(lua_State * L)
  256. {
  257. Color c = instance->getBackgroundColor();
  258. lua_pushinteger(L, c.r);
  259. lua_pushinteger(L, c.g);
  260. lua_pushinteger(L, c.b);
  261. lua_pushinteger(L, c.a);
  262. return 4;
  263. }
  264. int w_setFont1(lua_State * L)
  265. {
  266. // The second parameter is an optional int.
  267. int size = luaL_optint(L, 2, 12);
  268. // If the first parameter is a string, convert it to a file.
  269. if(lua_isstring(L, 1))
  270. luax_convobj(L, 1, "filesystem", "newFile");
  271. // If the first parameter is a File, use another setFont function.
  272. if(luax_istype(L, 1, FILESYSTEM_FILE_T))
  273. {
  274. love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
  275. instance->setFont(file->read(), size);
  276. return 0;
  277. }
  278. else if(luax_istype(L, 1, DATA_T))
  279. {
  280. Data * data = luax_checktype<Data>(L, 1, "Data", DATA_T);
  281. instance->setFont(data, size);
  282. return 0;
  283. }
  284. Font * font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
  285. instance->setFont(font);
  286. return 0;
  287. }
  288. int w_getFont(lua_State * L)
  289. {
  290. Font * f = instance->getFont();
  291. if(f == 0)
  292. return 0;
  293. f->retain();
  294. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)f);
  295. return 1;
  296. }
  297. int w_setBlendMode(lua_State * L)
  298. {
  299. Graphics::BlendMode mode;
  300. const char * str = luaL_checkstring(L, 1);
  301. if(!Graphics::getConstant(str, mode))
  302. return luaL_error(L, "Invalid blend mode: %s", str);
  303. instance->setBlendMode(mode);
  304. return 0;
  305. }
  306. int w_setColorMode(lua_State * L)
  307. {
  308. Graphics::ColorMode mode;
  309. const char * str = luaL_checkstring(L, 1);
  310. if(!Graphics::getConstant(str, mode))
  311. return luaL_error(L, "Invalid color mode: %s", str);
  312. instance->setColorMode(mode);
  313. return 0;
  314. }
  315. int w_getBlendMode(lua_State * L)
  316. {
  317. Graphics::BlendMode mode = instance->getBlendMode();
  318. const char * str;
  319. if(!Graphics::getConstant(mode, str))
  320. return luaL_error(L, "Invalid blend mode: %s", str);
  321. lua_pushstring(L, str);
  322. return 1;
  323. }
  324. int w_getColorMode(lua_State * L)
  325. {
  326. Graphics::ColorMode mode = instance->getColorMode();
  327. const char * str;
  328. if(!Graphics::getConstant(mode, str))
  329. return luaL_error(L, "Invalid color mode: %s", str);
  330. lua_pushstring(L, str);
  331. return 1;
  332. }
  333. int w_setLineWidth(lua_State * L)
  334. {
  335. float width = (float)luaL_checknumber(L, 1);
  336. instance->setLineWidth(width);
  337. return 0;
  338. }
  339. int w_setLineStyle(lua_State * L)
  340. {
  341. Graphics::LineStyle style;
  342. const char * str = luaL_checkstring(L, 1);
  343. if(!Graphics::getConstant(str, style))
  344. return luaL_error(L, "Invalid line style: %s", str);
  345. instance->setLineStyle(style);
  346. return 0;
  347. }
  348. int w_setLine(lua_State * L)
  349. {
  350. float width = (float)luaL_checknumber(L, 1);
  351. Graphics::LineStyle style = Graphics::LINE_SMOOTH;
  352. if(lua_gettop(L) >= 2)
  353. {
  354. const char * str = luaL_checkstring(L, 2);
  355. if(!Graphics::getConstant(str, style))
  356. return luaL_error(L, "Invalid line style: %s", str);
  357. }
  358. instance->setLine(width, style);
  359. return 0;
  360. }
  361. int w_setLineStipple(lua_State * L)
  362. {
  363. if(lua_gettop(L) == 0)
  364. {
  365. instance->setLineStipple();
  366. return 0;
  367. }
  368. unsigned short pattern = (unsigned short)luaL_checkint(L, 1);
  369. int repeat = luaL_optint(L, 2, 1);
  370. instance->setLineStipple(pattern, repeat);
  371. return 0;
  372. }
  373. int w_getLineWidth(lua_State * L)
  374. {
  375. lua_pushnumber(L, instance->getLineWidth());
  376. return 1;
  377. }
  378. int w_getLineStyle(lua_State * L)
  379. {
  380. Graphics::LineStyle style = instance->getLineStyle();
  381. const char *str;
  382. Graphics::getConstant(style, str);
  383. lua_pushstring(L, str);
  384. return 1;
  385. }
  386. int w_getLineStipple(lua_State * L)
  387. {
  388. return instance->getLineStipple(L);
  389. }
  390. int w_setPointSize(lua_State * L)
  391. {
  392. float size = (float)luaL_checknumber(L, 1);
  393. instance->setPointSize(size);
  394. return 0;
  395. }
  396. int w_setPointStyle(lua_State * L)
  397. {
  398. Graphics::PointStyle style = Graphics::POINT_SMOOTH;
  399. if(lua_gettop(L) >= 2)
  400. {
  401. const char * str = luaL_checkstring(L, 1);
  402. if(!Graphics::getConstant(str, style))
  403. return luaL_error(L, "Invalid point style: %s", str);
  404. }
  405. instance->setPointStyle(style);
  406. return 0;
  407. }
  408. int w_setPoint(lua_State * L)
  409. {
  410. float size = (float)luaL_checknumber(L, 1);
  411. Graphics::PointStyle style;
  412. const char * str = luaL_checkstring(L, 2);
  413. if(!Graphics::getConstant(str, style))
  414. return luaL_error(L, "Invalid point style: %s", str);
  415. instance->setPoint(size, style);
  416. return 0;
  417. }
  418. int w_getPointSize(lua_State * L)
  419. {
  420. lua_pushnumber(L, instance->getPointSize());
  421. return 1;
  422. }
  423. int w_getPointStyle(lua_State * L)
  424. {
  425. lua_pushinteger(L, instance->getPointStyle());
  426. return 1;
  427. }
  428. int w_getMaxPointSize(lua_State * L)
  429. {
  430. lua_pushnumber(L, instance->getMaxPointSize());
  431. return 1;
  432. }
  433. int w_newScreenshot(lua_State * L)
  434. {
  435. love::image::Image * image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  436. love::image::ImageData * i = instance->newScreenshot(image);
  437. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)i);
  438. return 1;
  439. }
  440. /**
  441. * Draws an Image at the specified coordinates, with rotation and
  442. * scaling along both axes.
  443. * @param x The x-coordinate.
  444. * @param y The y-coordinate.
  445. * @param angle The amount of rotation.
  446. * @param sx The scale factor along the x-axis. (1 = normal).
  447. * @param sy The scale factor along the y-axis. (1 = normal).
  448. * @param ox The offset along the x-axis.
  449. * @param oy The offset along the y-axis.
  450. **/
  451. int w_draw(lua_State * L)
  452. {
  453. Drawable * drawable = luax_checktype<Drawable>(L, 1, "Drawable", GRAPHICS_DRAWABLE_T);
  454. float x = (float)luaL_optnumber(L, 2, 0.0f);
  455. float y = (float)luaL_optnumber(L, 3, 0.0f);
  456. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  457. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  458. float sy = (float)luaL_optnumber(L, 6, sx);
  459. float ox = (float)luaL_optnumber(L, 7, 0);
  460. float oy = (float)luaL_optnumber(L, 8, 0);
  461. drawable->draw(x, y, angle, sx, sy, ox, oy);
  462. return 0;
  463. }
  464. /**
  465. * Draws an Quad of an Image at the specified coordinates,
  466. * with rotation and scaling along both axes.
  467. *
  468. * @param q The Quad to draw.
  469. * @param x The x-coordinate.
  470. * @param y The y-coordinate.
  471. * @param angle The amount of rotation.
  472. * @param sx The scale factor along the x-axis. (1 = normal).
  473. * @param sy The scale factor along the y-axis. (1 = normal).
  474. * @param ox The offset along the x-axis.
  475. * @param oy The offset along the y-axis.
  476. **/
  477. int w_drawq(lua_State * L)
  478. {
  479. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  480. Quad * q = luax_checkframe(L, 2);
  481. float x = (float)luaL_checknumber(L, 3);
  482. float y = (float)luaL_checknumber(L, 4);
  483. float angle = (float)luaL_optnumber(L, 5, 0);
  484. float sx = (float)luaL_optnumber(L, 6, 1);
  485. float sy = (float)luaL_optnumber(L, 7, sx);
  486. float ox = (float)luaL_optnumber(L, 8, 0);
  487. float oy = (float)luaL_optnumber(L, 9, 0);
  488. image->drawq(q, x, y, angle, sx, sy, ox, oy);
  489. return 0;
  490. }
  491. int w_drawTest(lua_State * L)
  492. {
  493. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  494. float x = (float)luaL_optnumber(L, 2, 0.0f);
  495. float y = (float)luaL_optnumber(L, 3, 0.0f);
  496. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  497. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  498. float sy = (float)luaL_optnumber(L, 6, sx);
  499. float ox = (float)luaL_optnumber(L, 7, 0);
  500. float oy = (float)luaL_optnumber(L, 8, 0);
  501. instance->drawTest(image, x, y, angle, sx, sy, ox, oy);
  502. return 0;
  503. }
  504. int w_print1(lua_State * L)
  505. {
  506. const char * str = luaL_checkstring(L, 1);
  507. float x = (float)luaL_checknumber(L, 2);
  508. float y = (float)luaL_checknumber(L, 3);
  509. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  510. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  511. float sy = (float)luaL_optnumber(L, 6, sx);
  512. switch(lua_gettop(L))
  513. {
  514. case 3:
  515. instance->print(str, x, y);
  516. break;
  517. case 4:
  518. instance->print(str, x, y, angle);
  519. break;
  520. case 5:
  521. instance->print(str, x, y, angle, sx);
  522. break;
  523. case 6:
  524. instance->print(str, x, y, angle, sx, sy);
  525. break;
  526. default:
  527. return luaL_error(L, "Incorrect number of parameters");
  528. }
  529. return 0;
  530. }
  531. int w_printf1(lua_State * L)
  532. {
  533. const char * str = luaL_checkstring(L, 1);
  534. float x = (float)luaL_checknumber(L, 2);
  535. float y = (float)luaL_checknumber(L, 3);
  536. float wrap = (float)luaL_checknumber(L, 4);
  537. Graphics::AlignMode align = Graphics::ALIGN_LEFT;
  538. if(lua_gettop(L) >= 5)
  539. {
  540. const char * str = luaL_checkstring(L, 5);
  541. if(!Graphics::getConstant(str, align))
  542. return luaL_error(L, "Incorrect alignment: %s", str);
  543. }
  544. instance->printf(str, x, y, wrap, align);
  545. return 0;
  546. }
  547. int w_point(lua_State * L)
  548. {
  549. float x = (float)luaL_checknumber(L, 1);
  550. float y = (float)luaL_checknumber(L, 2);
  551. instance->point(x, y);
  552. return 0;
  553. }
  554. int w_line(lua_State * L)
  555. {
  556. float x1 = (float)luaL_checknumber(L, 1);
  557. float y1 = (float)luaL_checknumber(L, 2);
  558. float x2 = (float)luaL_checknumber(L, 3);
  559. float y2 = (float)luaL_checknumber(L, 4);
  560. instance->line(x1, y1, x2, y2);
  561. return 0;
  562. }
  563. int w_triangle(lua_State * L)
  564. {
  565. Graphics::DrawMode mode;
  566. const char * str = luaL_checkstring(L, 1);
  567. if(!Graphics::getConstant(str, mode))
  568. return luaL_error(L, "Incorrect draw mode %s", str);
  569. float x1 = (float)luaL_checknumber(L, 2);
  570. float y1 = (float)luaL_checknumber(L, 3);
  571. float x2 = (float)luaL_checknumber(L, 4);
  572. float y2 = (float)luaL_checknumber(L, 5);
  573. float x3 = (float)luaL_checknumber(L, 6);
  574. float y3 = (float)luaL_checknumber(L, 7);
  575. instance->triangle(mode, x1, y1, x2, y2, x3, y3);
  576. return 0;
  577. }
  578. int w_rectangle(lua_State * L)
  579. {
  580. Graphics::DrawMode mode;
  581. const char * str = luaL_checkstring(L, 1);
  582. if(!Graphics::getConstant(str, mode))
  583. return luaL_error(L, "Incorrect draw mode %s", str);
  584. float x = (float)luaL_checknumber(L, 2);
  585. float y = (float)luaL_checknumber(L, 3);
  586. float w = (float)luaL_checknumber(L, 4);
  587. float h = (float)luaL_checknumber(L, 5);
  588. instance->rectangle(mode, x, y, w, h);
  589. return 0;
  590. }
  591. int w_quad(lua_State * L)
  592. {
  593. Graphics::DrawMode mode;
  594. const char * str = luaL_checkstring(L, 1);
  595. if(!Graphics::getConstant(str, mode))
  596. return luaL_error(L, "Incorrect draw mode %s", str);
  597. float x1 = (float)luaL_checknumber(L, 2);
  598. float y1 = (float)luaL_checknumber(L, 3);
  599. float x2 = (float)luaL_checknumber(L, 4);
  600. float y2 = (float)luaL_checknumber(L, 5);
  601. float x3 = (float)luaL_checknumber(L, 6);
  602. float y3 = (float)luaL_checknumber(L, 7);
  603. float x4 = (float)luaL_checknumber(L, 8);
  604. float y4 = (float)luaL_checknumber(L, 9);
  605. instance->quad(mode, x1, y1, x2, y2, x3, y3, x4, y4);
  606. return 0;
  607. }
  608. int w_circle(lua_State * L)
  609. {
  610. Graphics::DrawMode mode;
  611. const char * str = luaL_checkstring(L, 1);
  612. if(!Graphics::getConstant(str, mode))
  613. return luaL_error(L, "Incorrect draw mode %s", str);
  614. float x = (float)luaL_checknumber(L, 2);
  615. float y = (float)luaL_checknumber(L, 3);
  616. float radius = (float)luaL_checknumber(L, 4);
  617. int points = luaL_optint(L, 5, 10);
  618. instance->circle(mode, x, y, radius, points);
  619. return 0;
  620. }
  621. int w_polygon(lua_State * L)
  622. {
  623. return instance->polygon(L);
  624. }
  625. int w_push(lua_State * L)
  626. {
  627. instance->push();
  628. return 0;
  629. }
  630. int w_pop(lua_State * L)
  631. {
  632. instance->pop();
  633. return 0;
  634. }
  635. int w_rotate(lua_State * L)
  636. {
  637. float deg = (float)luaL_checknumber(L, 1);
  638. instance->rotate(deg);
  639. return 0;
  640. }
  641. int w_scale(lua_State * L)
  642. {
  643. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  644. float sy = (float)luaL_optnumber(L, 2, sx);
  645. instance->scale(sx, sy);
  646. return 0;
  647. }
  648. int w_translate(lua_State * L)
  649. {
  650. float x = (float)luaL_checknumber(L, 1);
  651. float y = (float)luaL_checknumber(L, 2);
  652. instance->translate(x, y);
  653. return 0;
  654. }
  655. // List of functions to wrap.
  656. static const luaL_Reg functions[] = {
  657. { "checkMode", w_checkMode },
  658. { "setMode", w_setMode },
  659. { "toggleFullscreen", w_toggleFullscreen },
  660. { "reset", w_reset },
  661. { "clear", w_clear },
  662. { "present", w_present },
  663. { "newImage", w_newImage },
  664. { "newGlyph", w_newGlyph },
  665. { "newQuad", w_newQuad },
  666. { "newFont1", w_newFont1 },
  667. { "newImageFont", w_newImageFont },
  668. { "newSpriteBatch", w_newSpriteBatch },
  669. { "newParticleSystem", w_newParticleSystem },
  670. { "setColor", w_setColor },
  671. { "getColor", w_getColor },
  672. { "setBackgroundColor", w_setBackgroundColor },
  673. { "getBackgroundColor", w_getBackgroundColor },
  674. { "setFont1", w_setFont1 },
  675. { "getFont", w_getFont },
  676. { "setBlendMode", w_setBlendMode },
  677. { "setColorMode", w_setColorMode },
  678. { "getBlendMode", w_getBlendMode },
  679. { "getColorMode", w_getColorMode },
  680. { "setLineWidth", w_setLineWidth },
  681. { "setLineStyle", w_setLineStyle },
  682. { "setLine", w_setLine },
  683. { "setLineStipple", w_setLineStipple },
  684. { "getLineWidth", w_getLineWidth },
  685. { "getLineStyle", w_getLineStyle },
  686. { "getLineStipple", w_getLineStipple },
  687. { "setPointSize", w_setPointSize },
  688. { "setPointStyle", w_setPointStyle },
  689. { "setPoint", w_setPoint },
  690. { "getPointSize", w_getPointSize },
  691. { "getPointStyle", w_getPointStyle },
  692. { "getMaxPointSize", w_getMaxPointSize },
  693. { "newScreenshot", w_newScreenshot },
  694. { "draw", w_draw },
  695. { "drawq", w_drawq },
  696. { "drawTest", w_drawTest },
  697. { "print1", w_print1 },
  698. { "printf1", w_printf1 },
  699. { "setCaption", w_setCaption },
  700. { "getCaption", w_getCaption },
  701. { "getWidth", w_getWidth },
  702. { "getHeight", w_getHeight },
  703. { "isCreated", w_isCreated },
  704. { "getModes", w_getModes },
  705. { "setScissor", w_setScissor },
  706. { "getScissor", w_getScissor },
  707. { "point", w_point },
  708. { "line", w_line },
  709. { "triangle", w_triangle },
  710. { "rectangle", w_rectangle },
  711. { "quad", w_quad },
  712. { "circle", w_circle },
  713. { "polygon", w_polygon },
  714. { "push", w_push },
  715. { "pop", w_pop },
  716. { "rotate", w_rotate },
  717. { "scale", w_scale },
  718. { "translate", w_translate },
  719. { 0, 0 }
  720. };
  721. // Types for this module.
  722. static const lua_CFunction types[] = {
  723. luaopen_font,
  724. luaopen_image,
  725. luaopen_glyph,
  726. luaopen_frame,
  727. luaopen_spritebatch,
  728. luaopen_particlesystem,
  729. 0
  730. };
  731. int luaopen_love_graphics(lua_State * L)
  732. {
  733. if(instance == 0)
  734. {
  735. try
  736. {
  737. instance = new Graphics();
  738. }
  739. catch(Exception & e)
  740. {
  741. return luaL_error(L, e.what());
  742. }
  743. }
  744. WrappedModule w;
  745. w.module = instance;
  746. w.name = "graphics";
  747. w.flags = MODULE_T;
  748. w.functions = functions;
  749. w.types = types;
  750. luax_register_module(L, w);
  751. if (luaL_loadbuffer(L, graphics_lua, sizeof(graphics_lua), "graphics.lua") == 0)
  752. lua_call(L, 0, 0);
  753. return 0;
  754. }
  755. } // opengl
  756. } // graphics
  757. } // love