graphics.lua 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  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"},
  12. {name="VertexTexCoord", format="floatvec2"},
  13. {name="VertexColor", format="unorm8vec4"},
  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'},
  447. { name = 'VertexTexCoord', format = 'floatvec2'},
  448. { name = 'VertexColor', format = 'floatvec4'},
  449. { name = 'CustomValue1', format = 'floatvec2'},
  450. { name = 'CustomValue2', format = 'uint16'}
  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. in vec4 VertexPosition;
  822. 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="rgba32i"})
  839. love.graphics.push("all")
  840. love.graphics.setCanvas(canvas4)
  841. love.graphics.setShader(shader8)
  842. love.graphics.rectangle("fill", 0, 0, 16, 16)
  843. love.graphics.pop()
  844. local intimagedata = love.graphics.readbackTexture(canvas4)
  845. local imgdata3 = love.image.newImageData(16, 16, "rgba8")
  846. for y=0, 15 do
  847. for x=0, 15 do
  848. local ir, ig, ib, ia = intimagedata:getInt32(4 * 4 * (y * 16 + x), 4)
  849. imgdata3:setPixel(x, y, ir, ig, ib, ia)
  850. end
  851. end
  852. test:compareImg(imgdata3)
  853. else
  854. test:assertTrue(true, "skip shader IO test")
  855. end
  856. end
  857. -- SpriteBatch (love.graphics.newSpriteBatch)
  858. love.test.graphics.SpriteBatch = function(test)
  859. -- create batch
  860. local texture1 = love.graphics.newImage('resources/cubemap.png')
  861. local texture2 = love.graphics.newImage('resources/love.png')
  862. local quad1 = love.graphics.newQuad(32, 12, 1, 1, texture2) -- lovepink
  863. local quad2 = love.graphics.newQuad(32, 32, 1, 1, texture2) -- white
  864. local sbatch = love.graphics.newSpriteBatch(texture1, 5000)
  865. test:assertObject(sbatch)
  866. -- check initial count
  867. test:assertEquals(0, sbatch:getCount(), 'check batch size')
  868. -- check buffer size
  869. test:assertEquals(5000, sbatch:getBufferSize(), 'check batch size')
  870. -- check height/width/texture
  871. test:assertEquals(texture1:getWidth(), sbatch:getTexture():getWidth(), 'check texture match w')
  872. test:assertEquals(texture1:getHeight(), sbatch:getTexture():getHeight(), 'check texture match h')
  873. sbatch:setTexture(texture2)
  874. test:assertEquals(texture2:getWidth(), sbatch:getTexture():getWidth(), 'check texture change w')
  875. test:assertEquals(texture2:getHeight(), sbatch:getTexture():getHeight(), 'check texture change h')
  876. -- check colors
  877. local r1, g1, b1, a1 = sbatch:getColor()
  878. test:assertEquals(1, r1, 'check initial color r')
  879. test:assertEquals(1, g1, 'check initial color g')
  880. test:assertEquals(1, b1, 'check initial color b')
  881. test:assertEquals(1, a1, 'check initial color a')
  882. sbatch:setColor(1, 0, 0, 1)
  883. local r2, g2, b2, a2 = sbatch:getColor()
  884. test:assertEquals(1, r2, 'check set color r')
  885. test:assertEquals(0, g2, 'check set color g')
  886. test:assertEquals(0, b2, 'check set color b')
  887. test:assertEquals(1, a2, 'check set color a')
  888. -- check adding sprites
  889. local offset_x = 0
  890. local offset_y = 0
  891. local color = 'white'
  892. sbatch:setColor(1, 1, 1, 1)
  893. local sprites = {}
  894. for s=1,4096 do
  895. local spr = sbatch:add(quad1, offset_x, offset_y, 0, 1, 1)
  896. table.insert(sprites, {spr, offset_x, offset_y})
  897. offset_x = offset_x + 1
  898. if s % 64 == 0 then
  899. -- alternate row colors
  900. if color == 'white' then
  901. color = 'red'
  902. sbatch:setColor(1, 0, 0, 1)
  903. else
  904. color = 'white'
  905. sbatch:setColor(1, 1, 1, 1)
  906. end
  907. offset_y = offset_y + 1
  908. offset_x = 0
  909. end
  910. end
  911. test:assertEquals(4096, sbatch:getCount())
  912. -- test drawing and setting
  913. local canvas = love.graphics.newCanvas(64, 64)
  914. love.graphics.setCanvas(canvas)
  915. love.graphics.clear(0, 0, 0, 1)
  916. love.graphics.draw(sbatch, 0, 0)
  917. love.graphics.setCanvas()
  918. local imgdata1 = love.graphics.readbackTexture(canvas)
  919. test:compareImg(imgdata1)
  920. -- use set to change some sprites
  921. for s=1,2048 do
  922. sbatch:set(sprites[s][1], quad2, sprites[s][2], sprites[s][3]+1, 0, 1, 1)
  923. end
  924. love.graphics.setCanvas(canvas)
  925. love.graphics.clear(0, 0, 0, 1)
  926. love.graphics.draw(sbatch, 0, 0)
  927. love.graphics.setCanvas()
  928. local imgdata2 = love.graphics.readbackTexture(canvas)
  929. test:compareImg(imgdata2)
  930. -- set drawRange and redraw
  931. sbatch:setDrawRange(1025, 2048)
  932. love.graphics.setCanvas(canvas)
  933. love.graphics.clear(0, 0, 0, 1)
  934. love.graphics.draw(sbatch, 0, 0)
  935. love.graphics.setCanvas()
  936. local imgdata3 = love.graphics.readbackTexture(canvas)
  937. test:compareImg(imgdata3)
  938. -- clear and redraw
  939. sbatch:clear()
  940. love.graphics.setCanvas(canvas)
  941. love.graphics.clear(0, 0, 0, 1)
  942. love.graphics.draw(sbatch, 0, 0)
  943. love.graphics.setCanvas()
  944. local imgdata4 = love.graphics.readbackTexture(canvas)
  945. test:compareImg(imgdata4)
  946. -- array texture sbatch
  947. local texture3 = love.graphics.newArrayImage({
  948. 'resources/love.png',
  949. 'resources/loveinv.png'
  950. })
  951. local asbatch = love.graphics.newSpriteBatch(texture3, 4096)
  952. local quad3 = love.graphics.newQuad(32, 52, 1, 1, texture3) -- loveblue
  953. sprites = {}
  954. for s=1,4096 do
  955. local spr = asbatch:addLayer(1, quad3, 0, s, math.floor(s/64), 1, 1)
  956. table.insert(sprites, {spr, s, math.floor(s/64)})
  957. end
  958. test:assertEquals(4096, asbatch:getCount(), 'check max batch size applies')
  959. for s=1,2048 do
  960. asbatch:setLayer(sprites[s][1], 2, sprites[s][2], sprites[s][3], 0, 1, 1)
  961. end
  962. love.graphics.setCanvas(canvas)
  963. love.graphics.clear(0, 0, 0, 1)
  964. love.graphics.draw(asbatch, 0, 0)
  965. love.graphics.setCanvas()
  966. local imgdata5 = love.graphics.readbackTexture(canvas)
  967. test:compareImg(imgdata5)
  968. end
  969. -- Text (love.graphics.newTextBatch)
  970. love.test.graphics.Text = function(test)
  971. -- setup text object
  972. local font = love.graphics.newFont('resources/font.ttf', 8)
  973. local plaintext = love.graphics.newTextBatch(font, 'test')
  974. test:assertObject(plaintext)
  975. -- check height/width/dimensions
  976. test:assertEquals(font:getHeight(), plaintext:getFont():getHeight(), 'check font matches')
  977. local tw, th = plaintext:getDimensions()
  978. test:assertEquals(24, tw, 'check initial dim w')
  979. test:assertEquals(8, th, 'check initial dim h')
  980. test:assertEquals(tw, plaintext:getWidth(), 'check initial dim w')
  981. test:assertEquals(th, plaintext:getHeight(), 'check initial dim h')
  982. -- check changing text effects dimensions
  983. plaintext:add('more text', 100, 0, 0)
  984. test:assertEquals(49, plaintext:getDimensions(), 'check adding text')
  985. plaintext:set('test')
  986. test:assertEquals(24, plaintext:getDimensions(), 'check resetting text')
  987. plaintext:clear()
  988. test:assertEquals(0, plaintext:getDimensions(), 'check clearing text')
  989. -- check drawing + setting more complex text
  990. local colortext = love.graphics.newTextBatch(font, {{1, 0, 0, 1}, 'test'})
  991. test:assertObject(colortext)
  992. colortext:setf('LÖVE is an *awesome* framework you can use to make 2D games in Lua', 60, 'right')
  993. colortext:addf({{1, 1, 0}, 'overlap'}, 1000, 'left')
  994. local font2 = love.graphics.newFont('resources/font.ttf', 8)
  995. colortext:setFont(font2)
  996. local canvas = love.graphics.newCanvas(64, 64)
  997. love.graphics.setCanvas(canvas)
  998. love.graphics.draw(colortext, 0, 10)
  999. love.graphics.setCanvas()
  1000. local imgdata = love.graphics.readbackTexture(canvas)
  1001. test:compareImg(imgdata)
  1002. end
  1003. -- Video (love.graphics.newVideo)
  1004. love.test.graphics.Video = function(test)
  1005. -- create video obj
  1006. local video = love.graphics.newVideo('resources/sample.ogv')
  1007. test:assertObject(video)
  1008. -- check dimensions
  1009. local w, h = video:getDimensions()
  1010. test:assertEquals(496, w, 'check vid dim w')
  1011. test:assertEquals(502, h, 'check vid dim h')
  1012. test:assertEquals(w, video:getWidth(), 'check vid width match')
  1013. test:assertEquals(h, video:getHeight(), 'check vid height match')
  1014. -- check filters
  1015. local min1, mag1, ani1 = video:getFilter()
  1016. test:assertEquals('nearest', min1, 'check def filter min')
  1017. test:assertEquals('nearest', mag1, 'check def filter mag')
  1018. test:assertEquals(1, ani1, 'check def filter ani')
  1019. video:setFilter('linear', 'linear', 2)
  1020. local min2, mag2, ani2 = video:getFilter()
  1021. test:assertEquals('linear', min2, 'check changed filter min')
  1022. test:assertEquals('linear', mag2, 'check changed filter mag')
  1023. test:assertEquals(2, ani2, 'check changed filter ani')
  1024. -- check video playing
  1025. test:assertFalse(video:isPlaying(), 'check paused by default')
  1026. test:assertEquals(0, video:tell(), 'check 0:00 by default')
  1027. -- covered by their own obj tests in video but check returns obj
  1028. local source = video:getSource()
  1029. test:assertObject(source)
  1030. local stream = video:getStream()
  1031. test:assertObject(stream)
  1032. -- check playing / pausing / seeking states
  1033. video:play()
  1034. test:waitSeconds(0.25)
  1035. video:pause()
  1036. -- runners can be a bit funny and just not play anything sometimes
  1037. if not GITHUB_RUNNER then
  1038. test:assertRange(video:tell(), 0.2, 0.35, 'check video playing for 0.25s')
  1039. end
  1040. video:seek(0.2)
  1041. test:assertEquals(0.2, video:tell(), 'check video seeking')
  1042. video:rewind()
  1043. test:assertEquals(0, video:tell(), 'check video rewind')
  1044. video:setFilter('nearest', 'nearest', 1)
  1045. -- check actuall drawing with the vid
  1046. local canvas = love.graphics.newCanvas(500, 500)
  1047. love.graphics.setCanvas(canvas)
  1048. love.graphics.clear(1, 0, 0, 1)
  1049. love.graphics.draw(video, 0, 0)
  1050. love.graphics.setCanvas()
  1051. local imgdata = love.graphics.readbackTexture(canvas)
  1052. test:compareImg(imgdata)
  1053. end
  1054. --------------------------------------------------------------------------------
  1055. --------------------------------------------------------------------------------
  1056. ------------------------------------DRAWING-------------------------------------
  1057. --------------------------------------------------------------------------------
  1058. --------------------------------------------------------------------------------
  1059. -- love.graphics.arc
  1060. love.test.graphics.arc = function(test)
  1061. -- draw some arcs using pi format
  1062. local canvas = love.graphics.newCanvas(32, 32)
  1063. love.graphics.setCanvas(canvas)
  1064. love.graphics.clear(0, 0, 0, 1)
  1065. love.graphics.setColor(1, 1, 1, 1)
  1066. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 360 * (math.pi/180), 10)
  1067. love.graphics.arc('fill', "pie", 16, 16, 16, 270 * (math.pi/180), 45 * (math.pi/180), 10)
  1068. love.graphics.setColor(1, 0, 0, 1)
  1069. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 90 * (math.pi/180), 10)
  1070. love.graphics.setColor(1, 1, 0, 1)
  1071. love.graphics.arc('line', "pie", 16, 16, 16, 180 * (math.pi/180), 135 * (math.pi/180), 10)
  1072. love.graphics.setColor(1, 1, 1, 1)
  1073. love.graphics.setCanvas()
  1074. local imgdata1 = love.graphics.readbackTexture(canvas)
  1075. -- draw some arcs with open format
  1076. love.graphics.setCanvas(canvas)
  1077. love.graphics.clear(0, 0, 0, 1)
  1078. love.graphics.setColor(1, 1, 1, 1)
  1079. love.graphics.arc('line', "open", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  1080. love.graphics.setColor(1, 0, 0, 1)
  1081. love.graphics.arc('fill', "open", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  1082. love.graphics.setColor(1, 1, 0, 1)
  1083. love.graphics.arc('fill', "open", 16, 16, 16, 180 * (math.pi/180), 270 * (math.pi/180), 10)
  1084. love.graphics.setColor(1, 1, 1, 1)
  1085. love.graphics.setCanvas()
  1086. local imgdata2 = love.graphics.readbackTexture(canvas)
  1087. -- draw some arcs with closed format
  1088. love.graphics.setCanvas(canvas)
  1089. love.graphics.clear(0, 0, 0, 1)
  1090. love.graphics.setColor(1, 1, 1, 1)
  1091. love.graphics.arc('line', "closed", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  1092. love.graphics.setColor(1, 0, 0, 1)
  1093. love.graphics.arc('fill', "closed", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  1094. love.graphics.setColor(1, 1, 0, 1)
  1095. love.graphics.arc('line', "closed", 16, 16, 16, 180 * (math.pi/180), 90 * (math.pi/180), 10)
  1096. love.graphics.setColor(1, 1, 1, 1)
  1097. love.graphics.setCanvas()
  1098. local imgdata3 = love.graphics.readbackTexture(canvas)
  1099. if GITHUB_RUNNER and test:isOS('OS X') then
  1100. -- on macosx runners, the arcs are not drawn as accurately at low res
  1101. -- there's a couple pixels different in the curve of the arc but as we
  1102. -- are at such a low resolution I think that can be expected
  1103. -- on real hardware the test passes fine though
  1104. test:assertTrue(true, 'skip test')
  1105. else
  1106. test:compareImg(imgdata1)
  1107. test:compareImg(imgdata2)
  1108. test:compareImg(imgdata3)
  1109. end
  1110. end
  1111. -- love.graphics.circle
  1112. love.test.graphics.circle = function(test)
  1113. -- draw some circles
  1114. local canvas = love.graphics.newCanvas(32, 32)
  1115. love.graphics.setCanvas(canvas)
  1116. love.graphics.clear(0, 0, 0, 1)
  1117. love.graphics.setColor(1, 1, 1, 1)
  1118. love.graphics.circle('fill', 16, 16, 16)
  1119. love.graphics.setColor(1, 0, 0, 1)
  1120. love.graphics.circle('line', 16, 16, 16)
  1121. love.graphics.setColor(1, 1, 0, 1)
  1122. love.graphics.circle('fill', 16, 16, 8)
  1123. love.graphics.setColor(0, 1, 0, 1)
  1124. love.graphics.circle('fill', 16, 16, 4)
  1125. love.graphics.setColor(1, 1, 1, 1)
  1126. love.graphics.setCanvas()
  1127. local imgdata = love.graphics.readbackTexture(canvas)
  1128. test:compareImg(imgdata)
  1129. end
  1130. -- love.graphics.clear
  1131. love.test.graphics.clear = function(test)
  1132. local canvas = love.graphics.newCanvas(16, 16)
  1133. love.graphics.setCanvas(canvas)
  1134. love.graphics.clear(0, 0, 0, 1)
  1135. love.graphics.clear(1, 1, 0, 1)
  1136. love.graphics.setCanvas()
  1137. local imgdata = love.graphics.readbackTexture(canvas)
  1138. test:compareImg(imgdata)
  1139. end
  1140. -- love.graphics.discard
  1141. love.test.graphics.discard = function(test)
  1142. -- from the docs: "on some desktops this may do nothing"
  1143. test:skipTest('cant test this worked')
  1144. end
  1145. -- love.graphics.draw
  1146. love.test.graphics.draw = function(test)
  1147. local canvas1 = love.graphics.newCanvas(32, 32)
  1148. local canvas2 = love.graphics.newCanvas(32, 32)
  1149. local transform = love.math.newTransform( )
  1150. transform:translate(16, 0)
  1151. transform:scale(0.5, 0.5)
  1152. love.graphics.setCanvas(canvas1)
  1153. love.graphics.clear(0, 0, 0, 1)
  1154. -- img, offset
  1155. love.graphics.draw(Logo.texture, Logo.img, 0, 0, 0, 1, 1, 16, 16)
  1156. love.graphics.setCanvas()
  1157. love.graphics.setCanvas(canvas2)
  1158. love.graphics.clear(1, 0, 0, 1)
  1159. -- canvas, scale, shear, transform obj
  1160. love.graphics.draw(canvas1, 0, 0, 0, 1, 1, 0, 0, 2, 2)
  1161. love.graphics.draw(canvas1, 0, 16, 0, 0.5, 0.5)
  1162. love.graphics.draw(canvas1, 16, 16, 0, 0.5, 0.5)
  1163. love.graphics.draw(canvas1, transform)
  1164. love.graphics.setCanvas()
  1165. local imgdata = love.graphics.readbackTexture(canvas2)
  1166. test:compareImg(imgdata)
  1167. end
  1168. -- love.graphics.drawInstanced
  1169. love.test.graphics.drawInstanced = function(test)
  1170. local image = love.graphics.newImage('resources/love.png')
  1171. local vertices = {
  1172. { 0, 0, 0, 0, 1, 0, 0 },
  1173. { image:getWidth(), 0, 1, 0, 0, 1, 0 },
  1174. { image:getWidth(), image:getHeight(), 1, 1, 0, 0, 1 },
  1175. { 0, image:getHeight(), 0, 1, 1, 1, 0 },
  1176. }
  1177. local mesh = love.graphics.newMesh(vertices, 'fan')
  1178. local canvas = love.graphics.newCanvas(64, 64)
  1179. love.graphics.setCanvas(canvas)
  1180. love.graphics.clear(0, 0, 0, 1)
  1181. love.graphics.drawInstanced(mesh, 1000, 0, 0, 0, 1, 1)
  1182. love.graphics.setCanvas()
  1183. local imgdata = love.graphics.readbackTexture(canvas)
  1184. -- need 1 tolerance here just cos of the amount of colors
  1185. test.rgba_tolerance = 1
  1186. test:compareImg(imgdata)
  1187. end
  1188. -- love.graphics.drawLayer
  1189. love.test.graphics.drawLayer = function(test)
  1190. local image = love.graphics.newArrayImage({
  1191. 'resources/love.png', 'resources/loveinv.png',
  1192. 'resources/love.png', 'resources/loveinv.png'
  1193. })
  1194. local canvas = love.graphics.newCanvas(64, 64)
  1195. love.graphics.setCanvas(canvas)
  1196. love.graphics.clear(0, 0, 0, 1)
  1197. love.graphics.drawLayer(image, 1, 0, 0, 0, 1, 1)
  1198. love.graphics.drawLayer(image, 2, 32, 0, 0, 0.5, 0.5)
  1199. love.graphics.drawLayer(image, 4, 0, 32, 0, 0.5, 0.5)
  1200. love.graphics.drawLayer(image, 3, 32, 32, 0, 2, 2, 16, 16)
  1201. love.graphics.setCanvas()
  1202. local imgdata = love.graphics.readbackTexture(canvas)
  1203. test:compareImg(imgdata)
  1204. end
  1205. -- love.graphics.ellipse
  1206. love.test.graphics.ellipse = function(test)
  1207. local canvas = love.graphics.newCanvas(32, 32)
  1208. love.graphics.setCanvas(canvas)
  1209. love.graphics.clear(0, 0, 0, 1)
  1210. love.graphics.setColor(1, 0, 0, 1)
  1211. love.graphics.ellipse('fill', 16, 16, 16, 8)
  1212. love.graphics.setColor(1, 1, 0, 1)
  1213. love.graphics.ellipse('fill', 24, 24, 10, 24)
  1214. love.graphics.setColor(1, 0, 1, 1)
  1215. love.graphics.ellipse('fill', 16, 0, 8, 16)
  1216. love.graphics.setColor(1, 1, 1, 1)
  1217. love.graphics.setCanvas()
  1218. local imgdata = love.graphics.readbackTexture(canvas)
  1219. test:compareImg(imgdata)
  1220. end
  1221. -- love.graphics.flushBatch
  1222. love.test.graphics.flushBatch = function(test)
  1223. love.graphics.flushBatch()
  1224. local initial = love.graphics.getStats()['drawcalls']
  1225. local canvas = love.graphics.newCanvas(32, 32)
  1226. love.graphics.setCanvas(canvas)
  1227. love.graphics.clear(0, 0, 0, 1)
  1228. love.graphics.rectangle('fill', 0, 0, 32, 32)
  1229. love.graphics.setColor(1, 1, 1, 1)
  1230. love.graphics.setCanvas()
  1231. love.graphics.flushBatch()
  1232. local after = love.graphics.getStats()['drawcalls']
  1233. test:assertEquals(initial+1, after, 'check drawcalls increased')
  1234. end
  1235. -- love.graphics.line
  1236. love.test.graphics.line = function(test)
  1237. local canvas = love.graphics.newCanvas(16, 16)
  1238. love.graphics.setCanvas(canvas)
  1239. love.graphics.clear(0, 0, 0, 1)
  1240. love.graphics.setColor(1, 0, 0, 1)
  1241. love.graphics.line(1,1,16,1,16,16,1,16,1,1)
  1242. love.graphics.setColor(1, 1, 0, 1)
  1243. love.graphics.line({0,0,8,8,16,0,8,8,16,16,8,8,0,16})
  1244. love.graphics.setColor(1, 1, 1, 1)
  1245. love.graphics.setCanvas()
  1246. local imgdata = love.graphics.readbackTexture(canvas)
  1247. test:compareImg(imgdata)
  1248. end
  1249. -- love.graphics.points
  1250. love.test.graphics.points = function(test)
  1251. local canvas = love.graphics.newCanvas(16, 16)
  1252. love.graphics.push("all")
  1253. love.graphics.setCanvas(canvas)
  1254. love.graphics.clear(0, 0, 0, 1)
  1255. love.graphics.translate(0.5, 0.5) -- draw points at the center of pixels
  1256. love.graphics.setColor(1, 0, 0, 1)
  1257. love.graphics.points(0,0,15,0,15,15,0,15,0,0)
  1258. love.graphics.setColor(1, 1, 0, 1)
  1259. love.graphics.points({1,1,7,7,14,1,7,8,14,14,8,8,1,14,8,7})
  1260. love.graphics.pop()
  1261. local imgdata = love.graphics.readbackTexture(canvas)
  1262. test:compareImg(imgdata)
  1263. end
  1264. -- love.graphics.polygon
  1265. love.test.graphics.polygon = function(test)
  1266. local canvas = love.graphics.newCanvas(16, 16)
  1267. love.graphics.setCanvas(canvas)
  1268. love.graphics.clear(0, 0, 0, 1)
  1269. love.graphics.setColor(1, 0, 0, 1)
  1270. love.graphics.polygon("fill", 1, 1, 4, 5, 8, 10, 16, 2, 7, 3, 5, 16, 16, 16, 1, 8)
  1271. love.graphics.setColor(1, 1, 0, 1)
  1272. love.graphics.polygon("line", {2, 2, 4, 5, 3, 7, 8, 15, 12, 4, 5, 10})
  1273. love.graphics.setColor(1, 1, 1, 1)
  1274. love.graphics.setCanvas()
  1275. local imgdata = love.graphics.readbackTexture(canvas)
  1276. test:compareImg(imgdata)
  1277. end
  1278. -- love.graphics.print
  1279. love.test.graphics.print = function(test)
  1280. love.graphics.setFont(Font)
  1281. local canvas = love.graphics.newCanvas(16, 16)
  1282. love.graphics.setCanvas(canvas)
  1283. love.graphics.clear(0, 0, 0, 1)
  1284. love.graphics.setColor(1, 0, 0, 1)
  1285. love.graphics.print('love', 0, 3, 0, 1, 1, 0, 0)
  1286. love.graphics.setColor(0, 1, 0, 1)
  1287. love.graphics.print('ooo', 0, 3, 0, 2, 2, 0, 0)
  1288. love.graphics.setColor(0, 0, 1, 1)
  1289. love.graphics.print('hello', 0, 3, 90*(math.pi/180), 1, 1, 0, 8)
  1290. love.graphics.setColor(1, 1, 1, 1)
  1291. love.graphics.setCanvas()
  1292. local imgdata = love.graphics.readbackTexture(canvas)
  1293. test:compareImg(imgdata)
  1294. end
  1295. -- love.graphics.printf
  1296. love.test.graphics.printf = function(test)
  1297. love.graphics.setFont(Font)
  1298. local canvas = love.graphics.newCanvas(32, 32)
  1299. love.graphics.setCanvas(canvas)
  1300. love.graphics.clear(0, 0, 0, 1)
  1301. love.graphics.setColor(1, 0, 0, 1)
  1302. love.graphics.printf('love', 0, 0, 8, "left")
  1303. love.graphics.setColor(0, 1, 0, 1)
  1304. love.graphics.printf('love', 0, 5, 16, "right")
  1305. love.graphics.setColor(0, 0, 1, 1)
  1306. love.graphics.printf('love', 0, 7, 32, "center")
  1307. love.graphics.setColor(1, 1, 1, 1)
  1308. love.graphics.setCanvas()
  1309. local imgdata = love.graphics.readbackTexture(canvas)
  1310. test:compareImg(imgdata)
  1311. end
  1312. -- love.graphics.rectangle
  1313. love.test.graphics.rectangle = function(test)
  1314. -- setup, draw a 16x16 red rectangle with a blue central square
  1315. local canvas = love.graphics.newCanvas(16, 16)
  1316. love.graphics.setCanvas(canvas)
  1317. love.graphics.clear(0, 0, 0, 1)
  1318. love.graphics.setColor(1, 0, 0, 1)
  1319. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1320. love.graphics.setColor(0, 0, 1, 1)
  1321. love.graphics.rectangle('fill', 6, 6, 4, 4)
  1322. love.graphics.setColor(1, 1, 1, 1)
  1323. love.graphics.setCanvas()
  1324. local imgdata1 = love.graphics.readbackTexture(canvas)
  1325. test:compareImg(imgdata1)
  1326. -- clear canvas to do some line testing
  1327. love.graphics.setCanvas(canvas)
  1328. love.graphics.clear(0, 0, 0, 1)
  1329. love.graphics.setColor(1, 0, 0, 1)
  1330. love.graphics.rectangle('line', 1, 1, 15, 15) -- red border
  1331. love.graphics.setColor(0, 0, 1, 1)
  1332. love.graphics.rectangle('line', 1, 1, 2, 15) -- 3x16 left aligned blue outline
  1333. love.graphics.setColor(0, 1, 0, 1)
  1334. love.graphics.rectangle('line', 11, 1, 5, 15) -- 6x16 right aligned green outline
  1335. love.graphics.setColor(1, 1, 1, 1)
  1336. love.graphics.setCanvas()
  1337. local imgdata2 = love.graphics.readbackTexture(canvas)
  1338. test:compareImg(imgdata2)
  1339. end
  1340. --------------------------------------------------------------------------------
  1341. --------------------------------------------------------------------------------
  1342. --------------------------------OBJECT CREATION---------------------------------
  1343. --------------------------------------------------------------------------------
  1344. --------------------------------------------------------------------------------
  1345. -- love.graphics.captureScreenshot
  1346. love.test.graphics.captureScreenshot = function(test)
  1347. love.graphics.captureScreenshot('example-screenshot.png')
  1348. test:waitFrames(1)
  1349. -- need to wait until end of the frame for the screenshot
  1350. test:assertTrue(love.filesystem.exists('example-screenshot.png'))
  1351. love.filesystem.remove('example-screenshot.png')
  1352. -- test callback version
  1353. local cbdata = nil
  1354. local prevtextcommand = TextCommand
  1355. TextCommand = "Capturing screenshot"
  1356. love.graphics.captureScreenshot(function (idata)
  1357. test:assertNotEquals(nil, idata, 'check we have image data')
  1358. cbdata = idata
  1359. end)
  1360. test:waitFrames(1)
  1361. TextCommand = prevtextcommand
  1362. test:assertNotNil(cbdata)
  1363. if test:isOS('iOS', 'Android') then
  1364. -- Mobile operating systems don't let us control the window resolution,
  1365. -- so we can't compare the reference image properly.
  1366. test:assertTrue(true, 'skip test')
  1367. else
  1368. test:compareImg(cbdata)
  1369. end
  1370. end
  1371. -- love.graphics.newArrayImage
  1372. -- @NOTE this is just basic nil checking, objs have their own test method
  1373. love.test.graphics.newArrayImage = function(test)
  1374. test:assertObject(love.graphics.newArrayImage({
  1375. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  1376. }))
  1377. end
  1378. -- love.graphics.newCanvas
  1379. -- @NOTE this is just basic nil checking, objs have their own test method
  1380. love.test.graphics.newCanvas = function(test)
  1381. test:assertObject(love.graphics.newCanvas(16, 16, {
  1382. type = '2d',
  1383. format = 'normal',
  1384. readable = true,
  1385. msaa = 0,
  1386. dpiscale = 1,
  1387. mipmaps = 'none'
  1388. }))
  1389. test:assertObject(love.graphics.newCanvas(1000, 1000))
  1390. end
  1391. -- love.graphics.newCubeImage
  1392. -- @NOTE this is just basic nil checking, objs have their own test method
  1393. love.test.graphics.newCubeImage = function(test)
  1394. test:assertObject(love.graphics.newCubeImage('resources/cubemap.png', {
  1395. mipmaps = false,
  1396. linear = false
  1397. }))
  1398. end
  1399. -- love.graphics.newFont
  1400. -- @NOTE this is just basic nil checking, objs have their own test method
  1401. love.test.graphics.newFont = function(test)
  1402. test:assertObject(love.graphics.newFont('resources/font.ttf'))
  1403. test:assertObject(love.graphics.newFont('resources/font.ttf', 8, "normal", 1))
  1404. end
  1405. -- love.graphics.newImage
  1406. -- @NOTE this is just basic nil checking, objs have their own test method
  1407. love.test.graphics.newImage = function(test)
  1408. test:assertObject(love.graphics.newImage('resources/love.png', {
  1409. mipmaps = false,
  1410. linear = false,
  1411. dpiscale = 1
  1412. }))
  1413. end
  1414. -- love.graphics.newImageFont
  1415. -- @NOTE this is just basic nil checking, objs have their own test method
  1416. love.test.graphics.newImageFont = function(test)
  1417. test:assertObject(love.graphics.newImageFont('resources/love.png', 'ABCD', 1))
  1418. end
  1419. -- love.graphics.newMesh
  1420. -- @NOTE this is just basic nil checking, objs have their own test method
  1421. love.test.graphics.newMesh = function(test)
  1422. test:assertObject(love.graphics.newMesh({{1, 1, 0, 0, 1, 1, 1, 1}}, 'fan', 'dynamic'))
  1423. end
  1424. -- love.graphics.newParticleSystem
  1425. -- @NOTE this is just basic nil checking, objs have their own test method
  1426. love.test.graphics.newParticleSystem = function(test)
  1427. local imgdata = love.graphics.newImage('resources/love.png')
  1428. test:assertObject(love.graphics.newParticleSystem(imgdata, 1000))
  1429. end
  1430. -- love.graphics.newQuad
  1431. -- @NOTE this is just basic nil checking, objs have their own test method
  1432. love.test.graphics.newQuad = function(test)
  1433. local imgdata = love.graphics.newImage('resources/love.png')
  1434. test:assertObject(love.graphics.newQuad(0, 0, 16, 16, imgdata))
  1435. end
  1436. -- love.graphics.newShader
  1437. -- @NOTE this is just basic nil checking, objs have their own test method
  1438. love.test.graphics.newShader = function(test)
  1439. local pixelcode = [[
  1440. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  1441. vec4 texturecolor = Texel(tex, texture_coords);
  1442. return texturecolor * color;
  1443. }
  1444. ]]
  1445. local vertexcode = [[
  1446. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  1447. return transform_projection * vertex_position;
  1448. }
  1449. ]]
  1450. test:assertObject(love.graphics.newShader(pixelcode, vertexcode))
  1451. end
  1452. -- love.graphics.newSpriteBatch
  1453. -- @NOTE this is just basic nil checking, objs have their own test method
  1454. love.test.graphics.newSpriteBatch = function(test)
  1455. local imgdata = love.graphics.newImage('resources/love.png')
  1456. test:assertObject(love.graphics.newSpriteBatch(imgdata, 1000))
  1457. end
  1458. -- love.graphics.newTextBatch
  1459. -- @NOTE this is just basic nil checking, objs have their own test method
  1460. love.test.graphics.newTextBatch = function(test)
  1461. local font = love.graphics.newFont('resources/font.ttf')
  1462. test:assertObject(love.graphics.newTextBatch(font, 'helloworld'))
  1463. end
  1464. -- love.graphics.newTexture
  1465. -- @NOTE this is just basic nil checking, objs have their own test method
  1466. love.test.graphics.newTexture = function(test)
  1467. local imgdata = love.image.newImageData('resources/love.png')
  1468. test:assertObject(love.graphics.newTexture(imgdata))
  1469. end
  1470. -- love.graphics.newVideo
  1471. -- @NOTE this is just basic nil checking, objs have their own test method
  1472. love.test.graphics.newVideo = function(test)
  1473. test:assertObject(love.graphics.newVideo('resources/sample.ogv', {
  1474. audio = false,
  1475. dpiscale = 1
  1476. }))
  1477. end
  1478. -- love.graphics.newVolumeImage
  1479. -- @NOTE this is just basic nil checking, objs have their own test method
  1480. love.test.graphics.newVolumeImage = function(test)
  1481. test:assertObject(love.graphics.newVolumeImage({
  1482. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  1483. }, {
  1484. mipmaps = false,
  1485. linear = false
  1486. }))
  1487. end
  1488. -- love.graphics.validateShader
  1489. love.test.graphics.validateShader = function(test)
  1490. local pixelcode = [[
  1491. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  1492. vec4 texturecolor = Texel(tex, texture_coords);
  1493. return texturecolor * color;
  1494. }
  1495. ]]
  1496. local vertexcode = [[
  1497. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  1498. return transform_projection * vertex_position;
  1499. }
  1500. ]]
  1501. -- check made up code first
  1502. local status, _ = love.graphics.validateShader(true, 'nothing here', 'or here')
  1503. test:assertFalse(status, 'check invalid shader code')
  1504. -- check real code
  1505. status, _ = love.graphics.validateShader(true, pixelcode, vertexcode)
  1506. test:assertTrue(status, 'check valid shader code')
  1507. end
  1508. --------------------------------------------------------------------------------
  1509. --------------------------------------------------------------------------------
  1510. ---------------------------------GRAPHICS STATE---------------------------------
  1511. --------------------------------------------------------------------------------
  1512. --------------------------------------------------------------------------------
  1513. -- love.graphics.getBackgroundColor
  1514. love.test.graphics.getBackgroundColor = function(test)
  1515. -- check default bg is black
  1516. local r, g, b, a = love.graphics.getBackgroundColor()
  1517. test:assertEquals(0, r, 'check default background r')
  1518. test:assertEquals(0, g, 'check default background g')
  1519. test:assertEquals(0, b, 'check default background b')
  1520. test:assertEquals(1, a, 'check default background a')
  1521. -- check set value returns correctly
  1522. love.graphics.setBackgroundColor(1, 1, 1, 0)
  1523. r, g, b, a = love.graphics.getBackgroundColor()
  1524. test:assertEquals(1, r, 'check updated background r')
  1525. test:assertEquals(1, g, 'check updated background g')
  1526. test:assertEquals(1, b, 'check updated background b')
  1527. test:assertEquals(0, a, 'check updated background a')
  1528. love.graphics.setBackgroundColor(0, 0, 0, 1) -- reset
  1529. end
  1530. -- love.graphics.getBlendMode
  1531. love.test.graphics.getBlendMode = function(test)
  1532. -- check default blend mode
  1533. local mode, alphamode = love.graphics.getBlendMode()
  1534. test:assertEquals('alpha', mode, 'check default blend mode')
  1535. test:assertEquals('alphamultiply', alphamode, 'check default alpha blend')
  1536. -- check set mode returns correctly
  1537. love.graphics.setBlendMode('add', 'premultiplied')
  1538. mode, alphamode = love.graphics.getBlendMode()
  1539. test:assertEquals('add', mode, 'check changed blend mode')
  1540. test:assertEquals('premultiplied', alphamode, 'check changed alpha blend')
  1541. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  1542. end
  1543. -- love.graphics.getCanvas
  1544. love.test.graphics.getCanvas = function(test)
  1545. -- by default should be nil if drawing to real screen
  1546. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  1547. -- should return not nil when we target a canvas
  1548. local canvas = love.graphics.newCanvas(16, 16)
  1549. love.graphics.setCanvas(canvas)
  1550. test:assertObject(love.graphics.getCanvas())
  1551. love.graphics.setCanvas()
  1552. end
  1553. -- love.graphics.getColor
  1554. love.test.graphics.getColor = function(test)
  1555. -- by default should be white
  1556. local r, g, b, a = love.graphics.getColor()
  1557. test:assertEquals(1, r, 'check default color r')
  1558. test:assertEquals(1, g, 'check default color g')
  1559. test:assertEquals(1, b, 'check default color b')
  1560. test:assertEquals(1, a, 'check default color a')
  1561. -- check set color is returned correctly
  1562. love.graphics.setColor(0, 0, 0, 0)
  1563. r, g, b, a = love.graphics.getColor()
  1564. test:assertEquals(0, r, 'check changed color r')
  1565. test:assertEquals(0, g, 'check changed color g')
  1566. test:assertEquals(0, b, 'check changed color b')
  1567. test:assertEquals(0, a, 'check changed color a')
  1568. love.graphics.setColor(1, 1, 1, 1) -- reset
  1569. end
  1570. -- love.graphics.getColorMask
  1571. love.test.graphics.getColorMask = function(test)
  1572. -- by default should all be active
  1573. local r, g, b, a = love.graphics.getColorMask()
  1574. test:assertTrue(r, 'check default color mask r')
  1575. test:assertTrue(g, 'check default color mask g')
  1576. test:assertTrue(b, 'check default color mask b')
  1577. test:assertTrue(a, 'check default color mask a')
  1578. -- check set color mask is returned correctly
  1579. love.graphics.setColorMask(false, false, true, false)
  1580. r, g, b, a = love.graphics.getColorMask()
  1581. test:assertFalse(r, 'check changed color mask r')
  1582. test:assertFalse(g, 'check changed color mask g')
  1583. test:assertTrue( b, 'check changed color mask b')
  1584. test:assertFalse(a, 'check changed color mask a')
  1585. love.graphics.setColorMask(true, true, true, true) -- reset
  1586. end
  1587. -- love.graphics.getDefaultFilter
  1588. love.test.graphics.getDefaultFilter = function(test)
  1589. -- we set this already for testsuite so we know what it should be
  1590. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  1591. test:assertEquals('nearest', min, 'check default filter min')
  1592. test:assertEquals('nearest', mag, 'check default filter mag')
  1593. test:assertEquals(1, anisotropy, 'check default filter mag')
  1594. end
  1595. -- love.graphics.getDepthMode
  1596. love.test.graphics.getDepthMode = function(test)
  1597. -- by default should be always/write
  1598. local comparemode, write = love.graphics.getDepthMode()
  1599. test:assertEquals('always', comparemode, 'check default compare depth')
  1600. test:assertFalse(write, 'check default depth buffer write')
  1601. end
  1602. -- love.graphics.getFont
  1603. love.test.graphics.getFont = function(test)
  1604. test:assertObject(love.graphics.getFont())
  1605. end
  1606. -- love.graphics.getFrontFaceWinding
  1607. love.test.graphics.getFrontFaceWinding = function(test)
  1608. -- check default winding
  1609. test:assertEquals('ccw', love.graphics.getFrontFaceWinding())
  1610. -- check setting value changes it correctly
  1611. love.graphics.setFrontFaceWinding('cw')
  1612. test:assertEquals('cw', love.graphics.getFrontFaceWinding())
  1613. love.graphics.setFrontFaceWinding('ccw') -- reset
  1614. end
  1615. -- love.graphics.getLineJoin
  1616. love.test.graphics.getLineJoin = function(test)
  1617. -- check default line join
  1618. test:assertEquals('miter', love.graphics.getLineJoin())
  1619. -- check set value returned correctly
  1620. love.graphics.setLineJoin('none')
  1621. test:assertEquals('none', love.graphics.getLineJoin())
  1622. love.graphics.setLineJoin('miter') -- reset
  1623. end
  1624. -- love.graphics.getLineStyle
  1625. love.test.graphics.getLineStyle = function(test)
  1626. -- we know this should be as testsuite sets it!
  1627. test:assertEquals('rough', love.graphics.getLineStyle())
  1628. -- check set value returned correctly
  1629. love.graphics.setLineStyle('smooth')
  1630. test:assertEquals('smooth', love.graphics.getLineStyle())
  1631. love.graphics.setLineStyle('rough') -- reset
  1632. end
  1633. -- love.graphics.getLineWidth
  1634. love.test.graphics.getLineWidth = function(test)
  1635. -- we know this should be as testsuite sets it!
  1636. test:assertEquals(1, love.graphics.getLineWidth())
  1637. -- check set value returned correctly
  1638. love.graphics.setLineWidth(10)
  1639. test:assertEquals(10, love.graphics.getLineWidth())
  1640. love.graphics.setLineWidth(1) -- reset
  1641. end
  1642. -- love.graphics.getMeshCullMode
  1643. love.test.graphics.getMeshCullMode = function(test)
  1644. -- get default mesh culling
  1645. test:assertEquals('none', love.graphics.getMeshCullMode())
  1646. -- check set value returned correctly
  1647. love.graphics.setMeshCullMode('front')
  1648. test:assertEquals('front', love.graphics.getMeshCullMode())
  1649. love.graphics.setMeshCullMode('back') -- reset
  1650. end
  1651. -- love.graphics.getPointSize
  1652. love.test.graphics.getPointSize = function(test)
  1653. -- get default point size
  1654. test:assertEquals(1, love.graphics.getPointSize())
  1655. -- check set value returned correctly
  1656. love.graphics.setPointSize(10)
  1657. test:assertEquals(10, love.graphics.getPointSize())
  1658. love.graphics.setPointSize(1) -- reset
  1659. end
  1660. -- love.graphics.getScissor
  1661. love.test.graphics.getScissor = function(test)
  1662. -- should be no scissor atm
  1663. local x, y, w, h = love.graphics.getScissor()
  1664. test:assertEquals(nil, x, 'check no scissor')
  1665. test:assertEquals(nil, y, 'check no scissor')
  1666. test:assertEquals(nil, w, 'check no scissor')
  1667. test:assertEquals(nil, h, 'check no scissor')
  1668. -- check set value returned correctly
  1669. love.graphics.setScissor(0, 0, 16, 16)
  1670. x, y, w, h = love.graphics.getScissor()
  1671. test:assertEquals(0, x, 'check scissor set')
  1672. test:assertEquals(0, y, 'check scissor set')
  1673. test:assertEquals(16, w, 'check scissor set')
  1674. test:assertEquals(16, h, 'check scissor set')
  1675. love.graphics.setScissor() -- reset
  1676. end
  1677. -- love.graphics.getShader
  1678. love.test.graphics.getShader = function(test)
  1679. -- should be no shader active
  1680. test:assertEquals(nil, love.graphics.getShader(), 'check no active shader')
  1681. end
  1682. -- love.graphics.getStackDepth
  1683. love.test.graphics.getStackDepth = function(test)
  1684. -- by default should be none
  1685. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  1686. -- now add 3
  1687. love.graphics.push()
  1688. love.graphics.push()
  1689. love.graphics.push()
  1690. test:assertEquals(3, love.graphics.getStackDepth(), 'check 3 transforms in stack')
  1691. -- now remove 2
  1692. love.graphics.pop()
  1693. love.graphics.pop()
  1694. test:assertEquals(1, love.graphics.getStackDepth(), 'check 1 transforms in stack')
  1695. -- now back to 0
  1696. love.graphics.pop()
  1697. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  1698. end
  1699. -- love.graphics.getStencilState
  1700. love.test.graphics.getStencilState = function(test)
  1701. -- check default vals
  1702. local action, comparemode, value = love.graphics.getStencilState( )
  1703. test:assertEquals('keep', action, 'check default stencil action')
  1704. test:assertEquals('always', comparemode, 'check default stencil compare')
  1705. test:assertEquals(0, value, 'check default stencil value')
  1706. -- check set stencil values is returned
  1707. love.graphics.setStencilState('replace', 'less', 255)
  1708. local action, comparemode, value = love.graphics.getStencilState()
  1709. test:assertEquals('replace', action, 'check changed stencil action')
  1710. test:assertEquals('less', comparemode, 'check changed stencil compare')
  1711. test:assertEquals(255, value, 'check changed stencil value')
  1712. love.graphics.setStencilState() -- reset
  1713. end
  1714. -- love.graphics.intersectScissor
  1715. love.test.graphics.intersectScissor = function(test)
  1716. -- make a scissor for the left half, then interset to make the top half
  1717. -- then we should be able to fill the canvas with red and only top 4x4 is filled
  1718. local canvas = love.graphics.newCanvas(16, 16)
  1719. love.graphics.setCanvas(canvas)
  1720. love.graphics.clear(0, 0, 0, 1)
  1721. love.graphics.origin()
  1722. love.graphics.setScissor(0, 0, 8, 16)
  1723. love.graphics.intersectScissor(0, 0, 4, 4)
  1724. love.graphics.setColor(1, 0, 0, 1)
  1725. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1726. love.graphics.setColor(1, 1, 1, 1)
  1727. love.graphics.setScissor()
  1728. love.graphics.setCanvas()
  1729. local imgdata = love.graphics.readbackTexture(canvas)
  1730. test:compareImg(imgdata)
  1731. end
  1732. -- love.graphics.isActive
  1733. love.test.graphics.isActive = function(test)
  1734. test:assertTrue(love.graphics.isActive(), 'check graphics is active') -- i mean if you got this far
  1735. end
  1736. -- love.graphics.isGammaCorrect
  1737. love.test.graphics.isGammaCorrect = function(test)
  1738. -- we know the config so know this is false
  1739. test:assertNotNil(love.graphics.isGammaCorrect())
  1740. end
  1741. -- love.graphics.isWireframe
  1742. love.test.graphics.isWireframe = function(test)
  1743. local name, version, vendor, device = love.graphics.getRendererInfo()
  1744. if string.match(name, 'OpenGL ES') then
  1745. test:skipTest('Wireframe not supported on OpenGL ES')
  1746. else
  1747. -- check off by default
  1748. test:assertFalse(love.graphics.isWireframe(), 'check no wireframe by default')
  1749. -- check on when enabled
  1750. love.graphics.setWireframe(true)
  1751. test:assertTrue(love.graphics.isWireframe(), 'check wireframe is set')
  1752. love.graphics.setWireframe(false) -- reset
  1753. end
  1754. end
  1755. -- love.graphics.reset
  1756. love.test.graphics.reset = function(test)
  1757. -- reset should reset current canvas and any colors/scissor
  1758. local canvas = love.graphics.newCanvas(16, 16)
  1759. love.graphics.setBackgroundColor(0, 0, 1, 1)
  1760. love.graphics.setColor(0, 1, 0, 1)
  1761. love.graphics.setCanvas(canvas)
  1762. love.graphics.reset()
  1763. local r, g, b, a = love.graphics.getBackgroundColor()
  1764. test:assertEquals(1, r+g+b+a, 'check background reset')
  1765. r, g, b, a = love.graphics.getColor()
  1766. test:assertEquals(4, r+g+b+a, 'check color reset')
  1767. test:assertEquals(nil, love.graphics.getCanvas(), 'check canvas reset')
  1768. love.graphics.setDefaultFilter("nearest", "nearest")
  1769. love.graphics.setLineStyle('rough')
  1770. love.graphics.setPointSize(1)
  1771. love.graphics.setLineWidth(1)
  1772. end
  1773. -- love.graphics.setBackgroundColor
  1774. love.test.graphics.setBackgroundColor = function(test)
  1775. -- check background is set
  1776. love.graphics.setBackgroundColor(1, 0, 0, 1)
  1777. local r, g, b, a = love.graphics.getBackgroundColor()
  1778. test:assertEquals(1, r, 'check set bg r')
  1779. test:assertEquals(0, g, 'check set bg g')
  1780. test:assertEquals(0, b, 'check set bg b')
  1781. test:assertEquals(1, a, 'check set bg a')
  1782. love.graphics.setBackgroundColor(0, 0, 0, 1)
  1783. end
  1784. -- love.graphics.setBlendMode
  1785. love.test.graphics.setBlendMode = function(test)
  1786. -- create fully white canvas, then draw diff. pixels through blendmodes
  1787. local canvas = love.graphics.newCanvas(16, 16)
  1788. love.graphics.setCanvas(canvas)
  1789. love.graphics.clear(0.5, 0.5, 0.5, 1)
  1790. love.graphics.setBlendMode('add', 'alphamultiply')
  1791. love.graphics.setColor(1, 0, 0, 1)
  1792. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1793. love.graphics.setBlendMode('subtract', 'alphamultiply')
  1794. love.graphics.setColor(1, 1, 1, 0.5)
  1795. love.graphics.rectangle('fill', 15, 0, 1, 1)
  1796. love.graphics.setBlendMode('multiply', 'premultiplied')
  1797. love.graphics.setColor(0, 1, 0, 1)
  1798. love.graphics.rectangle('fill', 15, 15, 1, 1)
  1799. love.graphics.setBlendMode('replace', 'premultiplied')
  1800. love.graphics.setColor(0, 0, 1, 0.5)
  1801. love.graphics.rectangle('fill', 0, 15, 1, 1)
  1802. love.graphics.setColor(1, 1, 1, 1)
  1803. love.graphics.setCanvas()
  1804. local imgdata = love.graphics.readbackTexture(canvas)
  1805. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  1806. -- need 1rgba tolerance here on some machines
  1807. test.rgba_tolerance = 1
  1808. test:compareImg(imgdata)
  1809. end
  1810. -- love.graphics.setCanvas
  1811. love.test.graphics.setCanvas = function(test)
  1812. -- make 2 canvas, set to each, draw one to the other, check output
  1813. local canvas1 = love.graphics.newCanvas(16, 16)
  1814. local canvas2 = love.graphics.newCanvas(16, 16)
  1815. love.graphics.setCanvas(canvas1)
  1816. test:assertEquals(canvas1, love.graphics.getCanvas(), 'check canvas 1 set')
  1817. love.graphics.clear(1, 0, 0, 1)
  1818. love.graphics.setCanvas(canvas2)
  1819. test:assertEquals(canvas2, love.graphics.getCanvas(), 'check canvas 2 set')
  1820. love.graphics.clear(0, 0, 0, 1)
  1821. love.graphics.draw(canvas1, 0, 0)
  1822. love.graphics.setCanvas()
  1823. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  1824. local imgdata = love.graphics.readbackTexture(canvas2)
  1825. test:compareImg(imgdata)
  1826. end
  1827. -- love.graphics.setColor
  1828. love.test.graphics.setColor = function(test)
  1829. -- set colors, draw rect, check color
  1830. local canvas = love.graphics.newCanvas(16, 16)
  1831. love.graphics.setCanvas(canvas)
  1832. love.graphics.clear(0, 0, 0, 1)
  1833. love.graphics.setColor(1, 0, 0, 1)
  1834. local r, g, b, a = love.graphics.getColor()
  1835. test:assertEquals(1, r, 'check r set')
  1836. test:assertEquals(0, g, 'check g set')
  1837. test:assertEquals(0, b, 'check b set')
  1838. test:assertEquals(1, a, 'check a set')
  1839. love.graphics.rectangle('fill', 0, 0, 16, 1)
  1840. love.graphics.setColor(1, 1, 0, 1)
  1841. love.graphics.rectangle('fill', 0, 1, 16, 1)
  1842. love.graphics.setColor(0, 1, 0, 0.5)
  1843. love.graphics.rectangle('fill', 0, 2, 16, 1)
  1844. love.graphics.setColor(0, 0, 1, 1)
  1845. love.graphics.rectangle('fill', 0, 3, 16, 1)
  1846. love.graphics.setColor(1, 1, 1, 1)
  1847. love.graphics.setCanvas()
  1848. local imgdata = love.graphics.readbackTexture(canvas)
  1849. test:compareImg(imgdata)
  1850. end
  1851. -- love.graphics.setColorMask
  1852. love.test.graphics.setColorMask = function(test)
  1853. -- set mask, draw stuff, check output pixels
  1854. local canvas = love.graphics.newCanvas(16, 16)
  1855. love.graphics.setCanvas(canvas)
  1856. love.graphics.clear(0, 0, 0, 1)
  1857. -- mask off blue
  1858. love.graphics.setColorMask(true, true, false, true)
  1859. local r, g, b, a = love.graphics.getColorMask()
  1860. test:assertEquals(r, true, 'check r mask')
  1861. test:assertEquals(g, true, 'check g mask')
  1862. test:assertEquals(b, false, 'check b mask')
  1863. test:assertEquals(a, true, 'check a mask')
  1864. -- draw "black" which should then turn to yellow
  1865. love.graphics.setColor(1, 1, 1, 1)
  1866. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1867. love.graphics.setColorMask(true, true, true, true)
  1868. love.graphics.setCanvas()
  1869. local imgdata = love.graphics.readbackTexture(canvas)
  1870. test:compareImg(imgdata)
  1871. end
  1872. -- love.graphics.setDefaultFilter
  1873. love.test.graphics.setDefaultFilter = function(test)
  1874. -- check setting filter val works
  1875. love.graphics.setDefaultFilter('linear', 'linear', 1)
  1876. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  1877. test:assertEquals('linear', min, 'check default filter min')
  1878. test:assertEquals('linear', mag, 'check default filter mag')
  1879. test:assertEquals(1, anisotropy, 'check default filter mag')
  1880. love.graphics.setDefaultFilter('nearest', 'nearest', 1) -- reset
  1881. end
  1882. -- love.graphics.setDepthMode
  1883. love.test.graphics.setDepthMode = function(test)
  1884. -- check documented modes are valid
  1885. local comparemode, write = love.graphics.getDepthMode()
  1886. local modes = {
  1887. 'equal', 'notequal', 'less', 'lequal', 'gequal',
  1888. 'greater', 'never', 'always'
  1889. }
  1890. for m=1,#modes do
  1891. love.graphics.setDepthMode(modes[m], true)
  1892. test:assertEquals(modes[m], love.graphics.getDepthMode(), 'check depth mode ' .. modes[m] .. ' set')
  1893. end
  1894. love.graphics.setDepthMode(comparemode, write)
  1895. -- @TODO better graphics drawing specific test
  1896. end
  1897. -- love.graphics.setFont
  1898. love.test.graphics.setFont = function(test)
  1899. -- set font doesnt return anything so draw with the test font
  1900. local canvas = love.graphics.newCanvas(16, 16)
  1901. love.graphics.setFont(Font)
  1902. love.graphics.setCanvas(canvas)
  1903. love.graphics.clear(0, 0, 0, 1)
  1904. love.graphics.setColor(1, 0, 0, 1)
  1905. love.graphics.print('love', 0, 3)
  1906. love.graphics.setColor(1, 1, 1, 1)
  1907. love.graphics.setCanvas()
  1908. local imgdata = love.graphics.readbackTexture(canvas)
  1909. test:compareImg(imgdata)
  1910. end
  1911. -- love.graphics.setFrontFaceWinding
  1912. love.test.graphics.setFrontFaceWinding = function(test)
  1913. -- check documented modes are valid
  1914. local original = love.graphics.getFrontFaceWinding()
  1915. love.graphics.setFrontFaceWinding('cw')
  1916. test:assertEquals('cw', love.graphics.getFrontFaceWinding(), 'check ffw cw set')
  1917. love.graphics.setFrontFaceWinding('ccw')
  1918. test:assertEquals('ccw', love.graphics.getFrontFaceWinding(), 'check ffw ccw set')
  1919. love.graphics.setFrontFaceWinding(original)
  1920. -- @TODO better graphics drawing specific test
  1921. local shader = love.graphics.newShader[[
  1922. vec4 effect(vec4 c, Image tex, vec2 tc, vec2 pc) {
  1923. return gl_FrontFacing ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0);
  1924. }
  1925. ]]
  1926. local dummyimg = love.graphics.newImage(love.image.newImageData(1, 1))
  1927. local canvas = love.graphics.newCanvas(16, 16)
  1928. love.graphics.push("all")
  1929. love.graphics.setCanvas(canvas)
  1930. love.graphics.setShader(shader)
  1931. love.graphics.draw(dummyimg, 0, 0, 0, 16, 16)
  1932. love.graphics.pop()
  1933. local imgdata = love.graphics.readbackTexture(canvas)
  1934. test:compareImg(imgdata)
  1935. end
  1936. -- love.graphics.setLineJoin
  1937. love.test.graphics.setLineJoin = function(test)
  1938. local canvas = love.graphics.newCanvas(16, 16)
  1939. love.graphics.setFont(Font)
  1940. love.graphics.setCanvas(canvas)
  1941. love.graphics.clear(0, 0, 0, 1)
  1942. local line = {0,1,8,1,8,8}
  1943. love.graphics.setLineStyle('rough')
  1944. love.graphics.setLineWidth(2)
  1945. love.graphics.setColor(1, 0, 0)
  1946. love.graphics.setLineJoin('bevel')
  1947. love.graphics.line(line)
  1948. love.graphics.translate(0, 4)
  1949. love.graphics.setColor(1, 1, 0)
  1950. love.graphics.setLineJoin('none')
  1951. love.graphics.line(line)
  1952. love.graphics.translate(0, 4)
  1953. love.graphics.setColor(0, 0, 1)
  1954. love.graphics.setLineJoin('miter')
  1955. love.graphics.line(line)
  1956. love.graphics.setColor(1, 1, 1)
  1957. love.graphics.setLineWidth(1)
  1958. love.graphics.origin()
  1959. love.graphics.setCanvas()
  1960. local imgdata = love.graphics.readbackTexture(canvas)
  1961. test:compareImg(imgdata)
  1962. end
  1963. -- love.graphics.setLineStyle
  1964. love.test.graphics.setLineStyle = function(test)
  1965. local canvas = love.graphics.newCanvas(16, 16)
  1966. love.graphics.setFont(Font)
  1967. love.graphics.setCanvas(canvas)
  1968. love.graphics.clear(0, 0, 0, 1)
  1969. love.graphics.setColor(1, 0, 0)
  1970. local line = {0,1,16,1}
  1971. love.graphics.setLineStyle('rough')
  1972. love.graphics.line(line)
  1973. love.graphics.translate(0, 4)
  1974. love.graphics.setLineStyle('smooth')
  1975. love.graphics.line(line)
  1976. love.graphics.setLineStyle('rough')
  1977. love.graphics.setColor(1, 1, 1)
  1978. love.graphics.origin()
  1979. love.graphics.setCanvas()
  1980. local imgdata = love.graphics.readbackTexture(canvas)
  1981. -- linux runner needs a 1/255 tolerance for the blend between a rough line + bg
  1982. if GITHUB_RUNNER and test:isOS('Linux') then
  1983. test.rgba_tolerance = 1
  1984. end
  1985. test:compareImg(imgdata)
  1986. end
  1987. -- love.graphics.setLineWidth
  1988. love.test.graphics.setLineWidth = function(test)
  1989. local canvas = love.graphics.newCanvas(16, 16)
  1990. love.graphics.setFont(Font)
  1991. love.graphics.setCanvas(canvas)
  1992. love.graphics.clear(0, 0, 0, 1)
  1993. local line = {0,1,8,1,8,8}
  1994. love.graphics.setColor(1, 0, 0)
  1995. love.graphics.setLineWidth(2)
  1996. love.graphics.line(line)
  1997. love.graphics.translate(0, 4)
  1998. love.graphics.setColor(1, 1, 0)
  1999. love.graphics.setLineWidth(3)
  2000. love.graphics.line(line)
  2001. love.graphics.translate(0, 4)
  2002. love.graphics.setColor(0, 0, 1)
  2003. love.graphics.setLineWidth(4)
  2004. love.graphics.line(line)
  2005. love.graphics.setColor(1, 1, 1)
  2006. love.graphics.setLineWidth(1)
  2007. love.graphics.origin()
  2008. love.graphics.setCanvas()
  2009. local imgdata = love.graphics.readbackTexture(canvas)
  2010. test:compareImg(imgdata)
  2011. end
  2012. -- love.graphics.setMeshCullMode
  2013. love.test.graphics.setMeshCullMode = function(test)
  2014. -- check documented modes are valid
  2015. local original = love.graphics.getMeshCullMode()
  2016. local modes = {'back', 'front', 'none'}
  2017. for m=1,#modes do
  2018. love.graphics.setMeshCullMode(modes[m])
  2019. test:assertEquals(modes[m], love.graphics.getMeshCullMode(), 'check mesh cull mode ' .. modes[m] .. ' was set')
  2020. end
  2021. love.graphics.setMeshCullMode(original)
  2022. -- @TODO better graphics drawing specific test
  2023. end
  2024. -- love.graphics.setScissor
  2025. love.test.graphics.setScissor = function(test)
  2026. -- make a scissor for the left half
  2027. -- then we should be able to fill the canvas with red and only left is filled
  2028. local canvas = love.graphics.newCanvas(16, 16)
  2029. love.graphics.setCanvas(canvas)
  2030. love.graphics.clear(0, 0, 0, 1)
  2031. love.graphics.origin()
  2032. love.graphics.setScissor(0, 0, 8, 16)
  2033. love.graphics.setColor(1, 0, 0, 1)
  2034. love.graphics.rectangle('fill', 0, 0, 16, 16)
  2035. love.graphics.setColor(1, 1, 1, 1)
  2036. love.graphics.setScissor()
  2037. love.graphics.setCanvas()
  2038. local imgdata = love.graphics.readbackTexture(canvas)
  2039. test:compareImg(imgdata)
  2040. end
  2041. -- love.graphics.setShader
  2042. love.test.graphics.setShader = function(test)
  2043. -- make a shader that will only ever draw yellow
  2044. local pixelcode = [[
  2045. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  2046. vec4 texturecolor = Texel(tex, texture_coords);
  2047. return vec4(1.0,1.0,0.0,1.0);
  2048. }
  2049. ]]
  2050. local vertexcode = [[
  2051. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  2052. return transform_projection * vertex_position;
  2053. }
  2054. ]]
  2055. local shader = love.graphics.newShader(pixelcode, vertexcode)
  2056. local canvas = love.graphics.newCanvas(16, 16)
  2057. love.graphics.setCanvas(canvas)
  2058. love.graphics.clear(0, 0, 0, 1)
  2059. love.graphics.setShader(shader)
  2060. -- draw red rectangle
  2061. love.graphics.setColor(1, 0, 0, 1)
  2062. love.graphics.rectangle('fill', 0, 0, 16, 16)
  2063. love.graphics.setShader()
  2064. love.graphics.setColor(1, 1, 1, 1)
  2065. love.graphics.setCanvas()
  2066. local imgdata = love.graphics.readbackTexture(canvas)
  2067. test:compareImg(imgdata)
  2068. end
  2069. -- love.graphics.setStencilState
  2070. love.test.graphics.setStencilState = function(test)
  2071. local canvas = love.graphics.newCanvas(16, 16)
  2072. love.graphics.setCanvas({canvas, stencil=true})
  2073. love.graphics.clear(0, 0, 0, 1)
  2074. love.graphics.setStencilState('replace', 'always', 1)
  2075. love.graphics.circle('fill', 8, 8, 6)
  2076. love.graphics.setStencilState('keep', 'greater', 0)
  2077. love.graphics.setColor(1, 0, 0, 1)
  2078. love.graphics.rectangle('fill', 0, 0, 16, 16)
  2079. love.graphics.setColor(1, 1, 1, 1)
  2080. love.graphics.setStencilState()
  2081. love.graphics.setCanvas()
  2082. local imgdata = love.graphics.readbackTexture(canvas)
  2083. test:compareImg(imgdata)
  2084. end
  2085. -- love.graphics.setWireframe
  2086. love.test.graphics.setWireframe = function(test)
  2087. local name, version, vendor, device = love.graphics.getRendererInfo()
  2088. if string.match(name, 'OpenGL ES') then
  2089. test:skipTest('Wireframe not supported on OpenGL ES')
  2090. else
  2091. -- check wireframe outlines
  2092. love.graphics.setWireframe(true)
  2093. local canvas = love.graphics.newCanvas(16, 16)
  2094. love.graphics.setCanvas(canvas)
  2095. love.graphics.clear(0, 0, 0, 1)
  2096. love.graphics.setColor(1, 1, 0, 1)
  2097. love.graphics.rectangle('fill', 2, 2, 13, 13)
  2098. love.graphics.setColor(1, 1, 1, 1)
  2099. love.graphics.setCanvas()
  2100. love.graphics.setWireframe(false)
  2101. local imgdata = love.graphics.readbackTexture(canvas)
  2102. -- on macOS runners wireframes are drawn 1px off from the target
  2103. if GITHUB_RUNNER and test:isOS('OS X') then
  2104. test.pixel_tolerance = 1
  2105. end
  2106. test:compareImg(imgdata)
  2107. end
  2108. end
  2109. --------------------------------------------------------------------------------
  2110. --------------------------------------------------------------------------------
  2111. -------------------------------COORDINATE SYSTEM--------------------------------
  2112. --------------------------------------------------------------------------------
  2113. --------------------------------------------------------------------------------
  2114. -- love.graphics.applyTransform
  2115. love.test.graphics.applyTransform = function(test)
  2116. -- use transform object to translate the drawn rectangle
  2117. local transform = love.math.newTransform()
  2118. transform:translate(10, 0)
  2119. local canvas = love.graphics.newCanvas(16, 16)
  2120. love.graphics.setCanvas(canvas)
  2121. love.graphics.clear(0, 0, 0, 1)
  2122. love.graphics.setColor(1, 0, 0, 1)
  2123. love.graphics.applyTransform(transform)
  2124. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2125. love.graphics.setColor(1, 1, 1, 1)
  2126. love.graphics.setCanvas()
  2127. local imgdata = love.graphics.readbackTexture(canvas)
  2128. test:compareImg(imgdata)
  2129. end
  2130. -- love.graphics.inverseTransformPoint
  2131. love.test.graphics.inverseTransformPoint = function(test)
  2132. -- start with 0, 0
  2133. local sx, sy = love.graphics.inverseTransformPoint(0, 0)
  2134. test:assertEquals(0, sx, 'check starting x is 0')
  2135. test:assertEquals(0, sy, 'check starting y is 0')
  2136. -- check translation effects the point
  2137. love.graphics.translate(1, 5)
  2138. sx, sy = love.graphics.inverseTransformPoint(1, 5)
  2139. test:assertEquals(0, sx, 'check transformed x is 0')
  2140. test:assertEquals(0, sy, 'check transformed y is 0')
  2141. love.graphics.origin()
  2142. end
  2143. -- love.graphics.origin
  2144. love.test.graphics.origin = function(test)
  2145. -- if we do some translations and scaling
  2146. -- using .origin() should reset it all and draw the pixel at 0,0
  2147. local canvas = love.graphics.newCanvas(16, 16)
  2148. love.graphics.setCanvas(canvas)
  2149. love.graphics.clear(0, 0, 0, 1)
  2150. love.graphics.origin()
  2151. love.graphics.translate(10, 10)
  2152. love.graphics.scale(1, 1)
  2153. love.graphics.shear(20, 20)
  2154. love.graphics.origin()
  2155. love.graphics.setColor(1, 0, 0, 1)
  2156. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2157. love.graphics.setColor(1, 1, 1, 1)
  2158. love.graphics.setCanvas()
  2159. local imgdata = love.graphics.readbackTexture(canvas)
  2160. test:compareImg(imgdata)
  2161. end
  2162. -- love.graphics.pop
  2163. love.test.graphics.pop = function(test)
  2164. -- if we push at the start, and then run a pop
  2165. -- it should reset it all and draw the pixel at 0,0
  2166. local canvas = love.graphics.newCanvas(16, 16)
  2167. love.graphics.setCanvas(canvas)
  2168. love.graphics.clear(0, 0, 0, 1)
  2169. love.graphics.push()
  2170. love.graphics.translate(10, 10)
  2171. love.graphics.scale(1, 1)
  2172. love.graphics.shear(20, 20)
  2173. love.graphics.pop()
  2174. love.graphics.setColor(1, 0, 0, 1)
  2175. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2176. love.graphics.setColor(1, 1, 1, 1)
  2177. love.graphics.setCanvas()
  2178. local imgdata = love.graphics.readbackTexture(canvas)
  2179. test:compareImg(imgdata)
  2180. end
  2181. -- love.graphics.push
  2182. love.test.graphics.push = function(test)
  2183. -- if we push at the start, do some stuff, then another push
  2184. -- 1 pop should only go back 1 push and draw the pixel at 1, 1
  2185. local canvas = love.graphics.newCanvas(16, 16)
  2186. love.graphics.setCanvas(canvas)
  2187. love.graphics.clear(0, 0, 0, 1)
  2188. love.graphics.push()
  2189. love.graphics.scale(1, 1)
  2190. love.graphics.shear(20, 20)
  2191. love.graphics.push()
  2192. love.graphics.translate(1, 1)
  2193. love.graphics.pop()
  2194. love.graphics.setColor(1, 0, 0, 1)
  2195. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2196. love.graphics.setColor(1, 1, 1, 1)
  2197. love.graphics.pop()
  2198. love.graphics.setCanvas()
  2199. local imgdata = love.graphics.readbackTexture(canvas)
  2200. test:compareImg(imgdata)
  2201. end
  2202. -- love.graphics.replaceTransform
  2203. love.test.graphics.replaceTransform = function(test)
  2204. -- if use transform object to translate
  2205. -- set some normal transforms first which should get overwritten
  2206. local transform = love.math.newTransform()
  2207. transform:translate(10, 0)
  2208. local canvas = love.graphics.newCanvas(16, 16)
  2209. love.graphics.setCanvas(canvas)
  2210. love.graphics.clear(0, 0, 0, 1)
  2211. love.graphics.setColor(1, 0, 0, 1)
  2212. love.graphics.scale(2, 2)
  2213. love.graphics.translate(10, 10)
  2214. love.graphics.replaceTransform(transform)
  2215. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2216. love.graphics.setColor(1, 1, 1, 1)
  2217. love.graphics.setCanvas()
  2218. local imgdata = love.graphics.readbackTexture(canvas)
  2219. test:compareImg(imgdata)
  2220. end
  2221. -- love.graphics.rotate
  2222. love.test.graphics.rotate = function(test)
  2223. -- starting at 0,0, we rotate by 90deg and then draw
  2224. -- we can then check the drawn rectangle is rotated
  2225. local canvas = love.graphics.newCanvas(16, 16)
  2226. love.graphics.setCanvas(canvas)
  2227. love.graphics.clear(0, 0, 0, 1)
  2228. love.graphics.setColor(1, 0, 0, 1)
  2229. love.graphics.translate(4, 0)
  2230. love.graphics.rotate(90 * (math.pi/180))
  2231. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2232. love.graphics.setColor(1, 1, 1, 1)
  2233. love.graphics.setCanvas()
  2234. local imgdata = love.graphics.readbackTexture(canvas)
  2235. test:compareImg(imgdata)
  2236. end
  2237. -- love.graphics.scale
  2238. love.test.graphics.scale = function(test)
  2239. -- starting at 0,0, we scale by 4x and then draw
  2240. -- we can then check the drawn rectangle covers the whole canvas
  2241. local canvas = love.graphics.newCanvas(16, 16)
  2242. love.graphics.setCanvas(canvas)
  2243. love.graphics.clear(0, 0, 0, 1)
  2244. love.graphics.setColor(1, 0, 0, 1)
  2245. love.graphics.scale(4, 4)
  2246. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2247. love.graphics.setColor(1, 1, 1, 1)
  2248. love.graphics.setCanvas()
  2249. local imgdata = love.graphics.readbackTexture(canvas)
  2250. test:compareImg(imgdata)
  2251. end
  2252. -- love.graphics.shear
  2253. love.test.graphics.shear = function(test)
  2254. -- starting at 0,0, we shear by 2x and then draw
  2255. -- we can then check the drawn rectangle has moved over
  2256. local canvas = love.graphics.newCanvas(16, 16)
  2257. love.graphics.setCanvas(canvas)
  2258. love.graphics.clear(0, 0, 0, 1)
  2259. love.graphics.origin()
  2260. love.graphics.setColor(1, 0, 0, 1)
  2261. love.graphics.shear(2, 0)
  2262. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2263. love.graphics.setColor(1, 1, 1, 1)
  2264. love.graphics.setCanvas()
  2265. local imgdata1 = love.graphics.readbackTexture(canvas)
  2266. test:compareImg(imgdata1)
  2267. -- same again at 0,0, we shear by 2y and then draw
  2268. -- we can then check the drawn rectangle has moved down
  2269. love.graphics.setCanvas(canvas)
  2270. love.graphics.clear(0, 0, 0, 1)
  2271. love.graphics.origin()
  2272. love.graphics.setColor(1, 0, 0, 1)
  2273. love.graphics.shear(0, 2)
  2274. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2275. love.graphics.setColor(1, 1, 1, 1)
  2276. love.graphics.setCanvas()
  2277. local imgdata2 = love.graphics.readbackTexture(canvas)
  2278. test:compareImg(imgdata2)
  2279. end
  2280. -- love.graphics.transformPoint
  2281. love.test.graphics.transformPoint = function(test)
  2282. -- start with 0, 0
  2283. local sx, sy = love.graphics.transformPoint(0, 0)
  2284. test:assertEquals(0, sx, 'check starting x is 0')
  2285. test:assertEquals(0, sy, 'check starting y is 0')
  2286. -- check translation effects the point
  2287. love.graphics.translate(1, 5)
  2288. sx, sy = love.graphics.transformPoint(0, 0)
  2289. test:assertEquals(1, sx, 'check transformed x is 0')
  2290. test:assertEquals(5, sy, 'check transformed y is 10')
  2291. end
  2292. -- love.graphics.translate
  2293. love.test.graphics.translate = function(test)
  2294. -- starting at 0,0, we translate 4 times and draw a pixel at each point
  2295. -- we can then check the 4 points are now red
  2296. local canvas = love.graphics.newCanvas(16, 16)
  2297. love.graphics.setCanvas(canvas)
  2298. love.graphics.clear(0, 0, 0, 1)
  2299. love.graphics.setColor(1, 0, 0, 1)
  2300. love.graphics.translate(5, 0)
  2301. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2302. love.graphics.translate(0, 5)
  2303. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2304. love.graphics.translate(-5, 0)
  2305. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2306. love.graphics.translate(0, -5)
  2307. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2308. love.graphics.setColor(1, 1, 1, 1)
  2309. love.graphics.setCanvas()
  2310. local imgdata = love.graphics.readbackTexture(canvas)
  2311. test:compareImg(imgdata)
  2312. end
  2313. --------------------------------------------------------------------------------
  2314. --------------------------------------------------------------------------------
  2315. -------------------------------------WINDOW-------------------------------------
  2316. --------------------------------------------------------------------------------
  2317. --------------------------------------------------------------------------------
  2318. -- love.graphics.getDPIScale
  2319. -- @NOTE hardware dependent so can't check result
  2320. love.test.graphics.getDPIScale = function(test)
  2321. test:assertNotNil(love.graphics.getDPIScale())
  2322. end
  2323. -- love.graphics.getDimensions
  2324. love.test.graphics.getDimensions = function(test)
  2325. -- check graphics dimensions match window dimensions
  2326. local gwidth, gheight = love.graphics.getDimensions()
  2327. local wwidth, wheight, _ = love.window.getMode()
  2328. test:assertEquals(wwidth, gwidth, 'check graphics dimension w matches window w')
  2329. test:assertEquals(wheight, gheight, 'check graphics dimension h matches window h')
  2330. end
  2331. -- love.graphics.getHeight
  2332. love.test.graphics.getHeight = function(test)
  2333. -- check graphics height match window height
  2334. local wwidth, wheight, _ = love.window.getMode()
  2335. test:assertEquals(wheight, love.graphics.getHeight(), 'check graphics h matches window h')
  2336. end
  2337. -- love.graphics.getPixelDimensions
  2338. love.test.graphics.getPixelDimensions = function(test)
  2339. -- check graphics dimensions match window dimensions relative to dpi
  2340. local dpi = love.graphics.getDPIScale()
  2341. local gwidth, gheight = love.graphics.getPixelDimensions()
  2342. local wwidth, wheight, _ = love.window.getMode()
  2343. test:assertEquals(wwidth, gwidth/dpi, 'check graphics pixel dpi w matches window w')
  2344. test:assertEquals(wheight, gheight/dpi, 'check graphics pixel dpi h matches window h')
  2345. end
  2346. -- love.graphics.getPixelHeight
  2347. love.test.graphics.getPixelHeight = function(test)
  2348. -- check graphics height match window height relative to dpi
  2349. local dpi = love.graphics.getDPIScale()
  2350. local wwidth, wheight, _ = love.window.getMode()
  2351. test:assertEquals(wheight,love.graphics.getPixelHeight()/dpi, 'check graphics pixel dpi h matches window h')
  2352. end
  2353. -- love.graphics.getPixelWidth
  2354. love.test.graphics.getPixelWidth = function(test)
  2355. -- check graphics width match window width relative to dpi
  2356. local dpi = love.graphics.getDPIScale()
  2357. local wwidth, wheight, _ = love.window.getMode()
  2358. test:assertEquals(wwidth, love.graphics.getWidth()/dpi, 'check graphics pixel dpi w matches window w')
  2359. end
  2360. -- love.graphics.getWidth
  2361. love.test.graphics.getWidth = function(test)
  2362. -- check graphics width match window width
  2363. local wwidth, wheight, _ = love.window.getMode()
  2364. test:assertEquals(wwidth, love.graphics.getWidth(), 'check graphics w matches window w')
  2365. end
  2366. --------------------------------------------------------------------------------
  2367. --------------------------------------------------------------------------------
  2368. -------------------------------SYSTEM INFORMATION-------------------------------
  2369. --------------------------------------------------------------------------------
  2370. --------------------------------------------------------------------------------
  2371. -- love.graphics.getTextureFormats
  2372. love.test.graphics.getTextureFormats = function(test)
  2373. local formats = {
  2374. 'hdr', 'r8i', 'r8ui', 'r16i', 'r16ui', 'r32i', 'r32ui', 'rg8i', 'rg8ui',
  2375. 'rg16i', 'rg16ui', 'rg32i', 'rg32ui', 'bgra8', 'r8', 'rgba8i', 'rgba8ui',
  2376. 'rgba16i', 'rg8', 'rgba32i', 'rgba32ui', 'rgba8', 'DXT1', 'r16', 'DXT5',
  2377. 'rg16', 'BC4s', 'rgba16', 'BC5s', 'r16f', 'BC6hs', 'BC7', 'PVR1rgb2',
  2378. 'rg16f', 'PVR1rgba2', 'rgba16f', 'ETC1', 'r32f', 'ETC2rgba', 'rg32f',
  2379. 'EACr', 'rgba32f', 'EACrg', 'rgba4', 'ASTC4x4', 'ASTC5x4', 'rgb5a1',
  2380. 'ASTC6x5', 'rgb565', 'ASTC8x5', 'ASTC8x6', 'rgb10a2', 'ASTC10x5',
  2381. 'rg11b10f', 'ASTC10x8', 'ASTC10x10', 'ASTC12x10', 'ASTC12x12', 'normal',
  2382. 'srgba8', 'la8', 'ASTC10x6', 'ASTC8x8', 'ASTC6x6', 'ASTC5x5', 'EACrgs',
  2383. 'EACrs', 'ETC2rgba1', 'ETC2rgb', 'PVR1rgba4', 'PVR1rgb4', 'BC6h',
  2384. 'BC5', 'BC4', 'DXT3', 'stencil8', 'rgba16ui', 'bgra8srgb'
  2385. }
  2386. local supported = love.graphics.getTextureFormats({ canvas = true })
  2387. test:assertNotNil(supported)
  2388. for f=1,#formats do
  2389. test:assertNotEquals(nil, supported[formats[f] ], 'expected a key for format: ' .. formats[f])
  2390. end
  2391. end
  2392. -- love.graphics.getRendererInfo
  2393. -- @NOTE hardware dependent so best can do is nil checking
  2394. love.test.graphics.getRendererInfo = function(test)
  2395. local name, version, vendor, device = love.graphics.getRendererInfo()
  2396. test:assertNotNil(name)
  2397. test:assertNotNil(version)
  2398. test:assertNotNil(vendor)
  2399. test:assertNotNil(device)
  2400. end
  2401. -- love.graphics.getStats
  2402. -- @NOTE cant really predict some of these so just nil check for most
  2403. love.test.graphics.getStats = function(test)
  2404. local stattypes = {
  2405. 'drawcalls', 'canvasswitches', 'texturememory', 'shaderswitches',
  2406. 'drawcallsbatched', 'textures', 'fonts'
  2407. }
  2408. local stats = love.graphics.getStats()
  2409. for s=1,#stattypes do
  2410. test:assertNotEquals(nil, stats[stattypes[s] ], 'expected a key for stat: ' .. stattypes[s])
  2411. end
  2412. end
  2413. -- love.graphics.getSupported
  2414. love.test.graphics.getSupported = function(test)
  2415. -- cant check values as hardware dependent but we can check the keys in the
  2416. -- table match what the documentation lists
  2417. local gfs = {
  2418. 'clampzero', 'lighten', 'glsl3', 'instancing', 'fullnpot',
  2419. 'pixelshaderhighp', 'shaderderivatives', 'indirectdraw', 'mipmaprange',
  2420. 'copyrendertargettobuffer', 'copytexturetobuffer', 'copybuffer',
  2421. 'copybuffertotexture', 'indexbuffer32bit', 'multirendertargetformats',
  2422. 'clampone', 'blendminmax', 'glsl4'
  2423. }
  2424. local features = love.graphics.getSupported()
  2425. for g=1,#gfs do
  2426. test:assertNotEquals(nil, features[gfs[g] ], 'expected a key for graphic feature: ' .. gfs[g])
  2427. end
  2428. end
  2429. -- love.graphics.getSystemLimits
  2430. love.test.graphics.getSystemLimits = function(test)
  2431. -- cant check values as hardware dependent but we can check the keys in the
  2432. -- table match what the documentation lists
  2433. local glimits = {
  2434. 'texelbuffersize', 'shaderstoragebuffersize', 'threadgroupsx',
  2435. 'threadgroupsy', 'pointsize', 'texturesize', 'texturelayers', 'volumetexturesize',
  2436. 'cubetexturesize', 'anisotropy', 'texturemsaa', 'rendertargets', 'threadgroupsz'
  2437. }
  2438. local limits = love.graphics.getSystemLimits()
  2439. for g=1,#glimits do
  2440. test:assertNotEquals(nil, limits[glimits[g] ], 'expected a key for system limit: ' .. glimits[g])
  2441. end
  2442. end
  2443. -- love.graphics.getTextureTypes
  2444. love.test.graphics.getTextureTypes = function(test)
  2445. -- cant check values as hardware dependent but we can check the keys in the
  2446. -- table match what the documentation lists
  2447. local ttypes = {
  2448. '2d', 'array', 'cube', 'volume'
  2449. }
  2450. local types = love.graphics.getTextureTypes()
  2451. for t=1,#ttypes do
  2452. test:assertNotEquals(nil, types[ttypes[t] ], 'expected a key for texture type: ' .. ttypes[t])
  2453. end
  2454. end