graphics.lua 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. -- love.graphics
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ------------------------------------DRAWING-------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- love.graphics.arc
  8. love.test.graphics.arc = function(test)
  9. -- draw some arcs using pi format
  10. local canvas = love.graphics.newCanvas(32, 32)
  11. love.graphics.setCanvas(canvas)
  12. love.graphics.clear(0, 0, 0, 1)
  13. love.graphics.setColor(1, 1, 1, 1)
  14. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 360 * (math.pi/180), 10)
  15. love.graphics.arc('fill', "pie", 16, 16, 16, 270 * (math.pi/180), 45 * (math.pi/180), 10)
  16. love.graphics.setColor(1, 0, 0, 1)
  17. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 90 * (math.pi/180), 10)
  18. love.graphics.setColor(1, 1, 0, 1)
  19. love.graphics.arc('line', "pie", 16, 16, 16, 180 * (math.pi/180), 135 * (math.pi/180), 10)
  20. love.graphics.setColor(1, 1, 1, 1)
  21. love.graphics.setCanvas()
  22. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  23. test:assertPixels(imgdata1, {
  24. white = {{11,0},{20,0},{0,13},{0,14},{31,13},{31,14},{15,14}},
  25. black = {{16,14},{16,16},{30,14},{30,16}},
  26. yellow = {{15,15},{15,16},{16,15},{0,15},{4,27},{5,26},{14,17}},
  27. red = {{15,17},{15,31},{17,15},{31,15},{28,26},{27,27}}
  28. }, 'arc pi')
  29. -- draw some arcs with open format
  30. love.graphics.setCanvas(canvas)
  31. love.graphics.clear(0, 0, 0, 1)
  32. love.graphics.setColor(1, 1, 1, 1)
  33. love.graphics.arc('line', "open", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  34. love.graphics.setColor(1, 0, 0, 1)
  35. love.graphics.arc('fill', "open", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  36. love.graphics.setColor(1, 1, 0, 1)
  37. love.graphics.arc('fill', "open", 16, 16, 16, 180 * (math.pi/180), 90 * (math.pi/180), 10)
  38. love.graphics.setColor(1, 1, 1, 1)
  39. love.graphics.setCanvas()
  40. local imgdata2 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  41. test:assertPixels(imgdata2, {
  42. white = {{11,0},{20,0},{26,4},{5,4},{0,15},{19,31},{31,19}},
  43. black = {{27,5},{27,4},{26,5},{1,15},{31,15}},
  44. yellow = {{0,17},{0,19},{12,31},{14,31},{6,23},{7,24}},
  45. red = {{0,16},{31,16},{31,18},{30,21},{18,31},{15,16},{16,16}}
  46. }, 'arc open')
  47. -- draw some arcs with closed format
  48. love.graphics.setCanvas(canvas)
  49. love.graphics.clear(0, 0, 0, 1)
  50. love.graphics.setColor(1, 1, 1, 1)
  51. love.graphics.arc('line', "closed", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  52. love.graphics.setColor(1, 0, 0, 1)
  53. love.graphics.arc('fill', "closed", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  54. love.graphics.setColor(1, 1, 0, 1)
  55. love.graphics.arc('line', "closed", 16, 16, 16, 180 * (math.pi/180), 90 * (math.pi/180), 10)
  56. love.graphics.setColor(1, 1, 1, 1)
  57. love.graphics.setCanvas()
  58. local imgdata3 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  59. test:assertPixels(imgdata3, {
  60. white = {{11,0},{20,0},{26,4},{5,4},{0,15},{19,31},{31,19}},
  61. yellow = {{0,17},{0,19},{12,31},{14,31}},
  62. red = {{31,16},{31,18},{30,21},{18,31},{15,16},{16,16}}
  63. }, 'arc open')
  64. test:exportImg(imgdata1)
  65. test:exportImg(imgdata2)
  66. test:exportImg(imgdata3)
  67. end
  68. -- love.graphics.circle
  69. love.test.graphics.circle = function(test)
  70. -- draw some circles
  71. local canvas = love.graphics.newCanvas(32, 32)
  72. love.graphics.setCanvas(canvas)
  73. love.graphics.clear(0, 0, 0, 1)
  74. love.graphics.setColor(1, 1, 1, 1)
  75. love.graphics.circle('fill', 16, 16, 16)
  76. love.graphics.setColor(1, 0, 0, 1)
  77. love.graphics.circle('line', 16, 16, 16)
  78. love.graphics.setColor(1, 1, 0, 1)
  79. love.graphics.circle('fill', 16, 16, 8)
  80. love.graphics.setColor(0, 1, 0, 1)
  81. love.graphics.circle('fill', 16, 16, 4)
  82. love.graphics.setColor(1, 1, 1, 1)
  83. love.graphics.setCanvas()
  84. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  85. test:assertPixels(imgdata, {
  86. white = {{13,8},{18,8},{23,13},{23,18}},
  87. green = {
  88. {15,12},{16,12},{13,13},{18,13},{12,15},{12,16},{13,18},{18,18},
  89. {15,19},{16,19},{19,15},{19,16}
  90. },
  91. black = {{10,0},{21,0},{0,10},{0,21},{31,10},{31,21},{10,31},{21,31}},
  92. yellow = {
  93. {11,10},{10,11},{8,14},{8,17},{10,20},{11,21},{14,23},{17,23},{20,21},
  94. {21,20},{23,17},{23,14},{20,10},{21,11},{17,8},{14,8}
  95. },
  96. red = {{11,0},{20,0},{11,31},{20,31},{0,11},{0,20},{31,20},{31,11}}
  97. }, 'circle')
  98. test:exportImg(imgdata)
  99. end
  100. -- love.graphics.clear
  101. love.test.graphics.clear = function(test)
  102. local canvas = love.graphics.newCanvas(16, 16)
  103. love.graphics.setCanvas(canvas)
  104. love.graphics.clear(0, 0, 0, 1)
  105. love.graphics.clear(1, 1, 0, 1)
  106. love.graphics.setCanvas()
  107. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  108. test:assertPixels(imgdata, {
  109. yellow = {{0,0},{15,0},{0,15},{15,15},{8,8}}
  110. }, 'clear')
  111. test:exportImg(imgdata)
  112. end
  113. -- love.graphics.discard
  114. love.test.graphics.discard = function(test)
  115. test:skipTest('cant test this worked')
  116. -- wrote all this before seeing "on some desktops this may do nothing"
  117. -- leaving in case we need it in future
  118. --local canvas = love.graphics.newCanvas(32, 32)
  119. --love.graphics.setCanvas(canvas)
  120. -- love.graphics.clear(0, 0, 0, 1)
  121. -- love.graphics.draw(Logo.texture, Logo.img, 0, 0)
  122. -- love.graphics.discard(true, true)
  123. --love.graphics.setCanvas()
  124. end
  125. -- love.graphics.draw
  126. love.test.graphics.draw = function(test)
  127. local canvas1 = love.graphics.newCanvas(32, 32)
  128. local canvas2 = love.graphics.newCanvas(32, 32)
  129. local transform = love.math.newTransform( )
  130. transform:translate(16, 0)
  131. transform:scale(0.5, 0.5)
  132. love.graphics.setCanvas(canvas1)
  133. love.graphics.clear(0, 0, 0, 1)
  134. -- img, offset
  135. love.graphics.draw(Logo.texture, Logo.img, 0, 0, 0, 1, 1, 16, 16)
  136. love.graphics.setCanvas()
  137. love.graphics.setCanvas(canvas2)
  138. love.graphics.clear(1, 0, 0, 1)
  139. -- canvas, scale, shear, transform obj
  140. love.graphics.draw(canvas1, 0, 0, 0, 0.5, 0.5, 0, 0, 2, 2)
  141. love.graphics.draw(canvas1, 0, 16, 0, 0.5, 0.5)
  142. love.graphics.draw(canvas1, 16, 16, 0, 0.5, 0.5)
  143. love.graphics.draw(canvas1, transform)
  144. love.graphics.setCanvas()
  145. local imgdata = love.graphics.readbackTexture(canvas2, {16, 0, 0, 0, 16, 16})
  146. test:assertPixels(imgdata, {
  147. lovepink = {{23,3},{23,19},{7,19},{0,0},{16,0},{0,16},{16,16}},
  148. loveblue = {{0,31},{15,17},{15,31},{16,31},{31,17},{31,31},{16,15},{31,15}},
  149. white = {{15, 15},{6,19},{8,19},{22,19},{24,19},{22,3},{24,3}},
  150. red = {{0,1},{1,0},{15,0},{15,7},{0,15},{7,15}}
  151. }, 'drawing')
  152. test:exportImg(imgdata)
  153. end
  154. -- love.graphics.drawInstanced
  155. love.test.graphics.drawInstanced = function(test)
  156. test:skipTest('test class needs writing')
  157. end
  158. -- love.graphics.drawLayer
  159. love.test.graphics.drawLayer = function(test)
  160. local image = love.graphics.newArrayImage({
  161. 'resources/love.png', 'resources/loveinv.png',
  162. 'resources/love.png', 'resources/loveinv.png'
  163. })
  164. local canvas = love.graphics.newCanvas(64, 64)
  165. love.graphics.setCanvas(canvas)
  166. love.graphics.clear(0, 0, 0, 1)
  167. love.graphics.drawLayer(image, 1, 0, 0, 0, 1, 1)
  168. love.graphics.drawLayer(image, 2, 32, 0, 0, 0.5, 0.5)
  169. love.graphics.drawLayer(image, 4, 0, 32, 0, 0.5, 0.5)
  170. love.graphics.drawLayer(image, 3, 32, 32, 0, 2, 2, 16, 16)
  171. love.graphics.setCanvas()
  172. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  173. test:assertPixels(imgdata, {
  174. lovepink = {{30,2},{33,2},{2,30},{2,33},{4,60},{4,63},{60,4},{63,4},{31,23},{32,23}},
  175. loveblue = {{14,33},{17,33},{46,1},{49,1},{1,46},{1,49},{33,14},{33,17}},
  176. black = {{0,0},{63,0},{0,63},{39,6},{40,6},{6,39},{6,40},{6,55},{55,6}},
  177. white = {{46,11},{48,11},{14,43},{16,43},{30,23},{33,23},{34,54},{53,40},{63,63}}
  178. }, 'draw layer')
  179. test:exportImg(imgdata)
  180. end
  181. -- love.graphics.ellipse
  182. love.test.graphics.ellipse = function(test)
  183. local canvas = love.graphics.newCanvas(32, 32)
  184. love.graphics.setCanvas(canvas)
  185. love.graphics.clear(0, 0, 0, 1)
  186. love.graphics.setColor(1, 0, 0, 1)
  187. love.graphics.ellipse('fill', 16, 16, 16, 8)
  188. love.graphics.setColor(1, 1, 0, 1)
  189. love.graphics.ellipse('fill', 24, 24, 10, 24)
  190. love.graphics.setColor(1, 0, 1, 1)
  191. love.graphics.ellipse('fill', 16, 0, 8, 16)
  192. love.graphics.setColor(1, 1, 1, 1)
  193. love.graphics.setCanvas()
  194. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  195. test:assertPixels(imgdata, {
  196. red = {{0,14},{0,17},{7,9},{7,22},{14,15},{14,16}},
  197. pink = {{15,15},{16,15},{8,0},{8,4},{23,0},{23,4},{13,14},{18,14}},
  198. yellow = {{24,0},{25,0},{14,17},{14,30},{15,31},{31,8}}
  199. }, 'ellipses')
  200. test:exportImg(imgdata)
  201. end
  202. -- love.graphics.flushBatch
  203. love.test.graphics.flushBatch = function(test)
  204. test:skipTest('not sure can be tested as used internally')
  205. end
  206. -- love.graphics.line
  207. love.test.graphics.line = function(test)
  208. local canvas = love.graphics.newCanvas(16, 16)
  209. love.graphics.setCanvas(canvas)
  210. love.graphics.clear(0, 0, 0, 1)
  211. love.graphics.setColor(1, 0, 0, 1)
  212. love.graphics.line(1,1,16,1,16,16,1,16,1,1)
  213. love.graphics.setColor(1, 1, 0, 1)
  214. love.graphics.line({0,0,8,8,16,0,8,8,16,16,8,8,0,16})
  215. love.graphics.setColor(1, 1, 1, 1)
  216. love.graphics.setCanvas()
  217. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  218. test:assertPixels(imgdata, {
  219. yellow = {{0,0},{15,0},{0,15},{15,15},{7,7},{8,7},{8,7},{8,8}},
  220. red = {{1,0},{14,0},{0,1},{0,14},{15,1},{15,14},{1,15},{14,15}}
  221. }, 'lines')
  222. test:exportImg(imgdata)
  223. end
  224. -- love.graphics.points
  225. love.test.graphics.points = function(test)
  226. local canvas = love.graphics.newCanvas(16, 16)
  227. love.graphics.setCanvas(canvas)
  228. love.graphics.clear(0, 0, 0, 1)
  229. love.graphics.setColor(1, 0, 0, 1)
  230. love.graphics.points(1,1,16,1,16,16,1,16,1,1)
  231. love.graphics.setColor(1, 1, 0, 1)
  232. love.graphics.points({2,2,8,8,15,2,8,9,15,15,9,9,2,15,9,8})
  233. love.graphics.setColor(1, 1, 1, 1)
  234. love.graphics.setCanvas()
  235. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  236. test:assertPixels(imgdata, {
  237. yellow = {{1,1},{14,1},{1,14},{14,14},{7,7},{8,7},{8,7},{8,8}},
  238. red = {{0,0},{15,0},{15,15},{0,15}}
  239. }, 'points')
  240. test:exportImg(imgdata)
  241. end
  242. -- love.graphics.polygon
  243. love.test.graphics.polygon = function(test)
  244. local canvas = love.graphics.newCanvas(16, 16)
  245. love.graphics.setCanvas(canvas)
  246. love.graphics.clear(0, 0, 0, 1)
  247. love.graphics.setColor(1, 0, 0, 1)
  248. love.graphics.polygon("fill", 1, 1, 4, 5, 8, 10, 16, 2, 7, 3, 5, 16, 16, 16, 1, 8)
  249. love.graphics.setColor(1, 1, 0, 1)
  250. love.graphics.polygon("line", {2, 2, 4, 5, 3, 7, 8, 15, 12, 4, 5, 10})
  251. love.graphics.setColor(1, 1, 1, 1)
  252. love.graphics.setCanvas()
  253. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  254. test:assertPixels(imgdata, {
  255. yellow = {{1,0},{1,1},{5,9},{7,14},{8,14},{12,3}},
  256. red = {{2,1},{1,2},{1,7},{5,15},{14,15},{8,8},{14,2},{7,1}}
  257. }, 'polygon')
  258. test:exportImg(imgdata)
  259. end
  260. -- love.graphics.present
  261. love.test.graphics.present = function(test)
  262. test:skipTest('test class needs writing')
  263. end
  264. -- love.graphics.print
  265. love.test.graphics.print = function(test)
  266. love.graphics.setFont(Font)
  267. local canvas = love.graphics.newCanvas(16, 16)
  268. love.graphics.setCanvas(canvas)
  269. love.graphics.clear(0, 0, 0, 1)
  270. love.graphics.setColor(1, 0, 0, 1)
  271. love.graphics.print('love', 0, 3, 0, 1, 1, 0, 0)
  272. love.graphics.setColor(0, 1, 0, 1)
  273. love.graphics.print('ooo', 0, 3, 0, 2, 2, 0, 0)
  274. love.graphics.setColor(0, 0, 1, 1)
  275. love.graphics.print('hello', 0, 3, 90*(math.pi/180), 1, 1, 0, 8)
  276. love.graphics.setColor(1, 1, 1, 1)
  277. love.graphics.setCanvas()
  278. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  279. test:assertPixels(imgdata, {
  280. red = {{0,0},{1,0},{1,1},{2,6},{4,4},{7,6},{10,2},{11,5},{14,3},{14,4}},
  281. green = {
  282. {2,1},{2,2},{0,3},{1,3},{1,8},{2,9},{7,10},{8,8},{9,4},{13,3},{14,2},
  283. {13,8},{14,9}
  284. },
  285. blue = {
  286. {4,15},{10,15},{4,12},{6,12},{8,12},{5,9},{7,9},{4,3},{10,3},{8,6},{7,7},
  287. {4,7},{7,13},{8,12}
  288. }
  289. }, 'print')
  290. test:exportImg(imgdata)
  291. end
  292. -- love.graphics.printf
  293. love.test.graphics.printf = function(test)
  294. love.graphics.setFont(Font)
  295. local canvas = love.graphics.newCanvas(32, 32)
  296. love.graphics.setCanvas(canvas)
  297. love.graphics.clear(0, 0, 0, 1)
  298. love.graphics.setColor(1, 0, 0, 1)
  299. love.graphics.printf('love', 0, 0, 8, "left")
  300. love.graphics.setColor(0, 1, 0, 1)
  301. love.graphics.printf('love', 0, 5, 16, "right")
  302. love.graphics.setColor(0, 0, 1, 1)
  303. love.graphics.printf('love', 0, 7, 32, "center")
  304. love.graphics.setColor(1, 1, 1, 1)
  305. love.graphics.setCanvas()
  306. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  307. test:assertPixels(imgdata, {
  308. red = {
  309. {1,0},{1,1},{0,3},{2,3},{2,7},{0,9},{3,11},{4,10},{0,15},{4,15},{2,19},
  310. {0,24},{1,23},{3,23},{4,24},{0,26},{1,27},{2,27},{3,27}
  311. },
  312. green = {
  313. {1,2},{0,8},{1,8},{2,8},{4,7},{5,8},{7,8},{8,7},{10,4},{14,4},{11,7},
  314. {12,8},{10,13},{11,12},{13,12},{14,13},{10,15},{11,16}
  315. },
  316. blue = {{6,4},{6,10},{9,7},{10,6},{16,9},{18,9},{21,8},{25,8}}
  317. }, 'printf')
  318. test:exportImg(imgdata)
  319. end
  320. -- love.graphics.rectangle
  321. love.test.graphics.rectangle = function(test)
  322. -- setup, draw a 16x16 red rectangle with a blue central square
  323. local canvas = love.graphics.newCanvas(16, 16)
  324. love.graphics.setCanvas(canvas)
  325. love.graphics.clear(0, 0, 0, 1)
  326. love.graphics.setColor(1, 0, 0, 1)
  327. love.graphics.rectangle('fill', 0, 0, 16, 16)
  328. love.graphics.setColor(0, 0, 1, 1)
  329. love.graphics.rectangle('fill', 6, 6, 4, 4)
  330. love.graphics.setColor(1, 1, 1, 1)
  331. love.graphics.setCanvas()
  332. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  333. -- test, check red bg and blue central square
  334. test:assertPixels(imgdata1, {
  335. red = {{0,0},{15,0},{15,15},{0,15}},
  336. blue = {{6,6},{9,6},{9,9},{6,9}}
  337. }, 'fill')
  338. -- clear canvas to do some line testing
  339. love.graphics.setCanvas(canvas)
  340. love.graphics.clear(0, 0, 0, 1)
  341. love.graphics.setColor(1, 0, 0, 1)
  342. love.graphics.rectangle('line', 1, 1, 15, 15) -- red border
  343. love.graphics.setColor(0, 0, 1, 1)
  344. love.graphics.rectangle('line', 1, 1, 2, 15) -- 3x16 left aligned blue outline
  345. love.graphics.setColor(0, 1, 0, 1)
  346. love.graphics.rectangle('line', 11, 1, 5, 15) -- 6x16 right aligned green outline
  347. love.graphics.setColor(1, 1, 1, 1)
  348. love.graphics.setCanvas()
  349. local imgdata2 = love.graphics.readbackTexture(canvas, {1, 1, 0, 0, 16, 16})
  350. -- -- check corners and inner corners
  351. test:assertPixels(imgdata2, {
  352. red = {{3,0},{9,0},{3,15,9,15}},
  353. blue = {{0,0},{2,0},{0,15},{2,15}},
  354. green = {{10,0},{15,0},{10,15},{15,15}},
  355. black = {
  356. {1,1},{1,14},{3,1},{9,1},{3,14},
  357. {9,14},{11,1},{14,1},{11,14},{14,14}
  358. }
  359. }, 'line')
  360. test:exportImg(imgdata1)
  361. test:exportImg(imgdata2)
  362. end
  363. --------------------------------------------------------------------------------
  364. --------------------------------------------------------------------------------
  365. --------------------------------OBJECT CREATION---------------------------------
  366. --------------------------------------------------------------------------------
  367. --------------------------------------------------------------------------------
  368. -- love.graphics.captureScreenshot
  369. love.test.graphics.captureScreenshot = function(test)
  370. if test:isDelayed() == false then
  371. love.graphics.captureScreenshot('example-screenshot.png')
  372. test:setDelay(10)
  373. -- need to wait until end of the frame for the screenshot
  374. else
  375. test:assertNotNil(love.filesystem.openFile('example-screenshot.png', 'r'))
  376. love.filesystem.remove('example-screenshot.png')
  377. end
  378. end
  379. -- love.graphics.newArrayImage
  380. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  381. love.test.graphics.newArrayImage = function(test)
  382. test:assertObject(love.graphics.newArrayImage({
  383. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  384. }))
  385. end
  386. -- love.graphics.newCanvas
  387. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  388. love.test.graphics.newCanvas = function(test)
  389. test:assertObject(love.graphics.newCanvas(16, 16, {
  390. type = '2d',
  391. format = 'normal',
  392. readable = true,
  393. msaa = 0,
  394. dpiscale = 1,
  395. mipmaps = 'none'
  396. }))
  397. test:assertObject(love.graphics.newCanvas(1000, 1000))
  398. end
  399. -- love.graphics.newCubeImage
  400. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  401. love.test.graphics.newCubeImage = function(test)
  402. test:assertObject(love.graphics.newCubeImage('resources/cubemap.png', {
  403. mipmaps = false,
  404. linear = false
  405. }))
  406. end
  407. -- love.graphics.newFont
  408. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  409. love.test.graphics.newFont = function(test)
  410. test:assertObject(love.graphics.newFont('resources/font.ttf'))
  411. test:assertObject(love.graphics.newFont('resources/font.ttf', 8, "normal", 1))
  412. end
  413. -- love.graphics.newImage
  414. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  415. love.test.graphics.newImage = function(test)
  416. test:assertObject(love.graphics.newImage('resources/love.png', {
  417. mipmaps = false,
  418. linear = false,
  419. dpiscale = 1
  420. }))
  421. end
  422. -- love.graphics.newImageFont
  423. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  424. love.test.graphics.newImageFont = function(test)
  425. test:assertObject(love.graphics.newImageFont('resources/love.png', 'ABCD', 1))
  426. end
  427. -- love.graphics.newMesh
  428. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  429. love.test.graphics.newMesh = function(test)
  430. test:assertObject(love.graphics.newMesh({{1, 1, 0, 0, 1, 1, 1, 1}}, 'fan', 'dynamic'))
  431. end
  432. -- love.graphics.newParticleSystem
  433. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  434. love.test.graphics.newParticleSystem = function(test)
  435. local imgdata = love.graphics.newImage('resources/love.png')
  436. test:assertObject(love.graphics.newParticleSystem(imgdata, 1000))
  437. end
  438. -- love.graphics.newQuad
  439. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  440. love.test.graphics.newQuad = function(test)
  441. local imgdata = love.graphics.newImage('resources/love.png')
  442. test:assertObject(love.graphics.newQuad(0, 0, 16, 16, imgdata))
  443. end
  444. -- love.graphics.newShader
  445. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  446. love.test.graphics.newShader = function(test)
  447. local pixelcode = 'vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) { vec4 texturecolor = Texel(tex, texture_coords); return texturecolor * color;}'
  448. local vertexcode = 'vec4 position(mat4 transform_projection, vec4 vertex_position) { return transform_projection * vertex_position; }'
  449. test:assertObject(love.graphics.newShader(pixelcode, vertexcode))
  450. end
  451. -- love.graphics.newSpriteBatch
  452. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  453. love.test.graphics.newSpriteBatch = function(test)
  454. local imgdata = love.graphics.newImage('resources/love.png')
  455. test:assertObject(love.graphics.newSpriteBatch(imgdata, 1000))
  456. end
  457. -- love.graphics.newText
  458. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  459. love.test.graphics.newTextBatch = function(test)
  460. local font = love.graphics.newFont('resources/font.ttf')
  461. test:assertObject(love.graphics.newTextBatch(font, 'helloworld'))
  462. end
  463. -- love.graphics.newVideo
  464. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  465. love.test.graphics.newVideo = function(test)
  466. test:assertObject(love.graphics.newVideo('resources/sample.ogv', {
  467. audio = false,
  468. dpiscale = 1
  469. }))
  470. end
  471. -- love.graphics.newVolumeImage
  472. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  473. love.test.graphics.newVolumeImage = function(test)
  474. test:assertObject(love.graphics.newVolumeImage({
  475. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  476. }, {
  477. mipmaps = false,
  478. linear = false
  479. }))
  480. end
  481. -- love.graphics.validateShader
  482. love.test.graphics.validateShader = function(test)
  483. local pixelcode = 'vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) { vec4 texturecolor = Texel(tex, texture_coords); return texturecolor * color;}'
  484. local vertexcode = 'vec4 position(mat4 transform_projection, vec4 vertex_position) { return transform_projection * vertex_position; }'
  485. -- check made up code first
  486. local status, _ = love.graphics.validateShader(true, 'nothing here', 'or here')
  487. test:assertEquals(false, status, 'check invalid shader code')
  488. -- check real code
  489. status, _ = love.graphics.validateShader(true, pixelcode, vertexcode)
  490. test:assertEquals(true, status, 'check valid shader code')
  491. end
  492. --------------------------------------------------------------------------------
  493. --------------------------------------------------------------------------------
  494. ---------------------------------GRAPHICS STATE---------------------------------
  495. --------------------------------------------------------------------------------
  496. --------------------------------------------------------------------------------
  497. -- love.graphics.getBackgroundColor
  498. love.test.graphics.getBackgroundColor = function(test)
  499. -- check default bg is black
  500. local r, g, b, a = love.graphics.getBackgroundColor()
  501. test:assertEquals(0, r, 'check default background r')
  502. test:assertEquals(0, g, 'check default background g')
  503. test:assertEquals(0, b, 'check default background b')
  504. test:assertEquals(1, a, 'check default background a')
  505. -- check set value returns correctly
  506. love.graphics.setBackgroundColor(1, 1, 1, 0)
  507. r, g, b, a = love.graphics.getBackgroundColor()
  508. test:assertEquals(1, r, 'check updated background r')
  509. test:assertEquals(1, g, 'check updated background g')
  510. test:assertEquals(1, b, 'check updated background b')
  511. test:assertEquals(0, a, 'check updated background a')
  512. love.graphics.setBackgroundColor(0, 0, 0, 1) -- reset
  513. end
  514. -- love.graphics.getBlendMode
  515. love.test.graphics.getBlendMode = function(test)
  516. -- check default blend mode
  517. local mode, alphamode = love.graphics.getBlendMode()
  518. test:assertEquals('alpha', mode, 'check default blend mode')
  519. test:assertEquals('alphamultiply', alphamode, 'check default alpha blend')
  520. -- check set mode returns correctly
  521. love.graphics.setBlendMode('add', 'premultiplied')
  522. mode, alphamode = love.graphics.getBlendMode()
  523. test:assertEquals('add', mode, 'check changed blend mode')
  524. test:assertEquals('premultiplied', alphamode, 'check changed alpha blend')
  525. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  526. end
  527. -- love.graphics.getCanvas
  528. love.test.graphics.getCanvas = function(test)
  529. -- by default should be nil if drawing to real screen
  530. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  531. -- should return not nil when we target a canvas
  532. local canvas = love.graphics.newCanvas(16, 16)
  533. love.graphics.setCanvas(canvas)
  534. test:assertObject(love.graphics.getCanvas())
  535. love.graphics.setCanvas()
  536. end
  537. -- love.graphics.getColor
  538. love.test.graphics.getColor = function(test)
  539. -- by default should be white
  540. local r, g, b, a = love.graphics.getColor()
  541. test:assertEquals(1, r, 'check default color r')
  542. test:assertEquals(1, g, 'check default color g')
  543. test:assertEquals(1, b, 'check default color b')
  544. test:assertEquals(1, a, 'check default color a')
  545. -- check set color is returned correctly
  546. love.graphics.setColor(0, 0, 0, 0)
  547. r, g, b, a = love.graphics.getColor()
  548. test:assertEquals(0, r, 'check changed color r')
  549. test:assertEquals(0, g, 'check changed color g')
  550. test:assertEquals(0, b, 'check changed color b')
  551. test:assertEquals(0, a, 'check changed color a')
  552. love.graphics.setColor(1, 1, 1, 1) -- reset
  553. end
  554. -- love.graphics.getColorMask
  555. love.test.graphics.getColorMask = function(test)
  556. -- by default should all be active
  557. local r, g, b, a = love.graphics.getColorMask()
  558. test:assertEquals(true, r, 'check default color mask r')
  559. test:assertEquals(true, g, 'check default color mask g')
  560. test:assertEquals(true, b, 'check default color mask b')
  561. test:assertEquals(true, a, 'check default color mask a')
  562. -- check set color mask is returned correctly
  563. love.graphics.setColorMask(false, false, true, false)
  564. r, g, b, a = love.graphics.getColorMask()
  565. test:assertEquals(false, r, 'check changed color mask r')
  566. test:assertEquals(false, g, 'check changed color mask g')
  567. test:assertEquals(true, b, 'check changed color mask b')
  568. test:assertEquals(false, a, 'check changed color mask a')
  569. love.graphics.setColorMask(true, true, true, true) -- reset
  570. end
  571. -- love.graphics.getDefaultFilter
  572. love.test.graphics.getDefaultFilter = function(test)
  573. -- we set this already for testsuite so we know what it should be
  574. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  575. test:assertEquals('nearest', min, 'check default filter min')
  576. test:assertEquals('nearest', mag, 'check default filter mag')
  577. test:assertEquals(1, anisotropy, 'check default filter mag')
  578. end
  579. -- love.graphics.getDepthMode
  580. love.test.graphics.getDepthMode = function(test)
  581. -- by default should be always/write
  582. local comparemode, write = love.graphics.getDepthMode()
  583. test:assertEquals('always', comparemode, 'check default compare depth')
  584. test:assertEquals(false, write, 'check default depth buffer write')
  585. end
  586. -- love.graphics.getFont
  587. love.test.graphics.getFont = function(test)
  588. test:assertObject(love.graphics.getFont())
  589. end
  590. -- love.graphics.getFrontFaceWinding
  591. love.test.graphics.getFrontFaceWinding = function(test)
  592. -- check default winding
  593. test:assertEquals('ccw', love.graphics.getFrontFaceWinding())
  594. -- check setting value changes it correctly
  595. love.graphics.setFrontFaceWinding('cw')
  596. test:assertEquals('cw', love.graphics.getFrontFaceWinding())
  597. love.graphics.setFrontFaceWinding('ccw') -- reset
  598. end
  599. -- love.graphics.getLineJoin
  600. love.test.graphics.getLineJoin = function(test)
  601. -- check default line join
  602. test:assertEquals('miter', love.graphics.getLineJoin())
  603. -- check set value returned correctly
  604. love.graphics.setLineJoin('none')
  605. test:assertEquals('none', love.graphics.getLineJoin())
  606. love.graphics.setLineJoin('miter') -- reset
  607. end
  608. -- love.graphics.getLineStyle
  609. love.test.graphics.getLineStyle = function(test)
  610. -- we know this should be as testsuite sets it!
  611. test:assertEquals('rough', love.graphics.getLineStyle())
  612. -- check set value returned correctly
  613. love.graphics.setLineStyle('smooth')
  614. test:assertEquals('smooth', love.graphics.getLineStyle())
  615. love.graphics.setLineStyle('rough') -- reset
  616. end
  617. -- love.graphics.getLineWidth
  618. love.test.graphics.getLineWidth = function(test)
  619. -- we know this should be as testsuite sets it!
  620. test:assertEquals(1, love.graphics.getLineWidth())
  621. -- check set value returned correctly
  622. love.graphics.setLineWidth(10)
  623. test:assertEquals(10, love.graphics.getLineWidth())
  624. love.graphics.setLineWidth(1) -- reset
  625. end
  626. -- love.graphics.getMeshCullMode
  627. love.test.graphics.getMeshCullMode = function(test)
  628. -- get default mesh culling
  629. test:assertEquals('none', love.graphics.getMeshCullMode())
  630. -- check set value returned correctly
  631. love.graphics.setMeshCullMode('front')
  632. test:assertEquals('front', love.graphics.getMeshCullMode())
  633. love.graphics.setMeshCullMode('back') -- reset
  634. end
  635. -- love.graphics.getPointSize
  636. love.test.graphics.getPointSize = function(test)
  637. -- get default point size
  638. test:assertEquals(1, love.graphics.getPointSize())
  639. -- check set value returned correctly
  640. love.graphics.setPointSize(10)
  641. test:assertEquals(10, love.graphics.getPointSize())
  642. love.graphics.setPointSize(1) -- reset
  643. end
  644. -- love.graphics.getScissor
  645. love.test.graphics.getScissor = function(test)
  646. -- should be no scissor atm
  647. local x, y, w, h = love.graphics.getScissor()
  648. test:assertEquals(nil, x, 'check no scissor')
  649. test:assertEquals(nil, y, 'check no scissor')
  650. test:assertEquals(nil, w, 'check no scissor')
  651. test:assertEquals(nil, h, 'check no scissor')
  652. -- check set value returned correctly
  653. love.graphics.setScissor(0, 0, 16, 16)
  654. x, y, w, h = love.graphics.getScissor()
  655. test:assertEquals(0, x, 'check scissor set')
  656. test:assertEquals(0, y, 'check scissor set')
  657. test:assertEquals(16, w, 'check scissor set')
  658. test:assertEquals(16, h, 'check scissor set')
  659. love.graphics.setScissor() -- reset
  660. end
  661. -- love.graphics.getShader
  662. love.test.graphics.getShader = function(test)
  663. -- should be no shader active
  664. test:assertEquals(nil, love.graphics.getShader(), 'check no active shader')
  665. end
  666. -- love.graphics.getStackDepth
  667. love.test.graphics.getStackDepth = function(test)
  668. -- by default should be none
  669. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  670. -- now add 3
  671. love.graphics.push()
  672. love.graphics.push()
  673. love.graphics.push()
  674. test:assertEquals(3, love.graphics.getStackDepth(), 'check 3 transforms in stack')
  675. -- now remove 2
  676. love.graphics.pop()
  677. love.graphics.pop()
  678. test:assertEquals(1, love.graphics.getStackDepth(), 'check 1 transforms in stack')
  679. -- now back to 0
  680. love.graphics.pop()
  681. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  682. end
  683. -- love.graphics.getStencilMode
  684. love.test.graphics.getStencilMode = function(test)
  685. -- check default vals
  686. local action, comparemode, value = love.graphics.getStencilMode( )
  687. test:assertEquals('keep', action, 'check default stencil action')
  688. test:assertEquals('always', comparemode, 'check default stencil compare')
  689. test:assertEquals(0, value, 'check default stencil value')
  690. -- check set stencil values is returned
  691. love.graphics.setStencilMode('replace', 'less', 255)
  692. local action, comparemode, value = love.graphics.getStencilMode()
  693. test:assertEquals('replace', action, 'check changed stencil action')
  694. test:assertEquals('less', comparemode, 'check changed stencil compare')
  695. test:assertEquals(255, value, 'check changed stencil value')
  696. love.graphics.setStencilMode() -- reset
  697. end
  698. -- love.graphics.intersectScissor
  699. love.test.graphics.intersectScissor = function(test)
  700. -- make a scissor for the left half, then interset to make the top half
  701. -- then we should be able to fill the canvas with red and only top 4x4 is filled
  702. local canvas = love.graphics.newCanvas(16, 16)
  703. love.graphics.setCanvas(canvas)
  704. love.graphics.clear(0, 0, 0, 1)
  705. love.graphics.origin()
  706. love.graphics.setScissor(0, 0, 8, 16)
  707. love.graphics.intersectScissor(0, 0, 4, 4)
  708. love.graphics.setColor(1, 0, 0, 1)
  709. love.graphics.rectangle('fill', 0, 0, 16, 16)
  710. love.graphics.setColor(1, 1, 1, 1)
  711. love.graphics.setScissor()
  712. love.graphics.setCanvas()
  713. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  714. test:assertPixels(imgdata, {
  715. red = {{0,0},{3,3}},
  716. black ={{4,0},{0,4},{4,4}}
  717. }, 'intersect scissor')
  718. test:exportImg(imgdata)
  719. end
  720. -- love.graphics.isActive
  721. love.test.graphics.isActive = function(test)
  722. test:assertEquals(true, love.graphics.isActive(), 'check graphics is active') -- i mean if you got this far
  723. end
  724. -- love.graphics.isGammaCorrect
  725. love.test.graphics.isGammaCorrect = function(test)
  726. -- we know the config so know this is false
  727. test:assertEquals(false, love.graphics.isGammaCorrect(), 'check gamma correct false')
  728. end
  729. -- love.graphics.isWireframe
  730. love.test.graphics.isWireframe = function(test)
  731. -- check off by default
  732. test:assertEquals(false, love.graphics.isWireframe(), 'check no wireframe by default')
  733. -- check on when enabled
  734. love.graphics.setWireframe(true)
  735. test:assertEquals(true, love.graphics.isWireframe(), 'check wireframe is set')
  736. love.graphics.setWireframe(false) -- reset
  737. end
  738. -- love.graphics.reset
  739. love.test.graphics.reset = function(test)
  740. -- reset should reset current canvas and any colors/scissor
  741. local canvas = love.graphics.newCanvas(16, 16)
  742. love.graphics.setBackgroundColor(0, 0, 1, 1)
  743. love.graphics.setColor(0, 1, 0, 1)
  744. love.graphics.setCanvas(canvas)
  745. love.graphics.reset()
  746. local r, g, b, a = love.graphics.getBackgroundColor()
  747. test:assertEquals(1, r+g+b+a, 'check background reset')
  748. r, g, b, a = love.graphics.getColor()
  749. test:assertEquals(4, r+g+b+a, 'check color reset')
  750. test:assertEquals(nil, love.graphics.getCanvas(), 'check canvas reset')
  751. end
  752. -- love.graphics.setBackgroundColor
  753. love.test.graphics.setBackgroundColor = function(test)
  754. -- check background is set
  755. love.graphics.setBackgroundColor(1, 0, 0, 1)
  756. local r, g, b, a = love.graphics.getBackgroundColor()
  757. test:assertEquals(1, r, 'check set bg r')
  758. test:assertEquals(0, g, 'check set bg g')
  759. test:assertEquals(0, b, 'check set bg b')
  760. test:assertEquals(1, a, 'check set bg a')
  761. love.graphics.setBackgroundColor(0, 0, 0, 1)
  762. end
  763. -- love.graphics.setBlendMode
  764. love.test.graphics.setBlendMode = function(test)
  765. -- create fully white canvas, then draw diff. pixels through blendmodes
  766. local canvas = love.graphics.newCanvas(16, 16)
  767. love.graphics.setCanvas(canvas)
  768. love.graphics.clear(0.5, 0.5, 0.5, 1)
  769. love.graphics.setBlendMode('add', 'alphamultiply')
  770. love.graphics.setColor(1, 0, 0, 1)
  771. love.graphics.points({1,1})
  772. love.graphics.setBlendMode('subtract', 'alphamultiply')
  773. love.graphics.setColor(1, 1, 1, 0.5)
  774. love.graphics.points({16,1})
  775. love.graphics.setBlendMode('multiply', 'premultiplied')
  776. love.graphics.setColor(0, 1, 0, 1)
  777. love.graphics.points({16,16})
  778. love.graphics.setBlendMode('replace', 'premultiplied')
  779. love.graphics.setColor(0, 0, 1, 0.5)
  780. love.graphics.points({1,16})
  781. love.graphics.setColor(1, 1, 1, 1)
  782. love.graphics.setCanvas()
  783. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  784. -- check the 4 corners
  785. test:assertPixels(imgdata, {
  786. redpale = {{0,0}},
  787. black = {{15,0}},
  788. greenhalf = {{15,15}},
  789. bluefade = {{0,15}}
  790. }, 'blend mode')
  791. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  792. test:exportImg(imgdata)
  793. end
  794. -- love.graphics.setCanvas
  795. love.test.graphics.setCanvas = function(test)
  796. -- make 2 canvas, set to each, draw one to the other, check output
  797. local canvas1 = love.graphics.newCanvas(16, 16)
  798. local canvas2 = love.graphics.newCanvas(16, 16)
  799. love.graphics.setCanvas(canvas1)
  800. test:assertEquals(canvas1, love.graphics.getCanvas(), 'check canvas 1 set')
  801. love.graphics.clear(1, 0, 0, 1)
  802. love.graphics.setCanvas(canvas2)
  803. test:assertEquals(canvas2, love.graphics.getCanvas(), 'check canvas 2 set')
  804. love.graphics.clear(0, 0, 0, 1)
  805. love.graphics.draw(canvas1, 0, 0)
  806. love.graphics.setCanvas()
  807. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  808. local imgdata = love.graphics.readbackTexture(canvas2, {16, 0, 0, 0, 16, 16})
  809. -- check 2nd canvas is red
  810. test:assertPixels(imgdata, {
  811. red = {{0,0},{15,0},{15,15},{0,15}}
  812. }, 'set canvas')
  813. test:exportImg(imgdata)
  814. end
  815. -- love.graphics.setColor
  816. love.test.graphics.setColor = function(test)
  817. -- set colors, draw rect, check color
  818. local canvas = love.graphics.newCanvas(16, 16)
  819. love.graphics.setCanvas(canvas)
  820. love.graphics.clear(0, 0, 0, 1)
  821. love.graphics.setColor(1, 0, 0, 1)
  822. local r, g, b, a = love.graphics.getColor()
  823. test:assertEquals(1, r, 'check r set')
  824. test:assertEquals(0, g, 'check g set')
  825. test:assertEquals(0, b, 'check b set')
  826. test:assertEquals(1, a, 'check a set')
  827. love.graphics.points({{1,1},{6,1},{11,1},{16,1}})
  828. love.graphics.setColor(1, 1, 0, 1)
  829. love.graphics.points({{1,2},{6,2},{11,2},{16,2}})
  830. love.graphics.setColor(0, 1, 0, 0.5)
  831. love.graphics.points({{1,3},{6,3},{11,3},{16,3}})
  832. love.graphics.setColor(0, 0, 1, 1)
  833. love.graphics.points({{1,4},{6,4},{11,4},{16,4}})
  834. love.graphics.setColor(1, 1, 1, 1)
  835. love.graphics.setCanvas()
  836. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  837. test:assertPixels(imgdata, {
  838. red = {{0,0},{5,0},{10,0},{15,0}},
  839. yellow = {{0,1},{5,1},{10,1},{15,1}},
  840. greenhalf = {{0,2},{5,2},{10,2},{15,2}},
  841. blue = {{0,3},{5,3},{10,3},{15,3}}
  842. }, 'set color')
  843. test:exportImg(imgdata)
  844. end
  845. -- love.graphics.setColorMask
  846. love.test.graphics.setColorMask = function(test)
  847. -- set mask, draw stuff, check output pixels
  848. local canvas = love.graphics.newCanvas(16, 16)
  849. love.graphics.setCanvas(canvas)
  850. love.graphics.clear(0, 0, 0, 1)
  851. -- mask off blue
  852. love.graphics.setColorMask(true, true, false, true)
  853. local r, g, b, a = love.graphics.getColorMask()
  854. test:assertEquals(r, true, 'check r mask')
  855. test:assertEquals(g, true, 'check g mask')
  856. test:assertEquals(b, false, 'check b mask')
  857. test:assertEquals(a, true, 'check a mask')
  858. -- draw "black" which should then turn to yellow
  859. love.graphics.setColor(1, 1, 1, 1)
  860. love.graphics.rectangle('fill', 0, 0, 16, 16)
  861. love.graphics.setColorMask(true, true, true, true)
  862. love.graphics.setCanvas()
  863. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  864. test:assertPixels(imgdata, {
  865. yellow = {{0,0},{0,15},{15,15},{15,0}}
  866. }, 'set color mask')
  867. test:exportImg(imgdata)
  868. end
  869. -- love.graphics.setDefaultFilter
  870. love.test.graphics.setDefaultFilter = function(test)
  871. -- check setting filter val works
  872. love.graphics.setDefaultFilter('linear', 'linear', 1)
  873. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  874. test:assertEquals('linear', min, 'check default filter min')
  875. test:assertEquals('linear', mag, 'check default filter mag')
  876. test:assertEquals(1, anisotropy, 'check default filter mag')
  877. love.graphics.setDefaultFilter('nearest', 'nearest', 1) -- reset
  878. end
  879. -- love.graphics.setDepthMode
  880. love.test.graphics.setDepthMode = function(test)
  881. -- check documented modes are valid
  882. local comparemode, write = love.graphics.getDepthMode()
  883. local modes = {
  884. 'equal', 'notequal', 'less', 'lequal', 'gequal',
  885. 'greater', 'never', 'always'
  886. }
  887. for m=1,#modes do
  888. love.graphics.setDepthMode(modes[m], true)
  889. test:assertEquals(modes[m], love.graphics.getDepthMode(), 'check depth mode ' .. modes[m] .. ' set')
  890. end
  891. love.graphics.setDepthMode(comparemode, write)
  892. -- @TODO better graphics drawing specific test
  893. end
  894. -- love.graphics.setFont
  895. love.test.graphics.setFont = function(test)
  896. -- set font doesnt return anything so draw with the test font
  897. local canvas = love.graphics.newCanvas(16, 16)
  898. love.graphics.setFont(Font)
  899. love.graphics.setCanvas(canvas)
  900. love.graphics.clear(0, 0, 0, 1)
  901. love.graphics.setColor(1, 0, 0, 1)
  902. love.graphics.print('love', 0, 3)
  903. love.graphics.setColor(1, 1, 1, 1)
  904. love.graphics.setCanvas()
  905. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  906. test:assertPixels(imgdata, {
  907. red = {
  908. {0,0},{0,6},{2,6},{6,2},
  909. {4,4},{8,4},{6,6},{10,2},
  910. {14,2},{12,6}
  911. }
  912. }, 'set font for print')
  913. test:exportImg(imgdata)
  914. end
  915. -- love.graphics.setFrontFaceWinding
  916. love.test.graphics.setFrontFaceWinding = function(test)
  917. -- check documented modes are valid
  918. local original = love.graphics.getFrontFaceWinding()
  919. love.graphics.setFrontFaceWinding('cw')
  920. test:assertEquals('cw', love.graphics.getFrontFaceWinding(), 'check ffw cw set')
  921. love.graphics.setFrontFaceWinding('ccw')
  922. test:assertEquals('ccw', love.graphics.getFrontFaceWinding(), 'check ffw ccw set')
  923. love.graphics.setFrontFaceWinding(original)
  924. -- @TODO better graphics drawing specific test
  925. end
  926. -- love.graphics.setLineJoin
  927. love.test.graphics.setLineJoin = function(test)
  928. local canvas = love.graphics.newCanvas(16, 16)
  929. love.graphics.setFont(Font)
  930. love.graphics.setCanvas(canvas)
  931. love.graphics.clear(0, 0, 0, 1)
  932. local line = {0,1,8,1,8,8}
  933. love.graphics.setLineStyle('rough')
  934. love.graphics.setLineWidth(2)
  935. love.graphics.setColor(1, 0, 0)
  936. love.graphics.setLineJoin('bevel')
  937. love.graphics.line(line)
  938. love.graphics.translate(0, 4)
  939. love.graphics.setColor(1, 1, 0)
  940. love.graphics.setLineJoin('none')
  941. love.graphics.line(line)
  942. love.graphics.translate(0, 4)
  943. love.graphics.setColor(0, 0, 1)
  944. love.graphics.setLineJoin('miter')
  945. love.graphics.line(line)
  946. love.graphics.setColor(1, 1, 1)
  947. love.graphics.setLineWidth(1)
  948. love.graphics.origin()
  949. love.graphics.setCanvas()
  950. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  951. test:assertPixels(imgdata, {
  952. black = {{8,0}},
  953. red = {{8,4}},
  954. yellow = {{8,7}},
  955. blue = {{8,8}}
  956. }, 'set line join')
  957. test:exportImg(imgdata)
  958. end
  959. -- love.graphics.setLineStyle
  960. love.test.graphics.setLineStyle = function(test)
  961. local canvas = love.graphics.newCanvas(16, 16)
  962. love.graphics.setFont(Font)
  963. love.graphics.setCanvas(canvas)
  964. love.graphics.clear(0, 0, 0, 1)
  965. love.graphics.setColor(1, 0, 0)
  966. local line = {0,1,16,1}
  967. love.graphics.setLineStyle('rough')
  968. love.graphics.line(line)
  969. love.graphics.translate(0, 4)
  970. love.graphics.setLineStyle('smooth')
  971. love.graphics.line(line)
  972. love.graphics.setLineStyle('rough')
  973. love.graphics.setColor(1, 1, 1)
  974. love.graphics.origin()
  975. love.graphics.setCanvas()
  976. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  977. test:assertPixels(imgdata, {
  978. red = {{0,0},{7,0},{15,0}},
  979. red07 = {{0,4},{7,4},{15,4}}
  980. }, 'set line style')
  981. test:exportImg(imgdata)
  982. end
  983. -- love.graphics.setLineWidth
  984. love.test.graphics.setLineWidth = function(test)
  985. local canvas = love.graphics.newCanvas(16, 16)
  986. love.graphics.setFont(Font)
  987. love.graphics.setCanvas(canvas)
  988. love.graphics.clear(0, 0, 0, 1)
  989. local line = {0,1,8,1,8,8}
  990. love.graphics.setColor(1, 0, 0)
  991. love.graphics.setLineWidth(2)
  992. love.graphics.line(line)
  993. love.graphics.translate(0, 4)
  994. love.graphics.setColor(1, 1, 0)
  995. love.graphics.setLineWidth(3)
  996. love.graphics.line(line)
  997. love.graphics.translate(0, 4)
  998. love.graphics.setColor(0, 0, 1)
  999. love.graphics.setLineWidth(4)
  1000. love.graphics.line(line)
  1001. love.graphics.setColor(1, 1, 1)
  1002. love.graphics.setLineWidth(1)
  1003. love.graphics.origin()
  1004. love.graphics.setCanvas()
  1005. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1006. test:assertPixels(imgdata, {
  1007. black = {{0,2},{6,2},{0,6},{5,6},{0,11},{5,11}},
  1008. red = {{0,0},{0,1},{7,2},{8,2}},
  1009. yellow = {{0,3},{0,5},{6,6},{8,6}},
  1010. blue = {{0,7},{0,10},{6,15},{9,15}}
  1011. }, 'set line width')
  1012. test:exportImg(imgdata)
  1013. end
  1014. -- love.graphics.setMeshCullMode
  1015. love.test.graphics.setMeshCullMode = function(test)
  1016. -- check documented modes are valid
  1017. local original = love.graphics.getMeshCullMode()
  1018. local modes = {'back', 'front', 'none'}
  1019. for m=1,#modes do
  1020. love.graphics.setMeshCullMode(modes[m])
  1021. test:assertEquals(modes[m], love.graphics.getMeshCullMode(), 'check mesh cull mode ' .. modes[m] .. ' was set')
  1022. end
  1023. love.graphics.setMeshCullMode(original)
  1024. -- @TODO better graphics drawing specific test
  1025. end
  1026. -- love.graphics.setScissor
  1027. love.test.graphics.setScissor = function(test)
  1028. -- make a scissor for the left half
  1029. -- then we should be able to fill the canvas with red and only left is filled
  1030. local canvas = love.graphics.newCanvas(16, 16)
  1031. love.graphics.setCanvas(canvas)
  1032. love.graphics.clear(0, 0, 0, 1)
  1033. love.graphics.origin()
  1034. love.graphics.setScissor(0, 0, 8, 16)
  1035. love.graphics.setColor(1, 0, 0, 1)
  1036. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1037. love.graphics.setColor(1, 1, 1, 1)
  1038. love.graphics.setScissor()
  1039. love.graphics.setCanvas()
  1040. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1041. test:assertPixels(imgdata, {
  1042. red = {{0,0},{7,0},{0,15},{7,15}},
  1043. black ={{8,0},{8,15},{15,0},{15,15}}
  1044. }, 'set scissor')
  1045. test:exportImg(imgdata)
  1046. end
  1047. -- love.graphics.setShader
  1048. love.test.graphics.setShader = function(test)
  1049. -- make a shader that will only ever draw yellow
  1050. local pixelcode = 'vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) { vec4 texturecolor = Texel(tex, texture_coords); return vec4(1.0,1.0,0.0,1.0);}'
  1051. local vertexcode = 'vec4 position(mat4 transform_projection, vec4 vertex_position) { return transform_projection * vertex_position; }'
  1052. local shader = love.graphics.newShader(pixelcode, vertexcode)
  1053. local canvas = love.graphics.newCanvas(16, 16)
  1054. love.graphics.setCanvas(canvas)
  1055. love.graphics.clear(0, 0, 0, 1)
  1056. love.graphics.setShader(shader)
  1057. -- draw red rectangle
  1058. love.graphics.setColor(1, 0, 0, 1)
  1059. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1060. love.graphics.setShader()
  1061. love.graphics.setColor(1, 1, 1, 1)
  1062. love.graphics.setCanvas()
  1063. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1064. test:assertPixels(imgdata, {
  1065. yellow = {{0,0},{15,0},{0,15},{15,15}},
  1066. }, 'check shader set to yellow')
  1067. test:exportImg(imgdata)
  1068. end
  1069. -- love.graphics.setStencilTest
  1070. love.test.graphics.setStencilTest = function(test)
  1071. local canvas = love.graphics.newCanvas(16, 16)
  1072. love.graphics.setCanvas({canvas, stencil=true})
  1073. love.graphics.clear(0, 0, 0, 1)
  1074. love.graphics.stencil(function()
  1075. love.graphics.circle('fill', 8, 8, 6)
  1076. end, 'replace', 1)
  1077. love.graphics.setStencilTest('greater', 0)
  1078. love.graphics.setColor(1, 0, 0, 1)
  1079. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1080. love.graphics.setColor(1, 1, 1, 1)
  1081. love.graphics.setStencilTest()
  1082. love.graphics.setCanvas()
  1083. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1084. test:assertPixels(imgdata, {
  1085. red = {{6,2},{9,2},{2,6},{2,9},{13,6},{9,6},{6,13},{9,13}}
  1086. }, 'check stencil test')
  1087. test:exportImg(imgdata)
  1088. end
  1089. -- love.graphics.setWireframe
  1090. love.test.graphics.setWireframe = function(test)
  1091. -- check wireframe outlines
  1092. love.graphics.setWireframe(true)
  1093. local canvas = love.graphics.newCanvas(16, 16)
  1094. love.graphics.setCanvas(canvas)
  1095. love.graphics.clear(0, 0, 0, 1)
  1096. love.graphics.setColor(1, 1, 0, 1)
  1097. love.graphics.rectangle('fill', 2, 2, 13, 13)
  1098. love.graphics.setColor(1, 1, 1, 1)
  1099. love.graphics.setWireframe(false)
  1100. love.graphics.setCanvas()
  1101. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1102. test:assertPixels(imgdata, {
  1103. yellow = {{1,14},{14,1},{14,14},{2,2},{13,13}},
  1104. black = {{2,13},{13,2}}
  1105. }, 'set wireframe')
  1106. test:exportImg(imgdata)
  1107. end
  1108. --------------------------------------------------------------------------------
  1109. --------------------------------------------------------------------------------
  1110. -------------------------------COORDINATE SYSTEM--------------------------------
  1111. --------------------------------------------------------------------------------
  1112. --------------------------------------------------------------------------------
  1113. -- love.graphics.applyTransform
  1114. love.test.graphics.applyTransform = function(test)
  1115. -- use transform object to translate the drawn rectangle
  1116. local transform = love.math.newTransform()
  1117. transform:translate(10, 0)
  1118. local canvas = love.graphics.newCanvas(16, 16)
  1119. love.graphics.setCanvas(canvas)
  1120. love.graphics.clear(0, 0, 0, 1)
  1121. love.graphics.setColor(1, 0, 0, 1)
  1122. love.graphics.applyTransform(transform)
  1123. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1124. love.graphics.setColor(1, 1, 1, 1)
  1125. love.graphics.setCanvas()
  1126. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1127. test:assertPixels(imgdata, { red = {{10, 0}} }, 'apply transform 10')
  1128. test:exportImg(imgdata)
  1129. end
  1130. -- love.graphics.inverseTransformPoint
  1131. love.test.graphics.inverseTransformPoint = function(test)
  1132. -- start with 0, 0
  1133. local sx, sy = love.graphics.inverseTransformPoint(0, 0)
  1134. test:assertEquals(0, sx, 'check starting x is 0')
  1135. test:assertEquals(0, sy, 'check starting y is 0')
  1136. -- check translation effects the point
  1137. love.graphics.translate(1, 5)
  1138. sx, sy = love.graphics.inverseTransformPoint(1, 5)
  1139. test:assertEquals(0, sx, 'check transformed x is 0')
  1140. test:assertEquals(0, sy, 'check transformed y is 0')
  1141. love.graphics.origin()
  1142. end
  1143. -- love.graphics.origin
  1144. love.test.graphics.origin = function(test)
  1145. -- if we do some translations and scaling
  1146. -- using .origin() should reset it all and draw the pixel at 0,0
  1147. local canvas = love.graphics.newCanvas(16, 16)
  1148. love.graphics.setCanvas(canvas)
  1149. love.graphics.clear(0, 0, 0, 1)
  1150. love.graphics.origin()
  1151. love.graphics.translate(10, 10)
  1152. love.graphics.scale(1, 1)
  1153. love.graphics.shear(20, 20)
  1154. love.graphics.origin()
  1155. love.graphics.setColor(1, 0, 0, 1)
  1156. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1157. love.graphics.setColor(1, 1, 1, 1)
  1158. love.graphics.setCanvas()
  1159. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1160. test:assertPixels(imgdata, { red = {{0, 0}} }, 'origin check')
  1161. test:exportImg(imgdata)
  1162. end
  1163. -- love.graphics.pop
  1164. love.test.graphics.pop = function(test)
  1165. -- if we push at the start, and then run a pop
  1166. -- it should reset it all and draw the pixel at 0,0
  1167. local canvas = love.graphics.newCanvas(16, 16)
  1168. love.graphics.setCanvas(canvas)
  1169. love.graphics.clear(0, 0, 0, 1)
  1170. love.graphics.push()
  1171. love.graphics.translate(10, 10)
  1172. love.graphics.scale(1, 1)
  1173. love.graphics.shear(20, 20)
  1174. love.graphics.pop()
  1175. love.graphics.setColor(1, 0, 0, 1)
  1176. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1177. love.graphics.setColor(1, 1, 1, 1)
  1178. love.graphics.setCanvas()
  1179. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1180. test:assertPixels(imgdata, { red = {{0, 0}} }, 'pop 1')
  1181. test:exportImg(imgdata)
  1182. end
  1183. -- love.graphics.push
  1184. love.test.graphics.push = function(test)
  1185. -- if we push at the start, do some stuff, then another push
  1186. -- 1 pop should only go back 1 push and draw the pixel at 1, 1
  1187. local canvas = love.graphics.newCanvas(16, 16)
  1188. love.graphics.setCanvas(canvas)
  1189. love.graphics.clear(0, 0, 0, 1)
  1190. love.graphics.push()
  1191. love.graphics.scale(1, 1)
  1192. love.graphics.shear(20, 20)
  1193. love.graphics.push()
  1194. love.graphics.translate(1, 1)
  1195. love.graphics.pop()
  1196. love.graphics.setColor(1, 0, 0, 1)
  1197. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1198. love.graphics.setColor(1, 1, 1, 1)
  1199. love.graphics.setCanvas()
  1200. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1201. test:assertPixels(imgdata, { red = {{1, 1}} }, 'push 1')
  1202. test:exportImg(imgdata)
  1203. end
  1204. -- love.graphics.replaceTransform
  1205. love.test.graphics.replaceTransform = function(test)
  1206. -- if use transform object to translate
  1207. -- set some normal transforms first which should get overwritten
  1208. local transform = love.math.newTransform()
  1209. transform:translate(10, 0)
  1210. local canvas = love.graphics.newCanvas(16, 16)
  1211. love.graphics.setCanvas(canvas)
  1212. love.graphics.clear(0, 0, 0, 1)
  1213. love.graphics.setColor(1, 0, 0, 1)
  1214. love.graphics.scale(2, 2)
  1215. love.graphics.translate(10, 10)
  1216. love.graphics.replaceTransform(transform)
  1217. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1218. love.graphics.setColor(1, 1, 1, 1)
  1219. love.graphics.setCanvas()
  1220. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1221. test:assertPixels(imgdata, { red = {{10, 0}} }, 'replace transform 10')
  1222. test:exportImg(imgdata)
  1223. end
  1224. -- love.graphics.rotate
  1225. love.test.graphics.rotate = function(test)
  1226. -- starting at 0,0, we rotate by 90deg and then draw
  1227. -- we can then check the drawn rectangle is rotated
  1228. local canvas = love.graphics.newCanvas(16, 16)
  1229. love.graphics.setCanvas(canvas)
  1230. love.graphics.clear(0, 0, 0, 1)
  1231. love.graphics.setColor(1, 0, 0, 1)
  1232. love.graphics.translate(4, 0)
  1233. love.graphics.rotate(90 * (math.pi/180))
  1234. love.graphics.rectangle('fill', 0, 0, 4, 4)
  1235. love.graphics.setColor(1, 1, 1, 1)
  1236. love.graphics.setCanvas()
  1237. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1238. test:assertPixels(imgdata, { red = {{0,0},{3,0},{3,3},{0,3}} }, 'rotate 90')
  1239. test:exportImg(imgdata)
  1240. end
  1241. -- love.graphics.scale
  1242. love.test.graphics.scale = function(test)
  1243. -- starting at 0,0, we scale by 4x and then draw
  1244. -- we can then check the drawn rectangle covers the whole canvas
  1245. local canvas = love.graphics.newCanvas(16, 16)
  1246. love.graphics.setCanvas(canvas)
  1247. love.graphics.clear(0, 0, 0, 1)
  1248. love.graphics.setColor(1, 0, 0, 1)
  1249. love.graphics.scale(4, 4)
  1250. love.graphics.rectangle('fill', 0, 0, 4, 4)
  1251. love.graphics.setColor(1, 1, 1, 1)
  1252. love.graphics.setCanvas()
  1253. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1254. test:assertPixels(imgdata, { red = {{1,1},{1,15},{15,1},{15,15}} }, 'scale 4x')
  1255. end
  1256. -- love.graphics.shear
  1257. love.test.graphics.shear = function(test)
  1258. -- starting at 0,0, we shear by 2x and then draw
  1259. -- we can then check the drawn rectangle has moved over
  1260. local canvas = love.graphics.newCanvas(16, 16)
  1261. love.graphics.setCanvas(canvas)
  1262. love.graphics.clear(0, 0, 0, 1)
  1263. love.graphics.origin()
  1264. love.graphics.setColor(1, 0, 0, 1)
  1265. love.graphics.shear(2, 0)
  1266. love.graphics.rectangle('fill', 0, 0, 4, 4)
  1267. love.graphics.setColor(1, 1, 1, 1)
  1268. love.graphics.setCanvas()
  1269. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1270. test:assertPixels(imgdata1, { red = {{1,0},{4,0},{7,3},{10,3}} }, 'shear x')
  1271. -- same again at 0,0, we shear by 2y and then draw
  1272. -- we can then check the drawn rectangle has moved down
  1273. love.graphics.setCanvas(canvas)
  1274. love.graphics.clear(0, 0, 0, 1)
  1275. love.graphics.origin()
  1276. love.graphics.setColor(1, 0, 0, 1)
  1277. love.graphics.shear(0, 2)
  1278. love.graphics.rectangle('fill', 0, 0, 4, 4)
  1279. love.graphics.setColor(1, 1, 1, 1)
  1280. love.graphics.setCanvas()
  1281. local imgdata2 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1282. test:assertPixels(imgdata2, { red = { {0,1},{0,4},{3,7},{3,10}} }, 'shear y')
  1283. test:exportImg(imgdata1)
  1284. test:exportImg(imgdata2)
  1285. end
  1286. -- love.graphics.transformPoint
  1287. love.test.graphics.transformPoint = function(test)
  1288. -- start with 0, 0
  1289. local sx, sy = love.graphics.transformPoint(0, 0)
  1290. test:assertEquals(0, sx, 'check starting x is 0')
  1291. test:assertEquals(0, sy, 'check starting y is 0')
  1292. -- check translation effects the point
  1293. love.graphics.translate(1, 5)
  1294. sx, sy = love.graphics.transformPoint(0, 0)
  1295. test:assertEquals(1, sx, 'check transformed x is 0')
  1296. test:assertEquals(5, sy, 'check transformed y is 10')
  1297. end
  1298. -- love.graphics.translate
  1299. love.test.graphics.translate = function(test)
  1300. -- starting at 0,0, we translate 4 times and draw a pixel at each point
  1301. -- we can then check the 4 points are now red
  1302. local canvas = love.graphics.newCanvas(16, 16)
  1303. love.graphics.setCanvas(canvas)
  1304. love.graphics.clear(0, 0, 0, 1)
  1305. love.graphics.setColor(1, 0, 0, 1)
  1306. love.graphics.translate(5, 0)
  1307. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1308. love.graphics.translate(0, 5)
  1309. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1310. love.graphics.translate(-5, 0)
  1311. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1312. love.graphics.translate(0, -5)
  1313. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1314. love.graphics.setColor(1, 1, 1, 1)
  1315. love.graphics.setCanvas()
  1316. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1317. test:assertPixels(imgdata, { red = {{5,0},{0,5},{5,5},{0,0}} }, 'translate 4x')
  1318. test:exportImg(imgdata)
  1319. end
  1320. --------------------------------------------------------------------------------
  1321. --------------------------------------------------------------------------------
  1322. -------------------------------------WINDOW-------------------------------------
  1323. --------------------------------------------------------------------------------
  1324. --------------------------------------------------------------------------------
  1325. -- love.graphics.getDPIScale
  1326. -- @NOTE hardware dependent so can't check result
  1327. love.test.graphics.getDPIScale = function(test)
  1328. test:assertNotNil(love.graphics.getDPIScale())
  1329. end
  1330. -- love.graphics.getDimensions
  1331. love.test.graphics.getDimensions = function(test)
  1332. -- check graphics dimensions match window dimensions
  1333. local gwidth, gheight = love.graphics.getDimensions()
  1334. local wwidth, wheight, _ = love.window.getMode()
  1335. test:assertEquals(wwidth, gwidth, 'check graphics dimension w matches window w')
  1336. test:assertEquals(wheight, gheight, 'check graphics dimension h matches window h')
  1337. end
  1338. -- love.graphics.getHeight
  1339. love.test.graphics.getHeight = function(test)
  1340. -- check graphics height match window height
  1341. local wwidth, wheight, _ = love.window.getMode()
  1342. test:assertEquals(wheight, love.graphics.getHeight(), 'check graphics h matches window h')
  1343. end
  1344. -- love.graphics.getPixelDimensions
  1345. love.test.graphics.getPixelDimensions = function(test)
  1346. -- check graphics dimensions match window dimensions relative to dpi
  1347. local dpi = love.graphics.getDPIScale()
  1348. local gwidth, gheight = love.graphics.getPixelDimensions()
  1349. local wwidth, wheight, _ = love.window.getMode()
  1350. test:assertEquals(wwidth, gwidth/dpi, 'check graphics pixel dpi w matches window w')
  1351. test:assertEquals(wheight, gheight/dpi, 'check graphics pixel dpi h matches window h')
  1352. end
  1353. -- love.graphics.getPixelHeight
  1354. love.test.graphics.getPixelHeight = function(test)
  1355. -- check graphics height match window height relative to dpi
  1356. local dpi = love.graphics.getDPIScale()
  1357. local wwidth, wheight, _ = love.window.getMode()
  1358. test:assertEquals(wheight,love.graphics.getPixelHeight()/dpi, 'check graphics pixel dpi h matches window h')
  1359. end
  1360. -- love.graphics.getPixelWidth
  1361. love.test.graphics.getPixelWidth = function(test)
  1362. -- check graphics width match window width relative to dpi
  1363. local dpi = love.graphics.getDPIScale()
  1364. local wwidth, wheight, _ = love.window.getMode()
  1365. test:assertEquals(wwidth, love.graphics.getWidth()/dpi, 'check graphics pixel dpi w matches window w')
  1366. end
  1367. -- love.graphics.getWidth
  1368. love.test.graphics.getWidth = function(test)
  1369. -- check graphics width match window width
  1370. local wwidth, wheight, _ = love.window.getMode()
  1371. test:assertEquals(wwidth, love.graphics.getWidth(), 'check graphics w matches window w')
  1372. end
  1373. --------------------------------------------------------------------------------
  1374. --------------------------------------------------------------------------------
  1375. -------------------------------SYSTEM INFORMATION-------------------------------
  1376. --------------------------------------------------------------------------------
  1377. --------------------------------------------------------------------------------
  1378. -- love.graphics.getTextureFormats
  1379. love.test.graphics.getTextureFormats = function(test)
  1380. local formats = {
  1381. 'hdr', 'r8i', 'r8ui', 'r16i', 'r16ui', 'r32i', 'r32ui', 'rg8i', 'rg8ui',
  1382. 'rg16i', 'rg16ui', 'rg32i', 'rg32ui', 'bgra8', 'r8', 'rgba8i', 'rgba8ui',
  1383. 'rgba16i', 'rg8', 'rgba32i', 'rgba32ui', 'rgba8', 'DXT1', 'r16', 'DXT5',
  1384. 'rg16', 'BC4s', 'rgba16', 'BC5s', 'r16f', 'BC6hs', 'BC7', 'PVR1rgb2',
  1385. 'rg16f', 'PVR1rgba2', 'rgba16f', 'ETC1', 'r32f', 'ETC2rgba', 'rg32f',
  1386. 'EACr', 'rgba32f', 'EACrg', 'rgba4', 'ASTC4x4', 'ASTC5x4', 'rgb5a1',
  1387. 'ASTC6x5', 'rgb565', 'ASTC8x5', 'ASTC8x6', 'rgb10a2', 'ASTC10x5',
  1388. 'rg11b10f', 'ASTC10x8', 'ASTC10x10', 'ASTC12x10', 'ASTC12x12', 'normal',
  1389. 'srgba8', 'la8', 'ASTC10x6', 'ASTC8x8', 'ASTC6x6', 'ASTC5x5', 'EACrgs',
  1390. 'EACrs', 'ETC2rgba1', 'ETC2rgb', 'PVR1rgba4', 'PVR1rgb4', 'BC6h',
  1391. 'BC5', 'BC4', 'DXT3', 'stencil8', 'rgba16ui', 'bgra8srgb'
  1392. }
  1393. local supported = love.graphics.getTextureFormats({ canvas = true })
  1394. test:assertNotNil(supported)
  1395. for f=1,#formats do
  1396. test:assertNotEquals(nil, supported[formats[f] ], 'expected a key for format: ' .. formats[f])
  1397. end
  1398. end
  1399. -- love.graphics.getRendererInfo
  1400. -- @NOTE hardware dependent so best can do is nil checking
  1401. love.test.graphics.getRendererInfo = function(test)
  1402. local name, version, vendor, device = love.graphics.getRendererInfo()
  1403. test:assertNotNil(name)
  1404. test:assertNotNil(version)
  1405. test:assertNotNil(vendor)
  1406. test:assertNotNil(device)
  1407. end
  1408. -- love.graphics.getStats
  1409. -- @NOTE cant really predict some of these so just nil check for most
  1410. love.test.graphics.getStats = function(test)
  1411. local stattypes = {
  1412. 'drawcalls', 'canvasswitches', 'texturememory', 'shaderswitches',
  1413. 'drawcallsbatched', 'textures', 'fonts'
  1414. }
  1415. local stats = love.graphics.getStats()
  1416. for s=1,#stattypes do
  1417. test:assertNotEquals(nil, stats[stattypes[s] ], 'expected a key for stat: ' .. stattypes[s])
  1418. end
  1419. end
  1420. -- love.graphics.getSupported
  1421. love.test.graphics.getSupported = function(test)
  1422. -- cant check values as hardware dependent but we can check the keys in the
  1423. -- table match what the documentation lists
  1424. local gfs = {
  1425. 'clampzero', 'lighten', 'glsl3', 'instancing', 'fullnpot',
  1426. 'pixelshaderhighp', 'shaderderivatives', 'indirectdraw', 'mipmaprange',
  1427. 'copyrendertargettobuffer', 'copytexturetobuffer', 'copybuffer',
  1428. 'indexbuffer32bit', 'multirendertargetformats', 'clampone', 'blendminmax',
  1429. 'glsl4'
  1430. }
  1431. local features = love.graphics.getSupported()
  1432. for g=1,#gfs do
  1433. test:assertNotEquals(nil, features[gfs[g] ], 'expected a key for graphic feature: ' .. gfs[g])
  1434. end
  1435. end
  1436. -- love.graphics.getSystemLimits
  1437. love.test.graphics.getSystemLimits = function(test)
  1438. -- cant check values as hardware dependent but we can check the keys in the
  1439. -- table match what the documentation lists
  1440. local glimits = {
  1441. 'texelbuffersize', 'shaderstoragebuffersize', 'threadgroupsx',
  1442. 'threadgroupsy', 'pointsize', 'texturesize', 'texturelayers', 'volumetexturesize',
  1443. 'cubetexturesize', 'anisotropy', 'texturemsaa', 'rendertargets', 'threadgroupsz'
  1444. }
  1445. local limits = love.graphics.getSystemLimits()
  1446. for g=1,#glimits do
  1447. test:assertNotEquals(nil, limits[glimits[g] ], 'expected a key for system limit: ' .. glimits[g])
  1448. end
  1449. end
  1450. -- love.graphics.getTextureTypes
  1451. love.test.graphics.getTextureTypes = function(test)
  1452. -- cant check values as hardware dependent but we can check the keys in the
  1453. -- table match what the documentation lists
  1454. local ttypes = {
  1455. '2d', 'array', 'cube', 'volume'
  1456. }
  1457. local types = love.graphics.getTextureTypes()
  1458. for t=1,#ttypes do
  1459. test:assertNotEquals(nil, types[ttypes[t] ], 'expected a key for texture type: ' .. ttypes[t])
  1460. end
  1461. end