Transparency in 2D

Blitz3D Forums/Blitz3D Programming/Transparency in 2D

Hey!

I'm doing another game in 2d, and this time we want a circle where everything is full-bright, and everything outside it is half-bright. Is there a simple way to overlay a sprite with transparency, or do I gotta copy black 128-alpha pixels all over the outside part?

Yes, of course, you can use a Sprite IF you are in 3D Graphic Mode. use a TGA and define the circles transparency in the alpha channel, then use the alpha mode for the sprite.

Set the cameraclsmode to Not delete the background
position the sprite in front of the camera and paretn it to the camera.

In the loop:

Draw all 2D stuff
renderworld
flip

and since you are working with it, you can also add some nice explosions using sprites :) - if you want.

I may do that. In the mean time, I have a question about writepixel and alpha:

SetBuffer(FrontBuffer())
Color 255, 255, 255
Rect 0, 0, 639, 479

imbuf = ImageBuffer(image)

For x = 0 To 639
	For y = 0 To 479
		pix = ReadPixel(x, y, imbuf)
		If ((pix And $00ffffff)) = 0 Then
			WritePixel(x, y, $80000000)
		EndIf
	Next
Next


image has black and white pixels in it. This code draws black to the front buffer wherever there are pixels that are black in the image.

But I thought the "80" part of the writepixel command meant alpha of 50%, and should yield a grey pixel.

What's going on?

the alpha-byte works only with textures. and even there only when you have the alpha-flag set (2).

If you really want to darken a pixel to half its brightness, you could:

ibuffer=imagebuffer(image)
fbuffer=frontbuffer()
lockbuffer ibuffer
lockbuffer fbuffer
for y=0 to 479
  for x=0 to 639
    mask=readpixelfast(x,y,ibuffer) and $FFFFFF
    if mask=0
      rgb=readpixelfast(x,y,fbuffer) and $FEFEFE
      writepixelfast x,y rgb shr 1,fbuffer
    endif
  next
next
unlockbuffer ibuffer
unlockbuffer fbuffer


Got it. Thanks a bunch for the help.

Got another question:

Graphics3D 640, 480, 16, 1

thesprite = LoadSprite("Gfx/Torch1.tga", 3)
thecamera = CreateCamera()
thetimer = CreateTimer(60)

CameraClsMode(thecamera, False, False)

MoveEntity(thecamera, 0, 0, 0)
AlignToVector(thecamera, 0, 1, 0, 2)
AlignToVector(thecamera, 0, 0, 1, 3)

MoveEntity(thesprite, 0, 0, 1)
SpriteViewMode(thesprite, 3)

Color 255, 255, 255

While Not KeyHit(1)
	SetBuffer(BackBuffer())
	Rect 0, 0, 640, 480
	
	RenderWorld
	Flip
	WaitTimer(thetimer)
Wend

Stop


This is working, almost. The sprite is rendering with odd bitmap garbage on it. It's a 640x480 texture... is that too big?

640*480 is a bad texture size, use power-of-2 sizes: 32,64,128,512,1024 etc.

So you could externally resize your Sprite Texture to 512*512 and then scale the sprite in Blitz to fit the screen. I don't know, maybe that's the reason why you've got that garbage onscreen.

Okay, tried a 64x64 texture, still getting the garbage... Like 75% of the image is correct, but there are lines and stripes in it...

Hey jfk... could I mail you my example? It's pretty small...

Got it sorted. From above:

CameraClsMode(thecamera, False, False)


Note the second False... was setting it not to clear the z-buffer. D'oh.

Out of interest... is using WritePixel/ReadPixel to write to the frontbuffer fast enough for you?

It took a long time to loop through all the pixels doing that. Certainly couldn't be done on a frame by frame basis.