How to color a sprite?

Just starting out? Need help? Post your questions and find answers here.
X
Enthusiast
Enthusiast
Posts: 311
Joined: Tue Apr 04, 2006 6:27 am

How to color a sprite?

Post 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



User avatar
Innesoft
Enthusiast
Enthusiast
Posts: 105
Joined: Mon Jan 18, 2010 10:30 am
Location: UK
Contact:

Re: How to color a sprite?

Post 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
X
Enthusiast
Enthusiast
Posts: 311
Joined: Tue Apr 04, 2006 6:27 am

Re: How to color a sprite?

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to color a sprite?

Post 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) !
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: How to color a sprite?

Post 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.
Image
X
Enthusiast
Enthusiast
Posts: 311
Joined: Tue Apr 04, 2006 6:27 am

Re: How to color a sprite?

Post 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!
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: How to color a sprite?

Post 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 )
Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to color a sprite?

Post 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()
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply