clean up examples

This commit is contained in:
Tim Anema 2021-11-09 20:15:14 -05:00
parent d8ad342739
commit fe2250b1df
No known key found for this signature in database
GPG Key ID: 222D82A25F3857A4
15 changed files with 360 additions and 899 deletions

View File

@ -1,77 +1,34 @@
-- Example: Animation Example
local LightWorld = require "lib"
local anim8 = require 'lib.anim8'
local x, y, z, scale = 20, -55, 1, 3.5
function love.load()
x, y, z, scale = 0, 0, 1, 1
-- load images
image = love.graphics.newImage("examples/gfx/scott_pilgrim.png")
image_normal = love.graphics.newImage("examples/gfx/scott_pilgrim_NRM.png")
-- create light world
lightWorld = LightWorld({
ambient = {55,55,55},
refractionStrength = 32.0,
reflectionVisibility = 0.75,
})
-- create light
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 300)
lightMouse:setGlowStrength(0.3)
lightMouse.normalInvert = true
-- create shadow bodys
animation = lightWorld:newAnimationGrid(image, 100, 100)
local function load()
-- load images
image = love.graphics.newImage("examples/gfx/scott_pilgrim.png")
image_normal = love.graphics.newImage("examples/gfx/scott_pilgrim_NRM.png")
-- create light world
lightWorld = LightWorld({ambient = {0.49, 0.49, 0.49}})
-- create light
lightMouse = lightWorld:newLight(0, 0, 1, 0.49, 0.24, 300)
-- create shadow bodys
animation = lightWorld:newAnimationGrid(image, 100, 100)
animation:setNormalMap(image_normal)
grid = animation:newGrid(108, 140)
animation:addAnimation('run right', grid('1-8', 1), 0.1)
animation:addAnimation('run left', grid('8-1', 2), 0.1)
local g = anim8.newGrid(108, 140, image:getWidth(), image:getHeight())
animation2 = anim8.newAnimation(g('1-8', 1), 0.1)
end
function love.update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("down") then
y = y - dt * 200
elseif love.keyboard.isDown("up") then
y = y + dt * 200
end
if love.keyboard.isDown("right") then
x = x - dt * 200
elseif love.keyboard.isDown("left") then
x = x + dt * 200
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
if love.keyboard.isDown("a") then
local function update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("a") then
animation:setAnimation('run left')
elseif love.keyboard.isDown("d") then
elseif love.keyboard.isDown("d") then
animation:setAnimation('run right')
end
animation2:update(dt)
end
lightWorld:update(dt)
lightMouse:setPosition((love.mouse.getX() - x)/scale, (love.mouse.getY() - y)/scale, z)
lightMouse:setPosition((love.mouse.getX() - x)/scale, (love.mouse.getY() - y)/scale, z)
end
function love.mousepressed(x, y, c)
if c == "wu" then
z = z + 1
elseif c == "wd" then
z = z - 1
end
end
function love.draw()
local function draw()
lightWorld:setTranslation(x, y, scale)
love.graphics.push()
love.graphics.translate(x, y)
@ -80,9 +37,13 @@ function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", -x/scale, -y/scale, love.graphics.getWidth()/scale, love.graphics.getHeight()/scale)
animation:drawAnimation()
animation2:draw(image, 200, 30)
end)
love.graphics.pop()
end
return {
load = load,
update = update,
draw = draw,
mousepressed = mousepressed,
}

View File

@ -1,4 +1,3 @@
-- Example: Complex Example
local LightWorld = require "lib"
function initScene()
@ -28,7 +27,7 @@ function initScene()
phyLight = {}
end
function love.load()
local function load()
love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setDefaultFilter("nearest", "nearest")
@ -125,12 +124,12 @@ function love.load()
tileY = 0
end
function love.update(dt)
local function update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
mx, my = (love.mouse.getX() - offsetX)/scale, (love.mouse.getY() - offsetY)/scale
mouseLight:setPosition(mx, my, 1 + (math.sin(lightDirection) + 1.0))
mouseLight:setPosition(mx, my, 1 + (math.sin(lightDirection)+1.0))
lightDirection = lightDirection + dt
colorAberration = math.max(0.0, colorAberration - dt * 10.0)
@ -187,7 +186,7 @@ function love.update(dt)
lightWorld:update(dt)
end
function love.draw()
local function draw()
-- set shader buffer
lightWorld:setTranslation(offsetX,offsetY, scale)
love.graphics.push()
@ -316,7 +315,7 @@ function love.draw()
end
end
function love.mousepressed(x, y, c)
local function mousepressed(x, y, c)
if c == "m" then
-- add light
local r = lightWorld:getLightCount() % 3
@ -361,7 +360,7 @@ function love.mousepressed(x, y, c)
end
end
function love.keypressed(k, u)
local function keypressed(k, u)
-- debug options
if k == "f1" then
helpOn = not helpOn
@ -574,3 +573,11 @@ function love.keypressed(k, u)
end
end
end
return {
load = load,
update = update,
draw = draw,
keypressed = keypressed,
mousepressed = mousepressed,
}

View File

@ -0,0 +1,80 @@
local List = require('examples.lib.list')
local LightWorld = require('lib')
local examples = {
["Animation Example"] = "animation.lua",
["Complex Example"] = "complex.lua",
["Gamera Example"] = "gamera.lua",
["Hump Example"] = "hump.lua",
["Normal Map Example"] = "normalMap.lua",
["Only Postshader Example"] = "postshaders.lua",
["Simple Example"] = "simple.lua",
["STI Example"] = "simple_tiled_impl.lua",
}
local list, smallfont, bigfont, bigball, lightWorld, lightMouse
local function start(file)
local state = love.filesystem.load("examples/" .. file)()
love.update = state.update
love.draw = state.draw
love.keyreleased = state.keyrelease
love.mousepressed = state.mousepressed
love.mousereleased = state.mousereleased
love.wheelmoved = state.wheelmoved
love.keypressed = function(k)
if k == "escape"then
if file ~= "examples_list.lua" then start("examples_list.lua") else love.event.quit() end
end
if state.keypressed ~= nil then state.keypressed(k) end
end
state.load()
end
local function load()
list = List:new(50, 100, 400, 23)
smallfont = love.graphics.newFont(12)
bigfont = love.graphics.newFont(24)
list.font = smallfont
bigball = love.graphics.newImage("examples/gfx/love-big-ball.png")
local n = 0
for c, v in pairs(examples) do
n = n + 1
local title = string.format("%04d", n) .. " " .. c .. " (" .. v .. ")"
list:add(title, v, function() start(v) end)
end
love.window.setTitle("LOVE Example Browser")
lightWorld = LightWorld({ambient = {0.49, 0.49, 0.49}})
lightMouse = lightWorld:newLight(20, 20, 1, 0.49, 0.24, 500)
lightWorld.post_shader:toggleEffect("scanlines")
lightMouse:setSmooth(2)
lightWorld:newCircle(620, 200, bigball:getWidth()*0.5)
end
local function update(dt)
list:update(dt)
lightMouse:setPosition(love.mouse.getX(), love.mouse.getY())
lightWorld:update(dt)
end
local function draw()
lightWorld:draw(function()
love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setColor(0.18, 0.61, 1)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.setColor(1, 1, 1, 1)
love.graphics.setFont(bigfont)
love.graphics.print("Examples:", 50, 30)
love.graphics.setFont(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)
list:draw()
love.graphics.setColor(1, 1, 1)
love.graphics.draw(bigball, 620, 200, love.timer.getTime()/5, 1, 1, bigball:getWidth() * 0.5, bigball:getHeight() * 0.5)
end)
end
return {
start = start,
draw = draw,
update = update,
load = load,
mousepressed = function(x, y, b) list:mousepressed(x, y, b) end,
}

View File

@ -1,84 +1,28 @@
-- Example: Gamera Example
local gamera = require "examples/vendor/gamera"
local LightWorld = require "lib"
local simple = require "examples.simple"
local keyboard = require "examples.lib.keyboard"
function love.load()
x = 0
y = 0
scale = 1
local function load()
cam = gamera.new(0, 0, love.graphics.getWidth(), love.graphics.getHeight())
image = love.graphics.newImage("examples/gfx/machine2.png")
image_normal = love.graphics.newImage("examples/gfx/cone_normal.png")
normal = love.graphics.newImage("examples/gfx/refraction_normal.png")
glow = love.graphics.newImage("examples/gfx/machine2_glow.png")
-- create light world
lightWorld = LightWorld({
ambient = {55,55,55},
refractionStrength = 32.0,
reflectionVisibility = 0.75,
})
-- create light
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 300)
lightMouse:setGlowStrength(0.3)
-- create shadow bodys
circleTest = lightWorld:newCircle(256, 256, 16)
rectangleTest = lightWorld:newRectangle(512, 512, 64, 64)
imageTest = lightWorld:newImage(image, 64, 64, 24, 6)
imageTest:setNormalMap(image_normal)
imageTest:setGlowMap(glow)
imageTest:setOffset(12, -10)
-- create body object
objectTest = lightWorld:newRefraction(normal, 64, 64, 128, 128)
objectTest:setReflection(true)
simple.load()
end
function love.update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("down") then
y = y - dt * 200
elseif love.keyboard.isDown("up") then
y = y + dt * 200
end
if love.keyboard.isDown("right") then
x = x - dt * 200
elseif love.keyboard.isDown("left") then
x = x + dt * 200
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
lightMouse:setPosition((love.mouse.getX() - x)/scale, (love.mouse.getY() - y)/scale)
local function update(dt)
local x, y, scale = keyboard.update(dt)
simple.update(dt, x, y, scale)
cam:setScale(scale)
cam:setPosition(x, y)
lightWorld:update(dt)
lightWorld:setTranslation(x, y, scale)
end
function love.draw()
local function draw()
cam:draw(function(l,t,w,h)
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)
simple.draw()
end)
end
return {
load = load,
update = update,
draw = draw,
keypressed = keypressed,
}

BIN
examples/gfx/kingscard.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

View File

@ -1,83 +1,27 @@
-- Example: Hump Example
local Camera = require "examples/vendor/hump/camera"
local LightWorld = require "lib"
local simple = require "examples.simple"
local keyboard = require "examples.lib.keyboard"
function love.load()
x = 0
y = 0
scale = 1
local function load()
cam = Camera(love.graphics.getWidth()/2, love.graphics.getHeight()/2)
image = love.graphics.newImage("examples/gfx/machine2.png")
image_normal = love.graphics.newImage("examples/gfx/cone_normal.png")
normal = love.graphics.newImage("examples/gfx/refraction_normal.png")
glow = love.graphics.newImage("examples/gfx/machine2_glow.png")
-- create light world
lightWorld = LightWorld({
ambient = {55,55,55},
refractionStrength = 32.0,
reflectionVisibility = 0.75,
})
-- create light
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 300)
lightMouse:setGlowStrength(0.3)
-- create shadow bodys
circleTest = lightWorld:newCircle(256, 256, 16)
rectangleTest = lightWorld:newRectangle(512, 512, 64, 64)
imageTest = lightWorld:newImage(image, 64, 64, 24, 6)
imageTest:setNormalMap(image_normal)
imageTest:setGlowMap(glow)
imageTest:setOffset(12, -10)
-- create body object
objectTest = lightWorld:newRefraction(normal, 64, 64, 128, 128)
objectTest:setReflection(true)
simple.load()
end
function love.update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("down") then
y = y - dt * 200
elseif love.keyboard.isDown("up") then
y = y + dt * 200
end
if love.keyboard.isDown("right") then
x = x - dt * 200
elseif love.keyboard.isDown("left") then
x = x + dt * 200
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
lightMouse:setPosition((love.mouse.getX() - x)/scale, (love.mouse.getY() - y)/scale)
local function update(dt)
local x, y, scale = keyboard.update(dt)
simple.update(dt, x, y, scale)
cam:lookAt(x, y)
cam:zoom(scale)
lightWorld:update(dt)
lightWorld:setTranslation(x, y, scale)
end
function love.draw()
local function draw()
cam:attach()
lightWorld:draw(function()
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("fill", -x/scale, -y/scale, love.graphics.getWidth()/scale, love.graphics.getHeight()/scale)
love.graphics.setColor(63/ 255, 1, 0.5)
local cx, cy = circleTest:getPosition()
love.graphics.circle("fill", cx, cy, circleTest:getRadius())
love.graphics.polygon("fill", rectangleTest:getPoints())
love.graphics.setColor(1, 1, 1)
love.graphics.draw(image, 64 - image:getWidth() * 0.5, 64 - image:getHeight() * 0.5)
end)
simple.draw()
cam:detach()
end
return {
load = load,
update = update,
draw = draw,
}

32
examples/lib/keyboard.lua Normal file
View File

@ -0,0 +1,32 @@
local x, y, scale = 0, 0, 1
local function update(dt)
if love.keyboard.isDown("down") then
y = y - dt * 200
elseif love.keyboard.isDown("up") then
y = y + dt * 200
end
if love.keyboard.isDown("right") then
x = x - dt * 200
elseif love.keyboard.isDown("left") then
x = x + dt * 200
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
return x, y, scale
end
local function status()
return x, y, scale
end
return {
update = update,
status = status,
}

43
examples/lib/list.lua Normal file
View File

@ -0,0 +1,43 @@
local List = {}
function List:new(x, y, w, h)
o = {
x = x, y = y, w = w, h = h,
items = {},
}
setmetatable(o, self)
self.__index = self
return o
end
function List:add(item, file, cb)
local x, y = self.x+2, self.y+((self.h+1)*(#self.items-1)+1)
local w, h = self.w-3, self.h
table.insert(self.items, {item = item, file = file, cb = cb, x = x, y = y, w = w, h = h, hover = false})
end
function List:update(dt)
local mx, my = love.mouse.getPosition()
for i, item in ipairs(self.items) do
item.hover = mx >= item.x and mx <= (item.x+item.w) and my >= item.y and my <= (item.y+item.h)
end
end
function List:mousepressed(mx, my, b)
for i, item in ipairs(self.items) do
if item.hover then return item.cb() end
end
end
function List:draw()
love.graphics.setFont(self.font)
love.graphics.setColor(0.18, 0.61, 1)
for i, item in ipairs(self.items) do
love.graphics.setColor(0, 0, 0, item.hover and 0.49 or 0.24)
love.graphics.rectangle("fill", item.x+1, item.y+1, item.w-3, item.h)
love.graphics.setColor(1, 1, 1, item.hover and 1 or 0.49)
love.graphics.print(item.item, item.x+10, item.y+6)
end
end
return List

View File

@ -1,69 +1,38 @@
-- Example: Normal map Example
local LightWorld = require "lib"
local image, image_normal
local lightWorld, lightMouse
function love.load()
x = 0
y = 0
scale = 1
local function load()
-- load images
image = love.graphics.newImage("examples/gfx/rock.png")
image_normal = love.graphics.newImage("examples/gfx/rock_n.png")
-- create light world
lightWorld = LightWorld({
ambient = {55,55,55},
refractionStrength = 32.0,
reflectionVisibility = 0.75,
})
lightWorld = LightWorld({ambient = {0.21,0.21,0.21}})
-- create light
lightMouse = lightWorld:newLight(0, 0, 160, 160, 160, 300)
lightMouse:setGlowStrength(0.3)
--lightMouse.normalInvert = true
lightMouse.normalInvert = true
-- create shadow bodys
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
imageTest = lightWorld:newImage(image, w/2, h/2)
imageTest:setNormalMap(image_normal)
lightWorld:newImage(image, w/2, h/2):setNormalMap(image_normal)
end
function love.update(dt)
local function update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("up") then
y = y - dt * 200
elseif love.keyboard.isDown("down") then
y = y + dt * 200
end
if love.keyboard.isDown("left") then
x = x - dt * 200
elseif love.keyboard.isDown("right") then
x = x + dt * 200
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
lightWorld:update(dt)
lightMouse:setPosition(love.mouse.getX()/scale, love.mouse.getY()/scale)
lightMouse:setPosition(love.mouse.getX(), love.mouse.getY())
end
function love.draw()
lightWorld:setTranslation(x,y,scale)
love.graphics.push()
love.graphics.translate(x, y)
love.graphics.scale(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()
local function draw()
love.graphics.clear(0.21,0.21,0.21)
lightWorld:draw(function(l, t, w, h, s)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(image, w/2-(image:getWidth()/2), h/2-(image:getHeight()/2))
end)
end
return {
load = load,
update = update,
draw = draw,
}

View File

@ -1,25 +1,17 @@
-- Example: Only Postshader Example
local PostShader = require "lib/postshader"
local colorAberration = 0
local post_shader, render_buffer
local img
function love.load()
testShader = 0
x = 0
y = 0
scale = 1
colorAberration = 0.0
-- load images
image = love.graphics.newImage("examples/gfx/machine2.png")
quadScreen = love.graphics.newQuad(0, 0, love.graphics.getWidth() + 32, love.graphics.getHeight() + 24, 32, 24)
imgFloor = love.graphics.newImage("examples/gfx/floor.png")
imgFloor:setWrap("repeat", "repeat")
local function load()
post_shader = PostShader()
render_buffer = love.graphics.newCanvas(love.graphics.getWidth(), love.graphics.getHeight())
img = love.graphics.newImage("examples/gfx/kingscard.jpeg")
end
function love.keypressed(k)
local function keypressed(k)
if k == "1" then
post_shader:toggleEffect("four_colors", {15, 56, 15}, {48, 98, 48}, {139, 172, 15}, {155, 188, 15})
post_shader:toggleEffect("four_colors", {0.05, 0.21, 0.05}, {0.18, 0.38, 0.18}, {0.54, 0.67, 0.05}, {0.60, 0.73, 0.05})
elseif k == "2" then
post_shader:toggleEffect("monochrome")
elseif k == "3" then
@ -27,93 +19,63 @@ function love.keypressed(k)
elseif k == "4" then
post_shader:toggleEffect("tilt_shift", 4.0)
elseif k == "5" then
post_shader:toggleEffect("bloom", 2.0, 0.25)
post_shader:toggleEffect("bloom", 2.0, 0.25)
elseif k == "6" then
post_shader:toggleEffect("blur", 2.0, 2.0)
post_shader:toggleEffect("blur", 10.0, 10.0)
elseif k == "7" then
post_shader:toggleEffect("black_and_white")
post_shader:toggleEffect("black_and_white")
elseif k == "8" then
post_shader:toggleEffect("curvature")
post_shader:toggleEffect("curvature")
elseif k == "9" then
post_shader:toggleEffect("edges")
post_shader:toggleEffect("edges")
elseif k == "0" then
post_shader:toggleEffect("hdr_tv")
post_shader:toggleEffect("hdr_tv")
elseif k == "q" then
post_shader:toggleEffect("phosphor")
post_shader:toggleEffect("phosphor")
elseif k == "w" then
post_shader:toggleEffect("phosphorish")
post_shader:toggleEffect("phosphorish")
elseif k == "e" then
post_shader:toggleEffect("pip")
post_shader:toggleEffect("pip")
elseif k == "r" then
post_shader:toggleEffect("pixellate")
post_shader:toggleEffect("pixellate")
elseif k == "t" then
post_shader:toggleEffect("radialblur")
post_shader:toggleEffect("radialblur")
elseif k == "y" then
post_shader:toggleEffect("waterpaint")
elseif k == "c" then
if colorAberration == 0.0 then
colorAberration = 3.0
end
post_shader:toggleEffect("waterpaint")
elseif k == "c" then
colorAberration = 3.0
post_shader:addEffect("blur", 2.0, 2.0)
post_shader:addEffect("chromatic_aberration")
end
end
function love.update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("down") then
y = y - dt * 200
elseif love.keyboard.isDown("up") then
y = y + dt * 200
end
if love.keyboard.isDown("right") then
x = x - dt * 200
elseif love.keyboard.isDown("left") then
x = x + dt * 200
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
colorAberration = math.max(0.0, colorAberration - dt * 10.0)
if colorAberration > 0.0 then
post_shader:addEffect("blur", 2.0, 2.0)
post_shader:addEffect("chromatic_aberration")
else
post_shader:removeEffect("blur")
post_shader:removeEffect("chromatic_aberration")
end
lightMouse:setPosition(love.mouse.getX()/scale, love.mouse.getY()/scale)
local function update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
colorAberration = math.max(0.0, colorAberration - dt * 10.0)
if colorAberration <= 0.0 then
post_shader:removeEffect("blur")
post_shader:removeEffect("chromatic_aberration")
end
end
function love.draw()
local function draw()
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
love.graphics.push()
love.graphics.setCanvas(render_buffer)
love.graphics.clear()
love.graphics.translate(x, y)
love.graphics.scale(scale)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(imgFloor, quadScreen, 0,0)
love.graphics.setColor(63, 255, 127)
love.graphics.circle("fill", 256, 256, 16)
love.graphics.rectangle("fill", 512, 512, 64, 64)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(image, 64 - image:getWidth() * 0.5, 64 - image:getHeight() * 0.5)
love.graphics.pop()
love.graphics.setCanvas(render_buffer)
love.graphics.clear()
love.graphics.setColor(1, 1, 1)
love.graphics.draw(img, 250, 100, 0, 0.5)
love.graphics.setCanvas()
post_shader:drawWith(render_buffer)
love.graphics.setBlendMode("alpha")
love.graphics.setColor(0, 0, 0, 191)
love.graphics.setColor(0, 0, 0, 0.74)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), 24)
love.graphics.setColor(0, 255, 0)
love.graphics.print("To toggle postshaders, use 0-9 and q->y, to scale use - and =, and to translate use arrows")
love.graphics.setColor(0, 1, 0)
love.graphics.print("To toggle postshaders, use 0-9 and q->y, c for chromatic aberration")
end
return {
load = load,
update = update,
draw = draw,
keypressed = keypressed,
}

View File

@ -1,178 +0,0 @@
-- Example: Short Example
local LightWorld = require "lib"
function love.load()
testShader = 0
x = 0
y = 0
z = 1
scale = 1
colorAberration = 0.0
-- load images
image = love.graphics.newImage("examples/gfx/machine2.png")
image_normal = love.graphics.newImage("examples/gfx/cone_normal.png")
normal = love.graphics.newImage("examples/gfx/refraction_normal.png")
glow = love.graphics.newImage("examples/gfx/machine2_glow.png")
-- create light world
lightWorld = LightWorld({
ambient = {55,55,55},
refractionStrength = 32.0,
reflectionVisibility = 0.75,
shadowBlur = 0.0
})
-- create light
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 300)
lightMouse:setGlowStrength(0.3)
-- create shadow bodys
circleTest = lightWorld:newCircle(256, 256, 16)
rectangleTest = lightWorld:newRectangle(512, 512, 64, 64)
local px, py, pw, ph = 100, 200, 20, 50
polygonTest = lightWorld:newPolygon(
px, py,
px+pw, py,
px+pw, py+ph,
px-50, py+ph)
imageTest = lightWorld:newImage(image, 64, 64, 24, 6)
imageTest:setNormalMap(image_normal)
imageTest:setGlowMap(glow)
imageTest:setOffset(12, -10)
-- create body object
objectTest = lightWorld:newRefraction(normal, 64, 64, 128, 128)
objectTest:setReflection(true)
end
function love.keypressed(k)
if k == "1" then
lightWorld.post_shader:toggleEffect("four_colors", {15, 56, 15}, {48, 98, 48}, {139, 172, 15}, {155, 188, 15})
elseif k == "2" then
lightWorld.post_shader:toggleEffect("monochrome")
elseif k == "3" then
lightWorld.post_shader:toggleEffect("scanlines")
elseif k == "4" then
lightWorld.post_shader:toggleEffect("tilt_shift", 4.0)
elseif k == "5" then
lightWorld.post_shader:toggleEffect("bloom", 2.0, 0.25)
elseif k == "6" then
lightWorld.post_shader:toggleEffect("blur", 2.0, 2.0)
elseif k == "7" then
lightWorld.post_shader:toggleEffect("black_and_white")
elseif k == "8" then
lightWorld.post_shader:toggleEffect("curvature")
elseif k == "9" then
lightWorld.post_shader:toggleEffect("edges")
elseif k == "0" then
lightWorld.post_shader:toggleEffect("hdr_tv")
elseif k == "q" then
lightWorld.post_shader:toggleEffect("phosphor")
elseif k == "w" then
lightWorld.post_shader:toggleEffect("phosphorish")
elseif k == "e" then
lightWorld.post_shader:toggleEffect("pip")
elseif k == "r" then
lightWorld.post_shader:toggleEffect("pixellate")
elseif k == "t" then
lightWorld.post_shader:toggleEffect("radialblur")
elseif k == "y" then
lightWorld.post_shader:toggleEffect("waterpaint")
elseif k == "c" then
if colorAberration == 0.0 then
colorAberration = 3.0
end
end
end
function love.update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("down") then
y = y - dt * 200
elseif love.keyboard.isDown("up") then
y = y + dt * 200
end
if love.keyboard.isDown("right") then
x = x - dt * 200
elseif love.keyboard.isDown("left") then
x = x + dt * 200
end
if love.keyboard.isDown("k") then
polygonTest:move(0, -(dt * 200))
elseif love.keyboard.isDown("j") then
polygonTest:move(0, (dt * 200))
end
if love.keyboard.isDown("h") then
polygonTest:move(-(dt * 200), 0)
elseif love.keyboard.isDown("l") then
polygonTest:move((dt * 200), 0)
end
if love.keyboard.isDown("f") then
polygonTest:scale(0.005)
elseif love.keyboard.isDown("a") then
polygonTest:scale(-0.005)
end
if love.keyboard.isDown("s") then
polygonTest:rotate(0.05)
elseif love.keyboard.isDown("d") then
polygonTest:rotate(-0.05)
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
colorAberration = math.max(0.0, colorAberration - dt * 10.0)
if colorAberration > 0.0 then
lightWorld.post_shader:addEffect("blur", 2.0, 2.0)
lightWorld.post_shader:addEffect("chromatic_aberration")
else
lightWorld.post_shader:removeEffect("blur")
lightWorld.post_shader:removeEffect("chromatic_aberration")
end
lightWorld:update(dt)
lightMouse:setPosition((love.mouse.getX() - x)/scale, (love.mouse.getY() - y)/scale, z)
end
function love.mousepressed(x, y, c)
if c == "wu" then
z = z + 1
elseif c == "wd" then
z = z - 1
end
end
function love.draw()
lightWorld:setTranslation(x, y, scale)
love.graphics.push()
love.graphics.translate(x, y)
love.graphics.scale(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.polygon("fill", polygonTest: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")
love.graphics.setColor(0, 0, 0, 191)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), 24)
love.graphics.setColor(0, 255, 0)
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

51
examples/simple.lua Normal file
View File

@ -0,0 +1,51 @@
local LightWorld = require "lib"
local lightWorld, lightMouse
local image, image_normal, glow, circleTest, rectangleTest, imageTest, objectTest
local box_locations = {
{200, 200},
{600, 200},
{600, 400},
{200, 400},
}
local function load()
image = love.graphics.newImage("examples/gfx/machine.png")
normal = love.graphics.newImage("examples/gfx/machine_normal.png")
glow = love.graphics.newImage("examples/gfx/machine_glow.png")
-- create light world
lightWorld = LightWorld({ambient = {0.21,0.21,0.21}})
-- create light
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 300)
lightMouse:setGlowStrength(0.3)
-- create shadow bodys
for i, v in ipairs(box_locations) do
imageTest = lightWorld:newImage(image, v[1], v[2])
imageTest:setNormalMap(normal)
imageTest:setGlowMap(glow)
end
end
local function update(dt, x, y, scale)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
x, y, scale = x or 0, y or 0, scale or 1
lightMouse:setPosition((love.mouse.getX() - x)/scale, (love.mouse.getY() - y)/scale)
lightWorld:update(dt)
lightWorld:setTranslation(x, y, scale)
end
local function draw()
lightWorld:draw(function()
love.graphics.clear(1, 1, 1)
love.graphics.setColor(1, 1, 1)
for i, v in ipairs(box_locations) do
love.graphics.draw(image, v[1] - image:getWidth() * 0.5, v[2] - image:getHeight() * 0.5)
end
end)
end
return {
load = load,
update = update,
draw = draw,
}

View File

@ -1,81 +1,43 @@
-- Example: STI Example
local LightWorld = require "lib"
local sti = require 'examples.vendor.sti'
local lightWorld, map, image_normal, lightMouse
function love.load()
x = 0
y = 0
z = 1
scale = 1
local function load()
-- create light world
lightWorld = LightWorld({
ambient = {55,55,55},
shadowBlur = 0.0
})
lightWorld = LightWorld({ambient = {0.49, 0.49, 0.49}})
map = sti.new("examples/gfx/map.lua")
image_normal = love.graphics.newImage("examples/gfx/border_NRM.png")
-- create light
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 300)
lightMouse = lightWorld:newLight(0, 0, 1, 0.49, 0.24, 300)
lightMouse:setGlowStrength(0.3)
-- walls
-- create light blocking bodies for sections of the map
-- create walls
lightWorld:newRectangle(400, 32, 800, 64):setNormalMap(image_normal, 800, 64)
lightWorld:newRectangle(32, 272, 64, 416):setNormalMap(image_normal, 64, 416)
lightWorld:newRectangle(400, 464, 800, 32):setNormalMap(image_normal, 800, 32)
lightWorld:newRectangle(784, 272, 32, 416):setNormalMap(image_normal, 32, 416)
--blocks
-- create blocks
lightWorld:newRectangle(224, 256, 128, 124):setNormalMap(image_normal, 128, 124)
lightWorld:newRectangle(592, 224, 224, 64):setNormalMap(image_normal, 224, 64)
end
function love.update(dt)
local function update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("down") then
y = y - dt * 200
elseif love.keyboard.isDown("up") then
y = y + dt * 200
end
if love.keyboard.isDown("right") then
x = x - dt * 200
elseif love.keyboard.isDown("left") then
x = x + dt * 200
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
map:update(dt)
lightWorld:update(dt)
lightMouse:setPosition((love.mouse.getX() - x)/scale, (love.mouse.getY() - y)/scale, z)
lightMouse:setPosition(love.mouse.getX(), love.mouse.getY())
end
function love.mousepressed(x, y, c)
if c == "wu" then
z = z + 1
elseif c == "wd" then
z = z - 1
end
local function draw()
love.graphics.setBackgroundColor(1, 1, 1)
lightWorld:draw(function()
map:draw()
end)
end
function love.draw()
lightWorld:setTranslation(x, y, scale)
love.graphics.push()
love.graphics.translate(x, y)
love.graphics.scale(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)
map:draw()
end)
love.graphics.pop()
end
return {
load = load,
mousepressed = mousepressed,
update = update,
draw = draw,
}

View File

@ -1,193 +0,0 @@
local List = {}
function inside(mx, my, x, y, w, h)
return mx >= x and mx <= (x+w) and my >= y and my <= (y+h)
end
function List:new()
o = {}
setmetatable(o, self)
self.__index = self
o.items = {}
o.files = {}
o.x = 50
o.y = 70
o.width = 400
o.height = 500
o.item_height = 23
o.sum_item_height = 0
o.bar_size = 20
o.bar_pos = 0
o.bar_max_pos = 0
o.bar_width = 15
o.bar_lock = nil
return o
end
function List:add(item, file)
table.insert(self.items, item)
table.insert(self.files, file)
self.items.n = #self.items
self.bar_pos = 0
local num_items = (self.height/self.item_height)
local ratio = num_items/self.items.n
self.bar_size = self.height * ratio
self.bar_max_pos = self.height - self.bar_size - 3
self.sum_item_height = (self.item_height+1) * self.items.n + 2
end
function List:hasBar()
return self.sum_item_height > self.height
end
function List:getBarRatio()
return self.bar_pos/self.bar_max_pos
end
function List:getOffset()
local ratio = self.bar_pos/self.bar_max_pos
return math.floor((self.sum_item_height-self.height)*ratio + 0.5)
end
function List:update(dt)
if self.bar_lock then
local dy = math.floor(love.mouse.getY()-self.bar_lock.y+0.5)
self.bar_pos = self.bar_pos + dy
if self.bar_pos < 0 then
self.bar_pos = 0
elseif self.bar_pos > self.bar_max_pos then
self.bar_pos = self.bar_max_pos
end
self.bar_lock.y = love.mouse.getY()
end
end
function List:mousepressed(mx, my, b)
if self:hasBar() then
if b == 1 then
local x, y, w, h = self:getBarRect()
if inside(mx, my, x, y, w, h) then
self.bar_lock = { x = mx, y = my }
end
end
local per_pixel = (self.sum_item_height-self.height)/self.bar_max_pos
local bar_pixel_dt = math.floor(((self.item_height)*3)/per_pixel + 0.5)
if b == "wd" then
self.bar_pos = self.bar_pos + bar_pixel_dt
if self.bar_pos > self.bar_max_pos then
self.bar_pos = self.bar_max_pos
end
elseif b == "wu" then
self.bar_pos = self.bar_pos - bar_pixel_dt
if self.bar_pos < 0 then
self.bar_pos = 0
end
end
end
if b == 1 and inside(mx, my, self.x+2, self.y+1, self.width-3, self.height-3) then
local tx, ty = mx-self.x, my + self:getOffset() - self.y
local index = math.floor((ty/self.sum_item_height)*self.items.n)
local i = self.items[index+1]
local f = self.files[index+1]
if f then
exf.start(i, f)
end
end
end
function List:mousereleased(x, y, b)
if self:hasBar() then
if b == 1 then
self.bar_lock = nil
end
end
end
function List:getBarRect()
return self.x+self.width+2, self.y+1+self.bar_pos,
self.bar_width-3, self.bar_size
end
function List:getItemRect(i)
return self.x+2, self.y+((self.item_height+1)*(i-1)+1)-self:getOffset(),
self.width-3, self.item_height
end
function List:draw()
love.graphics.setLineWidth(2)
love.graphics.setLineStyle("rough")
love.graphics.setFont(self.font)
love.graphics.setColor(48/255, 156/255, 225 / 255)
local mx, my = love.mouse.getPosition()
-- Get interval to display.
local start_i = math.floor( self:getOffset()/(self.item_height+1) ) + 1
local end_i = start_i+math.floor( self.height/(self.item_height+1) ) + 1
if end_i > self.items.n then
end_i = self.items.n
end
love.graphics.setScissor(self.x, self.y, self.width, self.height)
-- Items.
for i = start_i,end_i do
local x, y, w, h = self:getItemRect(i)
local hover = inside(mx, my, x, y, w, h)
if hover then
love.graphics.setColor(0, 0, 0, 127/255)
else
love.graphics.setColor(0, 0, 0, 63/255)
end
love.graphics.rectangle("fill", x+1, y+i+1, w-3, h)
if hover then
love.graphics.setColor(1, 1, 1, 1)
else
love.graphics.setColor(1, 1, 1, 127/255)
end
local e_id = string.sub(self.items[i], 1, 5)
local e_rest = string.sub(self.items[i], 5)
love.graphics.print(e_id, x+10, y+i+6) --Updated y placement -- Used to change position of Example IDs
love.graphics.print(e_rest, x+50, y+i+6) --Updated y placement -- Used to change position of Example Titles
end
love.graphics.setScissor()
-- Bar.
if self:hasBar() then
local x, y, w, h = self:getBarRect()
local hover = inside(mx, my, x, y, w, h)
if hover or self.bar_lock then
love.graphics.setColor(0, 0, 0, 127/255)
else
love.graphics.setColor(0, 0, 0, 63/255)
end
love.graphics.rectangle("fill", x, y, w, h)
end
-- Border.
love.graphics.setColor(0, 0, 0, 63/255)
love.graphics.rectangle("line", self.x+self.width, self.y, self.bar_width, self.height)
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
return List

127
main.lua
View File

@ -1,128 +1,5 @@
--
-- EXF: Example Framework
--
-- This should make examples easier and more enjoyable to use.
-- All examples in one application! Yaay!
--
-- Updated by Dresenpai
require "lib/postshader"
local LightWorld = require "lib"
local ProFi = require 'examples.vendor.ProFi'
local List = require 'examples.vendor.list'
exf = {}
exf.current = nil
exf.available = {}
local mainlist = require('examples.examples_list')
function love.load()
exf.list = List:new()
exf.smallfont = love.graphics.newFont(12)
exf.bigfont = love.graphics.newFont(24)
exf.list.font = exf.smallfont
exf.bigball = love.graphics.newImage("examples/gfx/love-big-ball.png")
-- Find available demos.
local files = love.filesystem.getDirectoryItems("examples")
local n = 0
for i, v in ipairs(files) do
is_file = (love.filesystem.getInfo("examples/".. v ).type == "file")
if is_file then
n = n + 1
table.insert(exf.available, v);
local file = love.filesystem.newFile(v, love.file_read)
file:open("r")
local contents = love.filesystem.read("examples/" .. v, 100)
local s, e, c = string.find(contents, "Example: ([%a%p ]-)[\r\n]")
file:close(file)
if not c then c = "Untitled" end
local title = string.format("%04d", n) .. " " .. c .. " (" .. v .. ")"
exf.list:add(title, v)
end
end
exf.load()
end
function exf.empty() end
function exf.keypressed(k) end
function exf.keyreleased(k) end
function exf.mousepressed(x, y, b) exf.list:mousepressed(x, y, b) end
function exf.mousereleased(x, y, b) exf.list:mousereleased(x, y, b) end
function exf.load()
ProFi:stop()
ProFi:writeReport( 'light_world_profiling_report.txt' )
load = nil
love.update = exf.update
love.draw = exf.draw
love.keypressed = exf.keypressed
love.keyreleased = exf.keyreleased
love.mousepressed = exf.mousepressed
love.mousereleased = exf.mousereleased
love.mouse.setVisible(true)
love.window.setTitle("LOVE Example Browser")
lightWorld = LightWorld({ambient = {0.49, 0.49, 0.49}})
lightMouse = lightWorld:newLight(20, 20, 1, 0.49, 0.24, 500)
lightMouse:setSmooth(2)
circleTest = lightWorld:newCircle(800 - 128, 600 - 128, exf.bigball:getWidth()*0.5)
end
function exf.update(dt)
exf.list:update(dt)
lightMouse:setPosition(love.mouse.getX(), love.mouse.getY())
lightWorld:update(dt)
end
function exf.draw()
lightWorld:draw(function()
love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setColor(0.18, 0.61, 1)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.setColor(1, 1, 1, 1)
love.graphics.setFont(exf.bigfont)
love.graphics.print("Examples:", 50, 30)
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()
love.graphics.setColor(1, 1, 1)
love.graphics.draw(exf.bigball, 800 - 128, 600 - 128, love.timer.getTime()/5, 1, 1, exf.bigball:getWidth() * 0.5, exf.bigball:getHeight() * 0.5)
end)
end
function exf.start(item, file)
love.load = exf.empty
love.update = exf.empty
love.draw = exf.empty
love.keypressed = exf.empty
love.keyreleased = exf.empty
love.mousepressed = exf.empty
love.mousereleased = exf.empty
love.filesystem.load("examples/" .. file)()
love.graphics.setBackgroundColor(0,0,0)
love.graphics.setColor(1, 1, 1)
love.graphics.setLineWidth(1)
love.graphics.setLineStyle("smooth")
love.graphics.setBlendMode("alpha")
love.mouse.setVisible(true)
local o_keypressed = love.keypressed
love.keypressed = function(k)
if k == "escape" then
exf.load()
end
o_keypressed(k)
end
love.load()
mainlist.start("examples_list.lua")
end