Page 1 of 1

Animated Sprite w/Alpha Channel

Posted: Wed Feb 04, 2009 3:02 pm
by J. Baker
Just a small test of an animated sprite with alpha channel. Figured it could help someone out. Thanks to S.M. for the ClipSprite3D code.

Download Code & Graphics

Code: Select all

;ClipSprite3D by S.M.
;Graphics & Animated Sprite w/Alpha by J. Baker
;PureBasic v4.30 (x86)

Structure DDPIXELFORMAT
  dwSize.l
  dwFlags.l
  dwFourCC.l
  dwRGBBitCount.l
  dwRBitMask.l
  dwGBitMask.l
  dwBBitMask.l
  dwRGBAlphaBitMask.l
EndStructure

Structure DDCOLORKEY
  dwColorSpaceLowValue.l
  dwColorSpaceHighValue.l
EndStructure

Structure DDSCAPS2
  dwCaps.l
  dwCaps2.l
  dwCaps3.l
  dwCaps4.l
EndStructure

Structure DDSURFACEDESC2
  dwSize.l
  dwFlags.l
  dwHeight.l
  dwWidth.l
  lPitch.l
  dwBackBufferCount.l
  dwRefreshRate.l
  dwAlphaBitDepth.l
  dwReserved.l
  lpSurface.l
  ddckCKDestOverlay.DDCOLORKEY
  ddckCKDestBlt.DDCOLORKEY
  ddckCKSrcOverlay.DDCOLORKEY
  ddckCKSrcBlt.DDCOLORKEY
  ddpfPixelFormat.DDPIXELFORMAT
  ddsCaps.DDSCAPS2
  dwTextureStage.l
EndStructure

Structure D3DTLVERTEX
  sx.f
  sy.f
  sz.f
  rhw.f
  color.l
  specular.l
  tu.f
  tv.f
EndStructure

Structure PB_Sprite3D
  Texture.IDirectDrawSurface7 ; DirectDrawSurface7
  Vertice.D3DTLVERTEX[4]      ; The 4 vertices for the rectangle sprite
  Width.w          ; width set with ZoomSprite3D()
  Height.w       ; height set with ZoomSprite3D()
  unknown.l
EndStructure

Procedure ClipSprite3D(Sprite3D,ClipX,ClipY,ClipWidth,ClipHeight)
  *Sprite3D.PB_Sprite3D=IsSprite3D(Sprite3D)
 
  If *Sprite3D=0:ProcedureReturn 0:EndIf
  *DDS.IDirectDrawSurface7=*Sprite3D\Texture
 
  DDSDESC.DDSURFACEDESC2
  DDSDESC\dwSize=SizeOf(DDSURFACEDESC2)
  Result=*DDS\GetSurfaceDesc(DDSDESC)
  If Result:ProcedureReturn 0:EndIf
 
  *Sprite3D\Vertice[0]\tu=ClipX/DDSDESC\dwWidth
  *Sprite3D\Vertice[0]\tv=ClipY/DDSDESC\dwHeight
 
  *Sprite3D\Vertice[1]\tu=(ClipX+ClipWidth)/DDSDESC\dwWidth
  *Sprite3D\Vertice[1]\tv=ClipY/DDSDESC\dwHeight
 
  *Sprite3D\Vertice[2]\tu=ClipX/DDSDESC\dwWidth
  *Sprite3D\Vertice[2]\tv=(ClipY+ClipHeight)/DDSDESC\dwHeight
 
  *Sprite3D\Vertice[3]\tu=(ClipX+ClipWidth)/DDSDESC\dwWidth
  *Sprite3D\Vertice[3]\tv=(ClipY+ClipHeight)/DDSDESC\dwHeight
 
  ProcedureReturn 1 ; return success
EndProcedure

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Sprite system can't be initialized", 0)
  End
EndIf

If InitSprite3D() = 0
  MessageRequester("Error", "Sprite3D system can't be initialized correctly", 0)
  End
EndIf

UsePNGImageDecoder()

If OpenWindow(0, 0, 0, 640, 480, "Animated Sprite", #PB_Window_SystemMenu| #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

    If OpenWindowedScreen(WindowID(0), 0, 0, 640, 480, 0, 0, 0)
    Else
      MessageRequester("Error", "Can't open windowed screen!", 0)
      End
    EndIf
  EndIf
  CatchSprite(0, ?Pic1, #PB_Sprite_AlphaBlending | #PB_Sprite_Texture)
  CatchSprite(1, ?Pic2)
  ;LoadSprite(0, "flower.png", #PB_Sprite_AlphaBlending | #PB_Sprite_Texture)
  CreateSprite3D(0, 0)
  CreateSprite3D(1, 0)
  CreateSprite3D(2, 0)
  
  Sprite3DQuality(1)
  
  ;LoadSprite(1, "scene.png", 0)
  
  iW = 0
  iH = 0
  
  Repeat
    ; It's very important to process all the events remaining in the queue at each frame
    ;
    
    If Start3D()
      DisplaySprite(1, 0, 0)
      ZoomSprite3D(0, 160, 120)
      ClipSprite3D(0, iW, iH, 160, 120)
      DisplaySprite3D(0, 20, 340)
      DisplaySprite3D(0, 160, 340)
      DisplaySprite3D(0, 300, 340)
      DisplaySprite3D(0, 440, 340)
      
      
     If iW = 160 And iH = 360
      iW = 0
      iH = 0
     EndIf
      
       Delay(166) ;roughly 6 fps
       
      iW +160
     If iW = 640
      iH +120
      iW = 0 
     EndIf
      
      Stop3D()
    EndIf
    
    Repeat
      Event = WindowEvent()
      
      
      
      If Event = #PB_Event_CloseWindow
        End 
      EndIf
    Until Event = 0
    
    FlipBuffers() 
    ClearScreen(RGB(0, 96, 159))
    ;Delay(1)
  ForEver
  
Pic1: IncludeBinary "flower.png"
Pic2: IncludeBinary "scene.png"

Posted: Sat Feb 07, 2009 12:44 am
by zxtunes.com
Cool! :roll:

And it will work in 16 bit mode?

Posted: Sat Feb 07, 2009 2:03 am
by djes
Cute :)

Posted: Sat Feb 07, 2009 2:09 am
by Kaeru Gaman
zxtunes.com wrote:And it will work in 16 bit mode?
well, surely NOT, because 16bit mode does not have any alpha transparency.

Posted: Tue Feb 17, 2009 4:24 am
by Amiga5k
Kaeru, nice to see your POV-Ray link (It's good to see that modeller is still around, even with Blender and trueSpace out there in the 'free' sw world).

Russell

Posted: Sun Mar 15, 2009 5:38 pm
by Kaeru Gaman
@Amiga5k

oops.. just happened to find this thread again today...

since POVRay is no Modeller but a RayTracer, it will always have it's place regardless of blender etc.
the Main Difference for me is, that I can script the scenes and use CSG,
that is something completely different from clicking Meshes together. ;)