optimization in whole tiles creation
This commit is contained in:
parent
dedb0d884c
commit
75b7aad1e6
|
@ -50,11 +50,11 @@ end
|
||||||
function GameDraw()
|
function GameDraw()
|
||||||
|
|
||||||
GameworldDrawPrepare()
|
GameworldDrawPrepare()
|
||||||
GameworldDrawParticles()
|
|
||||||
GameworldDrawBackground()
|
GameworldDrawBackground()
|
||||||
GameworldDrawLighting()
|
|
||||||
GameworldDrawEntities()
|
|
||||||
GameworldDrawForeground()
|
GameworldDrawForeground()
|
||||||
|
GameworldDrawLighting()
|
||||||
|
GameworldDrawParticles()
|
||||||
|
GameworldDrawEntities()
|
||||||
GameworldDrawEnd()
|
GameworldDrawEnd()
|
||||||
|
|
||||||
-- hud
|
-- hud
|
||||||
|
|
|
@ -20,7 +20,7 @@ function GameworldDrawBackground()
|
||||||
for j = 1, #LevelTiles[i] do
|
for j = 1, #LevelTiles[i] do
|
||||||
if LevelTiles[i][j].id ~= 0 then
|
if LevelTiles[i][j].id ~= 0 then
|
||||||
|
|
||||||
local depth = TileGetDepth(LevelTiles[i][j])
|
local depth = TileData[LevelTiles[i][j].id].depth
|
||||||
DrawTile(
|
DrawTile(
|
||||||
LevelTiles[i][j],
|
LevelTiles[i][j],
|
||||||
tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.width) - Camera.pos.x,
|
tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.width) - Camera.pos.x,
|
||||||
|
@ -53,7 +53,7 @@ function GameworldDrawForeground()
|
||||||
for j = 1, #LevelTiles[i] do
|
for j = 1, #LevelTiles[i] do
|
||||||
if LevelTiles[i][j].id ~= 0 then
|
if LevelTiles[i][j].id ~= 0 then
|
||||||
|
|
||||||
local depth = TileGetDepth(LevelTiles[i][j])
|
local depth = TileData[LevelTiles[i][j].id].depth
|
||||||
DrawTile(
|
DrawTile(
|
||||||
LevelTiles[i][j],
|
LevelTiles[i][j],
|
||||||
tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.width) - Camera.pos.x,
|
tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.width) - Camera.pos.x,
|
||||||
|
@ -82,7 +82,8 @@ function GameworldDrawLighting()
|
||||||
DoBorder()
|
DoBorder()
|
||||||
-- apply to game canvas
|
-- apply to game canvas
|
||||||
love.graphics.setColor(1,1,1,1)
|
love.graphics.setColor(1,1,1,1)
|
||||||
|
love.graphics.scale(game.scale,game.scale)
|
||||||
love.graphics.setCanvas()
|
love.graphics.setCanvas()
|
||||||
love.graphics.scale(1,1)
|
|
||||||
DrawDarkness()
|
DrawDarkness()
|
||||||
|
love.graphics.scale(1/game.scale,1/game.scale)
|
||||||
end
|
end
|
||||||
|
|
|
@ -230,24 +230,6 @@ function SetTile(i,j,id)
|
||||||
LevelTiles[i][j] = InstanceTile(id)
|
LevelTiles[i][j] = InstanceTile(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
function TileGetType(tile)
|
|
||||||
if TileData[tile.id] ~= nil then
|
|
||||||
return TileData[tile.id].type
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function TileGetDepth(tile)
|
|
||||||
if TileData[tile.id] ~= nil then
|
|
||||||
return TileData[tile.id].depth
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function TileGetLight(tile)
|
|
||||||
if TileData[tile.id] ~= nil then
|
|
||||||
return TileData[tile.id].light
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function GridDisplay()
|
function GridDisplay()
|
||||||
for i = 1, #LevelTiles do
|
for i = 1, #LevelTiles do
|
||||||
for j = 1, #LevelTiles[i] do
|
for j = 1, #LevelTiles[i] do
|
||||||
|
@ -262,17 +244,75 @@ function GridDisplay()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function TileOptimizeObjects()
|
||||||
|
print("new optimization")
|
||||||
|
local unoptimized = 0
|
||||||
|
local isTileOptimized = {}
|
||||||
|
|
||||||
|
for i = 1, #LevelTiles do
|
||||||
|
isTileOptimized[i] = {}
|
||||||
|
for j= 1, #LevelTiles[i] do
|
||||||
|
isTileOptimized[i][j] = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, #LevelTiles do
|
||||||
|
for j = 1, #LevelTiles[i] do
|
||||||
|
if LevelTiles[i][j].id ~= 0 then
|
||||||
|
local type = TileData[LevelTiles[i][j].id].type
|
||||||
|
local light = TileData[LevelTiles[i][j].id].light
|
||||||
|
|
||||||
|
if type == "whole" and not isTileOptimized[i][j] then
|
||||||
|
isTileOptimized[i][j] = true
|
||||||
|
local n = 1
|
||||||
|
local check = true
|
||||||
|
while check do
|
||||||
|
check = false
|
||||||
|
if LevelTiles[i][j+n] ~= nil then
|
||||||
|
if TileData[LevelTiles[i][j+n].id] ~= nil then
|
||||||
|
local type_check = TileData[LevelTiles[i][j+n].id].type
|
||||||
|
if type_check == "whole" and not isTileOptimized[i][j+n] then
|
||||||
|
check = true
|
||||||
|
isTileOptimized[i][j+n] = true
|
||||||
|
n = n + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print(n)
|
||||||
|
unoptimized = unoptimized + n
|
||||||
|
local base_x = tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.height)
|
||||||
|
local base_y = tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height)
|
||||||
|
|
||||||
|
local col = Collision:New(
|
||||||
|
base_x,
|
||||||
|
base_y,
|
||||||
|
base_x + tileProperties.width * tileProperties.scale * n,
|
||||||
|
base_y + tileProperties.height * tileProperties.scale
|
||||||
|
)
|
||||||
|
table.insert(LoadedObjects.Collisions,col)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print("collisions optimized from " .. unoptimized .. " to " .. #LoadedObjects.Collisions)
|
||||||
|
end
|
||||||
|
|
||||||
function TileCreateObjects()
|
function TileCreateObjects()
|
||||||
LoadedObjects.Collisions = {}
|
LoadedObjects.Collisions = {}
|
||||||
LoadedObjects.Platforms = {}
|
LoadedObjects.Platforms = {}
|
||||||
LoadedObjects.Ladders = {}
|
LoadedObjects.Ladders = {}
|
||||||
|
|
||||||
|
TileOptimizeObjects()
|
||||||
|
|
||||||
for i = 1, #LevelTiles do
|
for i = 1, #LevelTiles do
|
||||||
for j = 1, #LevelTiles[i] do
|
for j = 1, #LevelTiles[i] do
|
||||||
if LevelTiles[i][j].id ~= 0 then
|
if LevelTiles[i][j].id ~= 0 then
|
||||||
|
|
||||||
local type = TileGetType(LevelTiles[i][j])
|
local type = TileData[LevelTiles[i][j].id].type
|
||||||
local light = TileGetLight(LevelTiles[i][j])
|
local light = TileData[LevelTiles[i][j].id].light
|
||||||
local base_x = tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.height)
|
local base_x = tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.height)
|
||||||
local base_y = tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height)
|
local base_y = tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height)
|
||||||
|
|
||||||
|
@ -285,7 +325,8 @@ function TileCreateObjects()
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if type == "whole" then
|
-- wholes are handled in optimization now
|
||||||
|
--[[if type == "whole" then
|
||||||
local col = Collision:New(
|
local col = Collision:New(
|
||||||
base_x,
|
base_x,
|
||||||
base_y,
|
base_y,
|
||||||
|
@ -293,8 +334,7 @@ function TileCreateObjects()
|
||||||
base_y + tileProperties.height * tileProperties.scale
|
base_y + tileProperties.height * tileProperties.scale
|
||||||
)
|
)
|
||||||
table.insert(LoadedObjects.Collisions,col)
|
table.insert(LoadedObjects.Collisions,col)
|
||||||
|
else]]if type == "half_bottom" then
|
||||||
elseif type == "half_bottom" then
|
|
||||||
|
|
||||||
local col = Collision:New(
|
local col = Collision:New(
|
||||||
base_x,
|
base_x,
|
||||||
|
|
|
@ -59,20 +59,25 @@ function DoLights()
|
||||||
light.flicker = math.min(math.max(light.flicker, -light.flicker_value),light.flicker_value)
|
light.flicker = math.min(math.max(light.flicker, -light.flicker_value),light.flicker_value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
love.graphics.setBlendMode("replace")
|
love.graphics.setBlendMode("replace")
|
||||||
for _, light in pairs(Lights) do
|
for _, light in pairs(Lights) do
|
||||||
if light.range ~= 0 then
|
if light.range ~= 0 then
|
||||||
love.graphics.setColor(light.color[1],light.color[2],light.color[3],1)
|
love.graphics.setColor(light.color[1],light.color[2],light.color[3],1)
|
||||||
|
local position = {
|
||||||
|
x = (light.pos.x - Camera.pos.x) / game.scale,
|
||||||
|
y = (light.pos.y - Camera.pos.y) / game.scale
|
||||||
|
}
|
||||||
|
local range = (1 + light.range + light.flicker) / game.scale
|
||||||
love.graphics.circle(
|
love.graphics.circle(
|
||||||
"fill",
|
"fill",
|
||||||
(light.pos.x - Camera.pos.x) / game.scale,
|
position.x,
|
||||||
(light.pos.y - Camera.pos.y) / game.scale,
|
position.y,
|
||||||
(light.range + light.flicker + 0) / game.scale
|
range
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
love.graphics.setColor(0,0,0,0)
|
||||||
myShader:send("game_scale", game.scale)
|
|
||||||
for _, light in pairs(Lights) do
|
for _, light in pairs(Lights) do
|
||||||
if light.range ~= 0 then
|
if light.range ~= 0 then
|
||||||
local position = {
|
local position = {
|
||||||
|
@ -80,21 +85,30 @@ function DoLights()
|
||||||
y = (light.pos.y - Camera.pos.y) / game.scale
|
y = (light.pos.y - Camera.pos.y) / game.scale
|
||||||
}
|
}
|
||||||
local range = (light.range + light.flicker) / game.scale
|
local range = (light.range + light.flicker) / game.scale
|
||||||
love.graphics.setBlendMode("replace")
|
|
||||||
love.graphics.setColor(0,0,0,0)
|
|
||||||
love.graphics.setShader()
|
|
||||||
love.graphics.circle(
|
love.graphics.circle(
|
||||||
"fill",
|
"fill",
|
||||||
position.x,
|
position.x,
|
||||||
position.y,
|
position.y,
|
||||||
range
|
range
|
||||||
)
|
)
|
||||||
love.graphics.setBlendMode("alpha")
|
end
|
||||||
love.graphics.setColor(1,1,1)
|
end
|
||||||
myShader:send("light_color", light.color)
|
|
||||||
myShader:send("light_pos", {position.x*game.scale, position.y*game.scale})
|
love.graphics.setBlendMode("alpha")
|
||||||
myShader:send("range", range)
|
love.graphics.setColor(0,0,0,1)
|
||||||
love.graphics.setShader(myShader)
|
Shaders.InsideLight:send("game_scale", game.scale)
|
||||||
|
|
||||||
|
for _, light in pairs(Lights) do
|
||||||
|
if light.range ~= 0 then
|
||||||
|
local position = {
|
||||||
|
x = (light.pos.x - Camera.pos.x) / game.scale,
|
||||||
|
y = (light.pos.y - Camera.pos.y) / game.scale
|
||||||
|
}
|
||||||
|
local range = (light.range + light.flicker) / game.scale
|
||||||
|
Shaders.InsideLight:send("light_color", light.color)
|
||||||
|
Shaders.InsideLight:send("light_pos", {position.x*game.scale, position.y*game.scale})
|
||||||
|
Shaders.InsideLight:send("range", range)
|
||||||
|
love.graphics.setShader(Shaders.InsideLight)
|
||||||
love.graphics.circle(
|
love.graphics.circle(
|
||||||
"fill",
|
"fill",
|
||||||
position.x,
|
position.x,
|
||||||
|
@ -115,5 +129,5 @@ function DoBorder()
|
||||||
end
|
end
|
||||||
|
|
||||||
function DrawDarkness()
|
function DrawDarkness()
|
||||||
love.graphics.draw(Canvas.Darkness, 0, 0, 0, game.scale)
|
love.graphics.draw(Canvas.Darkness, 0, 0, 0, 1/game.scale)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
myShader = love.graphics.newShader[[
|
Shaders = {}
|
||||||
|
Shaders.InsideLight = love.graphics.newShader[[
|
||||||
uniform vec2 light_pos;
|
uniform vec2 light_pos;
|
||||||
uniform vec3 light_color;
|
uniform vec3 light_color;
|
||||||
uniform float range;
|
uniform float range;
|
||||||
|
@ -10,12 +11,10 @@ myShader = love.graphics.newShader[[
|
||||||
|
|
||||||
float distance_x = light_pos.x - screen_coords.x;
|
float distance_x = light_pos.x - screen_coords.x;
|
||||||
float distance_y = light_pos.y - screen_coords.y;
|
float distance_y = light_pos.y - screen_coords.y;
|
||||||
float distance = 1-sqrt( pow(distance_x,2) + pow(distance_y,2) ) / game_scale;
|
float distance = sqrt( pow(distance_x,2) + pow(distance_y,2) ) / game_scale;
|
||||||
if (distance < range){
|
if (distance < range){
|
||||||
float alpha = (distance/range);
|
float alpha = 1-(distance/10);
|
||||||
if (pixel.a > alpha){
|
pixel.a = alpha;
|
||||||
pixel.a = alpha;
|
|
||||||
}
|
|
||||||
if (color.r<light_color.r){
|
if (color.r<light_color.r){
|
||||||
color.r = light_color.r;
|
color.r = light_color.r;
|
||||||
}
|
}
|
||||||
|
@ -29,3 +28,25 @@ myShader = love.graphics.newShader[[
|
||||||
return pixel * color;
|
return pixel * color;
|
||||||
}
|
}
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
Shaders.OutsideDark = love.graphics.newShader[[
|
||||||
|
uniform vec2 light_pos;
|
||||||
|
uniform float range;
|
||||||
|
uniform float game_scale;
|
||||||
|
|
||||||
|
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
|
||||||
|
|
||||||
|
vec4 pixel = Texel(texture, texture_coords );//This is the current pixel color
|
||||||
|
|
||||||
|
float distance_x = light_pos.x - screen_coords.x;
|
||||||
|
float distance_y = light_pos.y - screen_coords.y;
|
||||||
|
float distance = sqrt( pow(distance_x,2) + pow(distance_y,2) ) / game_scale;
|
||||||
|
if (distance < range){
|
||||||
|
float alpha = (distance/range);
|
||||||
|
if (pixel.a > alpha){
|
||||||
|
pixel.a = alpha;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pixel * color;
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
|
Loading…
Reference in New Issue