graphics.lua 86 KB

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