Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Anyone got a code snipet for find FREE numbers for Images ?

Author
Message
DemonHill
18
Years of Service
User Offline
Joined: 20th Mar 2008
Location:
Posted: 23rd Sep 2011 16:58
I have a program with several hundred images.

Does anyone have any code that can tell you what numbers you have are FREE and not used as images ?

Quel
17
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 23rd Sep 2011 17:08 Edited at: 23rd Sep 2011 17:08
Make a loop which is checking through the slots with IMAGE EXIST(), this doesn't sound that hard that you should run for a snippet.

-In.Dev.X: A unique heavy story based shoot'em ~35%
-CoreFleet: An underground commander unit based RTS ~15%
-TailsVSEggman: An Sonic themed RTS under development for idea presentation to Sega ~15%
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 23rd Sep 2011 17:47 Edited at: 23rd Sep 2011 17:48


I hope that's correct cos I just typed it straight in here...

EDIT: Oops, just re-read the original post. That's what I get for being full of flu...

Warning! May contain Nuts!
BlackChaos
16
Years of Service
User Offline
Joined: 2nd May 2010
Location: London
Posted: 23rd Sep 2011 17:50
Try this.



Fairly simple function

bchaos101
BlackChaos
16
Years of Service
User Offline
Joined: 2nd May 2010
Location: London
Posted: 23rd Sep 2011 17:52
Quote: "//Return the next available image number
Function FindFreeImage()
//Range to search
FromLowest# = 0
ToHighest# = 100

//Search through image numbers untill the next available image
For n=FromLowest# to ToHighest#
If Image Exist(n) = 0
ReturnImageNumber# = n

//Exit when found to save FPS
Exit `Exist this for loop
Endif
Next n


//Return the number
Endfunction ReturnImageNumber#"
]

Change the FromLowest# value to 1, Image number zero does not exist.

bchaos101
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 23rd Sep 2011 17:53 Edited at: 23rd Sep 2011 17:53
Just a couple of comments on your snippet BlackChaos. You don't need the '#' as you're working with integers. And Image ID's start from 1, not 0.

Ah you got there before me...

Warning! May contain Nuts!
BlackChaos
16
Years of Service
User Offline
Joined: 2nd May 2010
Location: London
Posted: 23rd Sep 2011 17:57
Quote: "
Just a couple of comments on your snippet BlackChaos. You don't need the '#' as you're working with integers.
"


It's a dirty habbit of mine

bchaos101
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 23rd Sep 2011 18:00
30 years and I've still got bad programming habits...

Warning! May contain Nuts!
Jimpo
21
Years of Service
User Offline
Joined: 9th Apr 2005
Location:
Posted: 23rd Sep 2011 18:13
The suggested functions will be very slow when you have several hundred images, as the function will have to loop through every image before finding a free one.

I would suggest just using IanM's Matrix1 Utility plugin as he has a built in command 'find free image()' that will probably run faster than any code you could write for it in DBPro.

Or you can use something like this, but IanM's plugin is still faster.


If you were using say 200-300 images, this code has a good shot at finding the free image on the first try, or within a couple loops.

WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 23rd Sep 2011 18:47
Actually that's a good idea using a random number. Nice one Jimpo!

In the original post he was asking about keeping track of images.

The only way around that is to make sure your program stores the ID's in either variables or arrays and when they are not in use any more then you can get rid of them. If your code is consistent and runs through stages then after each stage just clear up those images needed. It really is down to how you handle your media. It goes for all your game media.

Warning! May contain Nuts!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Sep 2011 21:27
Quote: "Actually that's a good idea using a random number."

That's the way I used to do it in my plug-in, but the method I use now actually tracks the images as you create & delete them.

It's pretty much unbeatable when compared to any code you'd write to do the job in DBPro.

BlackChaos
16
Years of Service
User Offline
Joined: 2nd May 2010
Location: London
Posted: 24th Sep 2011 15:14
Can somebody please explain the efficiency of randomly cycling numbers?

bchaos101
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 24th Sep 2011 15:27 Edited at: 24th Sep 2011 15:29
I think that an auto search replace would be good, but it would have to know the associated commands. It would also have to figure out the for/ next loops as well.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Sep 2011 17:35
Quote: "Can somebody please explain the efficiency of randomly cycling numbers?"

Let's assume you allocate a range of 1000 numbers for your images, and that you have used 200 of them for images.

If you start looking for a free image at the start of the range, and if you haven't yet unloaded any existing images, you will carry out 200 checks against existing images before you finally find the free one.

Now if you start at a random point in your number range, the chances of finding a free image are 4 in 5 (or 800 out of 1000 if you prefer). Even if the image number is in use, there's a good chance that the next won't be. That's 1 or 2 checks to find a free image.

If you start looking from the last image you used, then everything will be just fine initially - you only carry out a check on the next number, find it's free, and then use it. When you get to the end of the range and start again from the beginning of the range, you'll start running through image numbers that you have previously used. Generally, we tend to load images in groups - for example 20 or 30 frames of an animated sprite, or images for a GUI - and won't unload them (maybe they last for the lifetime of the program). That means that when we hit the first image in that group we now have a run of image numbers to get past before there's a chance of finding a free image. That's generally 1 check to find a free image, but on occasions it will take more as you work through those runs.

The first method (starting from the beginning of the range) gets worst as you load more images.

The second method gives no real guarantees, but will tend to immediately return a free image if the ratio of images to free numbers is high.

The third method gives no guarantees either, and will also tend to immediately return a free image, but does have the problem of dealing with runs of images that will unpredictably give worst performance on occasion.

Generally the choice should be the random start point, but if you know that there are no runs of images and you delete every image over time, or if your range is effectively infinite (or 22,000,000 which is DBPro's limit on image ids IIRC), then pick the third. Never pick the first (although it's what I see posted most often ).

Or use my plug-in commands and don't worry about the method - it always returns the lowest number available in the range that you specify, and it's very quick compared to any of these methods.

Login to post a reply

Server time is: 2026-07-11 04:44:21
Your offset time is: 2026-07-11 04:44:21