How to color a sprite?
Posted: Tue Sep 14, 2010 5:11 am
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 
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


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