Page 1 of 2

sprite3d transparency

Posted: Fri Jan 13, 2006 12:28 pm
by diGGa
hey
why doesnt work my code?

Code: Select all

; ###########################
;
; Projekt Titel: Mouse Avoider
; Autor: Jaap 'Thoniel.NET' Groeneveld
; Beginn: 10.1.06
; Idee: http://www.actionflash.com/mouseavoider2.php
;
; ###########################

;{/ ----- Initialisieren -----
InitSprite()
InitSprite3D()
InitMouse()
InitKeyboard()
UsePNGImageDecoder()

OpenWindow(#PB_Any, 10, 10, 640, 480, #PB_Window_ScreenCentered|#PB_Window_SystemMenu, "Mouse Avoider by Thoniel.NET /minigames")
OpenWindowedScreen(WindowID(),0,0,640,480, 1,0,0)
; OpenScreen(640,480,32,"Mouse Avoider")
;} ----- Initialisieren -----

;{/ ----- Variablen -----
; ----- Variablen Deklarieren -----
Global mx.f,my.f, Event
Global imgPlayer
; ----- Variablen Laden -----
#cfgPlayerTailLength = 4
Enumeration
        #imgPlayer
EndEnumeration

;} ----- Variablen Ende -----

;{/ ----- Strukturen -----
Structure player
        x.f
        y.f
        oldx.f
        oldy.f
EndStructure
Global spieler.player\x, spieler.player\y

Structure playertail Extends player
        remains.w
EndStructure
Dim tail.playertail(#cfgPlayerTailLength)


;} ----- Strukturen Ende -----

;{/ ----- Prozeduren -----
Procedure UpdateEngines()
        FlipBuffers()
       
        ;maus
        If GetFocus_() = WindowID()
                If WindowMouseX()>0 And WindowMouseX()<640-1 And WindowMouseY()>0 And WindowMouseY()<480-1
                        ExamineMouse()   
                        If captured = 0
                                ReleaseMouse(0)
                                captured = 1
                                MouseLocate(WindowMouseX(),WindowMouseY())
                        EndIf
                Else
                        ReleaseMouse(1)
                        captured = 0
                EndIf
        Else
                ReleaseMouse(1)
                captured = 0
        EndIf
        ; /maus     
        ExamineKeyboard()
        ClearScreen(0,0,0)   
       
        Event = WindowEvent()         
        mx=WindowMouseX()
        my=WindowMouseY() 
EndProcedure

Procedure UpdatePlayerPosition()
        spieler.player\oldx = spieler.player\x
        spieler.player\oldy = spieler.player\y
        spieler.player\x = mx
        spieler.player\y = my
EndProcedure

Procedure UpdatePlayerTail()
        Shared spieler.player\oldx, spieler.player\oldy
        For i=0 To #cfgPlayerTailLength
                tail.playertail(i)\oldx = tail.playertail(i)\x
                tail.playertail(i)\oldy = tail.playertail(i)\y
        Next
       
       
        tail.playertail(0)\x = spieler.player\oldx
        tail.playertail(0)\y = spieler.player\oldy
       
        For i = 1 To #cfgPlayerTailLength
                tail.playertail(i)\x = tail.playertail(i-1)\oldx
                tail.playertail(i)\y = tail.playertail(i-1)\oldy
        Next
EndProcedure

Procedure DrawPlayer()
        DisplaySprite3D(#imgPlayer, spieler.player\x, spieler.player\y)
EndProcedure

Procedure DrawPlayerTail()
        transparency.w = 100
        faktor.w = #cfgPlayerTailLength/100
        For i = 0 To #cfgPlayerTailLength
                transparency = transparency - faktor
                DisplaySprite3D(#imgPlayer, tail.playertail(i)\x, tail.playertail(i)\y, transparency)
        Next
EndProcedure
;} ----- Prozeduren Ende -----

;{/ ----- Start Prozeduren -----

;} ----- Start Prozeduren Ende -----

;{/ ----- Mediendaten Laden -----

LoadSprite(#imgPlayer, "player.png", #PB_Sprite_Texture)
CreateSprite3D(#imgPlayer, #imgPlayer)
TransparentSpriteColor(#imgPlayer, 255, 0, 255)

;} ----- Mediendaten Laden Ende -----

;{/ ----- Hauptschleife -----
Repeat
        UpdateEngines()

        UpdatePlayerPosition()
        UpdatePlayerTail()
       
        Start3D()
        DrawPlayerTail()
        DrawPlayer()
        Stop3D()
Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow
End
;} ----- Hauptschleife Ende ----- 
the problem is that the mousesprite/mousetailsprite is not transparent. the 255,0,255 is still on my screen...
why is that?

thanx for help

Posted: Fri Jan 13, 2006 4:22 pm
by netmaestro
Seems fine here.

Posted: Fri Jan 13, 2006 6:27 pm
by Truth_Seeker
Program works fine for me also. You might be seeing pink if the part you want to be not transparent on your image is anti-aliased. To get rid of all the pink you need to have non anti-aliased for the object you want displayed. When a object anti-aliased is used it smooths the edges by blending the background and the your object. Therefore the color you want transparent would not only be pink but the many variations between your object's color and the background. Which if you set all of those transparent you would end up with a non smooth edged object.

So until we have alphablending through png files (which I think is going to be in purebasic 4). We can not really do anything with anti-alias. So just remove the smoothing or anti-alias, whatever your program calls it, from the object and your program should work fine.

Posted: Fri Jan 13, 2006 7:31 pm
by netmaestro
There is probably something wrong with your image, as it should work. If The image is compressed, the transparent color won't be perfect. It must be perfect to work right.

Posted: Fri Jan 13, 2006 7:46 pm
by diGGa
the problem is when im not using sprite3d ive my transparency so it cant go with the image...
i will upload my image so you can test it.

http://web484.servana.de/temp/player.png

//edit
ok you're right but i dont know why my image was fucked up... i made a new one and it worked (beside this anti alias stuff)... so thx for your help...

//edit2
now im totaly confused... i made again a new "cursor" and this time it didnt work again... a 5x5 pixel one workd a 16x16 and 32x32 didnt work.

Posted: Fri Jan 13, 2006 8:02 pm
by netmaestro
Little tip: For an image that size a .png file is going to be no smaller than a .bmp and the bmp is better quality and does not require the overhead of a decoder. Even when I'm already using a decoder for larger images in my projects, I leave the little ones .bmp for the quality.

Posted: Fri Jan 13, 2006 8:09 pm
by diGGa
now im totaly confused... i made again a new "cursor" and this time it didnt work again... a 5x5 pixel one workd a 16x16 and 32x32 didnt work.

Posted: Fri Jan 13, 2006 8:10 pm
by Max.
Void.

Posted: Fri Jan 13, 2006 8:14 pm
by diGGa
well... bmp doesnt even compress if im right

Posted: Fri Jan 13, 2006 8:22 pm
by netmaestro
That's right, BMP's are uncompressed. But at small image sizes their filesize rivals that of the compressed formats. It's just the bigger images where you see a huge difference.

Posted: Fri Jan 13, 2006 8:25 pm
by diGGa
doesnt works... i dont unterstand this...

ive got this code

Code: Select all

TransparentSpriteColor(#PB_Any, 255, 0, 255) 
LoadSprite(#imgPlayer, "player.png", #PB_Sprite_Texture)
CreateSprite3D(#imgPlayer, #imgPlayer)


LoadSprite(#imgTileset,"tileset.png") : CreateAnimSprite(Tileset.AnimSprite,#imgTileset,32,32)
my tileseit is transparent but my cursor remains pink.

Posted: Fri Jan 13, 2006 9:39 pm
by netmaestro
You can't use #PB_Any to set TransparentSpriteColor. It must be set to a specific sprite.

Posted: Fri Jan 13, 2006 10:07 pm
by diGGa
so then why is my tileset transparent?

Posted: Fri Jan 13, 2006 10:07 pm
by Fred
sprite3() size should be square multiple of 2 to work (almost) everywhere. So, if you need a 5x5 sprite, do a 8x8 one with the remaining pixels as transparent (it's documented in the sprite3D doc IIRC).

Posted: Tue Mar 21, 2006 7:31 pm
by dagcrack
BMP does handle RLE as well, Its just never been widely supported.
"Never say never".

PNG is lossless as said. The encoding is pretty much slow (on high compression settings of course), the decoding however is pretty fast (although PB implementation -is pnglib?- Is actually pretty slow).

If you want to save space (both in disk and memory).. and your sprites has limited amounts of color, best thing is to save them in 8bpp. (Just make sure, in the case that you've got a transparent color, for example: R=255,G=0,B=255, that you save this color in the indexed table, because if you let your program generate the index table by itself, it might only give you an approximate of said color, hence, you will have trouble with the transparency since the color is not 255,0,255 anymore in RGB, So, it is wise to lock this color on index 1 if you can).

8 bits per pixel means a byte per pixel- A pixel can have therefore one of 255 different colors which are stored in an indexing list (usualy at the header of the image file format).

I can't tell how PB handles different bpp ... However my advice is that you shouldnt mix this (ie: have some tiles with 24bpp and some others with 8bpp or less, just to prevent compatibility issues. But then again, I don't know how PB handles this internally).


If you're feeling adventurous, you can always design your own image format among with your own color space system, etc. ;)

If HDD space is an issue for you, PM me for some tips regarding image compression (both lossy and lossless).

As said, theres always a tradeoff between speed, size, etc. So choose wisely!.



(Sorry for the ugly formatting of the text, I'm just in a hurry!).