love.graphics.newImage using a thread: yes or no?

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.
gcmartijn
Party member
Posts: 136
Joined: Sat Dec 28, 2019 6:35 pm

love.graphics.newImage using a thread: yes or no?

Post by gcmartijn »

Today, I have created a simple popup inside my game.
In this popup there is an image, and that image is not inside the memory/use inside the running game.
And maybe the player never going to see / use this popup (image).

I don't like the idea to put all images into the memory, when not needed.

So I did something like:
If open popup then do love.graphics.newImage()
If close popup then destroy the image (clean up).

But now I see a glitch when loading a newImage when the game is already running.
This is because this image loading is on the main thread.

So I though, well, let's check if we can use another thread for loading images.
- https://github.com/kikito/love-loader (old code, don't know if still oke)
- https://love2d.org/wiki/love.thread
The love.graphics, love.window, love.joystick, love.keyboard, love.mouse, and love.touch modules have several restrictions and therefore can only be used in the main thread.
So I guess I can make it work (I guess), but the wiki say that there are several restrictions.

Questions:
1. Is is oke to load some images into the memory with a thread?
2. Is there another way (without a loading screen) to do this?

Thanks for the information.
Martijn
User avatar
EngineerSmith
Prole
Posts: 38
Joined: Thu Dec 02, 2021 11:38 am
Contact:

Re: love.graphics.newImage using a thread: yes or no?

Post by EngineerSmith »

You can't safely use love.graphics in threads. You can load imageData in a thread and send it back to the main thread to create an image from it.
I personally use lily ( https://github.com/MikuAuahDark/lily ) for multithreaded loading (works in 11.4, so no need to mess about with it).
gcmartijn
Party member
Posts: 136
Joined: Sat Dec 28, 2019 6:35 pm

Re: love.graphics.newImage using a thread: yes or no?

Post by gcmartijn »

You can load imageData in a thread and send it back to the main thread to create an image from it.
That is what I try to say hahah, if it safe to use it like that.
Is that working oke in IOS/Windows/Mac ?

Thanks for your link, going to check it.
User avatar
BrotSagtMist
Party member
Posts: 615
Joined: Fri Aug 06, 2021 10:30 pm

Re: love.graphics.newImage using a thread: yes or no?

Post by BrotSagtMist »

I really dont see what the benefit of that would be unless you try to load hundreds of mb of assets.

Anyway if you worry about pictures never being used you can put the loader in a metatable and have it only load if its actually rendered.

Code: Select all

tiles=setmetatable({},{__index=function(tab,ein) tab[ein]=love.graphics.newImage(ein..".png") return tab[ein]  end})
love.graphics.draw(tiles.wall) --loads and draws wall.png
obey
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.graphics.newImage using a thread: yes or no?

Post by zorg »

BrotSagtMist wrote: Wed Jun 01, 2022 3:54 pm I really dont see what the benefit of that would be unless you try to load hundreds of mb of assets.

Anyway if you worry about pictures never being used you can put the loader in a metatable and have it only load if its actually rendered.

Code: Select all

tiles=setmetatable({},{__index=function(tab,ein) tab[ein]=love.graphics.newImage(ein..".png") return tab[ein]  end})
love.graphics.draw(tiles.wall) --loads and draws wall.png
Depending on the amount, and size of the images you want to load in "just in time", this might cause gigantic lag spikes.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
BrotSagtMist
Party member
Posts: 615
Joined: Fri Aug 06, 2021 10:30 pm

Re: love.graphics.newImage using a thread: yes or no?

Post by BrotSagtMist »

If you manage to have this lagging the game is probably too big for anyone to download in the first place.
obey
MrFariator
Party member
Posts: 515
Joined: Wed Oct 05, 2016 11:53 am

Re: love.graphics.newImage using a thread: yes or no?

Post by MrFariator »

Generally streaming assets by doing the file operations on a separate thread from the main one is a good thing, yes. The game I'm working on does this exact thing whenever it transitions between levels. This allows löve's main thread to truck along without interruptions, and perhaps even display some fancier visuals than a simple, blank screen when you're loading audio, textures and level data. Even the slightest microstutter from file access could make the loading screen animation (or anything else that might be going on) play less smoothly, making the overall thing seem less polished. An additional benefit is that you can just load the first chunk of the level, and stream the rest of the assets in as the player progresses, though this may not be worth doing for most simple games.

If your game instead utilizes fade to white/black transitions, then it doesn't make much of a difference.
gcmartijn
Party member
Posts: 136
Joined: Sat Dec 28, 2019 6:35 pm

Re: love.graphics.newImage using a thread: yes or no?

Post by gcmartijn »

The main reason I ask was because I see a little stutter, when I load an image during gameplay.
I'm from the past, there the rule was that you don't wast memory if not needed ;)

I already using fade in/out when changing levels, to eliminate the stutter problem ;)

The image is just about 450 bytes.
How much MB is 'safe' to load into memory? I think you have to keep track on the computer memory and make it more complicated.
But it is more easy to make, and smooth.

I can add all those popup images into memory, maybe its +/- 10MB, then I don't have any microstutter.
MrFariator
Party member
Posts: 515
Joined: Wed Oct 05, 2016 11:53 am

Re: love.graphics.newImage using a thread: yes or no?

Post by MrFariator »

It's possible to get a stutter during gameplay, even if the file you're loading is just a couple of bytes. File I/O can just be slow, no ifs or buts about it, hence why you might want to do that stuff on a separate thread - particularly during gameplay. If you can, just load the assets ahead of time, or over time with threads.

As for how much is safe to load? As much as user has RAM. 450 bytes, and even 10MB is basically nothing in this day and age. Steam survey (as of April 2022) seems to imply that 50.59% of its users have 16GB RAM, and 24.02% have 8GB VRAM. While it's a noble goal to minimize filesizes and memory usage, it ultimately doesn't matter with such low numbers, unless the hardware you're targeting has very limited specs. Even most phones are in gigabyte ranges these days. That's not to say you shouldn't care, Hitman 3 managed to save quite the few gigabytes, but don't stress over a few bytes.
gcmartijn
Party member
Posts: 136
Joined: Sat Dec 28, 2019 6:35 pm

Re: love.graphics.newImage using a thread: yes or no?

Post by gcmartijn »

Good point, now I have to choose haha.

I think I choose for the easy way and pre-load all images (non thread).
I guess everyone that play a game have 1GB available for assets, so I have enough free space at the moment.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 59 guests