So far so good, but when my system is under load, or I load a bigger chunk of the map, so the framerate drops, it sometimes simply doesn't draw the background for a frame or two, which is, well... bad.
The problem with lags when a map-part is buffered is a whole other story, and the next thing I'll fix, but I've at least got some idea as to what I need to do there.
I thought it might be a problem with my GPU not catching up with rendering the background on time, since that's the only part which *might* run serially, like the rest of the code, since it's performed by the GPU...
It works fine if I directly draw the different layers to the screen (each of them already is a canvas)
This is the function in question:
Code: Select all
function update_parallax_background ()
love.graphics.push()
love.graphics.setCanvas(assets.bg_canvas)
assets.skybox:draw(0, 0)
local curr_tile_size = map_manager.tile_size * camera.scale_factor --is also the window width
local offset_x = (((camera.offset_x / map_manager.tile_size) * -25) % map_manager.tile_size) * camera.scale_factor
local inner_offset_x = ((curr_tile_size - offset_x) / camera.scale_factor) % curr_tile_size
assets.background.quad:setViewport(0, 0, inner_offset_x, map_manager.tile_size)
assets.background:draw(offset_x, 0)
assets.background.quad:setViewport(inner_offset_x, 0, map_manager.tile_size - inner_offset_x, map_manager.tile_size)
assets.background:draw(0, 0)
offset_x = (((camera.offset_x / map_manager.tile_size) * -50) % map_manager.tile_size) * camera.scale_factor
inner_offset_x = ((curr_tile_size - offset_x) / camera.scale_factor) % curr_tile_size
assets.background2.quad:setViewport(0, 0, inner_offset_x, curr_tile_size)
assets.background2:draw(offset_x, 0)
assets.background2.quad:setViewport(inner_offset_x, 0, map_manager.tile_size - inner_offset_x, curr_tile_size)
assets.background2:draw(0, 0)
love.graphics.setCanvas()
love.graphics.pop()
end
Code: Select all
function render ()
love.graphics.setBlendMode("alpha")
love.graphics.draw(assets.bg_canvas, 0, 0)
love.graphics.push()
love.graphics.translate(-camera.offset_x * camera.scale_factor, -camera.offset_y * camera.scale_factor)
draw_map()
draw_players()
draw_dynamic_objects()
love.graphics.pop()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw(ui.upper_canvas, 0, 0)
if #ui.middle_texts > 0 then
love.graphics.draw(ui.middle_canvas, 0, ui.height)
end
if #ui.bottom_texts > 0 then
love.graphics.draw(ui.bottom_canvas, 0, ui.height * 2)
elseif ui.keyboard_hint_duration > 0 then
local keyboard_width, keyboard_height = assets.keyboard.canvas:getDimensions()
assets.keyboard:draw(ui.width / 2 - keyboard_width / 2, ui.height * 2 + H_UNIT_UI * 5)
end
if ui.frame_counter % ui.parallax_refresh_rate == 0 then
update_parallax_background()
end
if ui.fade_opacity > 0 then
love.graphics.setColor(0, 0, 0, ui.fade_opacity)
love.graphics.rectangle("fill",0,0,love.graphics.getWidth(), love.graphics.getHeight())
end
love.graphics.print(love.timer.getFPS(), 10, 10)
ui.frame_counter = ui.frame_counter + 1
end