SpriteEngine: Atlas,View,Layer,HotSpot,Flip,Animation,FX

Share your advanced PureBasic knowledge/code with the community.
User avatar
[blendman]
Enthusiast
Enthusiast
Posts: 297
Joined: Thu Apr 07, 2011 1:14 pm
Location: 3 arks
Contact:

Re: SpriteEngine: Atlas,View,Layer,HotSpot,Flip,Animation,FX

Post by [blendman] »

Hi
I have tried your spritenegine, but it didn't work.
The texture of my sprite player is always the orange box.

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  
  XIncludeFile "SpriteEngine.pbi"

  UsePNGImageDecoder()
  UseJPEGImageDecoder()
  
  DisableExplicit
   If InitEngine3D()=0 Or InitSpriteEngine()=0
      MessageRequester("Init", "Failed!")
      End
   EndIf
   InitKeyboard()

   AntialiasingMode(#PB_AntialiasingMode_None)
   
   win=OpenWindow(#PB_Any, 0, 0, 800, 600, "Sprite Engine FX", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   OpenWindowedScreen(WindowID(win), 0, 0, WindowWidth(win), WindowHeight(win))
   
      
   Procedure LoadImage2(image,filename$)
     
     img = LoadImage(#PB_Any,filename$)
     w = ImageWidth(img)
     h = ImageHeight(img)
     Atlas=CreateAtlas(#PB_Any, w, h)
     If StartDrawing(AtlasOutput(Atlas))     
        DrawingMode(#PB_2DDrawing_AlphaBlend)
        DrawAlphaImage(ImageID(img),0,0)    
        StopDrawing()
      EndIf
      
      Frame=SetAtlasFrame(Atlas, #PB_Any, 0, 0, w,h,w/2,h*0.8)
      
     ProcedureReturn Frame
   EndProcedure
      
   ;draw sprite image atlas
   fontID=LoadFont(0, "Arial", 22, #PB_Font_Bold|#PB_Font_HighQuality|#PB_Font_Italic)
   spriteAtlas=CreateAtlas(#PB_Any, 128, 128)
   If StartDrawing(AtlasOutput(spriteAtlas))     
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      FrontColor(RGBA(200, 110, 20, 255))
      Box(0, 0, 128, 128)     
      StopDrawing()
   EndIf
   ;define sprite frames (with centered hotspot)
   squareFrame = SetAtlasFrame(spriteAtlas, #PB_Any, 5, 5, 16, 64,8,32)
   dotFrame = SetAtlasFrame(spriteAtlas, #PB_Any, 5, 5, 2,2 ,1 ,1)
   
   ;create front view
   frontView=CreateView(#PB_Any, RGB(62, 62, 62), #False, 0, 0, 100, 100)
    
   If frontView
     
     ;add layer
      newLayer=CreateLayer(#PB_Any, frontView, AtlasID(spriteAtlas))
      
      sprite = LoadImage2(#PB_Any,"player.png")
      Perso =  CreateAnimatedSprite(#PB_Any, newLayer, 100, 300, sprite)
      
      dot sprites
      dot=CreateAnimatedSprite(#PB_Any, newLayer, 224, 153, dotFrame)
      SetSpriteColor(dot,RGBA(10, 156, 63, 255))
     
      skew sprites
      rotatingSprite=CreateAnimatedSprite(#PB_Any, newLayer, 224, 153, squareFrame)
      SkewSprite(rotatingSprite, 32, -32)
   EndIf
      
   Repeat
     
     ExamineKeyboard()
     If KeyboardPushed(#PB_Key_Right)
       MoveView(frontView,1,0)
     EndIf
     If KeyboardPushed(#PB_Key_Left)
       MoveView(frontView,-1,0)
     EndIf
     If KeyboardPushed(#PB_Key_Down)
       MoveView(frontView,0,-1)
     EndIf
     If KeyboardPushed(#PB_Key_Up)
       MoveView(frontView,0,1)
     EndIf         
      RenderSprites()
      RenderWorld()
      RotateSprite(rotatingSprite,1)
      FlipBuffers()
   Until WaitWindowEvent(1)=#PB_Event_CloseWindow
   End
CompilerEndIf
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: SpriteEngine: Atlas,View,Layer,HotSpot,Flip,Animation,FX

Post by eddy »

hmm...This code needs a serious upgrade. :?

But I'm tired to hack 3D API to make custom sprite lib. For the moment, there're some limitations.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply