Can't get a bitmap redrawn. [XP]

I am totally lost as to why this is not working, because I've done it like this before I'm sure and it has worked.


If I remove ".bitmap.clear" then it works fine. But I need bitmap.clear so I don't keep writing the same bitmap on top of each other whenever I want to redraw the flag.


Here's the piece of code:

Code:
	  def draw_flag
	    @flag[1] = Sprite.new(@viewport)
	    @flag[1].z = 9000 - 1
	    @flag[1].bitmap = RPG::Cache.picture(dir + 'bg_flag_fill')
	    set_flag_fill
	  end
	  
	  def set_flag_fill
	    @flag[1].bitmap.clear
	    @flag[1] = Sprite.new(@viewport)
	    @flag[1].z = 9000 - 1
	    @flag[1].opacity = 255
	    @flag[1].visible = true
	    @flag[1].bitmap = RPG::Cache.picture(dir + 'bg_flag_fill')
	    
	    src_bitmap = RPG::Cache.picture(dir + 'bg_flag_fill')
	    rect = Rect.new(0, 0, 640, 480)
	    @flag[1].bitmap = Bitmap.new(640, 480)
	    @flag[1].bitmap.blt(0, 0, src_bitmap, rect)
	    @flag[1].visible = true
	    
	    @flag[1].bitmap.draw_text(0,150,640,420,"alalalalalalalalalalala", 1)
	    @flag[1].bitmap.blt(-100, -100, src_bitmap, rect)
	    @flag[1].bitmap.fill_rect(0, 0, 50, 50, Color.new(200,200,200,50))
	end

Only fill_rect and draw_text show up, not the bitmap I'm actually trying to place. I have no idea why and it's really making me crazy.


Note that the code is a mess because I'm trying out lots of different things to make the bitmap appear, but nothing is working. s___s
There's a lot of redundancy there.


You clear the bitmap, then you do Sprite.new, which clears the bitmap anyway (at least I think so).


Then you set the bitmap to a picture, which should populate it, and then you put the picture into a variable and blit it onto the bitmap, which does the same thing you've already done, just in more steps.


Then you draw some text and blit into it from the same picture again.


It's just all weird.


What's the full size of the picture?  It doesn't have whitespace, does it, that's replacing what WAS in the bitmap before you get to see it?


I suggest you get rid of everything and start again, and only keep what's absolutely necessary:

Code:
def set_flag_fill
  @flag[1].bitmap.dispose
  @flag[1].dispose # maybe not really necessary
  @flag[1] = Sprite.new(@viewport)
  @flag[1].bitmap = RPG::Cache.picture(dir + 'bg_flag_fill')
end

I didn't test it, but I believe that's pretty much all you need.  What's dir?  It's not a variable, so it must be a method.  What does it return?  Are you certain dir + 'bg_flag_fill' is a valid path and file name?
Hi,


The main issue should be a combination of line 4, 5 and 9. The RPG::Cache memorizes the bitmaps in order to reuse them, so you should avoid modifying them. In this case, you are clearing a picture that you use as a background later.


Apart from that, there are some things I'd mention:



- line 10  @flag[1] = Sprite.new(@viewport)


At this point, @flag[1] already refers to another sprite that is abandoned when you reassign the variable. Sprites should always be disposed when you stop using them (@flag[1].dispose ), otherwise it is possible that they continue to get displayed for several frames until they are removed internally. In VX/VXAce, disposing a sprites viewport but not the sprite itself can lead to random crashes on some computers.


On the other hand, you probably don't need to create a new sprite here every time. You can just continue to use the old one.


- line 4, 10, 14  @flag[1].bitmap = ...


You are assigning the sprites bitmap three times in one go. Each sprite can only refer to one bitmap at a time, so the first two lines are mostly redundant. You probably don't have to create a new bitmap every time you redraw the flag. You could create an empty bitmap for the sprite once and only clear/refill it.


Recreating bitmaps on each refresh is mostly done when drawing lists where the bitmap size is not fixed but changes with the number of drawn items. I'd also recommend to dispose bitmaps when you stop using them, except those you took from RPG::Cache.


Edit: Too slow... (yes, I have to write that every time. 🙂 )
doesn't matter - you said different things and included more of an explanation 🙂   It's also good to see I was correct to dispose of the sprite as well as the bitmap (though does disposing the bitmap remove it from cache?)
As I said in my OP, the code contains a lot of useless and unnecessary lines, because it didn't work when I kept it simple, so I started messing around with removing/adding whatever just to see where the problem lies. Eventually I discovered draw_text and fill_rect work, but not blt or .bitmap =.


Let me take your suggestions into consideration and play around with it right now. 🙂  Thanks!


I'll report back in 30 mins.
Alright, so just ignoring all the nonsense code I wrote for a sec:


This code actually works:

Code:
  def set_flag_fill
	    
	    dir = 'BattleState/'
	    @flag[1].bitmap.dispose 
	    @flag[1].dispose # maybe not really necessary
	    @flag[1] = Sprite.new(@viewport)
	    @flag[1].bitmap = RPG::Cache.picture(dir + 'bg_flag_fill')
	 
	end



That begs the question: what is the difference between bitmap.dispose and bitmap.clear?

The RPG::Cache memorizes the bitmaps in order to reuse them, so you should avoid modifying them. In this case, you are clearing a picture that you use as a background later.





Is that what bitmap.clear does? Completely removing an image from the Cache?? If so, I need to go back and start changing some bitmap.clear-s I wrote into into bitmap.dispose-s. @_@


In what situation should one use .dispose vs .clear?
EDIT


Upon further inspection, it seems that doing this:

Code:
sprite = Sprite.new
	sprite.bitmap = Bitmap.new(640,480)
	sprite.bitmap.clear
	sprite.bitmap = Bitmap.new(640x480)
	src = RPG::Cache.picture(dir + 'bg_flag_fill')
	sprite.bitmap.blt(0, 0, RPG::Cache.picture(dir + 'bg_flag_fill'), Rect.new(0,0,640,480))



Works well, but this does not:

Code:
sprite = Sprite.new
	sprite.bitmap = RPG::Cache.picture(dir + 'bg_flag_fill'
	sprite.bitmap.clear
	sprite.bitmap = RPG::Cache.picture(dir + 'bg_flag_fill'



So I'm guessing bitmap.clear completely erases the chased file if the bitmap is related linked to one? Thus, bitmap.clear is only useful if you're using .blt and .draw_text and such?
what is the difference between bitmap.dispose and bitmap.clear? [...]


In what situation should one use .dispose vs .clear?



bitmap.dispose frees up the internal image data. This process is irreversable and trying to access data from a disposed bitmap will result in an error message. This should be used when you no longer need the bitmap and are about to drop it.


It seems there also is an internal bitmap limit after which no additional bitmaps can be created. Creating a lot of (small) bitmaps without disposing them again might cause you to reach it in some cases. Apart from that I don't know if it is that important to dispose smaller bitmaps, as they should get disposed anyway when they are finally garbage collected. I'd still recommend it of course. 🙂


bitmap.clear clears the bitmaps content, leaving a blank image. It has the same effect as


bitmap.fill_rect(bitmap.rect, Color.new(0,0,0,0))


and can be used to clear the bitmap before drawing new stuff onto it.

Upon further inspection, it seems that doing this:



This is because of the way RPG::Cache works. Every bitmap retrieved from the cache is also stored within an internal hash. When you request a bitmap that is already in the hash, the cache will just return it instead of creating a new bitmap.


So in the first example, you create a new bitmap, clear it, drop it and create a new one and finally draw something onto the new bitmap.


In the second example you retrieve a new bitmap, clear it, then retrieve the very same bitmap again (which is still empty). The cache does not recognize that the bitmap was modified at this point.


You can also look up the aource code of the RPG::Cache module in the manual (F1).



You can dispose a bitmap to effectively remove it from the cache (the cache will recreate it you request it again). The disposal would affect all sprites that are currently using that bitmap of course, so you probably shouldn't dispose cached bitmaps at random.
You can clone a bitmap and it will be treated separately, so disposing/clearing one instance will not result in disposing/clearing all bitmaps which use the same image, and drawing on one will not mean that the drawing will appear on all the others. Note that you must dispose all cloned bitmaps at some point, or else you might reach the limit of bitmaps quicker than you think.


This works in RGSS3, no idea if it works the same way in XP or not.

Spoiler
sprite.bitmap = Bitmap.new(640,480)
sprite.bitmap.clear
sprite.bitmap = Bitmap.new(640x480)

Code:
	sprite.bitmap = RPG::Cache.picture(dir + 'bg_flag_fill')
	sprite.bitmap.clear
	sprite.bitmap = RPG::Cache.picture(dir + 'bg_flag_fill')



The above codes make no sense, btw. 😛


Bitmaps created with Bitmap.new will be empty bitmaps anyway, so you don't need to clear them, neither you need to recreate the same empty bitmap again.


Also, if you are assigning an image to a bitmap, what is the point of immediately clearing that same bitmap AND assigning the same image again?

Original thread: https://forums.rpgmakerweb.com/threads/cant-get-a-bitmap-redrawn-xp.63568/