Hwo to optimize my GPU usage?[Solved]
Posted: Sat Apr 30, 2022 7:29 am
Hello, i have a problem, when i draw more then ~50 images on the screen my fps go from 60 to 45. I have a map function wich creates boxes for each number thats not 0 in a table of tables, and to draw that i give the boxes an image and later loop through them all to draw each box. They`re scaling the image btw, from 16 x 16 pixel to 50 x 50. I think i have to stitch all the images my boxes use to one big image and then draw that. Mybe love.graphics.newspritebatch() would help, but i dont get it.
Map making function:
Object creation and drawing code:
And i later loop thru the Objects to draw them
Map making function:
Code: Select all
function Map()
local tile_width = 64
blocks = {}
map = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,},
{0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,},
{1,1,1,1,1,1,2,0,0,0,0,0,0,0,0,},
{2,2,2,2,2,2,1,1,1,1,0,0,1,1,1,},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
}
for y, xs in ipairs (map) do
for x, value in ipairs (xs) do
if value == 1 then
b = Object.new((x-1)*tile_width,(y-1)*tile_width, tile_width, tile_width, grass)
table.insert(blocks, b)
elseif value == 2 then
b = Object.new((x-1)*tile_width,(y-1)*tile_width, tile_width, tile_width, dirt)
table.insert(blocks, b)
end
end
end
end
Code: Select all
love.graphics.setDefaultFilter("nearest", "nearest")
ground_atlas = love.graphics.newImage("artwork/atlas.png")
Object = {}
Object.__index = Object
function Object.new(x, y, w, h, img)
local instance = setmetatable({}, Object)
instance.x = x
instance.y = y
instance.w = w
instance.h = h
instance.img = img
instance.middle = {}
instance.middle.x = instance.x + instance.w / 2
instance.middle.y = instance.y + instance.h / 2
instance.num = amount
return instance
end
function Object:update(dt)
end
function Object:draw()
love.graphics.draw(ground_atlas, self.img, self.x, self.y, 0, 4)
end