wrap_Graphics.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /**
  2. * Copyright (c) 2006-2011 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 <font/Rasterizer.h>
  23. #include <scripts/graphics.lua.h>
  24. namespace love
  25. {
  26. namespace graphics
  27. {
  28. namespace opengl
  29. {
  30. static Graphics * instance = 0;
  31. int w_checkMode(lua_State * L)
  32. {
  33. int w = luaL_checkint(L, 1);
  34. int h = luaL_checkint(L, 2);
  35. bool fs = luax_toboolean(L, 3);
  36. luax_pushboolean(L, instance->checkMode(w, h, fs));
  37. return 1;
  38. }
  39. int w_setMode(lua_State * L)
  40. {
  41. int w = luaL_checkint(L, 1);
  42. int h = luaL_checkint(L, 2);
  43. bool fs = luax_optboolean(L, 3, false);
  44. bool vsync = luax_optboolean(L, 4, true);
  45. int fsaa = luaL_optint(L, 5, 0);
  46. luax_pushboolean(L, instance->setMode(w, h, fs, vsync, fsaa));
  47. return 1;
  48. }
  49. int w_toggleFullscreen(lua_State * L)
  50. {
  51. luax_pushboolean(L, instance->toggleFullscreen());
  52. return 1;
  53. }
  54. int w_reset(lua_State *)
  55. {
  56. instance->reset();
  57. return 0;
  58. }
  59. int w_clear(lua_State *)
  60. {
  61. instance->clear();
  62. return 0;
  63. }
  64. int w_present(lua_State *)
  65. {
  66. instance->present();
  67. return 0;
  68. }
  69. int w_setIcon(lua_State * L)
  70. {
  71. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  72. instance->setIcon(image);
  73. return 0;
  74. }
  75. int w_setCaption(lua_State * L)
  76. {
  77. const char * str = luaL_checkstring(L, 1);
  78. instance->setCaption(str);
  79. return 0;
  80. }
  81. int w_getCaption(lua_State * L)
  82. {
  83. return instance->getCaption(L);
  84. }
  85. int w_getWidth(lua_State * L)
  86. {
  87. lua_pushnumber(L, instance->getWidth());
  88. return 1;
  89. }
  90. int w_getHeight(lua_State * L)
  91. {
  92. lua_pushnumber(L, instance->getHeight());
  93. return 1;
  94. }
  95. int w_isCreated(lua_State * L)
  96. {
  97. luax_pushboolean(L, instance->isCreated());
  98. return 1;
  99. }
  100. int w_getModes(lua_State * L)
  101. {
  102. return instance->getModes(L);
  103. }
  104. int w_setScissor(lua_State * L)
  105. {
  106. if(lua_gettop(L) == 0)
  107. {
  108. instance->setScissor();
  109. return 0;
  110. }
  111. int x = luaL_checkint(L, 1);
  112. int y = luaL_checkint(L, 2);
  113. int w = luaL_checkint(L, 3);
  114. int h = luaL_checkint(L, 4);
  115. if (w < 0 || h < 0)
  116. return luaL_error(L, "Can't set scissor with negative width and/or height.");
  117. instance->setScissor(x, y, w, h);
  118. return 0;
  119. }
  120. int w_getScissor(lua_State * L)
  121. {
  122. return instance->getScissor(L);
  123. }
  124. int w_defineMask(lua_State * L)
  125. {
  126. // just return the function
  127. if (!lua_isfunction(L, 1))
  128. return luaL_typerror(L, 1, "function");
  129. lua_settop(L, 1);
  130. return 1;
  131. }
  132. int w_setMask(lua_State * L)
  133. {
  134. // no argument -> clear mask
  135. if (lua_isnoneornil(L, 1)) {
  136. instance->discardMask();
  137. return 0;
  138. }
  139. if (!lua_isfunction(L, 1))
  140. return luaL_typerror(L, 1, "mask");
  141. instance->defineMask();
  142. lua_call(L, lua_gettop(L) - 1, 0); // call mask(...)
  143. instance->useMask();
  144. return 0;
  145. }
  146. int w_newImage(lua_State * L)
  147. {
  148. // Convert to File, if necessary.
  149. if(lua_isstring(L, 1))
  150. luax_convobj(L, 1, "filesystem", "newFile");
  151. // Convert to ImageData, if necessary.
  152. if(luax_istype(L, 1, FILESYSTEM_FILE_T))
  153. luax_convobj(L, 1, "image", "newImageData");
  154. love::image::ImageData * data = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
  155. // Create the image.
  156. Image * image = 0;
  157. try {
  158. image = instance->newImage(data);
  159. } catch (love::Exception & e) {
  160. luaL_error(L, e.what());
  161. }
  162. if(image == 0)
  163. return luaL_error(L, "Could not load image.");
  164. // Push the type.
  165. luax_newtype(L, "Image", GRAPHICS_IMAGE_T, (void*)image);
  166. return 1;
  167. }
  168. int w_newQuad(lua_State * L)
  169. {
  170. int x = luaL_checkint(L, 1);
  171. int y = luaL_checkint(L, 2);
  172. int w = luaL_checkint(L, 3);
  173. int h = luaL_checkint(L, 4);
  174. int sw = luaL_checkint(L, 5);
  175. int sh = luaL_checkint(L, 6);
  176. Quad * frame = instance->newQuad(x, y, w, h, sw, sh);
  177. if (frame == 0)
  178. return luaL_error(L, "Could not create frame.");
  179. luax_newtype(L, "Quad", GRAPHICS_QUAD_T, (void*)frame);
  180. return 1;
  181. }
  182. int w_newFont1(lua_State * L)
  183. {
  184. // Convert to File, if necessary.
  185. if(lua_isstring(L, 1))
  186. luax_convobj(L, 1, "filesystem", "newFile");
  187. // Convert to Data, if necessary.
  188. if(luax_istype(L, 1, FILESYSTEM_FILE_T)) {
  189. love::filesystem::File * f = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
  190. Data * d;
  191. try {
  192. d = f->read();
  193. }
  194. catch (love::Exception & e) {
  195. return luaL_error(L, e.what());
  196. }
  197. lua_remove(L, 1); // get rid of the file
  198. luax_newtype(L, "Data", DATA_T, (void*)d);
  199. lua_insert(L, 1); // put it at the bottom of the stack
  200. }
  201. // Convert to Rasterizer, if necessary.
  202. if(luax_istype(L, 1, DATA_T)) {
  203. int idxs[] = {1, 2};
  204. luax_convobj(L, idxs, 2, "font", "newRasterizer");
  205. }
  206. love::font::Rasterizer * rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, "Rasterizer", FONT_RASTERIZER_T);
  207. // Create the font.
  208. Font * font = instance->newFont(rasterizer);
  209. if(font == 0)
  210. return luaL_error(L, "Could not load font.");
  211. // Push the type.
  212. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font);
  213. return 1;
  214. }
  215. int w_newImageFont(lua_State * L)
  216. {
  217. // filter for glyphs, defaults to linear/linear
  218. Image::Filter img_filter;
  219. // Convert to ImageData if necessary.
  220. 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)))
  221. luax_convobj(L, 1, "image", "newImageData");
  222. else if(luax_istype(L, 1, GRAPHICS_IMAGE_T)) {
  223. Image * i = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  224. img_filter = i->getFilter();
  225. love::image::ImageData * id = i->getData();
  226. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)id, false);
  227. lua_replace(L, 1);
  228. }
  229. // Convert to Rasterizer if necessary.
  230. if(luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) {
  231. int idxs[] = {1, 2};
  232. luax_convobj(L, idxs, 2, "font", "newRasterizer");
  233. }
  234. love::font::Rasterizer * rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, "Rasterizer", FONT_RASTERIZER_T);
  235. // Create the font.
  236. Font * font = instance->newFont(rasterizer, img_filter);
  237. if(font == 0)
  238. return luaL_error(L, "Could not load font.");
  239. // Push the type.
  240. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font);
  241. return 1;
  242. }
  243. int w_newSpriteBatch(lua_State * L)
  244. {
  245. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  246. int size = luaL_optint(L, 2, 1000);
  247. int usage = luaL_optint(L, 3, SpriteBatch::USAGE_DYNAMIC);
  248. SpriteBatch * t = instance->newSpriteBatch(image, size, usage);
  249. luax_newtype(L, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T, (void*)t);
  250. return 1;
  251. }
  252. int w_newParticleSystem(lua_State * L)
  253. {
  254. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  255. int size = luaL_checkint(L, 2);
  256. ParticleSystem * t = instance->newParticleSystem(image, size);
  257. luax_newtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, (void*)t);
  258. return 1;
  259. }
  260. int w_newFramebuffer(lua_State * L)
  261. {
  262. // check if width and height are given. else default to screen dimensions.
  263. int width = luaL_optint(L, 1, instance->getWidth());
  264. int height = luaL_optint(L, 2, instance->getHeight());
  265. glGetError(); // clear opengl error flag
  266. Framebuffer * framebuffer = instance->newFramebuffer(width, height);
  267. //and there we go with the status... still disliked
  268. if (framebuffer->getStatus() != GL_FRAMEBUFFER_COMPLETE) {
  269. switch (framebuffer->getStatus()) {
  270. case GL_FRAMEBUFFER_UNSUPPORTED:
  271. return luaL_error(L, "Cannot create Framebuffer: "
  272. "Not supported by your OpenGL implementation");
  273. // remaining error codes are highly unlikely:
  274. case GL_FRAMEBUFFER_UNDEFINED:
  275. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  276. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  277. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  278. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  279. case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  280. return luaL_error(L, "Cannot create Framebuffer: "
  281. "Error in implementation (please inform the love devs)");
  282. default:
  283. // my intel hda card wrongly returns 0 to glCheckFramebufferStatus() but sets
  284. // no error flag. I think it meant to return GL_FRAMEBUFFER_UNSUPPORTED, but who
  285. // knows.
  286. if (glGetError() == GL_NO_ERROR)
  287. return luaL_error(L, "Cannot create Framebuffer: "
  288. "May not be supported by your OpenGL implementation.");
  289. // the remaining error is an indication of a serious fuckup since it should
  290. // only be returned if glCheckFramebufferStatus() was called with the wrong
  291. // arguments.
  292. return luaL_error(L, "Cannot create Framebuffer: Aliens did it (OpenGL error code: %d)", glGetError());
  293. }
  294. }
  295. luax_newtype(L, "Framebuffer", GRAPHICS_FRAMEBUFFER_T, (void*)framebuffer);
  296. return 1;
  297. }
  298. int w_setColor(lua_State * L)
  299. {
  300. Color c;
  301. if (lua_istable(L, 1)) {
  302. lua_pushinteger(L, 1);
  303. lua_gettable(L, -2);
  304. c.r = (unsigned char)luaL_checkint(L, -1);
  305. lua_pop(L, 1);
  306. lua_pushinteger(L, 2);
  307. lua_gettable(L, -2);
  308. c.g = (unsigned char)luaL_checkint(L, -1);
  309. lua_pop(L, 1);
  310. lua_pushinteger(L, 3);
  311. lua_gettable(L, -2);
  312. c.b = (unsigned char)luaL_checkint(L, -1);
  313. lua_pop(L, 1);
  314. lua_pushinteger(L, 4);
  315. lua_gettable(L, -2);
  316. c.a = (unsigned char)luaL_optint(L, -1, 255);
  317. lua_pop(L, 1);
  318. }
  319. else
  320. {
  321. c.r = (unsigned char)luaL_checkint(L, 1);
  322. c.g = (unsigned char)luaL_checkint(L, 2);
  323. c.b = (unsigned char)luaL_checkint(L, 3);
  324. c.a = (unsigned char)luaL_optint(L, 4, 255);
  325. }
  326. instance->setColor(c);
  327. return 0;
  328. }
  329. int w_getColor(lua_State * L)
  330. {
  331. Color c = instance->getColor();
  332. lua_pushinteger(L, c.r);
  333. lua_pushinteger(L, c.g);
  334. lua_pushinteger(L, c.b);
  335. lua_pushinteger(L, c.a);
  336. return 4;
  337. }
  338. int w_setBackgroundColor(lua_State * L)
  339. {
  340. Color c;
  341. if (lua_istable(L, 1)) {
  342. lua_pushinteger(L, 1);
  343. lua_gettable(L, -2);
  344. c.r = (unsigned char)luaL_checkint(L, -1);
  345. lua_pop(L, 1);
  346. lua_pushinteger(L, 2);
  347. lua_gettable(L, -2);
  348. c.g = (unsigned char)luaL_checkint(L, -1);
  349. lua_pop(L, 1);
  350. lua_pushinteger(L, 3);
  351. lua_gettable(L, -2);
  352. c.b = (unsigned char)luaL_checkint(L, -1);
  353. lua_pop(L, 1);
  354. lua_pushinteger(L, 4);
  355. lua_gettable(L, -2);
  356. c.a = (unsigned char)luaL_optint(L, -1, 255);
  357. lua_pop(L, 1);
  358. }
  359. else
  360. {
  361. c.r = (unsigned char)luaL_checkint(L, 1);
  362. c.g = (unsigned char)luaL_checkint(L, 2);
  363. c.b = (unsigned char)luaL_checkint(L, 3);
  364. c.a = (unsigned char)luaL_optint(L, 4, 255);
  365. }
  366. instance->setBackgroundColor(c);
  367. return 0;
  368. }
  369. int w_getBackgroundColor(lua_State * L)
  370. {
  371. Color c = instance->getBackgroundColor();
  372. lua_pushinteger(L, c.r);
  373. lua_pushinteger(L, c.g);
  374. lua_pushinteger(L, c.b);
  375. lua_pushinteger(L, c.a);
  376. return 4;
  377. }
  378. int w_setFont1(lua_State * L)
  379. {
  380. // The second parameter is an optional int.
  381. int size = luaL_optint(L, 2, 12);
  382. Font * font;
  383. bool created = false;
  384. // If the first parameter isn't a Font, create a new one
  385. if (!luax_istype(L, 1, GRAPHICS_FONT_T)) {
  386. created = true;
  387. lua_pushinteger(L, size); // push the size
  388. lua_insert(L, 2); // move it to its proper place
  389. // Convert to File, if necessary.
  390. if(lua_isstring(L, 1))
  391. luax_convobj(L, 1, "filesystem", "newFile");
  392. // Convert to Data, if necessary.
  393. if(luax_istype(L, 1, FILESYSTEM_FILE_T)) {
  394. love::filesystem::File * f = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
  395. Data * d;
  396. try {
  397. d = f->read();
  398. } catch (love::Exception & e) {
  399. return luaL_error(L, e.what());
  400. }
  401. lua_remove(L, 1); // get rid of the file
  402. luax_newtype(L, "Data", DATA_T, (void*)d);
  403. lua_insert(L, 1); // put it at the bottom of the stack
  404. }
  405. // Convert to Rasterizer, if necessary.
  406. if(luax_istype(L, 1, DATA_T)) {
  407. int idxs[] = {1, 2};
  408. luax_convobj(L, idxs, 2, "font", "newRasterizer");
  409. }
  410. love::font::Rasterizer * rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, "Rasterizer", FONT_RASTERIZER_T);
  411. // Create the font.
  412. font = instance->newFont(rasterizer);
  413. if(font == 0)
  414. return luaL_error(L, "Could not load font.");
  415. }
  416. else font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
  417. instance->setFont(font);
  418. if (created)
  419. font->release();
  420. return 0;
  421. }
  422. int w_getFont(lua_State * L)
  423. {
  424. Font * f = instance->getFont();
  425. if(f == 0)
  426. return 0;
  427. f->retain();
  428. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)f);
  429. return 1;
  430. }
  431. int w_setBlendMode(lua_State * L)
  432. {
  433. Graphics::BlendMode mode;
  434. const char * str = luaL_checkstring(L, 1);
  435. if(!Graphics::getConstant(str, mode))
  436. return luaL_error(L, "Invalid blend mode: %s", str);
  437. instance->setBlendMode(mode);
  438. return 0;
  439. }
  440. int w_setColorMode(lua_State * L)
  441. {
  442. Graphics::ColorMode mode;
  443. const char * str = luaL_checkstring(L, 1);
  444. if(!Graphics::getConstant(str, mode))
  445. return luaL_error(L, "Invalid color mode: %s", str);
  446. instance->setColorMode(mode);
  447. return 0;
  448. }
  449. int w_getBlendMode(lua_State * L)
  450. {
  451. Graphics::BlendMode mode = instance->getBlendMode();
  452. const char * str;
  453. if(!Graphics::getConstant(mode, str))
  454. return luaL_error(L, "Invalid blend mode: %s", str);
  455. lua_pushstring(L, str);
  456. return 1;
  457. }
  458. int w_getColorMode(lua_State * L)
  459. {
  460. Graphics::ColorMode mode = instance->getColorMode();
  461. const char * str;
  462. if(!Graphics::getConstant(mode, str))
  463. return luaL_error(L, "Invalid color mode: %s", str);
  464. lua_pushstring(L, str);
  465. return 1;
  466. }
  467. int w_setLineWidth(lua_State * L)
  468. {
  469. float width = (float)luaL_checknumber(L, 1);
  470. instance->setLineWidth(width);
  471. return 0;
  472. }
  473. int w_setLineStyle(lua_State * L)
  474. {
  475. Graphics::LineStyle style;
  476. const char * str = luaL_checkstring(L, 1);
  477. if(!Graphics::getConstant(str, style))
  478. return luaL_error(L, "Invalid line style: %s", str);
  479. instance->setLineStyle(style);
  480. return 0;
  481. }
  482. int w_setLine(lua_State * L)
  483. {
  484. float width = (float)luaL_checknumber(L, 1);
  485. Graphics::LineStyle style = Graphics::LINE_SMOOTH;
  486. if(lua_gettop(L) >= 2)
  487. {
  488. const char * str = luaL_checkstring(L, 2);
  489. if(!Graphics::getConstant(str, style))
  490. return luaL_error(L, "Invalid line style: %s", str);
  491. }
  492. instance->setLine(width, style);
  493. return 0;
  494. }
  495. int w_getLineWidth(lua_State * L)
  496. {
  497. lua_pushnumber(L, instance->getLineWidth());
  498. return 1;
  499. }
  500. int w_getLineStyle(lua_State * L)
  501. {
  502. Graphics::LineStyle style = instance->getLineStyle();
  503. const char *str;
  504. Graphics::getConstant(style, str);
  505. lua_pushstring(L, str);
  506. return 1;
  507. }
  508. int w_setPointSize(lua_State * L)
  509. {
  510. float size = (float)luaL_checknumber(L, 1);
  511. instance->setPointSize(size);
  512. return 0;
  513. }
  514. int w_setPointStyle(lua_State * L)
  515. {
  516. Graphics::PointStyle style = Graphics::POINT_SMOOTH;
  517. if(lua_gettop(L) >= 2)
  518. {
  519. const char * str = luaL_checkstring(L, 1);
  520. if(!Graphics::getConstant(str, style))
  521. return luaL_error(L, "Invalid point style: %s", str);
  522. }
  523. instance->setPointStyle(style);
  524. return 0;
  525. }
  526. int w_setPoint(lua_State * L)
  527. {
  528. float size = (float)luaL_checknumber(L, 1);
  529. Graphics::PointStyle style;
  530. const char * str = luaL_checkstring(L, 2);
  531. if(!Graphics::getConstant(str, style))
  532. return luaL_error(L, "Invalid point style: %s", str);
  533. instance->setPoint(size, style);
  534. return 0;
  535. }
  536. int w_getPointSize(lua_State * L)
  537. {
  538. lua_pushnumber(L, instance->getPointSize());
  539. return 1;
  540. }
  541. int w_getPointStyle(lua_State * L)
  542. {
  543. lua_pushinteger(L, instance->getPointStyle());
  544. return 1;
  545. }
  546. int w_getMaxPointSize(lua_State * L)
  547. {
  548. lua_pushnumber(L, instance->getMaxPointSize());
  549. return 1;
  550. }
  551. int w_newScreenshot(lua_State * L)
  552. {
  553. love::image::Image * image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  554. love::image::ImageData * i = instance->newScreenshot(image);
  555. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)i);
  556. return 1;
  557. }
  558. int w_setRenderTarget(lua_State * L)
  559. {
  560. // called with nil or none -> reset to default buffer
  561. if (lua_isnoneornil(L,1)) {
  562. Framebuffer::bindDefaultBuffer();
  563. return 0;
  564. }
  565. Framebuffer * fbo = luax_checkfbo(L, 1);
  566. // this unbinds the previous fbo
  567. fbo->startGrab();
  568. return 0;
  569. }
  570. /**
  571. * Draws an Image at the specified coordinates, with rotation and
  572. * scaling along both axes.
  573. * @param x The x-coordinate.
  574. * @param y The y-coordinate.
  575. * @param angle The amount of rotation.
  576. * @param sx The scale factor along the x-axis. (1 = normal).
  577. * @param sy The scale factor along the y-axis. (1 = normal).
  578. * @param ox The offset along the x-axis.
  579. * @param oy The offset along the y-axis.
  580. **/
  581. int w_draw(lua_State * L)
  582. {
  583. Drawable * drawable = luax_checktype<Drawable>(L, 1, "Drawable", GRAPHICS_DRAWABLE_T);
  584. float x = (float)luaL_optnumber(L, 2, 0.0f);
  585. float y = (float)luaL_optnumber(L, 3, 0.0f);
  586. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  587. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  588. float sy = (float)luaL_optnumber(L, 6, sx);
  589. float ox = (float)luaL_optnumber(L, 7, 0);
  590. float oy = (float)luaL_optnumber(L, 8, 0);
  591. drawable->draw(x, y, angle, sx, sy, ox, oy);
  592. return 0;
  593. }
  594. /**
  595. * Draws an Quad of an Image at the specified coordinates,
  596. * with rotation and scaling along both axes.
  597. *
  598. * @param q The Quad to draw.
  599. * @param x The x-coordinate.
  600. * @param y The y-coordinate.
  601. * @param angle The amount of rotation.
  602. * @param sx The scale factor along the x-axis. (1 = normal).
  603. * @param sy The scale factor along the y-axis. (1 = normal).
  604. * @param ox The offset along the x-axis.
  605. * @param oy The offset along the y-axis.
  606. **/
  607. int w_drawq(lua_State * L)
  608. {
  609. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  610. Quad * q = luax_checkframe(L, 2);
  611. float x = (float)luaL_checknumber(L, 3);
  612. float y = (float)luaL_checknumber(L, 4);
  613. float angle = (float)luaL_optnumber(L, 5, 0);
  614. float sx = (float)luaL_optnumber(L, 6, 1);
  615. float sy = (float)luaL_optnumber(L, 7, sx);
  616. float ox = (float)luaL_optnumber(L, 8, 0);
  617. float oy = (float)luaL_optnumber(L, 9, 0);
  618. image->drawq(q, x, y, angle, sx, sy, ox, oy);
  619. return 0;
  620. }
  621. int w_drawTest(lua_State * L)
  622. {
  623. Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
  624. float x = (float)luaL_optnumber(L, 2, 0.0f);
  625. float y = (float)luaL_optnumber(L, 3, 0.0f);
  626. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  627. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  628. float sy = (float)luaL_optnumber(L, 6, sx);
  629. float ox = (float)luaL_optnumber(L, 7, 0);
  630. float oy = (float)luaL_optnumber(L, 8, 0);
  631. instance->drawTest(image, x, y, angle, sx, sy, ox, oy);
  632. return 0;
  633. }
  634. int w_print1(lua_State * L)
  635. {
  636. const char * str = luaL_checkstring(L, 1);
  637. float x = (float)luaL_checknumber(L, 2);
  638. float y = (float)luaL_checknumber(L, 3);
  639. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  640. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  641. float sy = (float)luaL_optnumber(L, 6, sx);
  642. instance->print(str, x, y, angle, sx, sy);
  643. return 0;
  644. }
  645. int w_printf1(lua_State * L)
  646. {
  647. const char * str = luaL_checkstring(L, 1);
  648. float x = (float)luaL_checknumber(L, 2);
  649. float y = (float)luaL_checknumber(L, 3);
  650. float wrap = (float)luaL_checknumber(L, 4);
  651. Graphics::AlignMode align = Graphics::ALIGN_LEFT;
  652. if(lua_gettop(L) >= 5)
  653. {
  654. const char * str = luaL_checkstring(L, 5);
  655. if(!Graphics::getConstant(str, align))
  656. return luaL_error(L, "Incorrect alignment: %s", str);
  657. }
  658. instance->printf(str, x, y, wrap, align);
  659. return 0;
  660. }
  661. int w_point(lua_State * L)
  662. {
  663. float x = (float)luaL_checknumber(L, 1);
  664. float y = (float)luaL_checknumber(L, 2);
  665. instance->point(x, y);
  666. return 0;
  667. }
  668. int w_line(lua_State * L)
  669. {
  670. int args = lua_gettop(L);
  671. bool is_table = false;
  672. if (args == 1 && lua_istable(L, 1)) {
  673. args = lua_objlen(L, 1);
  674. is_table = true;
  675. }
  676. if (args % 2 != 0)
  677. return luaL_error(L, "Number of vertices must be a multiple of two");
  678. else if (args < 4)
  679. return luaL_error(L, "Need at least two vertices to draw a line");
  680. float* coords = new float[args];
  681. if (is_table) {
  682. for (int i = 0; i < args; ++i) {
  683. lua_pushnumber(L, i + 1);
  684. lua_rawget(L, 1);
  685. coords[i] = lua_tonumber(L, -1);
  686. lua_pop(L, 1);
  687. }
  688. } else {
  689. for (int i = 0; i < args; ++i)
  690. coords[i] = lua_tonumber(L, i + 1);
  691. }
  692. instance->polyline(coords, args);
  693. delete[] coords;
  694. return 0;
  695. }
  696. int w_triangle(lua_State * L)
  697. {
  698. Graphics::DrawMode mode;
  699. const char * str = luaL_checkstring(L, 1);
  700. if(!Graphics::getConstant(str, mode))
  701. return luaL_error(L, "Incorrect draw mode %s", str);
  702. float x1 = (float)luaL_checknumber(L, 2);
  703. float y1 = (float)luaL_checknumber(L, 3);
  704. float x2 = (float)luaL_checknumber(L, 4);
  705. float y2 = (float)luaL_checknumber(L, 5);
  706. float x3 = (float)luaL_checknumber(L, 6);
  707. float y3 = (float)luaL_checknumber(L, 7);
  708. instance->triangle(mode, x1, y1, x2, y2, x3, y3);
  709. return 0;
  710. }
  711. int w_rectangle(lua_State * L)
  712. {
  713. Graphics::DrawMode mode;
  714. const char * str = luaL_checkstring(L, 1);
  715. if(!Graphics::getConstant(str, mode))
  716. return luaL_error(L, "Incorrect draw mode %s", str);
  717. float x = (float)luaL_checknumber(L, 2);
  718. float y = (float)luaL_checknumber(L, 3);
  719. float w = (float)luaL_checknumber(L, 4);
  720. float h = (float)luaL_checknumber(L, 5);
  721. instance->rectangle(mode, x, y, w, h);
  722. return 0;
  723. }
  724. int w_quad(lua_State * L)
  725. {
  726. Graphics::DrawMode mode;
  727. const char * str = luaL_checkstring(L, 1);
  728. if(!Graphics::getConstant(str, mode))
  729. return luaL_error(L, "Incorrect draw mode %s", str);
  730. float x1 = (float)luaL_checknumber(L, 2);
  731. float y1 = (float)luaL_checknumber(L, 3);
  732. float x2 = (float)luaL_checknumber(L, 4);
  733. float y2 = (float)luaL_checknumber(L, 5);
  734. float x3 = (float)luaL_checknumber(L, 6);
  735. float y3 = (float)luaL_checknumber(L, 7);
  736. float x4 = (float)luaL_checknumber(L, 8);
  737. float y4 = (float)luaL_checknumber(L, 9);
  738. instance->quad(mode, x1, y1, x2, y2, x3, y3, x4, y4);
  739. return 0;
  740. }
  741. int w_circle(lua_State * L)
  742. {
  743. Graphics::DrawMode mode;
  744. const char * str = luaL_checkstring(L, 1);
  745. if(!Graphics::getConstant(str, mode))
  746. return luaL_error(L, "Incorrect draw mode %s", str);
  747. float x = (float)luaL_checknumber(L, 2);
  748. float y = (float)luaL_checknumber(L, 3);
  749. float radius = (float)luaL_checknumber(L, 4);
  750. int points = luaL_optint(L, 5, 10);
  751. instance->circle(mode, x, y, radius, points);
  752. return 0;
  753. }
  754. int w_arc(lua_State * L)
  755. {
  756. Graphics::DrawMode mode;
  757. const char * str = luaL_checkstring(L, 1);
  758. if(!Graphics::getConstant(str, mode))
  759. return luaL_error(L, "Incorrect draw mode %s", str);
  760. float x = (float)luaL_checknumber(L, 2);
  761. float y = (float)luaL_checknumber(L, 3);
  762. float radius = (float)luaL_checknumber(L, 4);
  763. float angle1 = (float)luaL_checknumber(L, 5);
  764. float angle2 = (float)luaL_checknumber(L, 6);
  765. int points = luaL_optint(L, 7, 10);
  766. instance->arc(mode, x, y, radius, angle1, angle2, points);
  767. return 0;
  768. }
  769. int w_polygon(lua_State * L)
  770. {
  771. int args = lua_gettop(L) - 1;
  772. Graphics::DrawMode mode;
  773. const char * str = luaL_checkstring(L, 1);
  774. if(!Graphics::getConstant(str, mode))
  775. return luaL_error(L, "Invalid draw mode: %s", str);
  776. bool is_table = false;
  777. float* coords;
  778. if (args == 1 && lua_istable(L, 2)) {
  779. args = lua_objlen(L, 2);
  780. is_table = true;
  781. }
  782. if (args % 2 != 0)
  783. return luaL_error(L, "Number of vertices must be a multiple of two");
  784. else if (args < 6)
  785. return luaL_error(L, "Need at least three vertices to draw a polygon");
  786. // fetch coords
  787. coords = new float[args + 2];
  788. if (is_table) {
  789. for (int i = 0; i < args; ++i) {
  790. lua_pushnumber(L, i + 1);
  791. lua_rawget(L, 2);
  792. coords[i] = lua_tonumber(L, -1);
  793. lua_pop(L, 1);
  794. }
  795. } else {
  796. for (int i = 0; i < args; ++i)
  797. coords[i] = lua_tonumber(L, i + 2);
  798. }
  799. // make a closed loop
  800. coords[args] = coords[0];
  801. coords[args+1] = coords[1];
  802. instance->polygon(mode, coords, args+2);
  803. delete[] coords;
  804. return 0;
  805. }
  806. int w_push(lua_State *)
  807. {
  808. instance->push();
  809. return 0;
  810. }
  811. int w_pop(lua_State *)
  812. {
  813. instance->pop();
  814. return 0;
  815. }
  816. int w_rotate(lua_State * L)
  817. {
  818. float deg = (float)luaL_checknumber(L, 1);
  819. instance->rotate(deg);
  820. return 0;
  821. }
  822. int w_scale(lua_State * L)
  823. {
  824. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  825. float sy = (float)luaL_optnumber(L, 2, sx);
  826. instance->scale(sx, sy);
  827. return 0;
  828. }
  829. int w_translate(lua_State * L)
  830. {
  831. float x = (float)luaL_checknumber(L, 1);
  832. float y = (float)luaL_checknumber(L, 2);
  833. instance->translate(x, y);
  834. return 0;
  835. }
  836. int w_hasFocus(lua_State * L)
  837. {
  838. luax_pushboolean(L, instance->hasFocus());
  839. return 1;
  840. }
  841. // List of functions to wrap.
  842. static const luaL_Reg functions[] = {
  843. { "checkMode", w_checkMode },
  844. { "setMode", w_setMode },
  845. { "toggleFullscreen", w_toggleFullscreen },
  846. { "reset", w_reset },
  847. { "clear", w_clear },
  848. { "present", w_present },
  849. { "newImage", w_newImage },
  850. { "newQuad", w_newQuad },
  851. { "newFont1", w_newFont1 },
  852. { "newImageFont", w_newImageFont },
  853. { "newSpriteBatch", w_newSpriteBatch },
  854. { "newParticleSystem", w_newParticleSystem },
  855. { "newFramebuffer", w_newFramebuffer },
  856. { "setColor", w_setColor },
  857. { "getColor", w_getColor },
  858. { "setBackgroundColor", w_setBackgroundColor },
  859. { "getBackgroundColor", w_getBackgroundColor },
  860. { "setFont1", w_setFont1 },
  861. { "getFont", w_getFont },
  862. { "setBlendMode", w_setBlendMode },
  863. { "setColorMode", w_setColorMode },
  864. { "getBlendMode", w_getBlendMode },
  865. { "getColorMode", w_getColorMode },
  866. { "setLineWidth", w_setLineWidth },
  867. { "setLineStyle", w_setLineStyle },
  868. { "setLine", w_setLine },
  869. { "getLineWidth", w_getLineWidth },
  870. { "getLineStyle", w_getLineStyle },
  871. { "setPointSize", w_setPointSize },
  872. { "setPointStyle", w_setPointStyle },
  873. { "setPoint", w_setPoint },
  874. { "getPointSize", w_getPointSize },
  875. { "getPointStyle", w_getPointStyle },
  876. { "getMaxPointSize", w_getMaxPointSize },
  877. { "newScreenshot", w_newScreenshot },
  878. { "setRenderTarget", w_setRenderTarget },
  879. { "draw", w_draw },
  880. { "drawq", w_drawq },
  881. { "drawTest", w_drawTest },
  882. { "print1", w_print1 },
  883. { "printf1", w_printf1 },
  884. { "setCaption", w_setCaption },
  885. { "getCaption", w_getCaption },
  886. { "setIcon", w_setIcon },
  887. { "getWidth", w_getWidth },
  888. { "getHeight", w_getHeight },
  889. { "isCreated", w_isCreated },
  890. { "getModes", w_getModes },
  891. { "setScissor", w_setScissor },
  892. { "getScissor", w_getScissor },
  893. { "defineMask", w_defineMask },
  894. { "setMask", w_setMask },
  895. { "point", w_point },
  896. { "line", w_line },
  897. { "triangle", w_triangle },
  898. { "rectangle", w_rectangle },
  899. { "quad", w_quad },
  900. { "circle", w_circle },
  901. { "arc", w_arc },
  902. { "polygon", w_polygon },
  903. { "push", w_push },
  904. { "pop", w_pop },
  905. { "rotate", w_rotate },
  906. { "scale", w_scale },
  907. { "translate", w_translate },
  908. { "hasFocus", w_hasFocus },
  909. { 0, 0 }
  910. };
  911. // Types for this module.
  912. static const lua_CFunction types[] = {
  913. luaopen_font,
  914. luaopen_image,
  915. luaopen_frame,
  916. luaopen_spritebatch,
  917. luaopen_particlesystem,
  918. luaopen_framebuffer,
  919. 0
  920. };
  921. int luaopen_love_graphics(lua_State * L)
  922. {
  923. if(instance == 0)
  924. {
  925. try
  926. {
  927. instance = new Graphics();
  928. }
  929. catch(Exception & e)
  930. {
  931. return luaL_error(L, e.what());
  932. }
  933. }
  934. else
  935. instance->retain();
  936. WrappedModule w;
  937. w.module = instance;
  938. w.name = "graphics";
  939. w.flags = MODULE_T;
  940. w.functions = functions;
  941. w.types = types;
  942. luax_register_module(L, w);
  943. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "graphics.lua") == 0)
  944. lua_call(L, 0, 0);
  945. return 0;
  946. }
  947. } // opengl
  948. } // graphics
  949. } // love