Forum

> > CS2D > Scripts > Is there any way to change image order?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Is there any way to change image order?

6 replies
To the start Previous 1 Next To the start

old Is there any way to change image order?

MikuAuahDark
User Off Offline

Quote
Example: I have 2 images (image A and B). I want image B is drawn over image A with same image mode (like both image mode is 0, 1, or 2), but the order they created is not defined, so it's possible that image A is created first then image B or vice versa.

Is there any way to ensure or a trick to do this?

old Re: Is there any way to change image order?

Cure Pikachu
User Off Offline

Quote
The order the images are created should be determined by your script sequence, unless I am missing something. It's like stacking pancakes on top of one another.
Test Code >

old Re: Is there any way to change image order?

DC
Admin Off Offline

Quote
Like user Cure Pikachu says, the order is defined by the time of creation. The image you created last is always on top of all older ones.

@user VADemon: Images are always sent in reliable, ordered network packages so packet loss can't mess with their order.

@user MikuAuahDark: Unfortunately I can't think of a good solution for this. Here are some ideas but depending on what you're trying to do this might not work very well / at all:

If you know what sprites the images are going to use you could create them all beforehand in the right order and hide them (either set alpha to 0 or move them outside the visible area) until they are needed.

If you don't know which sprites are going to be used, you could still create the images beforehand but put all possible sprites in one big texture atlas and use multi frame sprites and change the frames as required with cs2d lua cmd imageframe.

The last resort would be to destroy and re-create images in the right order whenever you need to change the order. Note that this is quite expensive and traffic-heavy and will certainly lead to lags when you have many images.

user MikuAuahDark has written
@user Cure Pikachu: How bad will be the performance
when creating cs2d lua cmd image on every frame (or I can say, for every 20ms)? I actually want to implement image caching, but it seems impossible because the image order is undefined.

Something like this should be avoided if possible. It might work well for one image without big performance issues but it will cause an ridiculous amount of network traffic over time.

old Re: Is there any way to change image order?

MikuAuahDark
User Off Offline

Quote
@user DC: So looks like the best solution is to pre-load the images in right order, then set the opacity when needed.

user DC has written
If you don't know which sprites are going to be used, you could still create the images beforehand but put all possible sprites in one big texture atlas and use multi frame sprites and change the frames as required with cs2d lua cmd imageframe.


Just a note, this method doesn't work in my situation. The script only calls my callback function which tells them "do x with this additional useful data. I don't care how and which function do you use to do x". In this case, the script calls my callback and tell it "draw this image with this information (scale, rotatation, position)". Also pre-rendering the animation requires too much work on my side, because the data is stored in a instruction-like system, which can loop from beginning or jump directly. To make it simple, I only have animation data sequence and image information to be used.

Out of curiosity, how bad the performance if cs2d lua cmd imagepos, cs2d lua cmd imagescale, cs2d lua cmd imagealpha, and cs2d lua cmd imagecoor called on every frame? I think it's 4x slower than cs2d lua cmd image but I'm curious to know it.

old Re: Is there any way to change image order?

DC
Admin Off Offline

Quote
You would have to do a benchmark to test that...
I highly doubt that these 4 together are 4 times slower than creating a new image. I would assume that they are only slightly slower (if at all) because creating a new image is more expensive than changing an existing one.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview