Page 1 of 1

Hwo to optimize my GPU usage?[Solved]

Posted: Sat Apr 30, 2022 7:29 am
by NoreoAlles
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:

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
Object creation and drawing code:

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
And i later loop thru the Objects to draw them

Re: Hwo to optimize my GPU usage?[Solved]

Posted: Sun May 01, 2022 3:22 pm
by idbrii
Edit: oh, I didn't see this was solved. How'd you fix?

Check out love.graphics.getStats to see the number of drawcalls. Likely, your rendering is automatically batches.

Is Map() called from love.load?

You could set up a profiler (see Libraries subforum for a few) to get better information about what code is causing slowdown.

Include a complete .love (a whole project zipped up) to make it easier to help.

Re: Hwo to optimize my GPU usage?[Solved]

Posted: Sun May 01, 2022 5:43 pm
by NoreoAlles
idbrii wrote: Sun May 01, 2022 3:22 pm Edit: oh, I didn't see this was solved. How'd you fix?
I just added a qoud to a spritebatch for each block, instead of each block drawing a distinct image. I thought the love.graphics.newspritebatch() and love.graphics.newQuad() would be harder to work with then it was.