graphics.lua 101 KB

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