optimization in whole tiles creation

This commit is contained in:
lustlion 2022-02-07 09:24:41 +01:00
parent dedb0d884c
commit 75b7aad1e6
5 changed files with 131 additions and 55 deletions

View File

@ -50,11 +50,11 @@ end
function GameDraw()
GameworldDrawPrepare()
GameworldDrawParticles()
GameworldDrawBackground()
GameworldDrawLighting()
GameworldDrawEntities()
GameworldDrawForeground()
GameworldDrawLighting()
GameworldDrawParticles()
GameworldDrawEntities()
GameworldDrawEnd()
-- hud

View File

@ -20,7 +20,7 @@ function GameworldDrawBackground()
for j = 1, #LevelTiles[i] do
if LevelTiles[i][j].id ~= 0 then
local depth = TileGetDepth(LevelTiles[i][j])
local depth = TileData[LevelTiles[i][j].id].depth
DrawTile(
LevelTiles[i][j],
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
if LevelTiles[i][j].id ~= 0 then
local depth = TileGetDepth(LevelTiles[i][j])
local depth = TileData[LevelTiles[i][j].id].depth
DrawTile(
LevelTiles[i][j],
tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.width) - Camera.pos.x,
@ -82,7 +82,8 @@ function GameworldDrawLighting()
DoBorder()
-- apply to game canvas
love.graphics.setColor(1,1,1,1)
love.graphics.scale(game.scale,game.scale)
love.graphics.setCanvas()
love.graphics.scale(1,1)
DrawDarkness()
love.graphics.scale(1/game.scale,1/game.scale)
end

View File

@ -230,24 +230,6 @@ function SetTile(i,j,id)
LevelTiles[i][j] = InstanceTile(id)
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()
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
@ -262,17 +244,75 @@ function GridDisplay()
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()
LoadedObjects.Collisions = {}
LoadedObjects.Platforms = {}
LoadedObjects.Ladders = {}
TileOptimizeObjects()
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
if LevelTiles[i][j].id ~= 0 then
local type = TileGetType(LevelTiles[i][j])
local light = TileGetLight(LevelTiles[i][j])
local type = TileData[LevelTiles[i][j].id].type
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_y = tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height)
@ -285,7 +325,8 @@ function TileCreateObjects()
)
end
if type == "whole" then
-- wholes are handled in optimization now
--[[if type == "whole" then
local col = Collision:New(
base_x,
base_y,
@ -293,8 +334,7 @@ function TileCreateObjects()
base_y + tileProperties.height * tileProperties.scale
)
table.insert(LoadedObjects.Collisions,col)
elseif type == "half_bottom" then
else]]if type == "half_bottom" then
local col = Collision:New(
base_x,

View File

@ -59,20 +59,25 @@ function DoLights()
light.flicker = math.min(math.max(light.flicker, -light.flicker_value),light.flicker_value)
end
end
love.graphics.setBlendMode("replace")
for _, light in pairs(Lights) do
if light.range ~= 0 then
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(
"fill",
(light.pos.x - Camera.pos.x) / game.scale,
(light.pos.y - Camera.pos.y) / game.scale,
(light.range + light.flicker + 0) / game.scale
position.x,
position.y,
range
)
end
end
myShader:send("game_scale", game.scale)
love.graphics.setColor(0,0,0,0)
for _, light in pairs(Lights) do
if light.range ~= 0 then
local position = {
@ -80,21 +85,30 @@ function DoLights()
y = (light.pos.y - Camera.pos.y) / 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(
"fill",
position.x,
position.y,
range
)
love.graphics.setBlendMode("alpha")
love.graphics.setColor(1,1,1)
myShader:send("light_color", light.color)
myShader:send("light_pos", {position.x*game.scale, position.y*game.scale})
myShader:send("range", range)
love.graphics.setShader(myShader)
"fill",
position.x,
position.y,
range
)
end
end
love.graphics.setBlendMode("alpha")
love.graphics.setColor(0,0,0,1)
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(
"fill",
position.x,
@ -115,5 +129,5 @@ function DoBorder()
end
function DrawDarkness()
love.graphics.draw(Canvas.Darkness, 0, 0, 0, game.scale)
love.graphics.draw(Canvas.Darkness, 0, 0, 0, 1/game.scale)
end

View File

@ -1,4 +1,5 @@
myShader = love.graphics.newShader[[
Shaders = {}
Shaders.InsideLight = love.graphics.newShader[[
uniform vec2 light_pos;
uniform vec3 light_color;
uniform float range;
@ -10,12 +11,10 @@ myShader = love.graphics.newShader[[
float distance_x = light_pos.x - screen_coords.x;
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){
float alpha = (distance/range);
if (pixel.a > alpha){
pixel.a = alpha;
}
float alpha = 1-(distance/10);
pixel.a = alpha;
if (color.r<light_color.r){
color.r = light_color.r;
}
@ -29,3 +28,25 @@ myShader = love.graphics.newShader[[
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;
}
]]