UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on February 19, 2006, 09:42:21 PM

Title: Twitch Face
Post by: kevin on February 19, 2006, 09:42:21 PM
Here's the source the Twitch Face demo (link bellow). This code should work with PB1.11 or above.  But it's hard to remember now. :)


The demo includes a few tidbits worth noting.


1) Includes frame rate locking code. This forces the demo run the same speed regardless of the physical frame rate.  

  - The method employed in this demo is basically as you would achieve frame rate independence in your PB games.   That is,  providing the game logic and rendering are independent of each other.

  -  To achieve a desired frame rate the logic is updated at the required rate, over a 1 second period.  The slower the machine, means the game logic it's updated generally more than once per cycle.  The beauty of this, is that avoids interpolation.  Which can very messy and overly complex without any real noticeable improvement.  

2) Includes some functions to use the sprite render modes, as if they were image commands.  


That's about it.


Screen Shot Thread (http://www.underwaredesign.com/forums/index.php?topic=1129.0)

Title: Re: Twitch Face
Post by: kevin on July 13, 2020, 10:42:03 PM
PlayBASIC LIVE - Revisiting Twitch Face Demo - (2020-07-14 )


  Here we're taking a look back a demo called Twitch Face from way back in February of 2006. The demo is various stacked blends that 'twitch' or are offset from each other.

 

   
Video:
 
 
     




   
Source Code:
 

    [pbcode]
; PROJECT : Project1
; AUTHOR  : Kevin Picone
; CREATED : 20/02/2006
; EDITED  : 20/02/2006
; ---------------------------------------------------------------------
   

   Image=loadNewImage("face.jpg")


   w=getimagewidth(image)+30
   h=getimageheight(image)+30

   OriginalImage=ResizeFXImage(IMage,w,h,true)

   PrepareFXimage OriginalImage
   Scaleimage OriginalImage,200,100,1
   blurimage originalimage,10

   W=GetImageWidth(OriginalImage)
   H=GetImageHeight(OriginalImage)

   Image1=GetfreeImage()
   CopyImage OriginalIMage,Image1

   Image2=GetfreeImage()
   CopyImage OriginalIMage,Image2





   me=1
   s#=2.1
   max=5
   for me=1 to max
      CreateSprite me
      positionsprite me,400,300+(me*15)
      spriteimage me,Image2
      centerspritehandle me
      spritedrawmode me,2
      scalesprite me,s#
      s#=s#+1.15
      spritetransparent me,true-(me=max)
   next



   ; Calc the Number of milliseconds 1 frame Should Take
   TicksPerFrame=1000/50

   ; Init the Time the first frame started
   StartOfFrame=Timer()      

   
   ; ------------------------------------------------------------------
   ;  Start of Main Loop
   ; ------------------------------------------------------------------
            
   Do   

      ; Get the Curent Timer() value
      Time=Timer()

      ; Calc the number of Frames past since the last STart of last update pass
      FramesPast=(Time-StartOfFrame)/TicksPerFrame
   
      ; Set the Start of frame timer to the next frame
      StartOfFrame=StartOFFrame+(FramesPast*TicksPerFrame)

      ; Set the end of this frame
      NextFrame=STartOfFrame+TicksPerFrame

      ; Update the Logic for the number of frames that have past by
      For Frames=1 to FramesPast
            gosub Update_Logic
            FramesPerSecond=CalcFPS()
      next
            
      ; Render demo state
      gosub Render_Scene

      
      print FramesPerSecond

      Sync
      cls 0

      ; -------------------------------------------------------
      ; Check for a Short frame (Computer is too fast!) so wait
      ; -------------------------------------------------------

      if Timer()<NextFrame
         repeat
            Wait (NewFrame-1)-timer()
         until Timer()>=(NextFrame-1)
      endif


loop   




Render_Scene:

      Create_DistortFX(OriginalIMage,image1,image2,AddColour,FadeLevel#,AlphaLEvel#,dist)
      drawallsprites

      y=50
      DrawImage Originalimage,50,y,0
      DrawImage image1,300,y,0
      DrawImage image2,550,y,0

      y=getscreenheight()-150
      DrawImage image2,50,y,0
      DrawImage image1,300,y,0
      DrawImage OriginalImage,550,y,0

   Return




Update_Logic:

      ; step rotation angle   
      angle#=wrapangle(angle#,1)

      ; values foi the dist effect
      AddColour=Rgb(60+sinradius(angle#,60),20+cosradius(angle#,10),100+cosradius(angle#,100))
      FadeLevel#=0.25
      AlphaLEvel#=0.55-rnd#(0.5)
      dist=rndrange(1,5)

      ; turn sprites
      r#=1
      for lp=1 to max      
         turnsprite lp,(r#/max)*lp
      next

   return


   



Function Create_DistortFX(OriginalImage,image1,image2,AddColour,FadeLevel#,AlphaLEvel#,dist)
      
      ; Blur IMage 1 to image 2
      Apply_AlphaAdd(OriginalIMage,Image1,AddColour,false)

      copyimage Image1,Image2

      Apply_Smudge(Image1,IMage2,alphalevel#,true,dist)

      RenderToScreen

EndFunction







Function Apply_AlphaAdd(SourceIMage,DestImage,RgbColour,transparent)
   oldsurface =getsurface()   
      TempSprite=newsprite(0,0,SourceImage)
      SpriteTransparent TempSprite,Transparent
      SpriteAlphaAddColour TempSprite,RgbColour
      SpriteDrawMode tempsprite, 2+4096
      rendertoimage DestImage
      drawsprite TempSprite   
      deletesprite TempSprite
   rendertoimage OldSurface
EndFunction


Function Apply_Fade(SourceIMage,DestImage,FadeLevel#,transparent)
   oldsurface =getsurface()   
      TempSprite=newsprite(0,0,SourceImage)
      SpriteTransparent TempSprite,Transparent
      SpriteFadelevel  TempSprite,FadeLEvel#
      SpriteDrawMode tempsprite, 2+64

      rendertoimage DestImage
      drawsprite TempSprite   
      deletesprite TempSprite
   rendertoimage OldSurface
EndFunction



Function Apply_Smudge(SourceIMage,DestImage,Level#,transparent,dist)
   oldsurface =getsurface()   
      padx=dist   
      pady=dist   

      TempSprite=newsprite(0,0,SourceImage)
      autocenterspritehandle tempsprite,true
      
      W#=GetSpriteWidth(TempSprite)
      H#=GetSpriteHeight(TempSprite)

      positionSprite tempSprite,w#/2,h#/2
      nw#=(w#+(padX*2))   
      nh#=(h#+(padY*2))   
      scalespritexy tempsprite,nw#/w#,nh#/h#      
            
      SpriteTransparent TempSprite,Transparent
      SpriteAlphalevel  TempSprite,Level#
      SpriteDrawMode tempsprite, 2+4
      turnsprite Tempsprite,rnd(10)
      RenderToImage DestImage
      DrawSprite TempSprite   
      DeleteSprite TempSprite
   rendertoimage OldSurface
EndFunction




Function ResizeFXImage(ThisIMage,NewWidth,NewHeight,CenterFlag)
   oldsurface=getsurface()
   W=GetImageWidth(ThisImage)
   H=GetImageHeight(ThisImage)   
   TempImage=GetFreeImage()
   CreateImage TempImage,NewWidth,NewHeight

   RenderToImage tempimage

   If Centerflag
      CentX=(NewWidth/2)-(W/2)
      CentY=(NewHEight/2)-(h/2)
   endif

   drawimage Thisimage,CentX,CentY,0

   deleteimage ThisImage
   
   rendertoimage oldsurface
EndFunction TempImage



Function CalcFPS()
 Static FPS_NextSecond, FPS_CurrentRate, FPS_TempRate

 CurrentTimer=Timer()
 If currenttimer>FPS_NextSecond
  FPS_NextSecond=CurrentTimer+1000
FPS_CurrentRate=FPS_TempRate
FPS_TempRate=0
Else
FPS_TempRate=FPS_TempRate+1
 EndIf
EndFunction FPS_CurrentRate


    [/pbcode]