save background ?

skipsy

Member
Licensed User
Longtime User
Hello,

Iam looking for a way to save background under a sprite.

I have the background picture as bitmap
B4X:
BKGRND.Initialize( File.DirAssets, "fond.jpg" )
but the bitmap is not displayd at its original size but spread to the whole screen.
so I can't use BKGRND to restore the part of the background needed.

I did a test :
- Set the background with the picture
- display the same picture over the background :

B4X:
SCREEN_REC.initialize( 0, 0, activity.Width, activity.Height )
LAYER.Drawbitmap(BKGRND, SCREEN_REC, SCREEN_REC )
Activity.Invalidate2( SCREEN_REC )

It does not cover the background because the JPG is not resized to
activity.Width and activity.Height !!!!

Any trick for me ??? :BangHead:

Thks
WW
 

klaus

Expert
Licensed User
Longtime User
Instead of redrawing the background behind the sprite I would suggest you to draw the sprites on a transparent panel and draw a transparent rectangle to erase the sprite. The background will not be touched. With this principle you could have different layers that you could show or hide.

Best regards.
 
Upvote 0

skipsy

Member
Licensed User
Longtime User
Hello,
You already wrote something about transparent layers... But it did not work :BangHead:
This is obviously the right way. I need to do some more tests.

Thks,
WW
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
But it did not work
What program?
What did you do ?
What didn't work ?
You must be more precise.

Did you have a look at the Graphics chapter in the Beginner's Guide
there you have an example with several transparent panels that shows the priciple.

Best regards.
 
Upvote 0

skipsy

Member
Licensed User
Longtime User
Here is what I wrote :

B4X:
Sub Globals
   Dim LAYER As Canvas
   Dim TIMER1 As Timer
   Dim x, y As Float
   Dim BALLE_src_rec, BALLE_dst_rec As Rect
   Dim PAS As Int
   Dim PNG_BALLE As Bitmap
   Dim BKGRND As Bitmap
   Dim dBKGRND As BitmapDrawable
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      PNG_BALLE.Initialize( File.DirAssets, "balle.png" )
      BKGRND.Initialize( File.DirAssets, "fond.jpg" )
      dBKGRND.Initialize( BKGRND )
      activity.Background = dBKGRND
      LAYER.Initialize( activity)
      
      TIMER1.Initialize( "TIMER1", 100 )
      TIMER1.Enabled = True
      x = 0dip
      y = 0dip
      PAS = 1dip
   End If
   BALLE_dst_rec.Initialize( x, y, x+PNG_BALLE.Width, y+PNG_BALLE.Height )
   BALLE_src_rec.Initialize( 0, 0, PNG_BALLE.Width, PNG_BALLE.Height )
   
End Sub

Sub TIMER1_tick
   If x < 300dip Then
      LAYER.Drawrect( BALLE_dst_rec, Colors.transparent, True, 1dip )
      activity.Invalidate2( BALLE_dst_rec ) 
      y = y + 1
      BALLE_dst_rec.Initialize( x, y, x+PNG_BALLE.Width, y+PNG_BALLE.Height )
      LAYER.DrawBitmap(PNG_BALLE, BALLE_src_rec, BALLE_dst_rec )
      
      Activity.Invalidate2( BALLE_dst_rec ) 
   End If
End Sub
This does not restore the background under the sprite, it draws a black square !!!

Best regards,
WW
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
To add a layer, as I already mentioned in post #3, you must add a transparent full screen panel on your activity !
The LAYER Canvas must be set to that panel. Then it will work.
The Activity does NOT have any transparency. The Colors.Transparent color is black with 100% transparency, and as the Activity has no transparency the rectangle is black.

Best regards.
 
Upvote 0

skipsy

Member
Licensed User
Longtime User
Hi Klaus,

Works prefectly using panel.
Well I am not (yet) comfortable with this idea of panels, canvas... I used to
manage sprites saving/restoring background, swapping between virtual and
physical screens.

Thks a lot,
William.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Working with panels and canvases is a quite interesting feature. Because you can really use several different layers and not only two screens physical and virtual.
You could imagine having a background panel with a moving scene and sprites and other moving foreground images on different layers, playing with the transparency, hiding and showing etc.

Best regards.
 
Upvote 0
Top