graphics.lua 101 KB

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