Skip to content

Commit

Permalink
Luven v0.9
Browse files Browse the repository at this point in the history
Added error handling
And done some benchmark

Very close to release.
  • Loading branch information
halsten-dev committed Feb 20, 2019
1 parent bd5e326 commit 79b4cf1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 29 deletions.
6 changes: 5 additions & 1 deletion conf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ function love.conf(t)
t.window.title = "Luven - Exemple"
t.window.width = 1280
t.window.height = 720
t.window.display = 1
t.window.display = 3
t.window.highdpi = false
t.window.vsync = 1
t.window.msaa = 0
t.gammacorrect = false

t.console = true
end
116 changes: 93 additions & 23 deletions luven.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local luven = {
_VERSION = 'luven v0.8',
_VERSION = 'luven v0.9',
_URL = 'https://github.com/lionelleeser/Luven',
_DESCRIPTION = 'A minimalitic lighting system for Löve2D',
_CONTRIBUTORS = 'Lionel Leeser, Pedro Gimeno (Help with shader and camera)',
Expand Down Expand Up @@ -28,6 +28,33 @@ local luven = {
]]
}

-- ///////////////////////////////////////////////
-- /// Luven error management local functions
-- ///////////////////////////////////////////////

local function assertPositiveNumber(functionName, parameterName, parameterValue, level)
level = level or 3
if ((type(parameterValue) ~= "number") or (parameterValue < 0)) then
error(functionName .. "\n parameter : " .. parameterName .. ", expected positive number.", 3)
end -- if
end -- function

local function assertRangeNumber(functionName, parameterName, parameterValue, min, max, level)
min = min or 0
max = max or 1
level = level or 3
if ((type(parameterValue) ~= "number") or (parameterValue < min) or (parameterValue > max)) then
error(functionName .. "\n parameter : " .. parameterName .. ", expected range number between " .. min .. " and " .. max .. ".", 3)
end -- if
end -- function

local function assertType(functionName, parameterName, parameterValue, parameterType, level)
level = level or 3
if (type(parameterValue) ~= parameterType) then
error(functionName .. "\n parameter : " .. parameterName .. ", expected type ".. parameterType .. ".", 3)
end -- if
end -- function

-- ///////////////////////////////////////////////
-- /// Luven camera
-- ///////////////////////////////////////////////
Expand Down Expand Up @@ -70,6 +97,9 @@ end -- function
-- //////////////////////////////

function luven.camera:init(x, y)
local functionName = "luven.camera:init(x, y)"
assertType(functionName, "x", x, "number")
assertType(functionName, "y", y, "number")
self.transform = love.math.newTransform(x, y)
self.x = x
self.y = y
Expand Down Expand Up @@ -135,21 +165,27 @@ local shader_code = [[
const float linear = 0.09;
const float quadratic = 0.032;
vec2 norm_pos;
vec2 norm_screen;
vec3 diffuse;
vec4 pixel;
float distance;
float attenuation;
vec4 effect(vec4 color, Image image, vec2 uvs, vec2 screen_coords){
vec4 pixel = Texel(image, uvs);
pixel = Texel(image, uvs);
pixel *= color;
vec2 norm_screen = screen_coords / screen;
vec3 diffuse = ambientLightColor;
norm_screen = screen_coords / screen;
diffuse = ambientLightColor;
for (int i = 0; i < NUM_LIGHTS; i++) {
if (lights[i].enabled) {
Light light = lights[i];
vec2 norm_pos = (viewMatrix * vec4(light.position, 0.0, 1.0)).xy / screen;
float distance = length(norm_pos - norm_screen) / (light.power / 1000);
float attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
diffuse += light.diffuse * attenuation;
norm_pos = (viewMatrix * vec4(lights[i].position, 0.0, 1.0)).xy / screen;
distance = length(norm_pos - norm_screen) / (lights[i].power / 1000);
attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
diffuse += lights[i].diffuse * attenuation;
}
}
Expand Down Expand Up @@ -217,12 +253,7 @@ local function getNextId()
end -- if
end -- for

if (getNumberLights() == NUM_LIGHTS) then
-- All lights are in use
error("Light cannot be created, no more light available!")
else
return 0 -- first index
end -- if
return 0 -- first index
end -- function

local function randomFloat(min, max)
Expand All @@ -240,7 +271,8 @@ local function generateFlicker(lightId)

light.flickTimer = randomFloat(light.speedRange.min, light.speedRange.max)

updateLight(light.id)
luvenShader:send(light.name .. ".diffuse", light.color)
luvenShader:send(light.name .. ".power", light.power)
end -- if

-- ///////////////////////////////////////////////
Expand All @@ -256,6 +288,11 @@ function luven.init(screenWidth, screenHeight, useCamera)
useIntegratedCamera = true
end -- if

local functionName = "luven.init( [ screenWidth ], [ screenHeight ], [ useCamera ] )"
assertPositiveNumber(functionName, "screenWidth", screenWidth)
assertPositiveNumber(functionName, "screenHeight", screenHeight)
assertType(functionName, "useCamera", useIntegratedCamera, "boolean")

luvenShader = love.graphics.newShader(shader_code)
luvenShader:send("screen", {
screenWidth,
Expand All @@ -273,6 +310,14 @@ function luven.setAmbientLightColor(color)
luvenShader:send("ambientLightColor", color)
end -- function

function luven.getLightCount()
return getNumberLights()
end -- function

function luven.sendCustomViewMatrix(viewMatrix)
luvenShader:send("viewMatrix", viewMatrix)
end -- function

function luven.update(dt)
if (useIntegratedCamera) then
cameraUpdate(dt)
Expand All @@ -299,10 +344,6 @@ function luven.update(dt)
end -- for
end -- function

function luven.sendCustomViewMatrix(viewMatrix)
luvenShader:send("viewMatrix", viewMatrix)
end -- function

function luven.drawBegin()
if (useIntegratedCamera) then
cameraDraw()
Expand Down Expand Up @@ -340,6 +381,14 @@ end -- if
-- param : color = { r, g, b } (values between 0 - 1)
-- return : lightId
function luven.addNormalLight(x, y, color, power)
local functionName = "luven.addNormalLight(x, y, color, power)"
assertType(functionName, "x", x, "number")
assertType(functionName, "y", y, "number")
assertRangeNumber(functionName, "color[1]", color[1])
assertRangeNumber(functionName, "color[2]", color[2])
assertRangeNumber(functionName, "color[3]", color[3])
assertPositiveNumber(functionName, "power", power)

local light = {}

light.id = getNextId()
Expand All @@ -361,6 +410,20 @@ end -- function
-- speedRange = { min = n, max = n }
-- return : lightId
function luven.addFlickeringLight(x, y, colorRange, powerRange, speedRange)
local functionName = "luven.addFlickeringLight(x, y, colorRange, powerRange, speedRange)"
assertType(functionName, "x", x, "number")
assertType(functionName, "y", y, "number")
assertRangeNumber(functionName, "colorRange.min[1]", colorRange.min[1])
assertRangeNumber(functionName, "colorRange.min[2]", colorRange.min[2])
assertRangeNumber(functionName, "colorRange.min[3]", colorRange.min[3])
assertRangeNumber(functionName, "colorRange.max[1]", colorRange.max[1])
assertRangeNumber(functionName, "colorRange.max[2]", colorRange.max[2])
assertRangeNumber(functionName, "colorRange.max[3]", colorRange.max[3])
assertPositiveNumber(functionName, "powerRange.min", powerRange.min)
assertPositiveNumber(functionName, "powerRange.max", powerRange.max)
assertPositiveNumber(functionName, "speedRange.min", speedRange.min)
assertPositiveNumber(functionName, "speedRange.max", speedRange.max)

local light = {}

light.id = getNextId()
Expand All @@ -385,6 +448,15 @@ function luven.addFlickeringLight(x, y, colorRange, powerRange, speedRange)
end -- function

function luven.addFlashingLight(x, y, color, maxPower, speed)
local functionName = "luven.addFlashingLight(x, y, color, maxPower, speed)"
assertType(functionName, "x", x, "number")
assertType(functionName, "y", y, "number")
assertRangeNumber(functionName, "color[1]", color[1])
assertRangeNumber(functionName, "color[2]", color[2])
assertRangeNumber(functionName, "color[3]", color[3])
assertPositiveNumber(functionName, "maxPower", maxPower)
assertPositiveNumber(functionName, "speed", speed)

local light = {}

light.id = getNextId()
Expand All @@ -400,8 +472,6 @@ function luven.addFlashingLight(x, y, color, maxPower, speed)

light.enabled = true

print(light.id)

registerLight(light)
end -- function

Expand Down
13 changes: 8 additions & 5 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local image = nil
local moveSpeed = 150

local lightId = 0
local lightId2 = 0

function love.load()
image = love.graphics.newImage("Background.png")
Expand All @@ -20,11 +19,8 @@ function love.load()
Luven.camera:setScale(zoom)

lightId = Luven.addFlickeringLight(600, 400, { min = { 0.8, 0.0, 0.8 }, max = { 1.0, 0.0, 1.0 } }, { min = 2, max = 3 }, { min = 0.2, max = 0.3 })
lightId2 = Luven.addNormalLight(700, 400, {1.0, 1.0, 0.0 }, 10)
lightId = Luven.addNormalLight(700, 400, { 1.0, 0.0 , 1.0 }, 10)

print(Inspect.inspect(Luven.getLightColor(lightId)))
print(Luven.getLightPower(lightId))
print(Luven.getLightPosition(lightId))
-- Luven.setLightPower(lightId, 5)
-- Luven.setLightPower(lightId2, 19)
-- Luven.setLightColor(lightId, { 1.0, 0.0, 1.0 })
Expand Down Expand Up @@ -60,6 +56,10 @@ function love.keypressed(key)
Luven.camera:setShake(0.7, 5.5)
end -- if

if (key == "l") then
lightId = Luven.addNormalLight(Luven.camera.x, Luven.camera.y, { 1.0, 1.0 , 1.0 }, 10)
end -- if

if (key == "f") then
Luven.addFlashingLight(Luven.camera.x, Luven.camera.y, { 1.0, 0.0, 0.0 }, 15, 0.05)
end -- if
Expand All @@ -71,6 +71,9 @@ function love.draw()
love.graphics.draw(image, 0, 0, 0, 0.5, 0.5)

Luven.drawEnd()

love.graphics.print("Current FPS: " .. tostring(love.timer.getFPS()), 10, 10)
--love.graphics.print("Number of lights: " .. tostring(Luven.getLightCount()), 10, 30)
end -- function

function love.quit()
Expand Down

0 comments on commit 79b4cf1

Please sign in to comment.