From 5351b911681bb7b1c65380bf124ba4190831731c Mon Sep 17 00:00:00 2001 From: Tim Anema Date: Sun, 30 Nov 2014 15:24:58 -0500 Subject: [PATCH] no longer need two callback for drawing just one, which is nice --- examples/complex.lua | 99 ++++++++++++++++++++---------------------- examples/normalMap.lua | 21 ++++----- examples/short.lua | 27 +++++------- examples/test.lua | 18 +++----- lib/init.lua | 28 +++++++----- main.lua | 30 ++++++------- 6 files changed, 102 insertions(+), 121 deletions(-) diff --git a/examples/complex.lua b/examples/complex.lua index 524eff1..4632b57 100644 --- a/examples/complex.lua +++ b/examples/complex.lua @@ -235,10 +235,57 @@ end function love.draw() -- set shader buffer + lightWorld:setTranslation(offsetX,offsetY, scale) love.graphics.push() love.graphics.translate(offsetX, offsetY) love.graphics.scale(scale) - lightWorld:draw(offsetX,offsetY, scale) + lightWorld:draw(function(l, t, w, h, s) + love.graphics.setBlendMode("alpha") + if normalOn then + love.graphics.setColor(127, 127, 255) + love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) + else + love.graphics.setColor(255, 255, 255) + if textureOn then + love.graphics.draw(imgFloor, quadScreen, 0,0) + else + love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) + end + end + + for i = 1, phyCnt do + if phyLight[i]:getType() == "refraction" then + if not normalOn then + love.graphics.setBlendMode("alpha") + love.graphics.setColor(255, 255, 255, 191) + love.graphics.draw(water, phyLight[i].x - phyLight[i].ox, phyLight[i].y - phyLight[i].oy) + end + end + end + + love.graphics.setBlendMode("alpha") + for i = 1, phyCnt do + if phyLight[i]:getType() == "polygon" then + math.randomseed(i) + love.graphics.setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) + love.graphics.polygon("fill", phyLight[i]:getPoints()) + elseif phyLight[i]:getType() == "circle" then + math.randomseed(i) + love.graphics.setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) + local cx, cy = phyLight[i]:getPosition() + love.graphics.circle("fill", cx, cy, phyLight[i]:getRadius()) + elseif phyLight[i]:getType() == "image" then + if normalOn and phyLight[i].normal then + love.graphics.setColor(255, 255, 255) + love.graphics.draw(phyLight[i].normal, phyLight[i].x - phyLight[i].nx, phyLight[i].y - phyLight[i].ny) + elseif not phyLight[i].material then + math.randomseed(i) + love.graphics.setColor(math.random(127, 255), math.random(127, 255), math.random(127, 255)) + love.graphics.draw(phyLight[i].img, phyLight[i].x - phyLight[i].ix, phyLight[i].y - phyLight[i].iy) + end + end + end + end) love.graphics.pop() love.graphics.draw(imgLight, mx - 5, (my - 5) - (16.0 + (math.sin(lightDirection) + 1.0) * 64.0)) @@ -334,56 +381,6 @@ function love.draw() end end -function drawBackground(l,t,w,h) - love.graphics.setBlendMode("alpha") - if normalOn then - love.graphics.setColor(127, 127, 255) - love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) - else - love.graphics.setColor(255, 255, 255) - if textureOn then - love.graphics.draw(imgFloor, quadScreen, 0,0) - else - love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) - end - end - - for i = 1, phyCnt do - if phyLight[i]:getType() == "refraction" then - if not normalOn then - love.graphics.setBlendMode("alpha") - love.graphics.setColor(255, 255, 255, 191) - love.graphics.draw(water, phyLight[i].x - phyLight[i].ox, phyLight[i].y - phyLight[i].oy) - end - end - end -end - -function drawForeground(l,t,w,h) - love.graphics.setBlendMode("alpha") - for i = 1, phyCnt do - if phyLight[i]:getType() == "polygon" then - math.randomseed(i) - love.graphics.setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) - love.graphics.polygon("fill", phyLight[i]:getPoints()) - elseif phyLight[i]:getType() == "circle" then - math.randomseed(i) - love.graphics.setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255)) - local cx, cy = phyLight[i]:getPosition() - love.graphics.circle("fill", cx, cy, phyLight[i]:getRadius()) - elseif phyLight[i]:getType() == "image" then - if normalOn and phyLight[i].normal then - love.graphics.setColor(255, 255, 255) - love.graphics.draw(phyLight[i].normal, phyLight[i].x - phyLight[i].nx, phyLight[i].y - phyLight[i].ny) - elseif not phyLight[i].material then - math.randomseed(i) - love.graphics.setColor(math.random(127, 255), math.random(127, 255), math.random(127, 255)) - love.graphics.draw(phyLight[i].img, phyLight[i].x - phyLight[i].ix, phyLight[i].y - phyLight[i].iy) - end - end - end -end - function love.mousepressed(x, y, c) if c == "m" then -- add light diff --git a/examples/normalMap.lua b/examples/normalMap.lua index 05cb307..9cc1e02 100644 --- a/examples/normalMap.lua +++ b/examples/normalMap.lua @@ -54,22 +54,17 @@ function love.update(dt) end function love.draw() + lightWorld:setTranslation(x,y,scale) love.graphics.push() love.graphics.translate(x, y) love.graphics.scale(scale) - lightWorld:draw(x,y,scale) + lightWorld:draw(function(l, t, w, h, s) + love.graphics.setColor(255, 255, 255) + love.graphics.rectangle("fill", -l/scale, -t/scale, w/scale, h/scale) + love.graphics.setColor(255, 255, 255) + local w, h = love.graphics.getWidth(), love.graphics.getHeight() + love.graphics.draw(image, w/2-(image:getWidth()/2), h/2-(image:getHeight()/2)) + end) love.graphics.pop() end -function drawBackground(l,t,w,h) - love.graphics.setColor(255, 255, 255) - love.graphics.rectangle("fill", -l/scale, -t/scale, w/scale, h/scale) -end - -function drawForeground(l,t,w,h) - love.graphics.setColor(255, 255, 255) - local w, h = love.graphics.getWidth(), love.graphics.getHeight() - love.graphics.draw(image, w/2-(image:getWidth()/2), h/2-(image:getHeight()/2)) -end - - diff --git a/examples/short.lua b/examples/short.lua index f3af352..47528d8 100644 --- a/examples/short.lua +++ b/examples/short.lua @@ -130,10 +130,20 @@ function love.mousepressed(x, y, c) end function love.draw() + lightWorld:setTranslation(x, y, scale) love.graphics.push() love.graphics.translate(x, y) love.graphics.scale(scale) - lightWorld:draw(x,y,scale) + lightWorld:draw(function() + love.graphics.setColor(255, 255, 255) + love.graphics.rectangle("fill", -x/scale, -y/scale, love.graphics.getWidth()/scale, love.graphics.getHeight()/scale) + love.graphics.setColor(63, 255, 127) + local cx, cy = circleTest:getPosition() + love.graphics.circle("fill", cx, cy, circleTest:getRadius()) + love.graphics.polygon("fill", rectangleTest:getPoints()) + love.graphics.setColor(255, 255, 255) + love.graphics.draw(image, 64 - image:getWidth() * 0.5, 64 - image:getHeight() * 0.5) + end) love.graphics.pop() love.graphics.setBlendMode("alpha") @@ -143,18 +153,3 @@ function love.draw() love.graphics.print("To toggle postshaders, use 0-9 and q->y, to scale use - and =, and to translate use arrows") love.graphics.print("light z: " .. lightMouse.z, 0, 50) end - -function drawBackground(l,t,w,h) - love.graphics.setColor(255, 255, 255) - love.graphics.rectangle("fill", -l/scale, -t/scale, w/scale, h/scale) -end - -function drawForeground(l,t,w,h) - love.graphics.setColor(63, 255, 127) - local cx, cy = circleTest:getPosition() - love.graphics.circle("fill", cx, cy, circleTest:getRadius()) - love.graphics.polygon("fill", rectangleTest:getPoints()) - love.graphics.setColor(255, 255, 255) - love.graphics.draw(image, 64 - image:getWidth() * 0.5, 64 - image:getHeight() * 0.5) -end - diff --git a/examples/test.lua b/examples/test.lua index 29043cc..49e6a57 100644 --- a/examples/test.lua +++ b/examples/test.lua @@ -43,17 +43,11 @@ function love.update(dt) end function love.draw() - lightWorld:draw() + lightWorld:draw(function(l, t, w, h, s) + love.graphics.setColor(255, 255, 255) + love.graphics.rectangle("fill", l, t, w, h) + love.graphics.setColor(0, 255, 0) + love.graphics.circle('fill', 150, 150, 50) + end) end -function drawBackground(l,t,w,h) - love.graphics.setColor(255, 255, 255) - love.graphics.rectangle("fill", l, t, w, h) -end - -function drawForeground(l,t,w,h) - love.graphics.setColor(0, 255, 0) - love.graphics.circle('fill', 150, 150, 50) -end - - diff --git a/lib/init.lua b/lib/init.lua index d4745d6..1d7e7a0 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -44,6 +44,8 @@ function light_world:init(options) self.body = {} self.post_shader = PostShader() + self.l, self.t, self.s = 0, 0, 1 + self.ambient = {0, 0, 0} self.refractionStrength = 8.0 @@ -68,6 +70,7 @@ end function light_world:refreshScreenSize(w, h) w, h = w or love.window.getWidth(), h or love.window.getHeight() + self.w, self.h = w, h self.render_buffer = love.graphics.newCanvas(w, h) self.normal = love.graphics.newCanvas(w, h) self.normal2 = love.graphics.newCanvas(w, h) @@ -92,19 +95,20 @@ function light_world:refreshScreenSize(w, h) self.post_shader:refreshScreenSize(w, h) end -function light_world:draw(l,t,s) - l,t,s = (l or 0), (t or 0), s or 1 - local w, h = love.graphics.getWidth(), love.graphics.getHeight() - util.drawto(self.render_buffer, l, t, s, function() - self.drawBackground( l,t,w,h,s) - self.drawForeground( l,t,w,h,s) - self:drawMaterial( l,t,w,h,s) - self:drawNormalShading( l,t,w,h,s) - self:drawGlow( l,t,w,h,s) - self:drawRefraction( l,t,w,h,s) - self:drawReflection( l,t,w,h,s) +function light_world:setTranslation(l, t, s) + self.l, self.t, self.s = l, t, s +end + +function light_world:draw(cb) + util.drawto(self.render_buffer, self.l, self.t, self.s, function() + cb( self.l,self.t,self.w,self.h,self.s) + self:drawMaterial( self.l,self.t,self.w,self.h,self.s) + self:drawNormalShading( self.l,self.t,self.w,self.h,self.s) + self:drawGlow( self.l,self.t,self.w,self.h,self.s) + self:drawRefraction( self.l,self.t,self.w,self.h,self.s) + self:drawReflection( self.l,self.t,self.w,self.h,self.s) end) - self.post_shader:drawWith(self.render_buffer, l, t, s) + self.post_shader:drawWith(self.render_buffer, self.l, self.t, self.s) end -- draw normal shading diff --git a/main.lua b/main.lua index 6ca2a15..31b52fc 100644 --- a/main.lua +++ b/main.lua @@ -60,28 +60,24 @@ function exf.update(dt) end function exf.draw() - lightWorld:draw() -end + lightWorld:draw(function() + love.graphics.setBackgroundColor(0, 0, 0) -function exf.drawBackground() - love.graphics.setBackgroundColor(0, 0, 0) + love.graphics.setColor(48, 156, 225) + love.graphics.rectangle("fill", 0, 0, love.window.getWidth(), love.window.getHeight()) - love.graphics.setColor(48, 156, 225) - love.graphics.rectangle("fill", 0, 0, love.window.getWidth(), love.window.getHeight()) + love.graphics.setColor(255, 255, 255, 191) + love.graphics.setFont(exf.bigfont) + love.graphics.print("Examples:", 50, 50) - love.graphics.setColor(255, 255, 255, 191) - love.graphics.setFont(exf.bigfont) - love.graphics.print("Examples:", 50, 50) + love.graphics.setFont(exf.smallfont) + love.graphics.print("Browse and click on the example you \nwant to run. To return the the example \nselection screen, press escape.", 500, 80) - love.graphics.setFont(exf.smallfont) - love.graphics.print("Browse and click on the example you \nwant to run. To return the the example \nselection screen, press escape.", 500, 80) + exf.list:draw() - exf.list:draw() -end - -function exf.drawForeground() - love.graphics.setColor(255, 255, 255) - love.graphics.draw(exf.bigball, 800 - 128, 600 - 128, love.timer.getTime(), 1, 1, exf.bigball:getWidth() * 0.5, exf.bigball:getHeight() * 0.5) + love.graphics.setColor(255, 255, 255) + love.graphics.draw(exf.bigball, 800 - 128, 600 - 128, love.timer.getTime(), 1, 1, exf.bigball:getWidth() * 0.5, exf.bigball:getHeight() * 0.5) + end) end function exf.keypressed(k)