Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Bitmaps and memory management

I have a flashlight method that redraws a bitmaps whenever you change direction, since there's a different shape  of the light beam for facing north, west, etc.

Yesterday I started getting an error that I never got before.  Once you turned on the flashlight and changed directions a few times (thus redrawing the bitmap a few times) the game crashed on the line
Code:
@light.bitmap = Bitmap.new(1280, 960)
with the error, "Could not draw bitmap".

Today, the problem is gone!

So I figure it had something to do with the program running out of memory?

When you do an assignment line like
Code:
@light.bitmap = Bitmap.new(1280, 960)
does it automatically dispose from memory whatever was all ready in @light.bitmap?  Or do you always have to @light.bitmap.dispose beforehand?
 

khmp

Sponsor

I'd do a check to see if the bitmap exists before recreating it. I assume it doesn't occur automatically. But again its my assumption. Have you tried adjusting angle of the sprite? And then you wouldn't need to change the bitmap.
 
There´s two ways to free memory allocated from a bitmap. One is explicitly, by calling Bitmap#dispose. The other is implicitly done by the Ruby´s garbage collection routines, when the bitmap has no more references to any variables (e.g. is not being used anymore), but this is done in *not-so* regular times. This bitmap you created is just too big, so it´s easy to run out of memory (what i´m quite sure that was what happened). I know that RGSS has a limit, more of a recommendation, for bitmap´s sizes to ensure proper operation, i just can´t recall exactly how many but it´s in that order (2000 or more, on both sizes, does not matter if one size is less than 2000, if the other is greater than that the following operations can crash the game).

Bitmaps consumes a great amount of memory compared to the other RGSS classes, and this gets worse depending on the bitmap´s dimensions. In your example, if you simply define a new bitmap for the sprite, the other will be de-referenced but will still reside in memory for a while, until GC runs. This means that at the time of attribution you have twice the memory consumed by a 1280x960 bitmap. This is an easy way to run out of memory after some consecutive attributions (what shoudn´t happen btw...). As GC´s collection methods depends on some variants on the Interpreter, its execution time is kinda unpredictable. So, best is to do the work for it before it runs:
Code:
@light.bitmap.dispose if !@light.bitmap.nil? and !@light.bitmap.disposed?
@light.bitmap = Bitmap.new(1280x960)
Also, what are you trying to accomplish exactly? Perhaps, some sprite methods will do the work for you, instead of using *the generally more* time-consuming bitmap´s methods.
 
Well, I'm trying to make a flashlight method that creates the beam by subtracting from everywhere around the beam, rather than adding a beam-shaped object over the character.  This preserves the color data in the bright area, and allows for a flashlight effect in total darkness.  I did it by creating a new Plane object for the flashlight, like the Planes used for fog and panorama, which is turned totally white everywhere around the beam (so that it blackens the screen when subtracted)  Of course, since planes wrap around horizontally and vertically, it has to be bigger than the screen resolution to keep the flashlight beam from wrapping around to the opposite side of the screen.

Does that make any sense?  ;D  I'm at home for the holidays right now so I can't paste in any code.  But I'll certainly try adding that line you suggest.

@ khmp:  Alas, if the problem were that the file doesn't exist it would give an error like "File not found: whatever."  And the bitmap object would still have to be re-created each time, even if it's reading from the same file.
 

khmp

Sponsor

Gibmaker":z16h399s said:
@ khmp:  Alas, if the problem were that the file doesn't exist it would give an error like "File not found: whatever."  And the bitmap object would still have to be re-created each time, even if it's reading from the same file.

Not the file the actual object, the bitmap.
 
khmp is suggesting using a Sprite object instead of a Plane object.
You'll have to make the edges black instead of white.
The difference is, you create it once when the flashlight is turned on, then just use the
angle parameter to rotate it when the player changes direction.
Since it rotates, it will have to be about 1264x1264 if centered on the player.
However, you could make the bitmap smaller, and use the zoom parameters to scale it up.

Be Well
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top