graphics.lua 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846
  1. -- love.graphics
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ------------------------------------OBJECTS-------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- GraphicsBuffer (love.graphics.newBuffer)
  8. love.test.graphics.Buffer = function(test)
  9. -- setup vertex data and create some buffers
  10. local vertexformat = {
  11. {name="VertexPosition", format="floatvec2", location=0},
  12. {name="VertexTexCoord", format="floatvec2", location=1},
  13. {name="VertexColor", format="unorm8vec4", location=2},
  14. }
  15. local vertexdata = {
  16. {0, 0, 0, 0, 1, 0, 1, 1},
  17. {10, 0, 1, 0, 0, 1, 1, 1},
  18. {10, 10, 1, 1, 0, 0, 1, 1},
  19. {0, 10, 0, 1, 1, 0, 0, 1},
  20. }
  21. local flatvertexdata = {}
  22. for i, vert in ipairs(vertexdata) do
  23. for j, v in ipairs(vert) do
  24. table.insert(flatvertexdata, v)
  25. end
  26. end
  27. local vertexbuffer1 = love.graphics.newBuffer(vertexformat, 4, {vertex=true, debugname='testvertexbuffer'})
  28. local vertexbuffer2 = love.graphics.newBuffer(vertexformat, vertexdata, {vertex=true})
  29. test:assertObject(vertexbuffer1)
  30. test:assertObject(vertexbuffer2)
  31. -- check buffer properties
  32. test:assertEquals(4, vertexbuffer1:getElementCount(), 'check vertex count 1')
  33. test:assertEquals(4, vertexbuffer2:getElementCount(), 'check vertex count 2')
  34. -- vertex buffers have their elements tightly packed.
  35. test:assertEquals(20, vertexbuffer1:getElementStride(), 'check vertex array stride')
  36. test:assertEquals(20 * 4, vertexbuffer1:getSize(), 'check vertex buffer size')
  37. vertexbuffer1:setArrayData(vertexdata)
  38. vertexbuffer1:setArrayData(flatvertexdata)
  39. vertexbuffer1:clear(8, 8) -- partial clear (the first texcoord)
  40. -- check buffer types
  41. test:assertTrue(vertexbuffer1:isBufferType('vertex'), 'check is vertex buffer')
  42. test:assertFalse(vertexbuffer1:isBufferType('index'), 'check is not index buffer')
  43. test:assertFalse(vertexbuffer1:isBufferType('texel'), 'check is not texel buffer')
  44. test:assertFalse(vertexbuffer1:isBufferType('shaderstorage'), 'check is not shader storage buffer')
  45. -- check debug name
  46. test:assertEquals('testvertexbuffer', vertexbuffer1:getDebugName(), 'check buffer debug name')
  47. -- check buffer format and format properties
  48. local format = vertexbuffer1:getFormat()
  49. test:assertEquals('table', type(format), 'check buffer format is table')
  50. test:assertEquals(#vertexformat, #format, 'check buffer format length')
  51. for i, v in ipairs(vertexformat) do
  52. test:assertEquals(v.name, format[i].name, string.format('check buffer format %d name', i))
  53. test:assertEquals(v.format, format[i].format, string.format('check buffer format %d format', i))
  54. test:assertEquals(0, format[i].arraylength, string.format('check buffer format %d array length', i))
  55. test:assertNotNil(format[i].offset)
  56. end
  57. -- check index buffer
  58. local indexbuffer = love.graphics.newBuffer('uint16', 128, {index=true})
  59. test:assertTrue(indexbuffer:isBufferType('index'), 'check is index buffer')
  60. end
  61. -- Shader Storage GraphicsBuffer (love.graphics.newBuffer)
  62. -- Separated from the above test so we can skip it when they aren't supported.
  63. love.test.graphics.ShaderStorageBuffer = function(test)
  64. if not love.graphics.getSupported().glsl4 then
  65. test:skipTest('GLSL 4 and shader storage buffers are not supported on this system')
  66. return
  67. end
  68. -- setup buffer
  69. local format = {
  70. { name="1", format="float" },
  71. { name="2", format="floatmat4x4" },
  72. { name="3", format="floatvec2" }
  73. }
  74. local buffer = love.graphics.newBuffer(format, 1, {shaderstorage = true})
  75. test:assertEquals(96, buffer:getElementStride(), 'check shader storage buffer element stride')
  76. -- set new data
  77. local data = {}
  78. for i = 1, 19 do
  79. data[i] = 0
  80. end
  81. buffer:setArrayData(data)
  82. end
  83. -- Canvas (love.graphics.newCanvas)
  84. love.test.graphics.Canvas = function(test)
  85. -- create canvas with defaults
  86. local canvas = love.graphics.newCanvas(100, 100, {
  87. type = '2d',
  88. format = 'normal',
  89. readable = true,
  90. msaa = 0,
  91. dpiscale = love.graphics.getDPIScale(),
  92. mipmaps = 'auto',
  93. debugname = 'testcanvas'
  94. })
  95. test:assertObject(canvas)
  96. test:assertTrue(canvas:isCanvas(), 'check is canvas')
  97. test:assertFalse(canvas:isComputeWritable(), 'check not compute writable')
  98. -- check dpi
  99. test:assertEquals(love.graphics.getDPIScale(), canvas:getDPIScale(), 'check dpi scale')
  100. -- check depth
  101. test:assertEquals(1, canvas:getDepth(), 'check depth is 2d')
  102. test:assertEquals(nil, canvas:getDepthSampleMode(), 'check depth sample nil')
  103. local maxanisotropy = love.graphics.getSystemLimits().anisotropy
  104. -- check fliter
  105. local min1, mag1, ani1 = canvas:getFilter()
  106. test:assertEquals('nearest', min1, 'check filter def min')
  107. test:assertEquals('nearest', mag1, 'check filter def mag')
  108. test:assertEquals(1, ani1, 'check filter def ani')
  109. canvas:setFilter('linear', 'linear', 2)
  110. local min2, mag2, ani2 = canvas:getFilter()
  111. test:assertEquals('linear', min2, 'check filter changed min')
  112. test:assertEquals('linear', mag2, 'check filter changed mag')
  113. test:assertEquals(math.min(maxanisotropy, 2), ani2, 'check filter changed ani')
  114. -- check layer
  115. test:assertEquals(1, canvas:getLayerCount(), 'check 1 layer for 2d')
  116. -- check texture type
  117. test:assertEquals('2d', canvas:getTextureType(), 'check 2d')
  118. -- check texture wrap
  119. local horiz1, vert1 = canvas:getWrap()
  120. test:assertEquals('clamp', horiz1, 'check def wrap h')
  121. test:assertEquals('clamp', vert1, 'check def wrap v')
  122. canvas:setWrap('repeat', 'repeat')
  123. local horiz2, vert2 = canvas:getWrap()
  124. test:assertEquals('repeat', horiz2, 'check changed wrap h')
  125. test:assertEquals('repeat', vert2, 'check changed wrap v')
  126. -- check readable
  127. test:assertTrue(canvas:isReadable(), 'check canvas readable')
  128. -- check msaa
  129. test:assertEquals(1, canvas:getMSAA(), 'check samples match')
  130. -- check dimensions
  131. local cw, ch = canvas:getDimensions()
  132. test:assertEquals(100, cw, 'check canvas dim w')
  133. test:assertEquals(100, ch, 'check canvas dim h')
  134. test:assertEquals(cw, canvas:getWidth(), 'check canvas w matches dim')
  135. test:assertEquals(ch, canvas:getHeight(), 'check canvas h matches dim')
  136. local pw, ph = canvas:getPixelDimensions()
  137. test:assertEquals(100*love.graphics.getDPIScale(), pw, 'check pixel dim w')
  138. test:assertEquals(100*love.graphics.getDPIScale(), ph, 'check pixel dim h')
  139. test:assertEquals(pw, canvas:getPixelWidth(), 'check pixel w matches dim')
  140. test:assertEquals(ph, canvas:getPixelHeight(), 'check pixel h matches dim')
  141. -- check mipmaps
  142. local mode, sharpness = canvas:getMipmapFilter()
  143. test:assertEquals('linear', mode, 'check def minmap filter mode')
  144. test:assertEquals(0, sharpness, 'check def minmap filter sharpness')
  145. local name, version, vendor, device = love.graphics.getRendererInfo()
  146. canvas:setMipmapFilter('nearest', 1)
  147. mode, sharpness = canvas:getMipmapFilter()
  148. test:assertEquals('nearest', mode, 'check changed minmap filter mode')
  149. -- @NOTE mipmap sharpness wont work on opengl/metal
  150. if string.match(name, 'OpenGL ES') == nil and string.match(name, 'Metal') == nil then
  151. test:assertEquals(1, sharpness, 'check changed minmap filter sharpness')
  152. end
  153. test:assertGreaterEqual(2, canvas:getMipmapCount()) -- docs say no mipmaps should return 1
  154. test:assertEquals('auto', canvas:getMipmapMode())
  155. -- check debug name
  156. test:assertEquals('testcanvas', canvas:getDebugName())
  157. -- check basic rendering
  158. canvas:renderTo(function()
  159. love.graphics.setColor(1, 0, 0)
  160. love.graphics.rectangle('fill', 0, 0, 200, 200)
  161. love.graphics.setColor(1, 1, 1, 1)
  162. end)
  163. local imgdata1 = love.graphics.readbackTexture(canvas)
  164. test:compareImg(imgdata1)
  165. -- check using canvas in love.graphics.draw()
  166. local xcanvas = love.graphics.newCanvas()
  167. love.graphics.setCanvas(xcanvas)
  168. love.graphics.draw(canvas, 0, 0)
  169. love.graphics.setCanvas()
  170. local imgdata2 = love.graphics.readbackTexture(canvas)
  171. test:compareImg(imgdata2)
  172. -- check y-down
  173. local shader1 = love.graphics.newShader[[
  174. vec4 effect(vec4 c, Image tex, vec2 tc, vec2 pc) {
  175. return tc.y > 0.5 ? vec4(1.0, 0.0, 0.0, 1.0) : vec4(0.0, 1.0, 0.0, 1.0);
  176. }
  177. ]]
  178. local shader2 = love.graphics.newShader[[
  179. vec4 effect(vec4 c, Image tex, vec2 tc, vec2 pc) {
  180. // rounding during quantization from float to unorm8 doesn't seem to be
  181. // totally consistent across devices, lets do it ourselves.
  182. highp vec2 value = pc / love_ScreenSize.xy;
  183. highp vec2 quantized = (floor(255.0 * value + 0.5) + 0.1) / 255.0;
  184. return vec4(quantized, 0.0, 1.0);
  185. }
  186. ]]
  187. local img = love.graphics.newImage(love.image.newImageData(1, 1))
  188. love.graphics.push("all")
  189. love.graphics.setCanvas(canvas)
  190. love.graphics.setShader(shader1)
  191. love.graphics.draw(img, 0, 0, 0, canvas:getDimensions())
  192. love.graphics.pop()
  193. local imgdata3 = love.graphics.readbackTexture(canvas)
  194. test:compareImg(imgdata3)
  195. love.graphics.push("all")
  196. love.graphics.setCanvas(canvas)
  197. love.graphics.setShader(shader2)
  198. love.graphics.draw(img, 0, 0, 0, canvas:getDimensions())
  199. love.graphics.pop()
  200. local imgdata4 = love.graphics.readbackTexture(canvas)
  201. test:compareImg(imgdata4)
  202. -- check depth samples
  203. local dcanvas = love.graphics.newCanvas(100, 100, {
  204. type = '2d',
  205. format = 'depth16',
  206. readable = true
  207. })
  208. test:assertEquals(nil, dcanvas:getDepthSampleMode(), 'check depth sample mode nil by def')
  209. dcanvas:setDepthSampleMode('equal')
  210. test:assertEquals('equal', dcanvas:getDepthSampleMode(), 'check depth sample mode set')
  211. -- check compute writeable (wont work on opengl mac)
  212. if love.graphics.getSupported().glsl4 then
  213. local ccanvas = love.graphics.newCanvas(100, 100, {
  214. type = '2d',
  215. format = 'rgba8',
  216. computewrite = true
  217. })
  218. test:assertTrue(ccanvas:isComputeWritable())
  219. end
  220. end
  221. -- Font (love.graphics.newFont)
  222. love.test.graphics.Font = function(test)
  223. -- create obj
  224. local font = love.graphics.newFont('resources/font.ttf', 8)
  225. test:assertObject(font)
  226. -- check ascent/descent
  227. test:assertEquals(6, font:getAscent(), 'check ascent')
  228. test:assertEquals(-2, font:getDescent(), 'check descent')
  229. -- check baseline
  230. test:assertEquals(6, font:getBaseline(), 'check baseline')
  231. -- check dpi
  232. test:assertEquals(1, font:getDPIScale(), 'check dpi')
  233. -- check filter
  234. test:assertEquals('nearest', font:getFilter(), 'check filter def')
  235. font:setFilter('linear', 'linear')
  236. test:assertEquals('linear', font:getFilter(), 'check filter change')
  237. font:setFilter('nearest', 'nearest')
  238. -- check height + lineheight
  239. test:assertEquals(8, font:getHeight(), 'check height')
  240. test:assertEquals(1, font:getLineHeight(), 'check line height')
  241. font:setLineHeight(2)
  242. test:assertEquals(2, font:getLineHeight(), 'check changed line height')
  243. font:setLineHeight(1) -- reset for drawing + wrap later
  244. -- check width + kerning
  245. test:assertEquals(0, font:getKerning('a', 'b'), 'check kerning')
  246. test:assertEquals(24, font:getWidth('test'), 'check data size')
  247. -- check specific glyphs
  248. test:assertTrue(font:hasGlyphs('test'), 'check data size')
  249. -- check font wrapping
  250. local width, wrappedtext = font:getWrap('LÖVE is an *awesome* framework you can use to make 2D games in Lua.', 50)
  251. test:assertEquals(48, width, 'check actual wrap width')
  252. test:assertEquals(8, #wrappedtext, 'check wrapped lines')
  253. test:assertEquals('LÖVE is an ', wrappedtext[1], 'check wrapped line')
  254. -- check drawing font
  255. local canvas = love.graphics.newCanvas(16, 16)
  256. love.graphics.setCanvas(canvas)
  257. love.graphics.setFont(font)
  258. love.graphics.print('Aa', 0, 5)
  259. love.graphics.setCanvas()
  260. local imgdata = love.graphics.readbackTexture(canvas)
  261. test:compareImg(imgdata)
  262. -- check font substitution
  263. local fontab = love.graphics.newImageFont('resources/font-letters-ab.png', 'AB')
  264. local fontcd = love.graphics.newImageFont('resources/font-letters-cd.png', 'CD')
  265. fontab:setFallbacks(fontcd)
  266. love.graphics.setCanvas(canvas)
  267. love.graphics.clear(0, 0, 0, 0)
  268. love.graphics.setFont(fontab)
  269. love.graphics.print('AB', 0, 0) -- should come from fontab
  270. love.graphics.print('CD', 0, 9) -- should come from fontcd
  271. love.graphics.setCanvas()
  272. local imgdata2 = love.graphics.readbackTexture(canvas)
  273. test:compareImg(imgdata2)
  274. end
  275. -- Image (love.graphics.newImage)
  276. love.test.graphics.Image = function(test)
  277. -- create object
  278. local image = love.graphics.newImage('resources/love.png', {
  279. dpiscale = 1,
  280. mipmaps = true
  281. })
  282. test:assertObject(image)
  283. test:assertFalse(image:isCanvas(), 'check not canvas')
  284. test:assertFalse(image:isComputeWritable(), 'check not compute writable')
  285. -- check dpi
  286. test:assertEquals(love.graphics.getDPIScale(), image:getDPIScale(), 'check dpi scale')
  287. -- check depth
  288. test:assertEquals(1, image:getDepth(), 'check depth is 2d')
  289. test:assertEquals(nil, image:getDepthSampleMode(), 'check depth sample nil')
  290. local maxanisotropy = love.graphics.getSystemLimits().anisotropy
  291. -- check filter
  292. local min1, mag1, ani1 = image:getFilter()
  293. test:assertEquals('nearest', min1, 'check filter def min')
  294. test:assertEquals('nearest', mag1, 'check filter def mag')
  295. test:assertEquals(1, ani1, 'check filter def ani')
  296. image:setFilter('linear', 'linear', 2)
  297. local min2, mag2, ani2 = image:getFilter()
  298. test:assertEquals('linear', min2, 'check filter changed min')
  299. test:assertEquals('linear', mag2, 'check filter changed mag')
  300. test:assertEquals(math.min(maxanisotropy, 2), ani2, 'check filter changed ani')
  301. image:setFilter('nearest', 'nearest', 1)
  302. -- check layers
  303. test:assertEquals(1, image:getLayerCount(), 'check 1 layer for 2d')
  304. -- check texture type
  305. test:assertEquals('2d', image:getTextureType(), 'check 2d')
  306. -- check texture wrapping
  307. local horiz1, vert1 = image:getWrap()
  308. test:assertEquals('clamp', horiz1, 'check def wrap h')
  309. test:assertEquals('clamp', vert1, 'check def wrap v')
  310. image:setWrap('repeat', 'repeat')
  311. local horiz2, vert2 = image:getWrap()
  312. test:assertEquals('repeat', horiz2, 'check changed wrap h')
  313. test:assertEquals('repeat', vert2, 'check changed wrap v')
  314. -- check readable
  315. test:assertTrue(image:isReadable(), 'check canvas readable')
  316. -- check msaa
  317. test:assertEquals(1, image:getMSAA(), 'check samples match')
  318. -- check dimensions
  319. local cw, ch = image:getDimensions()
  320. test:assertEquals(64, cw, 'check canvas dim w')
  321. test:assertEquals(64, ch, 'check canvas dim h')
  322. test:assertEquals(cw, image:getWidth(), 'check canvas w matches dim')
  323. test:assertEquals(ch, image:getHeight(), 'check canvas h matches dim')
  324. local pw, ph = image:getPixelDimensions()
  325. test:assertEquals(64*love.graphics.getDPIScale(), pw, 'check pixel dim w')
  326. test:assertEquals(64*love.graphics.getDPIScale(), ph, 'check pixel dim h')
  327. test:assertEquals(pw, image:getPixelWidth(), 'check pixel w matches dim')
  328. test:assertEquals(ph, image:getPixelHeight(), 'check pixel h matches dim')
  329. -- check mipmaps
  330. local mode, sharpness = image:getMipmapFilter()
  331. test:assertEquals('linear', mode, 'check def minmap filter mode')
  332. test:assertEquals(0, sharpness, 'check def minmap filter sharpness')
  333. local name, version, vendor, device = love.graphics.getRendererInfo()
  334. -- @note mipmap sharpness wont work on opengl/metal
  335. image:setMipmapFilter('nearest', 1)
  336. mode, sharpness = image:getMipmapFilter()
  337. test:assertEquals('nearest', mode, 'check changed minmap filter mode')
  338. if string.match(name, 'OpenGL ES') == nil and string.match(name, 'Metal') == nil then
  339. test:assertEquals(1, sharpness, 'check changed minmap filter sharpness')
  340. end
  341. test:assertGreaterEqual(2, image:getMipmapCount()) -- docs say no mipmaps should return 1?
  342. -- check image properties
  343. test:assertFalse(image:isCompressed(), 'check not compressed')
  344. test:assertFalse(image:isFormatLinear(), 'check not linear')
  345. local cimage = love.graphics.newImage('resources/love.dxt1')
  346. test:assertObject(cimage)
  347. test:assertTrue(cimage:isCompressed(), 'check is compressed')
  348. -- check pixel replacement
  349. local rimage = love.image.newImageData('resources/loveinv.png')
  350. image:replacePixels(rimage)
  351. local canvas = love.graphics.newCanvas(64, 64)
  352. love.graphics.setCanvas(canvas)
  353. love.graphics.draw(image, 0, 0)
  354. love.graphics.setCanvas()
  355. local imgdata = love.graphics.readbackTexture(canvas)
  356. local r1, g1, b1 = imgdata:getPixel(25, 25)
  357. test:assertEquals(3, r1+g1+b1, 'check back to white')
  358. test:compareImg(imgdata)
  359. end
  360. -- Mesh (love.graphics.newMesh)
  361. love.test.graphics.Mesh = function(test)
  362. -- create 2d mesh with pretty colors
  363. local image = love.graphics.newImage('resources/love.png')
  364. local vertices = {
  365. { 0, 0, 0, 0, 1, 0, 0 },
  366. { image:getWidth(), 0, 1, 0, 0, 1, 0 },
  367. { image:getWidth(), image:getHeight(), 1, 1, 0, 0, 1 },
  368. { 0, image:getHeight(), 0, 1, 1, 1, 0 },
  369. }
  370. local mesh1 = love.graphics.newMesh(vertices, 'fan')
  371. test:assertObject(mesh1)
  372. -- check draw mode
  373. test:assertEquals('fan', mesh1:getDrawMode(), 'check draw mode')
  374. mesh1:setDrawMode('triangles')
  375. test:assertEquals('triangles', mesh1:getDrawMode(), 'check draw mode set')
  376. -- check draw range
  377. local min1, max1 = mesh1:getDrawRange()
  378. test:assertEquals(nil, min1, 'check draw range not set')
  379. mesh1:setDrawRange(1, 10)
  380. local min2, max2 = mesh1:getDrawRange()
  381. test:assertEquals(1, min2, 'check draw range set min')
  382. test:assertEquals(10, max2, 'check draw range set max')
  383. -- check texture pointer
  384. test:assertEquals(nil, mesh1:getTexture(), 'check no texture')
  385. mesh1:setTexture(image)
  386. test:assertEquals(image:getHeight(), mesh1:getTexture():getHeight(), 'check texture match w')
  387. test:assertEquals(image:getWidth(), mesh1:getTexture():getWidth(), 'check texture match h')
  388. -- check vertext count
  389. test:assertEquals(4, mesh1:getVertexCount(), 'check vertex count')
  390. -- check def vertex format
  391. local format = mesh1:getVertexFormat()
  392. test:assertEquals('floatvec2', format[2].format, 'check def vertex format 2')
  393. test:assertEquals('VertexColor', format[3].name, 'check def vertex format 3')
  394. -- check vertext attributes
  395. test:assertTrue(mesh1:isAttributeEnabled('VertexPosition'), 'check def attribute VertexPosition')
  396. test:assertTrue(mesh1:isAttributeEnabled('VertexTexCoord'), 'check def attribute VertexTexCoord')
  397. test:assertTrue(mesh1:isAttributeEnabled('VertexColor'), 'check def attribute VertexColor')
  398. mesh1:setAttributeEnabled('VertexPosition', false)
  399. mesh1:setAttributeEnabled('VertexTexCoord', false)
  400. mesh1:setAttributeEnabled('VertexColor', false)
  401. test:assertFalse(mesh1:isAttributeEnabled('VertexPosition'), 'check disable attribute VertexPosition')
  402. test:assertFalse(mesh1:isAttributeEnabled('VertexTexCoord'), 'check disable attribute VertexTexCoord')
  403. test:assertFalse(mesh1:isAttributeEnabled('VertexColor'), 'check disable attribute VertexColor')
  404. -- check vertex itself
  405. local x1, y1, u1, v1, r1, g1, b1, a1 = mesh1:getVertex(1)
  406. test:assertEquals(0, x1, 'check vertex props x')
  407. test:assertEquals(0, y1, 'check vertex props y')
  408. test:assertEquals(0, u1, 'check vertex props u')
  409. test:assertEquals(0, v1, 'check vertex props v')
  410. test:assertEquals(1, r1, 'check vertex props r')
  411. test:assertEquals(0, g1, 'check vertex props g')
  412. test:assertEquals(0, b1, 'check vertex props b')
  413. test:assertEquals(1, a1, 'check vertex props a')
  414. -- check setting a specific vertex
  415. mesh1:setVertex(2, image:getWidth(), 0, 1, 0, 0, 1, 1, 1)
  416. local x2, y2, u2, v2, r2, g2, b2, a2 = mesh1:getVertex(2)
  417. test:assertEquals(image:getWidth(), x2, 'check changed vertex props x')
  418. test:assertEquals(0, y2, 'check changed vertex props y')
  419. test:assertEquals(1, u2, 'check changed vertex props u')
  420. test:assertEquals(0, v2, 'check changed vertex props v')
  421. test:assertEquals(0, r2, 'check changed vertex props r')
  422. test:assertEquals(1, g2, 'check changed vertex props g')
  423. test:assertEquals(1, b2, 'check changed vertex props b')
  424. test:assertEquals(1, a2, 'check changed vertex props a')
  425. -- check setting a specific vertex attribute
  426. local r3, g3, b3, a3 = mesh1:getVertexAttribute(3, 3)
  427. test:assertEquals(1, b3, 'check specific vertex color')
  428. mesh1:setVertexAttribute(4, 3, 1, 0, 1)
  429. local r4, g4, b4, a4 = mesh1:getVertexAttribute(4, 3)
  430. test:assertEquals(0, g4, 'check changed vertex color')
  431. -- check setting a vertice
  432. mesh1:setVertices(vertices)
  433. local r5, g5, b5, a5 = mesh1:getVertexAttribute(4, 3)
  434. local x6, y6, u6, v6, r6, g6, b6, a6 = mesh1:getVertex(2)
  435. test:assertEquals(1, g5, 'check reset vertex color 1')
  436. test:assertEquals(0, b5, 'check reset vertex color 2')
  437. -- check setting the vertex map
  438. local vmap1 = mesh1:getVertexMap()
  439. test:assertEquals(nil, vmap1, 'check no map by def')
  440. mesh1:setVertexMap({4, 1, 2, 3})
  441. local vmap2 = mesh1:getVertexMap()
  442. test:assertEquals(4, #vmap2, 'check set map len')
  443. test:assertEquals(2, vmap2[3], 'check set map val')
  444. -- check using custom attributes
  445. local mesh2 = love.graphics.newMesh({
  446. { name = 'VertexPosition', format = 'floatvec2', location = 0},
  447. { name = 'VertexTexCoord', format = 'floatvec2', location = 1},
  448. { name = 'VertexColor', format = 'floatvec4', location = 2},
  449. { name = 'CustomValue1', format = 'floatvec2', location = 3},
  450. { name = 'CustomValue2', format = 'uint16', location = 4}
  451. }, {
  452. { 0, 0, 0, 0, 1, 0, 0, 1, 2, 1, 1005 },
  453. { image:getWidth(), 0, 1, 0, 0, 1, 0, 0, 2, 2, 2005 },
  454. { image:getWidth(), image:getHeight(), 1, 1, 0, 0, 1, 0, 2, 3, 3005 },
  455. { 0, image:getHeight(), 0, 1, 1, 1, 0, 0, 2, 4, 4005 },
  456. }, 'fan')
  457. local c1, c2 = mesh2:getVertexAttribute(1, 4)
  458. local c3 = mesh2:getVertexAttribute(1, 5)
  459. test:assertEquals(2, c1, 'check custom attribute val 1')
  460. test:assertEquals(1, c2, 'check custom attribute val 2')
  461. test:assertEquals(1005, c3, 'check custom attribute val 3')
  462. -- check attaching custom attribute + detaching
  463. mesh1:attachAttribute('CustomValue1', mesh2)
  464. test:assertTrue(mesh1:isAttributeEnabled('CustomValue1'), 'check custom attribute attached')
  465. mesh1:detachAttribute('CustomValue1')
  466. local obj, err = pcall(mesh1.isAttributeEnabled, mesh1, 'CustomValue1')
  467. test:assertNotEquals(nil, err, 'check attribute detached')
  468. mesh1:detachAttribute('VertexPosition')
  469. test:assertTrue(mesh1:isAttributeEnabled('VertexPosition'), 'check cant detach def attribute')
  470. end
  471. -- ParticleSystem (love.graphics.newParticleSystem)
  472. love.test.graphics.ParticleSystem = function(test)
  473. -- create new system
  474. local image = love.graphics.newImage('resources/pixel.png')
  475. local quad1 = love.graphics.newQuad(0, 0, 1, 1, image)
  476. local quad2 = love.graphics.newQuad(0, 0, 1, 1, image)
  477. local psystem = love.graphics.newParticleSystem(image, 1000)
  478. test:assertObject(psystem)
  479. -- check psystem state properties
  480. psystem:start()
  481. psystem:update(1)
  482. test:assertTrue(psystem:isActive(), 'check active')
  483. test:assertFalse(psystem:isPaused(), 'checked not paused by def')
  484. test:assertFalse(psystem:hasRelativeRotation(), 'check rel rot def')
  485. psystem:pause()
  486. test:assertTrue(psystem:isPaused(), 'check now paused')
  487. test:assertFalse(psystem:isStopped(), 'check not stopped by def')
  488. psystem:stop()
  489. test:assertTrue(psystem:isStopped(), 'check now stopped')
  490. psystem:start()
  491. psystem:reset()
  492. -- check emitting some particles
  493. -- need to set a lifespan at minimum or none will be counted
  494. local min, max = psystem:getParticleLifetime()
  495. test:assertEquals(0, min, 'check def lifetime min')
  496. test:assertEquals(0, max, 'check def lifetime max')
  497. psystem:setParticleLifetime(1, 2)
  498. psystem:emit(10)
  499. psystem:update(1)
  500. test:assertEquals(10, psystem:getCount(), 'check added particles')
  501. psystem:reset()
  502. test:assertEquals(0, psystem:getCount(), 'check reset')
  503. -- check setting colors
  504. local colors1 = {psystem:getColors()}
  505. test:assertEquals(1, #colors1, 'check 1 color by def')
  506. psystem:setColors(1, 1, 1, 1, 1, 0, 0, 1)
  507. local colors2 = {psystem:getColors()}
  508. test:assertEquals(2, #colors2, 'check set colors')
  509. test:assertEquals(1, colors2[2][1], 'check set color')
  510. -- check setting direction
  511. test:assertEquals(0, psystem:getDirection(), 'check def direction')
  512. psystem:setDirection(90 * (math.pi/180))
  513. test:assertEquals(math.floor(math.pi/2*100), math.floor(psystem:getDirection()*100), 'check set direction')
  514. -- check emission area options
  515. psystem:setEmissionArea('normal', 100, 50)
  516. psystem:setEmissionArea('ellipse', 100, 50)
  517. psystem:setEmissionArea('borderellipse', 100, 50)
  518. psystem:setEmissionArea('borderrectangle', 100, 50)
  519. psystem:setEmissionArea('none', 100, 50)
  520. psystem:setEmissionArea('uniform', 100, 50)
  521. local dist, dx, dy, angle, rel = psystem:getEmissionArea()
  522. test:assertEquals('uniform', dist, 'check emission area dist')
  523. test:assertEquals(100, dx, 'check emission area dx')
  524. test:assertEquals(50, dy, 'check emission area dy')
  525. test:assertEquals(0, angle, 'check emission area angle')
  526. test:assertFalse(rel, 'check emission area rel')
  527. -- check emission rate
  528. test:assertEquals(0, psystem:getEmissionRate(), 'check def emission rate')
  529. psystem:setEmissionRate(1)
  530. test:assertEquals(1, psystem:getEmissionRate(), 'check changed emission rate')
  531. -- check emission lifetime
  532. test:assertEquals(-1, psystem:getEmitterLifetime(), 'check def emitter life')
  533. psystem:setEmitterLifetime(10)
  534. test:assertEquals(10, psystem:getEmitterLifetime(), 'check changed emitter life')
  535. -- check insert mode
  536. test:assertEquals('top', psystem:getInsertMode(), 'check def insert mode')
  537. psystem:setInsertMode('bottom')
  538. psystem:setInsertMode('random')
  539. test:assertEquals('random', psystem:getInsertMode(), 'check change insert mode')
  540. -- check linear acceleration
  541. local xmin1, ymin1, xmax1, ymax1 = psystem:getLinearAcceleration()
  542. test:assertEquals(0, xmin1, 'check def lin acceleration xmin')
  543. test:assertEquals(0, ymin1, 'check def lin acceleration ymin')
  544. test:assertEquals(0, xmax1, 'check def lin acceleration xmax')
  545. test:assertEquals(0, ymax1, 'check def lin acceleration ymax')
  546. psystem:setLinearAcceleration(1, 2, 3, 4)
  547. local xmin2, ymin2, xmax2, ymax2 = psystem:getLinearAcceleration()
  548. test:assertEquals(1, xmin2, 'check change lin acceleration xmin')
  549. test:assertEquals(2, ymin2, 'check change lin acceleration ymin')
  550. test:assertEquals(3, xmax2, 'check change lin acceleration xmax')
  551. test:assertEquals(4, ymax2, 'check change lin acceleration ymax')
  552. -- check linear damping
  553. local min3, max3 = psystem:getLinearDamping()
  554. test:assertEquals(0, min3, 'check def lin damping min')
  555. test:assertEquals(0, max3, 'check def lin damping max')
  556. psystem:setLinearDamping(1, 2)
  557. local min4, max4 = psystem:getLinearDamping()
  558. test:assertEquals(1, min4, 'check change lin damping min')
  559. test:assertEquals(2, max4, 'check change lin damping max')
  560. -- check offset
  561. local ox1, oy1 = psystem:getOffset()
  562. test:assertEquals(0.5, ox1, 'check def offset x') -- 0.5 cos middle of pixel image which is 1x1
  563. test:assertEquals(0.5, oy1, 'check def offset y')
  564. psystem:setOffset(0, 10)
  565. local ox2, oy2 = psystem:getOffset()
  566. test:assertEquals(0, ox2, 'check change offset x')
  567. test:assertEquals(10, oy2, 'check change offset y')
  568. -- check lifetime (we set it earlier)
  569. local min5, max5 = psystem:getParticleLifetime()
  570. test:assertEquals(1, min5, 'check p lifetime min')
  571. test:assertEquals(2, max5, 'check p lifetime max')
  572. -- check position
  573. local x1, y1 = psystem:getPosition()
  574. test:assertEquals(0, x1, 'check emitter x')
  575. test:assertEquals(0, y1, 'check emitter y')
  576. psystem:setPosition(10, 12)
  577. local x2, y2 = psystem:getPosition()
  578. test:assertEquals(10, x2, 'check set emitter x')
  579. test:assertEquals(12, y2, 'check set emitter y')
  580. -- check quads
  581. test:assertEquals(0, #psystem:getQuads(), 'check def quads')
  582. psystem:setQuads({quad1})
  583. psystem:setQuads(quad1, quad2)
  584. test:assertEquals(2, #psystem:getQuads(), 'check set quads')
  585. -- check radial acceleration
  586. local min6, max6 = psystem:getRadialAcceleration()
  587. test:assertEquals(0, min6, 'check def rad accel min')
  588. test:assertEquals(0, max6, 'check def rad accel max')
  589. psystem:setRadialAcceleration(1, 2)
  590. local min7, max7 = psystem:getRadialAcceleration()
  591. test:assertEquals(1, min7, 'check change rad accel min')
  592. test:assertEquals(2, max7, 'check change rad accel max')
  593. -- check rotation
  594. local min8, max8 = psystem:getRotation()
  595. test:assertEquals(0, min8, 'check def rot min')
  596. test:assertEquals(0, max8, 'check def rot max')
  597. psystem:setRotation(90 * (math.pi/180), 180 * (math.pi/180))
  598. local min8, max8 = psystem:getRotation()
  599. test:assertEquals(math.floor(math.pi/2*100), math.floor(min8*100), 'check set rot min')
  600. test:assertEquals(math.floor(math.pi*100), math.floor(max8*100), 'check set rot max')
  601. -- check variation
  602. test:assertEquals(0, psystem:getSizeVariation(), 'check def variation')
  603. psystem:setSizeVariation(1)
  604. test:assertEquals(1, psystem:getSizeVariation(), 'check change variation')
  605. -- check sizes
  606. test:assertEquals(1, #{psystem:getSizes()}, 'check def size')
  607. psystem:setSizes(1, 2, 4, 1, 3, 2)
  608. local sizes = {psystem:getSizes()}
  609. test:assertEquals(6, #sizes, 'check set sizes')
  610. test:assertEquals(3, sizes[5], 'check set size')
  611. -- check speed
  612. local min9, max9 = psystem:getSpeed()
  613. test:assertEquals(0, min9, 'check def speed min')
  614. test:assertEquals(0, max9, 'check def speed max')
  615. psystem:setSpeed(1, 10)
  616. local min10, max10 = psystem:getSpeed()
  617. test:assertEquals(1, min10, 'check change speed min')
  618. test:assertEquals(10, max10, 'check change speed max')
  619. -- check variation + spin
  620. local variation = psystem:getSpinVariation()
  621. test:assertEquals(0, variation, 'check def spin variation')
  622. psystem:setSpinVariation(1)
  623. test:assertEquals(1, psystem:getSpinVariation(), 'check change spin variation')
  624. psystem:setSpin(1, 2)
  625. local min11, max11 = psystem:getSpin()
  626. test:assertEquals(1, min11, 'check change spin min')
  627. test:assertEquals(2, max11, 'check change spin max')
  628. -- check spread
  629. test:assertEquals(0, psystem:getSpread(), 'check def spread')
  630. psystem:setSpread(90 * (math.pi/180))
  631. test:assertEquals(math.floor(math.pi/2*100), math.floor(psystem:getSpread()*100), 'check change spread')
  632. -- tangential acceleration
  633. local min12, max12 = psystem:getTangentialAcceleration()
  634. test:assertEquals(0, min12, 'check def tan accel min')
  635. test:assertEquals(0, max12, 'check def tan accel max')
  636. psystem:setTangentialAcceleration(1, 2)
  637. local min13, max13 = psystem:getTangentialAcceleration()
  638. test:assertEquals(1, min13, 'check change tan accel min')
  639. test:assertEquals(2, max13, 'check change tan accel max')
  640. -- check texture
  641. test:assertNotEquals(nil, psystem:getTexture(), 'check texture obj')
  642. test:assertObject(psystem:getTexture())
  643. psystem:setTexture(love.graphics.newImage('resources/love.png'))
  644. test:assertObject(psystem:getTexture())
  645. -- try a graphics test!
  646. -- hard to get exactly because of the variation but we can use some pixel
  647. -- tolerance and volume to try and cover the randomness
  648. local psystem2 = love.graphics.newParticleSystem(image, 5000)
  649. psystem2:setEmissionArea('uniform', 2, 64)
  650. psystem2:setColors(1, 0, 0, 1)
  651. psystem2:setDirection(0 * math.pi/180)
  652. psystem2:setEmitterLifetime(100)
  653. psystem2:setEmissionRate(5000)
  654. local psystem3 = psystem2:clone()
  655. psystem3:setPosition(64, 0)
  656. psystem3:setColors(0, 1, 0, 1)
  657. psystem3:setDirection(180 * (math.pi/180))
  658. psystem2:start()
  659. psystem3:start()
  660. psystem2:update(1)
  661. psystem3:update(1)
  662. local canvas = love.graphics.newCanvas(64, 64)
  663. love.graphics.setCanvas(canvas)
  664. love.graphics.clear(0, 0, 0, 1)
  665. love.graphics.draw(psystem2, 0, 0)
  666. love.graphics.draw(psystem3, 0, 0)
  667. love.graphics.setCanvas()
  668. -- this should result in a bunch of red pixels on the left 2px of the canvas
  669. -- and a bunch of green pixels on the right 2px of the canvas
  670. local imgdata = love.graphics.readbackTexture(canvas)
  671. test.pixel_tolerance = 1
  672. test:compareImg(imgdata)
  673. end
  674. -- Quad (love.graphics.newQuad)
  675. love.test.graphics.Quad = function(test)
  676. -- create quad obj
  677. local texture = love.graphics.newImage('resources/love.png')
  678. local quad = love.graphics.newQuad(0, 0, 32, 32, texture)
  679. test:assertObject(quad)
  680. -- check properties
  681. test:assertEquals(1, quad:getLayer(), 'check default layer')
  682. quad:setLayer(2)
  683. test:assertEquals(2, quad:getLayer(), 'check changed layer')
  684. local sw, sh = quad:getTextureDimensions()
  685. test:assertEquals(64, sw, 'check texture w')
  686. test:assertEquals(64, sh, 'check texture h')
  687. -- check drawing and viewport changes
  688. local canvas = love.graphics.newCanvas(64, 64)
  689. love.graphics.setCanvas(canvas)
  690. love.graphics.draw(texture, quad, 0, 0)
  691. quad:setViewport(32, 32, 32, 32, 64, 64)
  692. love.graphics.draw(texture, quad, 32, 32)
  693. love.graphics.setCanvas()
  694. local imgdata = love.graphics.readbackTexture(canvas)
  695. test:compareImg(imgdata)
  696. end
  697. -- Shader (love.graphics.newShader)
  698. love.test.graphics.Shader = function(test)
  699. -- check valid shader
  700. local pixelcode1 = [[
  701. uniform Image tex2;
  702. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  703. vec4 texturecolor = Texel(tex2, texture_coords);
  704. return texturecolor * color;
  705. }
  706. ]]
  707. local vertexcode1 = [[
  708. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  709. return transform_projection * vertex_position;
  710. }
  711. ]]
  712. local shader1 = love.graphics.newShader(pixelcode1, vertexcode1, {debugname = 'testshader'})
  713. test:assertObject(shader1)
  714. test:assertEquals('', shader1:getWarnings(), 'check shader valid')
  715. test:assertFalse(shader1:hasUniform('tex1'), 'check invalid uniform')
  716. test:assertTrue(shader1:hasUniform('tex2'), 'check valid uniform')
  717. test:assertEquals('testshader', shader1:getDebugName())
  718. -- check invalid shader
  719. local pixelcode2 = [[
  720. uniform float ww;
  721. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  722. vec4 texturecolor = Texel(tex, texture_coords);
  723. float unused = ww * 3 * color;
  724. return texturecolor * color;
  725. }
  726. ]]
  727. local res, err = pcall(love.graphics.newShader, pixelcode2, vertexcode1)
  728. test:assertNotEquals(nil, err, 'check shader compile fails')
  729. -- check using a shader to draw + sending uniforms
  730. -- shader will return a given color if overwrite set to 1, otherwise def. draw
  731. local pixelcode3 = [[
  732. uniform vec4 col;
  733. uniform float overwrite;
  734. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  735. vec4 texcol = Texel(tex, texture_coords);
  736. if (overwrite == 1.0) {
  737. return col;
  738. } else {
  739. return texcol * color;
  740. }
  741. }
  742. ]]
  743. local shader3 = love.graphics.newShader(pixelcode3, vertexcode1)
  744. local canvas = love.graphics.newCanvas(16, 16)
  745. love.graphics.push("all")
  746. love.graphics.setCanvas(canvas)
  747. -- set color to yellow
  748. love.graphics.setColor(1, 1, 0, 1)
  749. -- turn shader 'on' and use red to draw
  750. shader3:send('overwrite', 1)
  751. shader3:sendColor('col', {1, 0, 0, 1})
  752. love.graphics.setShader(shader3)
  753. love.graphics.rectangle('fill', 0, 0, 8, 8)
  754. love.graphics.setShader()
  755. -- turn shader 'off' and draw again
  756. shader3:send('overwrite', 0)
  757. love.graphics.setShader(shader3)
  758. love.graphics.rectangle('fill', 8, 8, 8, 8)
  759. love.graphics.pop()
  760. local imgdata = love.graphics.readbackTexture(canvas)
  761. test:compareImg(imgdata)
  762. -- test some uncommon paths for shader uniforms
  763. local shader4 = love.graphics.newShader[[
  764. uniform bool booleans[5];
  765. vec4 effect(vec4 vcolor, Image tex, vec2 tc, vec2 pc) {
  766. return booleans[3] ? vec4(0, 1, 0, 0) : vec4(1, 0, 0, 0);
  767. }
  768. ]]
  769. shader4:send("booleans", false, true, true)
  770. local shader5 = love.graphics.newShader[[
  771. uniform sampler2D textures[5];
  772. vec4 effect(vec4 vcolor, Image tex, vec2 tc, vec2 pc) {
  773. return Texel(textures[2], tc) + Texel(textures[3], tc);
  774. }
  775. ]]
  776. local canvas2 = love.graphics.newCanvas(1, 1)
  777. love.graphics.setCanvas(canvas2)
  778. love.graphics.clear(0, 0.5, 0, 1)
  779. love.graphics.setCanvas()
  780. shader5:send("textures", canvas2, canvas2, canvas2, canvas2, canvas2)
  781. local shader6 = love.graphics.newShader[[
  782. struct Data {
  783. bool boolValue;
  784. float floatValue;
  785. sampler2D tex;
  786. };
  787. uniform Data data;
  788. uniform Data dataArray[3];
  789. vec4 effect(vec4 vcolor, Image tex, vec2 tc, vec2 pc) {
  790. return (data.boolValue && dataArray[1].boolValue) ? Texel(dataArray[0].tex, tc) : vec4(0.0, 0.0, 0.0, 0.0);
  791. }
  792. ]]
  793. shader6:send("data.boolValue", true)
  794. shader6:send("dataArray[1].boolValue", true)
  795. shader6:send("dataArray[0].tex", canvas2)
  796. local shader7 = love.graphics.newShader[[
  797. uniform vec3 vec3s[3];
  798. vec4 effect(vec4 vcolor, Image tex, vec2 tc, vec2 pc) {
  799. return vec4(vec3s[1], 1.0);
  800. }
  801. ]]
  802. shader7:send("vec3s", {0, 0, 1}, {0, 1, 0}, {1, 0, 0})
  803. local canvas3 = love.graphics.newCanvas(16, 16)
  804. love.graphics.push("all")
  805. love.graphics.setCanvas(canvas3)
  806. love.graphics.setShader(shader7)
  807. love.graphics.rectangle("fill", 0, 0, 16, 16)
  808. love.graphics.pop()
  809. local imgdata2 = love.graphics.readbackTexture(canvas3)
  810. test:compareImg(imgdata2)
  811. if love.graphics.getSupported().glsl3 then
  812. local shader8 = love.graphics.newShader[[
  813. #pragma language glsl3
  814. #ifdef GL_ES
  815. precision highp float;
  816. #endif
  817. varying vec4 VaryingUnused1;
  818. varying mat3 VaryingMatrix;
  819. flat varying ivec4 VaryingInt;
  820. #ifdef VERTEX
  821. layout(location = 0) in vec4 VertexPosition;
  822. layout(location = 1) in ivec4 IntAttributeUnused;
  823. void vertexmain()
  824. {
  825. VaryingMatrix = mat3(vec3(1, 0, 0), vec3(0, 1, 0), vec3(0, 0, 1));
  826. VaryingInt = ivec4(1, 1, 1, 1);
  827. love_Position = TransformProjectionMatrix * VertexPosition;
  828. }
  829. #endif
  830. #ifdef PIXEL
  831. out ivec4 outData;
  832. void pixelmain()
  833. {
  834. outData = ivec4(VaryingMatrix[1][1] > 0.0 ? 1 : 0, 1, VaryingInt.x, 1);
  835. }
  836. #endif
  837. ]]
  838. local canvas4 = love.graphics.newCanvas(16, 16, {format="rgba8i"})
  839. love.graphics.push("all")
  840. love.graphics.setBlendMode("none")
  841. love.graphics.setCanvas(canvas4)
  842. love.graphics.setShader(shader8)
  843. love.graphics.rectangle("fill", 0, 0, 16, 16)
  844. love.graphics.pop()
  845. local intimagedata = love.graphics.readbackTexture(canvas4)
  846. local imgdata3 = love.image.newImageData(16, 16, "rgba8")
  847. for y=0, 15 do
  848. for x=0, 15 do
  849. local ir, ig, ib, ia = intimagedata:getInt8(4 * (y * 16 + x), 4)
  850. imgdata3:setPixel(x, y, ir, ig, ib, ia)
  851. end
  852. end
  853. test:compareImg(imgdata3)
  854. else
  855. test:assertTrue(true, "skip shader IO test")
  856. end
  857. end
  858. -- SpriteBatch (love.graphics.newSpriteBatch)
  859. love.test.graphics.SpriteBatch = function(test)
  860. -- create batch
  861. local texture1 = love.graphics.newImage('resources/cubemap.png')
  862. local texture2 = love.graphics.newImage('resources/love.png')
  863. local quad1 = love.graphics.newQuad(32, 12, 1, 1, texture2) -- lovepink
  864. local quad2 = love.graphics.newQuad(32, 32, 1, 1, texture2) -- white
  865. local sbatch = love.graphics.newSpriteBatch(texture1, 5000)
  866. test:assertObject(sbatch)
  867. -- check initial count
  868. test:assertEquals(0, sbatch:getCount(), 'check batch size')
  869. -- check buffer size
  870. test:assertEquals(5000, sbatch:getBufferSize(), 'check batch size')
  871. -- check height/width/texture
  872. test:assertEquals(texture1:getWidth(), sbatch:getTexture():getWidth(), 'check texture match w')
  873. test:assertEquals(texture1:getHeight(), sbatch:getTexture():getHeight(), 'check texture match h')
  874. sbatch:setTexture(texture2)
  875. test:assertEquals(texture2:getWidth(), sbatch:getTexture():getWidth(), 'check texture change w')
  876. test:assertEquals(texture2:getHeight(), sbatch:getTexture():getHeight(), 'check texture change h')
  877. -- check colors
  878. local r1, g1, b1, a1 = sbatch:getColor()
  879. test:assertEquals(1, r1, 'check initial color r')
  880. test:assertEquals(1, g1, 'check initial color g')
  881. test:assertEquals(1, b1, 'check initial color b')
  882. test:assertEquals(1, a1, 'check initial color a')
  883. sbatch:setColor(1, 0, 0, 1)
  884. local r2, g2, b2, a2 = sbatch:getColor()
  885. test:assertEquals(1, r2, 'check set color r')
  886. test:assertEquals(0, g2, 'check set color g')
  887. test:assertEquals(0, b2, 'check set color b')
  888. test:assertEquals(1, a2, 'check set color a')
  889. -- check adding sprites
  890. local offset_x = 0
  891. local offset_y = 0
  892. local color = 'white'
  893. sbatch:setColor(1, 1, 1, 1)
  894. local sprites = {}
  895. for s=1,4096 do
  896. local spr = sbatch:add(quad1, offset_x, offset_y, 0, 1, 1)
  897. table.insert(sprites, {spr, offset_x, offset_y})
  898. offset_x = offset_x + 1
  899. if s % 64 == 0 then
  900. -- alternate row colors
  901. if color == 'white' then
  902. color = 'red'
  903. sbatch:setColor(1, 0, 0, 1)
  904. else
  905. color = 'white'
  906. sbatch:setColor(1, 1, 1, 1)
  907. end
  908. offset_y = offset_y + 1
  909. offset_x = 0
  910. end
  911. end
  912. test:assertEquals(4096, sbatch:getCount())
  913. -- test drawing and setting
  914. local canvas = love.graphics.newCanvas(64, 64)
  915. love.graphics.setCanvas(canvas)
  916. love.graphics.clear(0, 0, 0, 1)
  917. love.graphics.draw(sbatch, 0, 0)
  918. love.graphics.setCanvas()
  919. local imgdata1 = love.graphics.readbackTexture(canvas)
  920. test:compareImg(imgdata1)
  921. -- use set to change some sprites
  922. for s=1,2048 do
  923. sbatch:set(sprites[s][1], quad2, sprites[s][2], sprites[s][3]+1, 0, 1, 1)
  924. end
  925. love.graphics.setCanvas(canvas)
  926. love.graphics.clear(0, 0, 0, 1)
  927. love.graphics.draw(sbatch, 0, 0)
  928. love.graphics.setCanvas()
  929. local imgdata2 = love.graphics.readbackTexture(canvas)
  930. test:compareImg(imgdata2)
  931. -- set drawRange and redraw
  932. sbatch:setDrawRange(1025, 2048)
  933. love.graphics.setCanvas(canvas)
  934. love.graphics.clear(0, 0, 0, 1)
  935. love.graphics.draw(sbatch, 0, 0)
  936. love.graphics.setCanvas()
  937. local imgdata3 = love.graphics.readbackTexture(canvas)
  938. test:compareImg(imgdata3)
  939. -- clear and redraw
  940. sbatch:clear()
  941. love.graphics.setCanvas(canvas)
  942. love.graphics.clear(0, 0, 0, 1)
  943. love.graphics.draw(sbatch, 0, 0)
  944. love.graphics.setCanvas()
  945. local imgdata4 = love.graphics.readbackTexture(canvas)
  946. test:compareImg(imgdata4)
  947. -- array texture sbatch
  948. local texture3 = love.graphics.newArrayImage({
  949. 'resources/love.png',
  950. 'resources/loveinv.png'
  951. })
  952. local asbatch = love.graphics.newSpriteBatch(texture3, 4096)
  953. local quad3 = love.graphics.newQuad(32, 52, 1, 1, texture3) -- loveblue
  954. sprites = {}
  955. for s=1,4096 do
  956. local spr = asbatch:addLayer(1, quad3, 0, s, math.floor(s/64), 1, 1)
  957. table.insert(sprites, {spr, s, math.floor(s/64)})
  958. end
  959. test:assertEquals(4096, asbatch:getCount(), 'check max batch size applies')
  960. for s=1,2048 do
  961. asbatch:setLayer(sprites[s][1], 2, sprites[s][2], sprites[s][3], 0, 1, 1)
  962. end
  963. love.graphics.setCanvas(canvas)
  964. love.graphics.clear(0, 0, 0, 1)
  965. love.graphics.draw(asbatch, 0, 0)
  966. love.graphics.setCanvas()
  967. local imgdata5 = love.graphics.readbackTexture(canvas)
  968. test:compareImg(imgdata5)
  969. end
  970. -- Text (love.graphics.newTextBatch)
  971. love.test.graphics.Text = function(test)
  972. -- setup text object
  973. local font = love.graphics.newFont('resources/font.ttf', 8)
  974. local plaintext = love.graphics.newTextBatch(font, 'test')
  975. test:assertObject(plaintext)
  976. -- check height/width/dimensions
  977. test:assertEquals(font:getHeight(), plaintext:getFont():getHeight(), 'check font matches')
  978. local tw, th = plaintext:getDimensions()
  979. test:assertEquals(24, tw, 'check initial dim w')
  980. test:assertEquals(8, th, 'check initial dim h')
  981. test:assertEquals(tw, plaintext:getWidth(), 'check initial dim w')
  982. test:assertEquals(th, plaintext:getHeight(), 'check initial dim h')
  983. -- check changing text effects dimensions
  984. plaintext:add('more text', 100, 0, 0)
  985. test:assertEquals(49, plaintext:getDimensions(), 'check adding text')
  986. plaintext:set('test')
  987. test:assertEquals(24, plaintext:getDimensions(), 'check resetting text')
  988. plaintext:clear()
  989. test:assertEquals(0, plaintext:getDimensions(), 'check clearing text')
  990. -- check drawing + setting more complex text
  991. local colortext = love.graphics.newTextBatch(font, {{1, 0, 0, 1}, 'test'})
  992. test:assertObject(colortext)
  993. colortext:setf('LÖVE is an *awesome* framework you can use to make 2D games in Lua', 60, 'right')
  994. colortext:addf({{1, 1, 0}, 'overlap'}, 1000, 'left')
  995. local font2 = love.graphics.newFont('resources/font.ttf', 8)
  996. colortext:setFont(font2)
  997. local canvas = love.graphics.newCanvas(64, 64)
  998. love.graphics.setCanvas(canvas)
  999. love.graphics.draw(colortext, 0, 10)
  1000. love.graphics.setCanvas()
  1001. local imgdata = love.graphics.readbackTexture(canvas)
  1002. test:compareImg(imgdata)
  1003. end
  1004. -- Video (love.graphics.newVideo)
  1005. love.test.graphics.Video = function(test)
  1006. -- create video obj
  1007. local video = love.graphics.newVideo('resources/sample.ogv')
  1008. test:assertObject(video)
  1009. -- check dimensions
  1010. local w, h = video:getDimensions()
  1011. test:assertEquals(496, w, 'check vid dim w')
  1012. test:assertEquals(502, h, 'check vid dim h')
  1013. test:assertEquals(w, video:getWidth(), 'check vid width match')
  1014. test:assertEquals(h, video:getHeight(), 'check vid height match')
  1015. -- check filters
  1016. local min1, mag1, ani1 = video:getFilter()
  1017. test:assertEquals('nearest', min1, 'check def filter min')
  1018. test:assertEquals('nearest', mag1, 'check def filter mag')
  1019. test:assertEquals(1, ani1, 'check def filter ani')
  1020. video:setFilter('linear', 'linear', 2)
  1021. local min2, mag2, ani2 = video:getFilter()
  1022. test:assertEquals('linear', min2, 'check changed filter min')
  1023. test:assertEquals('linear', mag2, 'check changed filter mag')
  1024. test:assertEquals(2, ani2, 'check changed filter ani')
  1025. -- check video playing
  1026. test:assertFalse(video:isPlaying(), 'check paused by default')
  1027. test:assertEquals(0, video:tell(), 'check 0:00 by default')
  1028. -- covered by their own obj tests in video but check returns obj
  1029. local source = video:getSource()
  1030. test:assertObject(source)
  1031. local stream = video:getStream()
  1032. test:assertObject(stream)
  1033. -- check playing / pausing / seeking states
  1034. video:play()
  1035. test:waitSeconds(0.25)
  1036. video:pause()
  1037. -- runners can be a bit funny and just not play anything sometimes
  1038. if not GITHUB_RUNNER then
  1039. test:assertRange(video:tell(), 0.2, 0.35, 'check video playing for 0.25s')
  1040. end
  1041. video:seek(0.2)
  1042. test:assertEquals(0.2, video:tell(), 'check video seeking')
  1043. video:rewind()
  1044. test:assertEquals(0, video:tell(), 'check video rewind')
  1045. video:setFilter('nearest', 'nearest', 1)
  1046. -- check actuall drawing with the vid
  1047. local canvas = love.graphics.newCanvas(500, 500)
  1048. love.graphics.setCanvas(canvas)
  1049. love.graphics.clear(1, 0, 0, 1)
  1050. love.graphics.draw(video, 0, 0)
  1051. love.graphics.setCanvas()
  1052. local imgdata = love.graphics.readbackTexture(canvas)
  1053. test:compareImg(imgdata)
  1054. end
  1055. --------------------------------------------------------------------------------
  1056. --------------------------------------------------------------------------------
  1057. ------------------------------------DRAWING-------------------------------------
  1058. --------------------------------------------------------------------------------
  1059. --------------------------------------------------------------------------------
  1060. -- love.graphics.arc
  1061. love.test.graphics.arc = function(test)
  1062. -- draw some arcs using pi format
  1063. local canvas = love.graphics.newCanvas(32, 32)
  1064. love.graphics.setCanvas(canvas)
  1065. love.graphics.clear(0, 0, 0, 1)
  1066. love.graphics.setColor(1, 1, 1, 1)
  1067. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 360 * (math.pi/180), 10)
  1068. love.graphics.arc('fill', "pie", 16, 16, 16, 270 * (math.pi/180), 45 * (math.pi/180), 10)
  1069. love.graphics.setColor(1, 0, 0, 1)
  1070. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 90 * (math.pi/180), 10)
  1071. love.graphics.setColor(1, 1, 0, 1)
  1072. love.graphics.arc('line', "pie", 16, 16, 16, 180 * (math.pi/180), 135 * (math.pi/180), 10)
  1073. love.graphics.setColor(1, 1, 1, 1)
  1074. love.graphics.setCanvas()
  1075. local imgdata1 = love.graphics.readbackTexture(canvas)
  1076. -- draw some arcs with open format
  1077. love.graphics.setCanvas(canvas)
  1078. love.graphics.clear(0, 0, 0, 1)
  1079. love.graphics.setColor(1, 1, 1, 1)
  1080. love.graphics.arc('line', "open", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  1081. love.graphics.setColor(1, 0, 0, 1)
  1082. love.graphics.arc('fill', "open", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  1083. love.graphics.setColor(1, 1, 0, 1)
  1084. love.graphics.arc('fill', "open", 16, 16, 16, 180 * (math.pi/180), 270 * (math.pi/180), 10)
  1085. love.graphics.setColor(1, 1, 1, 1)
  1086. love.graphics.setCanvas()
  1087. local imgdata2 = love.graphics.readbackTexture(canvas)
  1088. -- draw some arcs with closed format
  1089. love.graphics.setCanvas(canvas)
  1090. love.graphics.clear(0, 0, 0, 1)
  1091. love.graphics.setColor(1, 1, 1, 1)
  1092. love.graphics.arc('line', "closed", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  1093. love.graphics.setColor(1, 0, 0, 1)
  1094. love.graphics.arc('fill', "closed", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  1095. love.graphics.setColor(1, 1, 0, 1)
  1096. love.graphics.arc('line', "closed", 16, 16, 16, 180 * (math.pi/180), 90 * (math.pi/180), 10)
  1097. love.graphics.setColor(1, 1, 1, 1)
  1098. love.graphics.setCanvas()
  1099. local imgdata3 = love.graphics.readbackTexture(canvas)
  1100. if GITHUB_RUNNER and test:isOS('OS X') then
  1101. -- on macosx runners, the arcs are not drawn as accurately at low res
  1102. -- there's a couple pixels different in the curve of the arc but as we
  1103. -- are at such a low resolution I think that can be expected
  1104. -- on real hardware the test passes fine though
  1105. test:assertTrue(true, 'skip test')
  1106. else
  1107. test:compareImg(imgdata1)
  1108. test:compareImg(imgdata2)
  1109. test:compareImg(imgdata3)
  1110. end
  1111. end
  1112. -- love.graphics.circle
  1113. love.test.graphics.circle = function(test)
  1114. -- draw some circles
  1115. local canvas = love.graphics.newCanvas(32, 32)
  1116. love.graphics.setCanvas(canvas)
  1117. love.graphics.clear(0, 0, 0, 1)
  1118. love.graphics.setColor(1, 1, 1, 1)
  1119. love.graphics.circle('fill', 16, 16, 16)
  1120. love.graphics.setColor(1, 0, 0, 1)
  1121. love.graphics.circle('line', 16, 16, 16)
  1122. love.graphics.setColor(1, 1, 0, 1)
  1123. love.graphics.circle('fill', 16, 16, 8)
  1124. love.graphics.setColor(0, 1, 0, 1)
  1125. love.graphics.circle('fill', 16, 16, 4)
  1126. love.graphics.setColor(1, 1, 1, 1)
  1127. love.graphics.setCanvas()
  1128. local imgdata = love.graphics.readbackTexture(canvas)
  1129. test:compareImg(imgdata)
  1130. end
  1131. -- love.graphics.clear
  1132. love.test.graphics.clear = function(test)
  1133. local canvas = love.graphics.newCanvas(16, 16)
  1134. love.graphics.setCanvas(canvas)
  1135. love.graphics.clear(0, 0, 0, 1)
  1136. love.graphics.clear(1, 1, 0, 1)
  1137. love.graphics.setCanvas()
  1138. local imgdata = love.graphics.readbackTexture(canvas)
  1139. test:compareImg(imgdata)
  1140. end
  1141. -- love.graphics.discard
  1142. love.test.graphics.discard = function(test)
  1143. -- from the docs: "on some desktops this may do nothing"
  1144. test:skipTest('cant test this worked')
  1145. end
  1146. -- love.graphics.draw
  1147. love.test.graphics.draw = function(test)
  1148. local canvas1 = love.graphics.newCanvas(32, 32)
  1149. local canvas2 = love.graphics.newCanvas(32, 32)
  1150. local transform = love.math.newTransform( )
  1151. transform:translate(16, 0)
  1152. transform:scale(0.5, 0.5)
  1153. love.graphics.setCanvas(canvas1)
  1154. love.graphics.clear(0, 0, 0, 1)
  1155. -- img, offset
  1156. love.graphics.draw(Logo.texture, Logo.img, 0, 0, 0, 1, 1, 16, 16)
  1157. love.graphics.setCanvas()
  1158. love.graphics.setCanvas(canvas2)
  1159. love.graphics.clear(1, 0, 0, 1)
  1160. -- canvas, scale, shear, transform obj
  1161. love.graphics.draw(canvas1, 0, 0, 0, 1, 1, 0, 0, 2, 2)
  1162. love.graphics.draw(canvas1, 0, 16, 0, 0.5, 0.5)
  1163. love.graphics.draw(canvas1, 16, 16, 0, 0.5, 0.5)
  1164. love.graphics.draw(canvas1, transform)
  1165. love.graphics.setCanvas()
  1166. local imgdata = love.graphics.readbackTexture(canvas2)
  1167. test:compareImg(imgdata)
  1168. end
  1169. -- love.graphics.drawInstanced
  1170. love.test.graphics.drawInstanced = function(test)
  1171. local image = love.graphics.newImage('resources/love.png')
  1172. local vertices = {
  1173. { 0, 0, 0, 0, 1, 0, 0 },
  1174. { image:getWidth(), 0, 1, 0, 0, 1, 0 },
  1175. { image:getWidth(), image:getHeight(), 1, 1, 0, 0, 1 },
  1176. { 0, image:getHeight(), 0, 1, 1, 1, 0 },
  1177. }
  1178. local mesh = love.graphics.newMesh(vertices, 'fan')
  1179. local canvas = love.graphics.newCanvas(64, 64)
  1180. love.graphics.setCanvas(canvas)
  1181. love.graphics.clear(0, 0, 0, 1)
  1182. love.graphics.drawInstanced(mesh, 1000, 0, 0, 0, 1, 1)
  1183. love.graphics.setCanvas()
  1184. local imgdata = love.graphics.readbackTexture(canvas)
  1185. -- need 1 tolerance here just cos of the amount of colors
  1186. test.rgba_tolerance = 1
  1187. test:compareImg(imgdata)
  1188. end
  1189. -- love.graphics.drawLayer
  1190. love.test.graphics.drawLayer = function(test)
  1191. local image = love.graphics.newArrayImage({
  1192. 'resources/love.png', 'resources/loveinv.png',
  1193. 'resources/love.png', 'resources/loveinv.png'
  1194. })
  1195. local canvas = love.graphics.newCanvas(64, 64)
  1196. love.graphics.setCanvas(canvas)
  1197. love.graphics.clear(0, 0, 0, 1)
  1198. love.graphics.drawLayer(image, 1, 0, 0, 0, 1, 1)
  1199. love.graphics.drawLayer(image, 2, 32, 0, 0, 0.5, 0.5)
  1200. love.graphics.drawLayer(image, 4, 0, 32, 0, 0.5, 0.5)
  1201. love.graphics.drawLayer(image, 3, 32, 32, 0, 2, 2, 16, 16)
  1202. love.graphics.setCanvas()
  1203. local imgdata = love.graphics.readbackTexture(canvas)
  1204. test:compareImg(imgdata)
  1205. end
  1206. -- love.graphics.ellipse
  1207. love.test.graphics.ellipse = function(test)
  1208. local canvas = love.graphics.newCanvas(32, 32)
  1209. love.graphics.setCanvas(canvas)
  1210. love.graphics.clear(0, 0, 0, 1)
  1211. love.graphics.setColor(1, 0, 0, 1)
  1212. love.graphics.ellipse('fill', 16, 16, 16, 8)
  1213. love.graphics.setColor(1, 1, 0, 1)
  1214. love.graphics.ellipse('fill', 24, 24, 10, 24)
  1215. love.graphics.setColor(1, 0, 1, 1)
  1216. love.graphics.ellipse('fill', 16, 0, 8, 16)
  1217. love.graphics.setColor(1, 1, 1, 1)
  1218. love.graphics.setCanvas()
  1219. local imgdata = love.graphics.readbackTexture(canvas)
  1220. test:compareImg(imgdata)
  1221. end
  1222. -- love.graphics.flushBatch
  1223. love.test.graphics.flushBatch = function(test)
  1224. love.graphics.flushBatch()
  1225. local initial = love.graphics.getStats()['drawcalls']
  1226. local canvas = love.graphics.newCanvas(32, 32)
  1227. love.graphics.setCanvas(canvas)
  1228. love.graphics.clear(0, 0, 0, 1)
  1229. love.graphics.rectangle('fill', 0, 0, 32, 32)
  1230. love.graphics.setColor(1, 1, 1, 1)
  1231. love.graphics.setCanvas()
  1232. love.graphics.flushBatch()
  1233. local after = love.graphics.getStats()['drawcalls']
  1234. test:assertEquals(initial+1, after, 'check drawcalls increased')
  1235. end
  1236. -- love.graphics.line
  1237. love.test.graphics.line = function(test)
  1238. local canvas = love.graphics.newCanvas(16, 16)
  1239. love.graphics.setCanvas(canvas)
  1240. love.graphics.clear(0, 0, 0, 1)
  1241. love.graphics.setColor(1, 0, 0, 1)
  1242. love.graphics.line(1,1,16,1,16,16,1,16,1,1)
  1243. love.graphics.setColor(1, 1, 0, 1)
  1244. love.graphics.line({0,0,8,8,16,0,8,8,16,16,8,8,0,16})
  1245. love.graphics.setColor(1, 1, 1, 1)
  1246. love.graphics.setCanvas()
  1247. local imgdata = love.graphics.readbackTexture(canvas)
  1248. test:compareImg(imgdata)
  1249. end
  1250. -- love.graphics.points
  1251. love.test.graphics.points = function(test)
  1252. local canvas = love.graphics.newCanvas(16, 16)
  1253. love.graphics.push("all")
  1254. love.graphics.setCanvas(canvas)
  1255. love.graphics.clear(0, 0, 0, 1)
  1256. love.graphics.translate(0.5, 0.5) -- draw points at the center of pixels
  1257. love.graphics.setColor(1, 0, 0, 1)
  1258. love.graphics.points(0,0,15,0,15,15,0,15,0,0)
  1259. love.graphics.setColor(1, 1, 0, 1)
  1260. love.graphics.points({1,1,7,7,14,1,7,8,14,14,8,8,1,14,8,7})
  1261. love.graphics.pop()
  1262. local imgdata = love.graphics.readbackTexture(canvas)
  1263. test:compareImg(imgdata)
  1264. end
  1265. -- love.graphics.polygon
  1266. love.test.graphics.polygon = function(test)
  1267. local canvas = love.graphics.newCanvas(16, 16)
  1268. love.graphics.setCanvas(canvas)
  1269. love.graphics.clear(0, 0, 0, 1)
  1270. love.graphics.setColor(1, 0, 0, 1)
  1271. love.graphics.polygon("fill", 1, 1, 4, 5, 8, 10, 16, 2, 7, 3, 5, 16, 16, 16, 1, 8)
  1272. love.graphics.setColor(1, 1, 0, 1)
  1273. love.graphics.polygon("line", {2, 2, 4, 5, 3, 7, 8, 15, 12, 4, 5, 10})
  1274. love.graphics.setColor(1, 1, 1, 1)
  1275. love.graphics.setCanvas()
  1276. local imgdata = love.graphics.readbackTexture(canvas)
  1277. test:compareImg(imgdata)
  1278. end
  1279. -- love.graphics.print
  1280. love.test.graphics.print = function(test)
  1281. love.graphics.setFont(Font)
  1282. local canvas = love.graphics.newCanvas(16, 16)
  1283. love.graphics.setCanvas(canvas)
  1284. love.graphics.clear(0, 0, 0, 1)
  1285. love.graphics.setColor(1, 0, 0, 1)
  1286. love.graphics.print('love', 0, 3, 0, 1, 1, 0, 0)
  1287. love.graphics.setColor(0, 1, 0, 1)
  1288. love.graphics.print('ooo', 0, 3, 0, 2, 2, 0, 0)
  1289. love.graphics.setColor(0, 0, 1, 1)
  1290. love.graphics.print('hello', 0, 3, 90*(math.pi/180), 1, 1, 0, 8)
  1291. love.graphics.setColor(1, 1, 1, 1)
  1292. love.graphics.setCanvas()
  1293. local imgdata = love.graphics.readbackTexture(canvas)
  1294. test:compareImg(imgdata)
  1295. end
  1296. -- love.graphics.printf
  1297. love.test.graphics.printf = function(test)
  1298. love.graphics.setFont(Font)
  1299. local canvas = love.graphics.newCanvas(32, 32)
  1300. love.graphics.setCanvas(canvas)
  1301. love.graphics.clear(0, 0, 0, 1)
  1302. love.graphics.setColor(1, 0, 0, 1)
  1303. love.graphics.printf('love', 0, 0, 8, "left")
  1304. love.graphics.setColor(0, 1, 0, 1)
  1305. love.graphics.printf('love', 0, 5, 16, "right")
  1306. love.graphics.setColor(0, 0, 1, 1)
  1307. love.graphics.printf('love', 0, 7, 32, "center")
  1308. love.graphics.setColor(1, 1, 1, 1)
  1309. love.graphics.setCanvas()
  1310. local imgdata = love.graphics.readbackTexture(canvas)
  1311. test:compareImg(imgdata)
  1312. end
  1313. -- love.graphics.rectangle
  1314. love.test.graphics.rectangle = function(test)
  1315. -- setup, draw a 16x16 red rectangle with a blue central square
  1316. local canvas = love.graphics.newCanvas(16, 16)
  1317. love.graphics.setCanvas(canvas)
  1318. love.graphics.clear(0, 0, 0, 1)
  1319. love.graphics.setColor(1, 0, 0, 1)
  1320. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1321. love.graphics.setColor(0, 0, 1, 1)
  1322. love.graphics.rectangle('fill', 6, 6, 4, 4)
  1323. love.graphics.setColor(1, 1, 1, 1)
  1324. love.graphics.setCanvas()
  1325. local imgdata1 = love.graphics.readbackTexture(canvas)
  1326. test:compareImg(imgdata1)
  1327. -- clear canvas to do some line testing
  1328. love.graphics.setCanvas(canvas)
  1329. love.graphics.clear(0, 0, 0, 1)
  1330. love.graphics.setColor(1, 0, 0, 1)
  1331. love.graphics.rectangle('line', 1, 1, 15, 15) -- red border
  1332. love.graphics.setColor(0, 0, 1, 1)
  1333. love.graphics.rectangle('line', 1, 1, 2, 15) -- 3x16 left aligned blue outline
  1334. love.graphics.setColor(0, 1, 0, 1)
  1335. love.graphics.rectangle('line', 11, 1, 5, 15) -- 6x16 right aligned green outline
  1336. love.graphics.setColor(1, 1, 1, 1)
  1337. love.graphics.setCanvas()
  1338. local imgdata2 = love.graphics.readbackTexture(canvas)
  1339. test:compareImg(imgdata2)
  1340. end
  1341. --------------------------------------------------------------------------------
  1342. --------------------------------------------------------------------------------
  1343. --------------------------------OBJECT CREATION---------------------------------
  1344. --------------------------------------------------------------------------------
  1345. --------------------------------------------------------------------------------
  1346. -- love.graphics.captureScreenshot
  1347. love.test.graphics.captureScreenshot = function(test)
  1348. love.graphics.captureScreenshot('example-screenshot.png')
  1349. test:waitFrames(1)
  1350. -- need to wait until end of the frame for the screenshot
  1351. test:assertTrue(love.filesystem.exists('example-screenshot.png'))
  1352. love.filesystem.remove('example-screenshot.png')
  1353. -- test callback version
  1354. local cbdata = nil
  1355. local prevtextcommand = TextCommand
  1356. TextCommand = "Capturing screenshot"
  1357. love.graphics.captureScreenshot(function (idata)
  1358. test:assertNotEquals(nil, idata, 'check we have image data')
  1359. cbdata = idata
  1360. end)
  1361. test:waitFrames(1)
  1362. TextCommand = prevtextcommand
  1363. test:assertNotNil(cbdata)
  1364. if test:isOS('iOS', 'Android') then
  1365. -- Mobile operating systems don't let us control the window resolution,
  1366. -- so we can't compare the reference image properly.
  1367. test:assertTrue(true, 'skip test')
  1368. else
  1369. test:compareImg(cbdata)
  1370. end
  1371. end
  1372. -- love.graphics.newArrayImage
  1373. -- @NOTE this is just basic nil checking, objs have their own test method
  1374. love.test.graphics.newArrayImage = function(test)
  1375. test:assertObject(love.graphics.newArrayImage({
  1376. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  1377. }))
  1378. end
  1379. -- love.graphics.newCanvas
  1380. -- @NOTE this is just basic nil checking, objs have their own test method
  1381. love.test.graphics.newCanvas = function(test)
  1382. test:assertObject(love.graphics.newCanvas(16, 16, {
  1383. type = '2d',
  1384. format = 'normal',
  1385. readable = true,
  1386. msaa = 0,
  1387. dpiscale = 1,
  1388. mipmaps = 'none'
  1389. }))
  1390. test:assertObject(love.graphics.newCanvas(1000, 1000))
  1391. end
  1392. -- love.graphics.newCubeImage
  1393. -- @NOTE this is just basic nil checking, objs have their own test method
  1394. love.test.graphics.newCubeImage = function(test)
  1395. test:assertObject(love.graphics.newCubeImage('resources/cubemap.png', {
  1396. mipmaps = false,
  1397. linear = false
  1398. }))
  1399. end
  1400. -- love.graphics.newFont
  1401. -- @NOTE this is just basic nil checking, objs have their own test method
  1402. love.test.graphics.newFont = function(test)
  1403. test:assertObject(love.graphics.newFont('resources/font.ttf'))
  1404. test:assertObject(love.graphics.newFont('resources/font.ttf', 8, "normal", 1))
  1405. end
  1406. -- love.graphics.newImage
  1407. -- @NOTE this is just basic nil checking, objs have their own test method
  1408. love.test.graphics.newImage = function(test)
  1409. test:assertObject(love.graphics.newImage('resources/love.png', {
  1410. mipmaps = false,
  1411. linear = false,
  1412. dpiscale = 1
  1413. }))
  1414. end
  1415. -- love.graphics.newImageFont
  1416. -- @NOTE this is just basic nil checking, objs have their own test method
  1417. love.test.graphics.newImageFont = function(test)
  1418. test:assertObject(love.graphics.newImageFont('resources/love.png', 'ABCD', 1))
  1419. end
  1420. -- love.graphics.newMesh
  1421. -- @NOTE this is just basic nil checking, objs have their own test method
  1422. love.test.graphics.newMesh = function(test)
  1423. test:assertObject(love.graphics.newMesh({{1, 1, 0, 0, 1, 1, 1, 1}}, 'fan', 'dynamic'))
  1424. end
  1425. -- love.graphics.newParticleSystem
  1426. -- @NOTE this is just basic nil checking, objs have their own test method
  1427. love.test.graphics.newParticleSystem = function(test)
  1428. local imgdata = love.graphics.newImage('resources/love.png')
  1429. test:assertObject(love.graphics.newParticleSystem(imgdata, 1000))
  1430. end
  1431. -- love.graphics.newQuad
  1432. -- @NOTE this is just basic nil checking, objs have their own test method
  1433. love.test.graphics.newQuad = function(test)
  1434. local imgdata = love.graphics.newImage('resources/love.png')
  1435. test:assertObject(love.graphics.newQuad(0, 0, 16, 16, imgdata))
  1436. end
  1437. -- love.graphics.newShader
  1438. -- @NOTE this is just basic nil checking, objs have their own test method
  1439. love.test.graphics.newShader = function(test)
  1440. local pixelcode = [[
  1441. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  1442. vec4 texturecolor = Texel(tex, texture_coords);
  1443. return texturecolor * color;
  1444. }
  1445. ]]
  1446. local vertexcode = [[
  1447. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  1448. return transform_projection * vertex_position;
  1449. }
  1450. ]]
  1451. test:assertObject(love.graphics.newShader(pixelcode, vertexcode))
  1452. end
  1453. -- love.graphics.newSpriteBatch
  1454. -- @NOTE this is just basic nil checking, objs have their own test method
  1455. love.test.graphics.newSpriteBatch = function(test)
  1456. local imgdata = love.graphics.newImage('resources/love.png')
  1457. test:assertObject(love.graphics.newSpriteBatch(imgdata, 1000))
  1458. end
  1459. -- love.graphics.newTextBatch
  1460. -- @NOTE this is just basic nil checking, objs have their own test method
  1461. love.test.graphics.newTextBatch = function(test)
  1462. local font = love.graphics.newFont('resources/font.ttf')
  1463. test:assertObject(love.graphics.newTextBatch(font, 'helloworld'))
  1464. end
  1465. -- love.graphics.newTexture
  1466. -- @NOTE this is just basic nil checking, objs have their own test method
  1467. love.test.graphics.newTexture = function(test)
  1468. local imgdata = love.image.newImageData('resources/love.png')
  1469. test:assertObject(love.graphics.newTexture(imgdata))
  1470. end
  1471. -- love.graphics.newVideo
  1472. -- @NOTE this is just basic nil checking, objs have their own test method
  1473. love.test.graphics.newVideo = function(test)
  1474. test:assertObject(love.graphics.newVideo('resources/sample.ogv', {
  1475. audio = false,
  1476. dpiscale = 1
  1477. }))
  1478. end
  1479. -- love.graphics.newVolumeImage
  1480. -- @NOTE this is just basic nil checking, objs have their own test method
  1481. love.test.graphics.newVolumeImage = function(test)
  1482. test:assertObject(love.graphics.newVolumeImage({
  1483. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  1484. }, {
  1485. mipmaps = false,
  1486. linear = false
  1487. }))
  1488. end
  1489. -- love.graphics.validateShader
  1490. love.test.graphics.validateShader = function(test)
  1491. local pixelcode = [[
  1492. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  1493. vec4 texturecolor = Texel(tex, texture_coords);
  1494. return texturecolor * color;
  1495. }
  1496. ]]
  1497. local vertexcode = [[
  1498. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  1499. return transform_projection * vertex_position;
  1500. }
  1501. ]]
  1502. -- check made up code first
  1503. local status, _ = love.graphics.validateShader(true, 'nothing here', 'or here')
  1504. test:assertFalse(status, 'check invalid shader code')
  1505. -- check real code
  1506. status, _ = love.graphics.validateShader(true, pixelcode, vertexcode)
  1507. test:assertTrue(status, 'check valid shader code')
  1508. end
  1509. --------------------------------------------------------------------------------
  1510. --------------------------------------------------------------------------------
  1511. ---------------------------------GRAPHICS STATE---------------------------------
  1512. --------------------------------------------------------------------------------
  1513. --------------------------------------------------------------------------------
  1514. -- love.graphics.getBackgroundColor
  1515. love.test.graphics.getBackgroundColor = function(test)
  1516. -- check default bg is black
  1517. local r, g, b, a = love.graphics.getBackgroundColor()
  1518. test:assertEquals(0, r, 'check default background r')
  1519. test:assertEquals(0, g, 'check default background g')
  1520. test:assertEquals(0, b, 'check default background b')
  1521. test:assertEquals(1, a, 'check default background a')
  1522. -- check set value returns correctly
  1523. love.graphics.setBackgroundColor(1, 1, 1, 0)
  1524. r, g, b, a = love.graphics.getBackgroundColor()
  1525. test:assertEquals(1, r, 'check updated background r')
  1526. test:assertEquals(1, g, 'check updated background g')
  1527. test:assertEquals(1, b, 'check updated background b')
  1528. test:assertEquals(0, a, 'check updated background a')
  1529. love.graphics.setBackgroundColor(0, 0, 0, 1) -- reset
  1530. end
  1531. -- love.graphics.getBlendMode
  1532. love.test.graphics.getBlendMode = function(test)
  1533. -- check default blend mode
  1534. local mode, alphamode = love.graphics.getBlendMode()
  1535. test:assertEquals('alpha', mode, 'check default blend mode')
  1536. test:assertEquals('alphamultiply', alphamode, 'check default alpha blend')
  1537. -- check set mode returns correctly
  1538. love.graphics.setBlendMode('add', 'premultiplied')
  1539. mode, alphamode = love.graphics.getBlendMode()
  1540. test:assertEquals('add', mode, 'check changed blend mode')
  1541. test:assertEquals('premultiplied', alphamode, 'check changed alpha blend')
  1542. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  1543. end
  1544. -- love.graphics.getCanvas
  1545. love.test.graphics.getCanvas = function(test)
  1546. -- by default should be nil if drawing to real screen
  1547. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  1548. -- should return not nil when we target a canvas
  1549. local canvas = love.graphics.newCanvas(16, 16)
  1550. love.graphics.setCanvas(canvas)
  1551. test:assertObject(love.graphics.getCanvas())
  1552. love.graphics.setCanvas()
  1553. end
  1554. -- love.graphics.getColor
  1555. love.test.graphics.getColor = function(test)
  1556. -- by default should be white
  1557. local r, g, b, a = love.graphics.getColor()
  1558. test:assertEquals(1, r, 'check default color r')
  1559. test:assertEquals(1, g, 'check default color g')
  1560. test:assertEquals(1, b, 'check default color b')
  1561. test:assertEquals(1, a, 'check default color a')
  1562. -- check set color is returned correctly
  1563. love.graphics.setColor(0, 0, 0, 0)
  1564. r, g, b, a = love.graphics.getColor()
  1565. test:assertEquals(0, r, 'check changed color r')
  1566. test:assertEquals(0, g, 'check changed color g')
  1567. test:assertEquals(0, b, 'check changed color b')
  1568. test:assertEquals(0, a, 'check changed color a')
  1569. love.graphics.setColor(1, 1, 1, 1) -- reset
  1570. end
  1571. -- love.graphics.getColorMask
  1572. love.test.graphics.getColorMask = function(test)
  1573. -- by default should all be active
  1574. local r, g, b, a = love.graphics.getColorMask()
  1575. test:assertTrue(r, 'check default color mask r')
  1576. test:assertTrue(g, 'check default color mask g')
  1577. test:assertTrue(b, 'check default color mask b')
  1578. test:assertTrue(a, 'check default color mask a')
  1579. -- check set color mask is returned correctly
  1580. love.graphics.setColorMask(false, false, true, false)
  1581. r, g, b, a = love.graphics.getColorMask()
  1582. test:assertFalse(r, 'check changed color mask r')
  1583. test:assertFalse(g, 'check changed color mask g')
  1584. test:assertTrue( b, 'check changed color mask b')
  1585. test:assertFalse(a, 'check changed color mask a')
  1586. love.graphics.setColorMask(true, true, true, true) -- reset
  1587. end
  1588. -- love.graphics.getDefaultFilter
  1589. love.test.graphics.getDefaultFilter = function(test)
  1590. -- we set this already for testsuite so we know what it should be
  1591. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  1592. test:assertEquals('nearest', min, 'check default filter min')
  1593. test:assertEquals('nearest', mag, 'check default filter mag')
  1594. test:assertEquals(1, anisotropy, 'check default filter mag')
  1595. end
  1596. -- love.graphics.getDepthMode
  1597. love.test.graphics.getDepthMode = function(test)
  1598. -- by default should be always/write
  1599. local comparemode, write = love.graphics.getDepthMode()
  1600. test:assertEquals('always', comparemode, 'check default compare depth')
  1601. test:assertFalse(write, 'check default depth buffer write')
  1602. end
  1603. -- love.graphics.getFont
  1604. love.test.graphics.getFont = function(test)
  1605. test:assertObject(love.graphics.getFont())
  1606. end
  1607. -- love.graphics.getFrontFaceWinding
  1608. love.test.graphics.getFrontFaceWinding = function(test)
  1609. -- check default winding
  1610. test:assertEquals('ccw', love.graphics.getFrontFaceWinding())
  1611. -- check setting value changes it correctly
  1612. love.graphics.setFrontFaceWinding('cw')
  1613. test:assertEquals('cw', love.graphics.getFrontFaceWinding())
  1614. love.graphics.setFrontFaceWinding('ccw') -- reset
  1615. end
  1616. -- love.graphics.getLineJoin
  1617. love.test.graphics.getLineJoin = function(test)
  1618. -- check default line join
  1619. test:assertEquals('miter', love.graphics.getLineJoin())
  1620. -- check set value returned correctly
  1621. love.graphics.setLineJoin('none')
  1622. test:assertEquals('none', love.graphics.getLineJoin())
  1623. love.graphics.setLineJoin('miter') -- reset
  1624. end
  1625. -- love.graphics.getLineStyle
  1626. love.test.graphics.getLineStyle = function(test)
  1627. -- we know this should be as testsuite sets it!
  1628. test:assertEquals('rough', love.graphics.getLineStyle())
  1629. -- check set value returned correctly
  1630. love.graphics.setLineStyle('smooth')
  1631. test:assertEquals('smooth', love.graphics.getLineStyle())
  1632. love.graphics.setLineStyle('rough') -- reset
  1633. end
  1634. -- love.graphics.getLineWidth
  1635. love.test.graphics.getLineWidth = function(test)
  1636. -- we know this should be as testsuite sets it!
  1637. test:assertEquals(1, love.graphics.getLineWidth())
  1638. -- check set value returned correctly
  1639. love.graphics.setLineWidth(10)
  1640. test:assertEquals(10, love.graphics.getLineWidth())
  1641. love.graphics.setLineWidth(1) -- reset
  1642. end
  1643. -- love.graphics.getMeshCullMode
  1644. love.test.graphics.getMeshCullMode = function(test)
  1645. -- get default mesh culling
  1646. test:assertEquals('none', love.graphics.getMeshCullMode())
  1647. -- check set value returned correctly
  1648. love.graphics.setMeshCullMode('front')
  1649. test:assertEquals('front', love.graphics.getMeshCullMode())
  1650. love.graphics.setMeshCullMode('back') -- reset
  1651. end
  1652. -- love.graphics.getPointSize
  1653. love.test.graphics.getPointSize = function(test)
  1654. -- get default point size
  1655. test:assertEquals(1, love.graphics.getPointSize())
  1656. -- check set value returned correctly
  1657. love.graphics.setPointSize(10)
  1658. test:assertEquals(10, love.graphics.getPointSize())
  1659. love.graphics.setPointSize(1) -- reset
  1660. end
  1661. -- love.graphics.getScissor
  1662. love.test.graphics.getScissor = function(test)
  1663. -- should be no scissor atm
  1664. local x, y, w, h = love.graphics.getScissor()
  1665. test:assertEquals(nil, x, 'check no scissor')
  1666. test:assertEquals(nil, y, 'check no scissor')
  1667. test:assertEquals(nil, w, 'check no scissor')
  1668. test:assertEquals(nil, h, 'check no scissor')
  1669. -- check set value returned correctly
  1670. love.graphics.setScissor(0, 0, 16, 16)
  1671. x, y, w, h = love.graphics.getScissor()
  1672. test:assertEquals(0, x, 'check scissor set')
  1673. test:assertEquals(0, y, 'check scissor set')
  1674. test:assertEquals(16, w, 'check scissor set')
  1675. test:assertEquals(16, h, 'check scissor set')
  1676. love.graphics.setScissor() -- reset
  1677. end
  1678. -- love.graphics.getShader
  1679. love.test.graphics.getShader = function(test)
  1680. -- should be no shader active
  1681. test:assertEquals(nil, love.graphics.getShader(), 'check no active shader')
  1682. end
  1683. -- love.graphics.getStackDepth
  1684. love.test.graphics.getStackDepth = function(test)
  1685. -- by default should be none
  1686. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  1687. -- now add 3
  1688. love.graphics.push()
  1689. love.graphics.push()
  1690. love.graphics.push()
  1691. test:assertEquals(3, love.graphics.getStackDepth(), 'check 3 transforms in stack')
  1692. -- now remove 2
  1693. love.graphics.pop()
  1694. love.graphics.pop()
  1695. test:assertEquals(1, love.graphics.getStackDepth(), 'check 1 transforms in stack')
  1696. -- now back to 0
  1697. love.graphics.pop()
  1698. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  1699. end
  1700. -- love.graphics.getStencilState
  1701. love.test.graphics.getStencilState = function(test)
  1702. -- check default vals
  1703. local action, comparemode, value = love.graphics.getStencilState( )
  1704. test:assertEquals('keep', action, 'check default stencil action')
  1705. test:assertEquals('always', comparemode, 'check default stencil compare')
  1706. test:assertEquals(0, value, 'check default stencil value')
  1707. -- check set stencil values is returned
  1708. love.graphics.setStencilState('replace', 'less', 255)
  1709. local action, comparemode, value = love.graphics.getStencilState()
  1710. test:assertEquals('replace', action, 'check changed stencil action')
  1711. test:assertEquals('less', comparemode, 'check changed stencil compare')
  1712. test:assertEquals(255, value, 'check changed stencil value')
  1713. love.graphics.setStencilState() -- reset
  1714. end
  1715. -- love.graphics.intersectScissor
  1716. love.test.graphics.intersectScissor = function(test)
  1717. -- make a scissor for the left half, then interset to make the top half
  1718. -- then we should be able to fill the canvas with red and only top 4x4 is filled
  1719. local canvas = love.graphics.newCanvas(16, 16)
  1720. love.graphics.setCanvas(canvas)
  1721. love.graphics.clear(0, 0, 0, 1)
  1722. love.graphics.origin()
  1723. love.graphics.setScissor(0, 0, 8, 16)
  1724. love.graphics.intersectScissor(0, 0, 4, 4)
  1725. love.graphics.setColor(1, 0, 0, 1)
  1726. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1727. love.graphics.setColor(1, 1, 1, 1)
  1728. love.graphics.setScissor()
  1729. love.graphics.setCanvas()
  1730. local imgdata = love.graphics.readbackTexture(canvas)
  1731. test:compareImg(imgdata)
  1732. end
  1733. -- love.graphics.isActive
  1734. love.test.graphics.isActive = function(test)
  1735. test:assertTrue(love.graphics.isActive(), 'check graphics is active') -- i mean if you got this far
  1736. end
  1737. -- love.graphics.isGammaCorrect
  1738. love.test.graphics.isGammaCorrect = function(test)
  1739. -- we know the config so know this is false
  1740. test:assertNotNil(love.graphics.isGammaCorrect())
  1741. end
  1742. -- love.graphics.isWireframe
  1743. love.test.graphics.isWireframe = function(test)
  1744. local name, version, vendor, device = love.graphics.getRendererInfo()
  1745. if string.match(name, 'OpenGL ES') then
  1746. test:skipTest('Wireframe not supported on OpenGL ES')
  1747. else
  1748. -- check off by default
  1749. test:assertFalse(love.graphics.isWireframe(), 'check no wireframe by default')
  1750. -- check on when enabled
  1751. love.graphics.setWireframe(true)
  1752. test:assertTrue(love.graphics.isWireframe(), 'check wireframe is set')
  1753. love.graphics.setWireframe(false) -- reset
  1754. end
  1755. end
  1756. -- love.graphics.reset
  1757. love.test.graphics.reset = function(test)
  1758. -- reset should reset current canvas and any colors/scissor
  1759. local canvas = love.graphics.newCanvas(16, 16)
  1760. love.graphics.setBackgroundColor(0, 0, 1, 1)
  1761. love.graphics.setColor(0, 1, 0, 1)
  1762. love.graphics.setCanvas(canvas)
  1763. love.graphics.reset()
  1764. local r, g, b, a = love.graphics.getBackgroundColor()
  1765. test:assertEquals(1, r+g+b+a, 'check background reset')
  1766. r, g, b, a = love.graphics.getColor()
  1767. test:assertEquals(4, r+g+b+a, 'check color reset')
  1768. test:assertEquals(nil, love.graphics.getCanvas(), 'check canvas reset')
  1769. love.graphics.setDefaultFilter("nearest", "nearest")
  1770. love.graphics.setLineStyle('rough')
  1771. love.graphics.setPointSize(1)
  1772. love.graphics.setLineWidth(1)
  1773. end
  1774. -- love.graphics.setBackgroundColor
  1775. love.test.graphics.setBackgroundColor = function(test)
  1776. -- check background is set
  1777. love.graphics.setBackgroundColor(1, 0, 0, 1)
  1778. local r, g, b, a = love.graphics.getBackgroundColor()
  1779. test:assertEquals(1, r, 'check set bg r')
  1780. test:assertEquals(0, g, 'check set bg g')
  1781. test:assertEquals(0, b, 'check set bg b')
  1782. test:assertEquals(1, a, 'check set bg a')
  1783. love.graphics.setBackgroundColor(0, 0, 0, 1)
  1784. end
  1785. -- love.graphics.setBlendMode
  1786. love.test.graphics.setBlendMode = function(test)
  1787. -- create fully white canvas, then draw diff. pixels through blendmodes
  1788. local canvas = love.graphics.newCanvas(16, 16)
  1789. love.graphics.setCanvas(canvas)
  1790. love.graphics.clear(0.5, 0.5, 0.5, 1)
  1791. love.graphics.setBlendMode('add', 'alphamultiply')
  1792. love.graphics.setColor(1, 0, 0, 1)
  1793. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1794. love.graphics.setBlendMode('subtract', 'alphamultiply')
  1795. love.graphics.setColor(1, 1, 1, 0.5)
  1796. love.graphics.rectangle('fill', 15, 0, 1, 1)
  1797. love.graphics.setBlendMode('multiply', 'premultiplied')
  1798. love.graphics.setColor(0, 1, 0, 1)
  1799. love.graphics.rectangle('fill', 15, 15, 1, 1)
  1800. love.graphics.setBlendMode('replace', 'premultiplied')
  1801. love.graphics.setColor(0, 0, 1, 0.5)
  1802. love.graphics.rectangle('fill', 0, 15, 1, 1)
  1803. love.graphics.setColor(1, 1, 1, 1)
  1804. love.graphics.setCanvas()
  1805. local imgdata = love.graphics.readbackTexture(canvas)
  1806. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  1807. -- need 1rgba tolerance here on some machines
  1808. test.rgba_tolerance = 1
  1809. test:compareImg(imgdata)
  1810. end
  1811. -- love.graphics.setCanvas
  1812. love.test.graphics.setCanvas = function(test)
  1813. -- make 2 canvas, set to each, draw one to the other, check output
  1814. local canvas1 = love.graphics.newCanvas(16, 16)
  1815. local canvas2 = love.graphics.newCanvas(16, 16, {mipmaps = "auto"})
  1816. love.graphics.setCanvas(canvas1)
  1817. test:assertEquals(canvas1, love.graphics.getCanvas(), 'check canvas 1 set')
  1818. love.graphics.clear(1, 0, 0, 1)
  1819. love.graphics.setCanvas(canvas2)
  1820. test:assertEquals(canvas2, love.graphics.getCanvas(), 'check canvas 2 set')
  1821. love.graphics.clear(0, 0, 0, 1)
  1822. love.graphics.draw(canvas1, 0, 0)
  1823. love.graphics.setCanvas()
  1824. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  1825. local imgdata = love.graphics.readbackTexture(canvas2)
  1826. test:compareImg(imgdata)
  1827. local imgdata2 = love.graphics.readbackTexture(canvas2, 1, 2) -- readback mipmap
  1828. test:compareImg(imgdata2)
  1829. end
  1830. -- love.graphics.setColor
  1831. love.test.graphics.setColor = function(test)
  1832. -- set colors, draw rect, check color
  1833. local canvas = love.graphics.newCanvas(16, 16)
  1834. love.graphics.setCanvas(canvas)
  1835. love.graphics.clear(0, 0, 0, 1)
  1836. love.graphics.setColor(1, 0, 0, 1)
  1837. local r, g, b, a = love.graphics.getColor()
  1838. test:assertEquals(1, r, 'check r set')
  1839. test:assertEquals(0, g, 'check g set')
  1840. test:assertEquals(0, b, 'check b set')
  1841. test:assertEquals(1, a, 'check a set')
  1842. love.graphics.rectangle('fill', 0, 0, 16, 1)
  1843. love.graphics.setColor(1, 1, 0, 1)
  1844. love.graphics.rectangle('fill', 0, 1, 16, 1)
  1845. love.graphics.setColor(0, 1, 0, 0.5)
  1846. love.graphics.rectangle('fill', 0, 2, 16, 1)
  1847. love.graphics.setColor(0, 0, 1, 1)
  1848. love.graphics.rectangle('fill', 0, 3, 16, 1)
  1849. love.graphics.setColor(1, 1, 1, 1)
  1850. love.graphics.setCanvas()
  1851. local imgdata = love.graphics.readbackTexture(canvas)
  1852. test:compareImg(imgdata)
  1853. end
  1854. -- love.graphics.setColorMask
  1855. love.test.graphics.setColorMask = function(test)
  1856. -- set mask, draw stuff, check output pixels
  1857. local canvas = love.graphics.newCanvas(16, 16)
  1858. love.graphics.setCanvas(canvas)
  1859. love.graphics.clear(0, 0, 0, 1)
  1860. -- mask off blue
  1861. love.graphics.setColorMask(true, true, false, true)
  1862. local r, g, b, a = love.graphics.getColorMask()
  1863. test:assertEquals(r, true, 'check r mask')
  1864. test:assertEquals(g, true, 'check g mask')
  1865. test:assertEquals(b, false, 'check b mask')
  1866. test:assertEquals(a, true, 'check a mask')
  1867. -- draw "black" which should then turn to yellow
  1868. love.graphics.setColor(1, 1, 1, 1)
  1869. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1870. love.graphics.setColorMask(true, true, true, true)
  1871. love.graphics.setCanvas()
  1872. local imgdata = love.graphics.readbackTexture(canvas)
  1873. test:compareImg(imgdata)
  1874. end
  1875. -- love.graphics.setDefaultFilter
  1876. love.test.graphics.setDefaultFilter = function(test)
  1877. -- check setting filter val works
  1878. love.graphics.setDefaultFilter('linear', 'linear', 1)
  1879. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  1880. test:assertEquals('linear', min, 'check default filter min')
  1881. test:assertEquals('linear', mag, 'check default filter mag')
  1882. test:assertEquals(1, anisotropy, 'check default filter mag')
  1883. love.graphics.setDefaultFilter('nearest', 'nearest', 1) -- reset
  1884. end
  1885. -- love.graphics.setDepthMode
  1886. love.test.graphics.setDepthMode = function(test)
  1887. -- check documented modes are valid
  1888. local comparemode, write = love.graphics.getDepthMode()
  1889. local modes = {
  1890. 'equal', 'notequal', 'less', 'lequal', 'gequal',
  1891. 'greater', 'never', 'always'
  1892. }
  1893. for m=1,#modes do
  1894. love.graphics.setDepthMode(modes[m], true)
  1895. test:assertEquals(modes[m], love.graphics.getDepthMode(), 'check depth mode ' .. modes[m] .. ' set')
  1896. end
  1897. love.graphics.setDepthMode(comparemode, write)
  1898. -- @TODO better graphics drawing specific test
  1899. end
  1900. -- love.graphics.setFont
  1901. love.test.graphics.setFont = function(test)
  1902. -- set font doesnt return anything so draw with the test font
  1903. local canvas = love.graphics.newCanvas(16, 16)
  1904. love.graphics.setFont(Font)
  1905. love.graphics.setCanvas(canvas)
  1906. love.graphics.clear(0, 0, 0, 1)
  1907. love.graphics.setColor(1, 0, 0, 1)
  1908. love.graphics.print('love', 0, 3)
  1909. love.graphics.setColor(1, 1, 1, 1)
  1910. love.graphics.setCanvas()
  1911. local imgdata = love.graphics.readbackTexture(canvas)
  1912. test:compareImg(imgdata)
  1913. end
  1914. -- love.graphics.setFrontFaceWinding
  1915. love.test.graphics.setFrontFaceWinding = function(test)
  1916. -- check documented modes are valid
  1917. local original = love.graphics.getFrontFaceWinding()
  1918. love.graphics.setFrontFaceWinding('cw')
  1919. test:assertEquals('cw', love.graphics.getFrontFaceWinding(), 'check ffw cw set')
  1920. love.graphics.setFrontFaceWinding('ccw')
  1921. test:assertEquals('ccw', love.graphics.getFrontFaceWinding(), 'check ffw ccw set')
  1922. love.graphics.setFrontFaceWinding(original)
  1923. -- @TODO better graphics drawing specific test
  1924. local shader = love.graphics.newShader[[
  1925. vec4 effect(vec4 c, Image tex, vec2 tc, vec2 pc) {
  1926. return gl_FrontFacing ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0);
  1927. }
  1928. ]]
  1929. local dummyimg = love.graphics.newImage(love.image.newImageData(1, 1))
  1930. local canvas = love.graphics.newCanvas(16, 16)
  1931. love.graphics.push("all")
  1932. love.graphics.setCanvas(canvas)
  1933. love.graphics.setShader(shader)
  1934. love.graphics.draw(dummyimg, 0, 0, 0, 16, 16)
  1935. love.graphics.pop()
  1936. local imgdata = love.graphics.readbackTexture(canvas)
  1937. test:compareImg(imgdata)
  1938. end
  1939. -- love.graphics.setLineJoin
  1940. love.test.graphics.setLineJoin = function(test)
  1941. local canvas = love.graphics.newCanvas(16, 16)
  1942. love.graphics.setFont(Font)
  1943. love.graphics.setCanvas(canvas)
  1944. love.graphics.clear(0, 0, 0, 1)
  1945. local line = {0,1,8,1,8,8}
  1946. love.graphics.setLineStyle('rough')
  1947. love.graphics.setLineWidth(2)
  1948. love.graphics.setColor(1, 0, 0)
  1949. love.graphics.setLineJoin('bevel')
  1950. love.graphics.line(line)
  1951. love.graphics.translate(0, 4)
  1952. love.graphics.setColor(1, 1, 0)
  1953. love.graphics.setLineJoin('none')
  1954. love.graphics.line(line)
  1955. love.graphics.translate(0, 4)
  1956. love.graphics.setColor(0, 0, 1)
  1957. love.graphics.setLineJoin('miter')
  1958. love.graphics.line(line)
  1959. love.graphics.setColor(1, 1, 1)
  1960. love.graphics.setLineWidth(1)
  1961. love.graphics.origin()
  1962. love.graphics.setCanvas()
  1963. local imgdata = love.graphics.readbackTexture(canvas)
  1964. test:compareImg(imgdata)
  1965. end
  1966. -- love.graphics.setLineStyle
  1967. love.test.graphics.setLineStyle = function(test)
  1968. local canvas = love.graphics.newCanvas(16, 16)
  1969. love.graphics.setFont(Font)
  1970. love.graphics.setCanvas(canvas)
  1971. love.graphics.clear(0, 0, 0, 1)
  1972. love.graphics.setColor(1, 0, 0)
  1973. local line = {0,1,16,1}
  1974. love.graphics.setLineStyle('rough')
  1975. love.graphics.line(line)
  1976. love.graphics.translate(0, 4)
  1977. love.graphics.setLineStyle('smooth')
  1978. love.graphics.line(line)
  1979. love.graphics.setLineStyle('rough')
  1980. love.graphics.setColor(1, 1, 1)
  1981. love.graphics.origin()
  1982. love.graphics.setCanvas()
  1983. local imgdata = love.graphics.readbackTexture(canvas)
  1984. -- linux runner needs a 1/255 tolerance for the blend between a rough line + bg
  1985. if GITHUB_RUNNER and test:isOS('Linux') then
  1986. test.rgba_tolerance = 1
  1987. end
  1988. test:compareImg(imgdata)
  1989. end
  1990. -- love.graphics.setLineWidth
  1991. love.test.graphics.setLineWidth = function(test)
  1992. local canvas = love.graphics.newCanvas(16, 16)
  1993. love.graphics.setFont(Font)
  1994. love.graphics.setCanvas(canvas)
  1995. love.graphics.clear(0, 0, 0, 1)
  1996. local line = {0,1,8,1,8,8}
  1997. love.graphics.setColor(1, 0, 0)
  1998. love.graphics.setLineWidth(2)
  1999. love.graphics.line(line)
  2000. love.graphics.translate(0, 4)
  2001. love.graphics.setColor(1, 1, 0)
  2002. love.graphics.setLineWidth(3)
  2003. love.graphics.line(line)
  2004. love.graphics.translate(0, 4)
  2005. love.graphics.setColor(0, 0, 1)
  2006. love.graphics.setLineWidth(4)
  2007. love.graphics.line(line)
  2008. love.graphics.setColor(1, 1, 1)
  2009. love.graphics.setLineWidth(1)
  2010. love.graphics.origin()
  2011. love.graphics.setCanvas()
  2012. local imgdata = love.graphics.readbackTexture(canvas)
  2013. test:compareImg(imgdata)
  2014. end
  2015. -- love.graphics.setMeshCullMode
  2016. love.test.graphics.setMeshCullMode = function(test)
  2017. -- check documented modes are valid
  2018. local original = love.graphics.getMeshCullMode()
  2019. local modes = {'back', 'front', 'none'}
  2020. for m=1,#modes do
  2021. love.graphics.setMeshCullMode(modes[m])
  2022. test:assertEquals(modes[m], love.graphics.getMeshCullMode(), 'check mesh cull mode ' .. modes[m] .. ' was set')
  2023. end
  2024. love.graphics.setMeshCullMode(original)
  2025. -- @TODO better graphics drawing specific test
  2026. end
  2027. -- love.graphics.setScissor
  2028. love.test.graphics.setScissor = function(test)
  2029. -- make a scissor for the left half
  2030. -- then we should be able to fill the canvas with red and only left is filled
  2031. local canvas = love.graphics.newCanvas(16, 16)
  2032. love.graphics.setCanvas(canvas)
  2033. love.graphics.clear(0, 0, 0, 1)
  2034. love.graphics.origin()
  2035. love.graphics.setScissor(0, 0, 8, 16)
  2036. love.graphics.setColor(1, 0, 0, 1)
  2037. love.graphics.rectangle('fill', 0, 0, 16, 16)
  2038. love.graphics.setColor(1, 1, 1, 1)
  2039. love.graphics.setScissor()
  2040. love.graphics.setCanvas()
  2041. local imgdata = love.graphics.readbackTexture(canvas)
  2042. test:compareImg(imgdata)
  2043. end
  2044. -- love.graphics.setShader
  2045. love.test.graphics.setShader = function(test)
  2046. -- make a shader that will only ever draw yellow
  2047. local pixelcode = [[
  2048. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  2049. vec4 texturecolor = Texel(tex, texture_coords);
  2050. return vec4(1.0,1.0,0.0,1.0);
  2051. }
  2052. ]]
  2053. local vertexcode = [[
  2054. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  2055. return transform_projection * vertex_position;
  2056. }
  2057. ]]
  2058. local shader = love.graphics.newShader(pixelcode, vertexcode)
  2059. local canvas = love.graphics.newCanvas(16, 16)
  2060. love.graphics.setCanvas(canvas)
  2061. love.graphics.clear(0, 0, 0, 1)
  2062. love.graphics.setShader(shader)
  2063. -- draw red rectangle
  2064. love.graphics.setColor(1, 0, 0, 1)
  2065. love.graphics.rectangle('fill', 0, 0, 16, 16)
  2066. love.graphics.setShader()
  2067. love.graphics.setColor(1, 1, 1, 1)
  2068. love.graphics.setCanvas()
  2069. local imgdata = love.graphics.readbackTexture(canvas)
  2070. test:compareImg(imgdata)
  2071. end
  2072. -- love.graphics.setStencilState
  2073. love.test.graphics.setStencilState = function(test)
  2074. local canvas = love.graphics.newCanvas(16, 16)
  2075. love.graphics.setCanvas({canvas, stencil=true})
  2076. love.graphics.clear(0, 0, 0, 1)
  2077. love.graphics.setStencilState('replace', 'always', 1)
  2078. love.graphics.circle('fill', 8, 8, 6)
  2079. love.graphics.setStencilState('keep', 'greater', 0)
  2080. love.graphics.setColor(1, 0, 0, 1)
  2081. love.graphics.rectangle('fill', 0, 0, 16, 16)
  2082. love.graphics.setColor(1, 1, 1, 1)
  2083. love.graphics.setStencilState()
  2084. love.graphics.setCanvas()
  2085. local imgdata = love.graphics.readbackTexture(canvas)
  2086. test:compareImg(imgdata)
  2087. end
  2088. -- love.graphics.setWireframe
  2089. love.test.graphics.setWireframe = function(test)
  2090. local name, version, vendor, device = love.graphics.getRendererInfo()
  2091. if string.match(name, 'OpenGL ES') then
  2092. test:skipTest('Wireframe not supported on OpenGL ES')
  2093. else
  2094. -- check wireframe outlines
  2095. love.graphics.setWireframe(true)
  2096. local canvas = love.graphics.newCanvas(16, 16)
  2097. love.graphics.setCanvas(canvas)
  2098. love.graphics.clear(0, 0, 0, 1)
  2099. love.graphics.setColor(1, 1, 0, 1)
  2100. love.graphics.rectangle('fill', 2, 2, 13, 13)
  2101. love.graphics.setColor(1, 1, 1, 1)
  2102. love.graphics.setCanvas()
  2103. love.graphics.setWireframe(false)
  2104. local imgdata = love.graphics.readbackTexture(canvas)
  2105. -- on macOS runners wireframes are drawn 1px off from the target
  2106. if GITHUB_RUNNER and test:isOS('OS X') then
  2107. test.pixel_tolerance = 1
  2108. end
  2109. test:compareImg(imgdata)
  2110. end
  2111. end
  2112. --------------------------------------------------------------------------------
  2113. --------------------------------------------------------------------------------
  2114. -------------------------------COORDINATE SYSTEM--------------------------------
  2115. --------------------------------------------------------------------------------
  2116. --------------------------------------------------------------------------------
  2117. -- love.graphics.applyTransform
  2118. love.test.graphics.applyTransform = function(test)
  2119. -- use transform object to translate the drawn rectangle
  2120. local transform = love.math.newTransform()
  2121. transform:translate(10, 0)
  2122. local canvas = love.graphics.newCanvas(16, 16)
  2123. love.graphics.setCanvas(canvas)
  2124. love.graphics.clear(0, 0, 0, 1)
  2125. love.graphics.setColor(1, 0, 0, 1)
  2126. love.graphics.applyTransform(transform)
  2127. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2128. love.graphics.setColor(1, 1, 1, 1)
  2129. love.graphics.setCanvas()
  2130. local imgdata = love.graphics.readbackTexture(canvas)
  2131. test:compareImg(imgdata)
  2132. end
  2133. -- love.graphics.inverseTransformPoint
  2134. love.test.graphics.inverseTransformPoint = function(test)
  2135. -- start with 0, 0
  2136. local sx, sy = love.graphics.inverseTransformPoint(0, 0)
  2137. test:assertEquals(0, sx, 'check starting x is 0')
  2138. test:assertEquals(0, sy, 'check starting y is 0')
  2139. -- check translation effects the point
  2140. love.graphics.translate(1, 5)
  2141. sx, sy = love.graphics.inverseTransformPoint(1, 5)
  2142. test:assertEquals(0, sx, 'check transformed x is 0')
  2143. test:assertEquals(0, sy, 'check transformed y is 0')
  2144. love.graphics.origin()
  2145. end
  2146. -- love.graphics.origin
  2147. love.test.graphics.origin = function(test)
  2148. -- if we do some translations and scaling
  2149. -- using .origin() should reset it all and draw the pixel at 0,0
  2150. local canvas = love.graphics.newCanvas(16, 16)
  2151. love.graphics.setCanvas(canvas)
  2152. love.graphics.clear(0, 0, 0, 1)
  2153. love.graphics.origin()
  2154. love.graphics.translate(10, 10)
  2155. love.graphics.scale(1, 1)
  2156. love.graphics.shear(20, 20)
  2157. love.graphics.origin()
  2158. love.graphics.setColor(1, 0, 0, 1)
  2159. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2160. love.graphics.setColor(1, 1, 1, 1)
  2161. love.graphics.setCanvas()
  2162. local imgdata = love.graphics.readbackTexture(canvas)
  2163. test:compareImg(imgdata)
  2164. end
  2165. -- love.graphics.pop
  2166. love.test.graphics.pop = function(test)
  2167. -- if we push at the start, and then run a pop
  2168. -- it should reset it all and draw the pixel at 0,0
  2169. local canvas = love.graphics.newCanvas(16, 16)
  2170. love.graphics.setCanvas(canvas)
  2171. love.graphics.clear(0, 0, 0, 1)
  2172. love.graphics.push()
  2173. love.graphics.translate(10, 10)
  2174. love.graphics.scale(1, 1)
  2175. love.graphics.shear(20, 20)
  2176. love.graphics.pop()
  2177. love.graphics.setColor(1, 0, 0, 1)
  2178. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2179. love.graphics.setColor(1, 1, 1, 1)
  2180. love.graphics.setCanvas()
  2181. local imgdata = love.graphics.readbackTexture(canvas)
  2182. test:compareImg(imgdata)
  2183. end
  2184. -- love.graphics.push
  2185. love.test.graphics.push = function(test)
  2186. -- if we push at the start, do some stuff, then another push
  2187. -- 1 pop should only go back 1 push and draw the pixel at 1, 1
  2188. local canvas = love.graphics.newCanvas(16, 16)
  2189. love.graphics.setCanvas(canvas)
  2190. love.graphics.clear(0, 0, 0, 1)
  2191. love.graphics.push()
  2192. love.graphics.scale(1, 1)
  2193. love.graphics.shear(20, 20)
  2194. love.graphics.push()
  2195. love.graphics.translate(1, 1)
  2196. love.graphics.pop()
  2197. love.graphics.setColor(1, 0, 0, 1)
  2198. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2199. love.graphics.setColor(1, 1, 1, 1)
  2200. love.graphics.pop()
  2201. love.graphics.setCanvas()
  2202. local imgdata = love.graphics.readbackTexture(canvas)
  2203. test:compareImg(imgdata)
  2204. end
  2205. -- love.graphics.replaceTransform
  2206. love.test.graphics.replaceTransform = function(test)
  2207. -- if use transform object to translate
  2208. -- set some normal transforms first which should get overwritten
  2209. local transform = love.math.newTransform()
  2210. transform:translate(10, 0)
  2211. local canvas = love.graphics.newCanvas(16, 16)
  2212. love.graphics.setCanvas(canvas)
  2213. love.graphics.clear(0, 0, 0, 1)
  2214. love.graphics.setColor(1, 0, 0, 1)
  2215. love.graphics.scale(2, 2)
  2216. love.graphics.translate(10, 10)
  2217. love.graphics.replaceTransform(transform)
  2218. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2219. love.graphics.setColor(1, 1, 1, 1)
  2220. love.graphics.setCanvas()
  2221. local imgdata = love.graphics.readbackTexture(canvas)
  2222. test:compareImg(imgdata)
  2223. end
  2224. -- love.graphics.rotate
  2225. love.test.graphics.rotate = function(test)
  2226. -- starting at 0,0, we rotate by 90deg and then draw
  2227. -- we can then check the drawn rectangle is rotated
  2228. local canvas = love.graphics.newCanvas(16, 16)
  2229. love.graphics.setCanvas(canvas)
  2230. love.graphics.clear(0, 0, 0, 1)
  2231. love.graphics.setColor(1, 0, 0, 1)
  2232. love.graphics.translate(4, 0)
  2233. love.graphics.rotate(90 * (math.pi/180))
  2234. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2235. love.graphics.setColor(1, 1, 1, 1)
  2236. love.graphics.setCanvas()
  2237. local imgdata = love.graphics.readbackTexture(canvas)
  2238. test:compareImg(imgdata)
  2239. end
  2240. -- love.graphics.scale
  2241. love.test.graphics.scale = function(test)
  2242. -- starting at 0,0, we scale by 4x and then draw
  2243. -- we can then check the drawn rectangle covers the whole canvas
  2244. local canvas = love.graphics.newCanvas(16, 16)
  2245. love.graphics.setCanvas(canvas)
  2246. love.graphics.clear(0, 0, 0, 1)
  2247. love.graphics.setColor(1, 0, 0, 1)
  2248. love.graphics.scale(4, 4)
  2249. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2250. love.graphics.setColor(1, 1, 1, 1)
  2251. love.graphics.setCanvas()
  2252. local imgdata = love.graphics.readbackTexture(canvas)
  2253. test:compareImg(imgdata)
  2254. end
  2255. -- love.graphics.shear
  2256. love.test.graphics.shear = function(test)
  2257. -- starting at 0,0, we shear by 2x and then draw
  2258. -- we can then check the drawn rectangle has moved over
  2259. local canvas = love.graphics.newCanvas(16, 16)
  2260. love.graphics.setCanvas(canvas)
  2261. love.graphics.clear(0, 0, 0, 1)
  2262. love.graphics.origin()
  2263. love.graphics.setColor(1, 0, 0, 1)
  2264. love.graphics.shear(2, 0)
  2265. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2266. love.graphics.setColor(1, 1, 1, 1)
  2267. love.graphics.setCanvas()
  2268. local imgdata1 = love.graphics.readbackTexture(canvas)
  2269. test:compareImg(imgdata1)
  2270. -- same again at 0,0, we shear by 2y and then draw
  2271. -- we can then check the drawn rectangle has moved down
  2272. love.graphics.setCanvas(canvas)
  2273. love.graphics.clear(0, 0, 0, 1)
  2274. love.graphics.origin()
  2275. love.graphics.setColor(1, 0, 0, 1)
  2276. love.graphics.shear(0, 2)
  2277. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2278. love.graphics.setColor(1, 1, 1, 1)
  2279. love.graphics.setCanvas()
  2280. local imgdata2 = love.graphics.readbackTexture(canvas)
  2281. test:compareImg(imgdata2)
  2282. end
  2283. -- love.graphics.transformPoint
  2284. love.test.graphics.transformPoint = function(test)
  2285. -- start with 0, 0
  2286. local sx, sy = love.graphics.transformPoint(0, 0)
  2287. test:assertEquals(0, sx, 'check starting x is 0')
  2288. test:assertEquals(0, sy, 'check starting y is 0')
  2289. -- check translation effects the point
  2290. love.graphics.translate(1, 5)
  2291. sx, sy = love.graphics.transformPoint(0, 0)
  2292. test:assertEquals(1, sx, 'check transformed x is 0')
  2293. test:assertEquals(5, sy, 'check transformed y is 10')
  2294. end
  2295. -- love.graphics.translate
  2296. love.test.graphics.translate = function(test)
  2297. -- starting at 0,0, we translate 4 times and draw a pixel at each point
  2298. -- we can then check the 4 points are now red
  2299. local canvas = love.graphics.newCanvas(16, 16)
  2300. love.graphics.setCanvas(canvas)
  2301. love.graphics.clear(0, 0, 0, 1)
  2302. love.graphics.setColor(1, 0, 0, 1)
  2303. love.graphics.translate(5, 0)
  2304. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2305. love.graphics.translate(0, 5)
  2306. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2307. love.graphics.translate(-5, 0)
  2308. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2309. love.graphics.translate(0, -5)
  2310. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2311. love.graphics.setColor(1, 1, 1, 1)
  2312. love.graphics.setCanvas()
  2313. local imgdata = love.graphics.readbackTexture(canvas)
  2314. test:compareImg(imgdata)
  2315. end
  2316. --------------------------------------------------------------------------------
  2317. --------------------------------------------------------------------------------
  2318. -------------------------------------WINDOW-------------------------------------
  2319. --------------------------------------------------------------------------------
  2320. --------------------------------------------------------------------------------
  2321. -- love.graphics.getDPIScale
  2322. -- @NOTE hardware dependent so can't check result
  2323. love.test.graphics.getDPIScale = function(test)
  2324. test:assertNotNil(love.graphics.getDPIScale())
  2325. end
  2326. -- love.graphics.getDimensions
  2327. love.test.graphics.getDimensions = function(test)
  2328. -- check graphics dimensions match window dimensions
  2329. local gwidth, gheight = love.graphics.getDimensions()
  2330. local wwidth, wheight, _ = love.window.getMode()
  2331. test:assertEquals(wwidth, gwidth, 'check graphics dimension w matches window w')
  2332. test:assertEquals(wheight, gheight, 'check graphics dimension h matches window h')
  2333. end
  2334. -- love.graphics.getHeight
  2335. love.test.graphics.getHeight = function(test)
  2336. -- check graphics height match window height
  2337. local wwidth, wheight, _ = love.window.getMode()
  2338. test:assertEquals(wheight, love.graphics.getHeight(), 'check graphics h matches window h')
  2339. end
  2340. -- love.graphics.getPixelDimensions
  2341. love.test.graphics.getPixelDimensions = function(test)
  2342. -- check graphics dimensions match window dimensions relative to dpi
  2343. local dpi = love.graphics.getDPIScale()
  2344. local gwidth, gheight = love.graphics.getPixelDimensions()
  2345. local wwidth, wheight, _ = love.window.getMode()
  2346. test:assertEquals(wwidth, gwidth/dpi, 'check graphics pixel dpi w matches window w')
  2347. test:assertEquals(wheight, gheight/dpi, 'check graphics pixel dpi h matches window h')
  2348. end
  2349. -- love.graphics.getPixelHeight
  2350. love.test.graphics.getPixelHeight = function(test)
  2351. -- check graphics height match window height relative to dpi
  2352. local dpi = love.graphics.getDPIScale()
  2353. local wwidth, wheight, _ = love.window.getMode()
  2354. test:assertEquals(wheight,love.graphics.getPixelHeight()/dpi, 'check graphics pixel dpi h matches window h')
  2355. end
  2356. -- love.graphics.getPixelWidth
  2357. love.test.graphics.getPixelWidth = function(test)
  2358. -- check graphics width match window width relative to dpi
  2359. local dpi = love.graphics.getDPIScale()
  2360. local wwidth, wheight, _ = love.window.getMode()
  2361. test:assertEquals(wwidth, love.graphics.getWidth()/dpi, 'check graphics pixel dpi w matches window w')
  2362. end
  2363. -- love.graphics.getWidth
  2364. love.test.graphics.getWidth = function(test)
  2365. -- check graphics width match window width
  2366. local wwidth, wheight, _ = love.window.getMode()
  2367. test:assertEquals(wwidth, love.graphics.getWidth(), 'check graphics w matches window w')
  2368. end
  2369. --------------------------------------------------------------------------------
  2370. --------------------------------------------------------------------------------
  2371. -------------------------------SYSTEM INFORMATION-------------------------------
  2372. --------------------------------------------------------------------------------
  2373. --------------------------------------------------------------------------------
  2374. -- love.graphics.getTextureFormats
  2375. love.test.graphics.getTextureFormats = function(test)
  2376. local formats = {
  2377. 'hdr', 'r8i', 'r8ui', 'r16i', 'r16ui', 'r32i', 'r32ui', 'rg8i', 'rg8ui',
  2378. 'rg16i', 'rg16ui', 'rg32i', 'rg32ui', 'bgra8', 'r8', 'rgba8i', 'rgba8ui',
  2379. 'rgba16i', 'rg8', 'rgba32i', 'rgba32ui', 'rgba8', 'DXT1', 'r16', 'DXT5',
  2380. 'rg16', 'BC4s', 'rgba16', 'BC5s', 'r16f', 'BC6hs', 'BC7', 'PVR1rgb2',
  2381. 'rg16f', 'PVR1rgba2', 'rgba16f', 'ETC1', 'r32f', 'ETC2rgba', 'rg32f',
  2382. 'EACr', 'rgba32f', 'EACrg', 'rgba4', 'ASTC4x4', 'ASTC5x4', 'rgb5a1',
  2383. 'ASTC6x5', 'rgb565', 'ASTC8x5', 'ASTC8x6', 'rgb10a2', 'ASTC10x5',
  2384. 'rg11b10f', 'ASTC10x8', 'ASTC10x10', 'ASTC12x10', 'ASTC12x12', 'normal',
  2385. 'srgba8', 'la8', 'ASTC10x6', 'ASTC8x8', 'ASTC6x6', 'ASTC5x5', 'EACrgs',
  2386. 'EACrs', 'ETC2rgba1', 'ETC2rgb', 'PVR1rgba4', 'PVR1rgb4', 'BC6h',
  2387. 'BC5', 'BC4', 'DXT3', 'rgba16ui', 'bgra8srgb',
  2388. 'depth16', 'depth24', 'depth32f', 'depth24stencil8', 'depth32fstencil8', 'stencil8'
  2389. }
  2390. local supported = love.graphics.getTextureFormats({ canvas = true })
  2391. test:assertNotNil(supported)
  2392. for f=1,#formats do
  2393. test:assertNotEquals(nil, supported[formats[f] ], 'expected a key for format: ' .. formats[f])
  2394. end
  2395. end
  2396. -- love.graphics.getRendererInfo
  2397. -- @NOTE hardware dependent so best can do is nil checking
  2398. love.test.graphics.getRendererInfo = function(test)
  2399. local name, version, vendor, device = love.graphics.getRendererInfo()
  2400. test:assertNotNil(name)
  2401. test:assertNotNil(version)
  2402. test:assertNotNil(vendor)
  2403. test:assertNotNil(device)
  2404. end
  2405. -- love.graphics.getStats
  2406. -- @NOTE cant really predict some of these so just nil check for most
  2407. love.test.graphics.getStats = function(test)
  2408. local stattypes = {
  2409. 'drawcalls', 'canvasswitches', 'texturememory', 'shaderswitches',
  2410. 'drawcallsbatched', 'textures', 'fonts'
  2411. }
  2412. local stats = love.graphics.getStats()
  2413. for s=1,#stattypes do
  2414. test:assertNotEquals(nil, stats[stattypes[s] ], 'expected a key for stat: ' .. stattypes[s])
  2415. end
  2416. end
  2417. -- love.graphics.getSupported
  2418. love.test.graphics.getSupported = function(test)
  2419. -- cant check values as hardware dependent but we can check the keys in the
  2420. -- table match what the documentation lists
  2421. local gfs = {
  2422. 'clampzero', 'lighten', 'glsl3', 'instancing', 'fullnpot',
  2423. 'pixelshaderhighp', 'shaderderivatives', 'indirectdraw',
  2424. 'copytexturetobuffer', 'multicanvasformats',
  2425. 'clampone', 'glsl4'
  2426. }
  2427. local features = love.graphics.getSupported()
  2428. for g=1,#gfs do
  2429. test:assertNotEquals(nil, features[gfs[g] ], 'expected a key for graphic feature: ' .. gfs[g])
  2430. end
  2431. end
  2432. -- love.graphics.getSystemLimits
  2433. love.test.graphics.getSystemLimits = function(test)
  2434. -- cant check values as hardware dependent but we can check the keys in the
  2435. -- table match what the documentation lists
  2436. local glimits = {
  2437. 'texelbuffersize', 'shaderstoragebuffersize', 'threadgroupsx',
  2438. 'threadgroupsy', 'pointsize', 'texturesize', 'texturelayers', 'volumetexturesize',
  2439. 'cubetexturesize', 'anisotropy', 'texturemsaa', 'multicanvas', 'threadgroupsz'
  2440. }
  2441. local limits = love.graphics.getSystemLimits()
  2442. for g=1,#glimits do
  2443. test:assertNotEquals(nil, limits[glimits[g] ], 'expected a key for system limit: ' .. glimits[g])
  2444. end
  2445. end
  2446. -- love.graphics.getTextureTypes
  2447. love.test.graphics.getTextureTypes = function(test)
  2448. -- cant check values as hardware dependent but we can check the keys in the
  2449. -- table match what the documentation lists
  2450. local ttypes = {
  2451. '2d', 'array', 'cube', 'volume'
  2452. }
  2453. local types = love.graphics.getTextureTypes()
  2454. for t=1,#ttypes do
  2455. test:assertNotEquals(nil, types[ttypes[t] ], 'expected a key for texture type: ' .. ttypes[t])
  2456. end
  2457. end