PUT behind background

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
angros47
Posts: 1671
Joined: Jun 21, 2005 19:04

PUT behind background

Postby angros47 » Aug 18, 2010 17:36

With PUT ... TRANS, it's possible to draw a sprite that covers the background (the alpha channel determine transparent area).

With this code, it's possible to draw a sprite behind the background: so, it will seem to be covered by the background (i.e. walk behind trees or bushes)


Code: Select all

Function Behind ( ByVal source_pixel As UInteger, ByVal destination_pixel As UInteger, ByVal parameter As Any Ptr ) As UInteger
   
 
    If destination_pixel shr 24 < source_pixel shr 24 Then
        Return source_pixel
    Else
        Return destination_pixel
    End If
   
End Function


'' set up the screen and fill the background with a color
ScreenRes 320, 200, 32
'Paint (0, 0), RGB(64, 128, 255)
color ,rgba(0,0,0,0)
cls
line (1,1)-(160,200),rgba(0,0,255,0),bf
line (160,1)-(320,200),rgba(255,0,0,255),bf


'' set up an image and draw something in it
Dim img As Any Ptr = ImageCreate( 32, 32, RGBA(255, 0, 255,0) )
Circle img, (16, 16), 15, RGB(255, 255, 0),     ,     , 1, f
Circle img, (10, 10), 3,  RGB(  0,   0, 0),     ,     , 2, f
Circle img, (23, 10), 3,  RGB(  0,   0, 0),     ,     , 2, f
Circle img, (16, 18), 10, RGB(  0,   0, 0), 3.14, 6.28

'' PUT the image in the center of the screen
Put (160 - 16, 100 - 16), img, custom, @Behind

'' free the image memory
ImageDestroy img

'' wait for a keypress
Sleep


In fact, background image, too, has alpha channel: with this program, the alpha channel is used to determine a pseudo Z-order: if alpha level in background image is higher than alpha level in sprite, the sprite won't be drawn. So, it's also possible to make part of the sprite that is covered by the background, and part that is still visible (i.e. the sprite enters a dark room, and in the darkness only sprite's eyes are visible)
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Postby rolliebollocks » Aug 18, 2010 18:31

Slick.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Aug 18, 2010 18:36

This is Z-sorting on individual pixels using the alpha channel?

So if I wanted a character to walk behind a tree, and have only the parts above the tree be rendered, I would set the tree's alpha values very high so the character's alpha would never be greater?

That's a very neat trick.
angros47
Posts: 1671
Joined: Jun 21, 2005 19:04

Postby angros47 » Aug 18, 2010 19:04

anonymous1337 wrote:This is Z-sorting on individual pixels using the alpha channel?


A sort of

So if I wanted a character to walk behind a tree, and have only the parts above the tree be rendered, I would set the tree's alpha values very high so the character's alpha would never be greater?


Right!
High alpha value->you see it
low alpha value->you don't see it

But, with this trick, alpha value applies to background, too: a low alpha level for a background pixel means that this pixel won't be visible (it will be covered by the sprite)

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 5 guests