graphics.lua 49 KB

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