Page 1 of 1

How to color a sprite?

Posted: Tue Sep 14, 2010 5:11 am
by X
I ripped the structure and color code from the following url, many thanks to the author:

http://www.purebasic.fr/english/viewtop ... ctx9+color

Below is the code I am using, you can use any 32x32 pixel image to replace stone.bmp :) What am I doing wrong? I wanter color a corner blue ... Oh, please be sure to compile on the 64bit compiler, with directx9, thread safe, and unicode on to emulate what I'm doing :)

Code: Select all

; DirectX9
Structure D3DTLVERTEX
  x.f:y.f:z.f
  rhw.f:Color.l
  tu.f:tv.f
EndStructure 

Structure PB_DX9Sprite3D
  TexRes.l                    ; TexRes
  Vertice.D3DTLVERTEX[4]      ; The 4 vertices for the rectangle sprite
  TmpVertice.D3DTLVERTEX[4]   ; The 4 vertices for the rectangle sprite
  Width.l                     ; width set with ZoomSprite3D()
  Height.l                    ; height set with ZoomSprite3D()
  RealWidth.l
  RealHeight.l
  Angle.f
  Transformed.l
EndStructure

Global Quit.i = #False

Global win.i

Global sprite.i
Global sprite3d.i

Global FPSCounter.i = 0
Global OneSecond.i = 1
Global FinalFPS.i
Global StartTime.i
Global ElapsedTime.i

Procedure calcFPS()
  FPSCounter+1
  
  If OneSecond = 1
    StartTime = ElapsedMilliseconds()
    OneSecond = 0
  EndIf   
  
  ElapsedTime = ElapsedMilliseconds() - StartTime
  
  If ElapsedTime >= 1000
    FinalFPS = FPSCounter 
    FPSCounter = 0
    OneSecond = 1
  EndIf
  
EndProcedure

Procedure SetColor(sprite3d, Color.l)
  Protected *Sprite3D.PB_DX9Sprite3D = IsSprite3D(sprite3d)
  With *Sprite3D
    \Vertice[0]\Color = Color.l
    \Vertice[1]\Color = Color.l
    \Vertice[2]\Color = Color.l
    \Vertice[3]\Color = Color.l
  EndWith
EndProcedure

InitSprite()
InitSprite3D()

TransparentSpriteColor(#PB_Default, #Black)
Sprite3DQuality(1)

win = OpenWindow(#PB_Any, 0, 0, 96, 96, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
OpenWindowedScreen(WindowID(win), 0, 0, 96, 96, 0, 0, 0, #PB_Screen_NoSynchronization)

sprite = LoadSprite(#PB_Any, "stone.bmp", #PB_Sprite_Texture)
sprite3d = CreateSprite3D(#PB_Any, sprite)

Repeat
  
  Repeat
    ; Always process all the events to flush the queue at every frame
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_CloseWindow
        Quit = #True
        
    EndSelect
    
  Until Event = 0 ; Quit the event loop only when no more events are available
  
  
  calcFPS()
  
  
  ; Clear the screen and draw our sprites
  ClearScreen(#Black)
  
  Start3D()
    
    For y = 0 To 3
      For x = 0 To 3
        SetColor(sprite3d, RGB(0,0,255)) ; color sprite blue
        DisplaySprite3D(sprite3d, (x * 32), (y * 32))
      Next
    Next
    
  Stop3D()
  
  StartDrawing(ScreenOutput())
    DrawingMode(#PB_2DDrawing_Default)
    DrawText(5, 5, "FPS " + Str(FinalFPS), RGB(255, 255, 255))
  StopDrawing()
  
  FlipBuffers()
  
  Delay(1)
Until Quit = #True




Re: How to color a sprite?

Posted: Tue Sep 14, 2010 2:52 pm
by Innesoft
What am I doing wrong? I wanter color a corner blue
Not sure exactly what you mean.. I ran the code and it colours the sprite. If you want to colour one corner (?), then just change 1 vertex, not all 4.

Also, for some unknown reason PB insists on swapping Red & Blue in RGB(), so it's RGB(255,0,0) for blue, and RGB(0,0,255) for red.

To colour the top-right corner, your procedure should look like this..

Code: Select all

;Vertex 0 = top-left
;Vertex 1 = top-right
;Vertex 2 = bottom-right
;Vertex 3 = bottom-left

Procedure SetColor(sprite3d, Color.l)
  Protected *Sprite3D.PB_DX9Sprite3D = IsSprite3D(sprite3d)
  With *Sprite3D
    \Vertice[1]\Color = Color.l
  EndWith
EndProcedure

Re: How to color a sprite?

Posted: Tue Sep 14, 2010 4:16 pm
by X
After going through the other thread, it looks like there is a bug in the 64bit compiler that doesn't run the code correctly :( The 32bit compiler runs it fine, though.

Re: How to color a sprite?

Posted: Tue Sep 14, 2010 4:46 pm
by STARGÅTE

Code: Select all

Structure PB_DX9Sprite3D
  TexRes.i                    ; TexRes
  Vertice.D3DTLVERTEX[4]      ; The 4 vertices for the rectangle sprite
  TmpVertice.D3DTLVERTEX[4]   ; The 4 vertices for the rectangle sprite
  Width.l                     ; width set with ZoomSprite3D()
  Height.l                    ; height set with ZoomSprite3D()
  RealWidth.l
  RealHeight.l
  Angle.f
  Transformed.l
EndStructure
TexRes.i !!
TexRes is no Long, its a Integer (Pointer) !

Re: How to color a sprite?

Posted: Tue Sep 14, 2010 5:02 pm
by moogle
X wrote:After going through the other thread, it looks like there is a bug in the 64bit compiler that doesn't run the code correctly :( The 32bit compiler runs it fine, though.
It works fine now (well for me on x64)

Also if you look in my code I posted
moogle wrote:

Code: Select all

SetColor(0, RGB(flameList()\b, flameList()\g, flameList()\r)) ; B,G,R
The R,G,B has to go in as B,G,R for the colors to come out right.

Re: How to color a sprite?

Posted: Tue Sep 14, 2010 5:06 pm
by X
Ah, in your thread (the particle one), the code was modified to use .i instead of a .l somewhere. :) I'll have to try that at home. The BGR and RGB reversal only works on some video cards for some reason. On some cards RGB is red, green, blue, and on other cards RGB is blue, green, red. It seems to be a toss up (random?) chance on which card is which :( But thats a seperate issue all together :)

I'll try the code posted in the other thread. Thanks!

Re: How to color a sprite?

Posted: Tue Sep 14, 2010 5:09 pm
by moogle
X wrote:Ah, in your thread (the particle one), the code was modified to use .i instead of a .l somewhere. :) I'll have to try that at home. The BGR and RGB reversal only works on some video cards for some reason. On some cards RGB is red, green, blue, and on other cards RGB is blue, green, red. It seems to be a toss up (random?) chance on which card is which :( But thats a seperate issue all together :)

I'll try the code posted in the other thread. Thanks!
No problems, I think then I'll add a check to see which format the buffer is in ( DrawingBufferPixelFormat() returns this )

Re: How to color a sprite?

Posted: Tue Sep 14, 2010 5:15 pm
by STARGÅTE
Color convert (A)RGB <-> (A)BGR:

Code: Select all

Procedure FlipColorFormat(Color) 
  ProcedureReturn Color&$FF00FF00 | (Color&$FF)<<16 | (Color&$FF0000)>>16
EndProcedure

Debug Hex(FlipColorFormat($80F0A050),#PB_Long)
Debug Hex(FlipColorFormat($F0A050),#PB_Long)
@X: thx for the information with "On some cards RGB is red, green, blue, and on other cards RGB is blue, green, red."

i will add same checks in my Includes with DrawingBufferPixelFormat()