Hwo to optimize my GPU usage?[Solved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Hwo to optimize my GPU usage?[Solved]

Post 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
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

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

Post 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.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

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

Post 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.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests