Animated Sprite w/Alpha Channel

Advanced game related topics
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Animated Sprite w/Alpha Channel

Post 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"
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
zxtunes.com
Enthusiast
Enthusiast
Posts: 375
Joined: Wed Apr 23, 2008 7:51 am
Location: Saint-Petersburg, Russia
Contact:

Post by zxtunes.com »

Cool! :roll:

And it will work in 16 bit mode?
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

Cute :)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post 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
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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. ;)
oh... and have a nice day.
Post Reply