News:

Function Finder  Find all the functions within source code files

Main Menu

Sprite lighting

Started by kevin, August 07, 2005, 07:50:46 AM

Previous topic - Next topic

kevin

This example uses the sprites Gouraud shade draw mode combined with the new posted rotated vertex commands to manually light 200 rebounding sprites within a circular zone.




Source Code

This example requires PB1.086 beta or higher



; This Demo Uses the GouraudShade sprite draw mode and the sprite vertex reading
; to create a light source styled zone effect.


; convert default font #1 from a true type font to a bitmap version
MakeBitmapFont 1,$ffffff

; load the bubble image
#if PBCompileMode=0
    Path$="..\..\"
#endif


LoadImage CurrentDir$()+path$+"gfx\bubble_64x64.bmp",1



; Prepare this image for Effects (rotation in this case)
   PrepareFXImage 1


; get the Screen widht/height
sw=GetScreenWidth()
sh=GetScreenHeight()

; set starting number of  Bubbles
BubblesN = 200

; Create the Bubble Type
Type Bubble
  X#,y#,XSpeed#,YSpeed#,Angle#,RotSpeed#
EndType

; Direct All Drawing to the screen (back buffer)
RenderToScreen



LightRadius=250
   




; Start of DO/Loop
Do
Cls rgb(0,0,0)

// ===================================================================
// Init Sprites, dim's an array sets a bunch of sprites ready for use
   // ====================================================================

  if InitStars=0
  InitStars=1
  Randomize 42
  Dim Bubbles(BubblesN) As Bubble
  deleteallsprites
  For starsI=1 To BubblesN
  Bubbles(starsI).x = Rnd(sw)
  Bubbles(starsI).y = Rnd(sh)
  Bubbles(starsI).xspeed = Rnd(5)
  Bubbles(starsI).yspeed = Rnd(5)
  Bubbles(starsI).angle = Rnd(360)
  INC speed#
  If speed#>10 Then speed#=1
  Bubbles(starsI).rotspeed = speed#

  CreateSprite starsI
  SpriteImage starsI,1
  SpriteTransparent starsI,off
  PositionSprite starsI,starsX,starxY
  PositionSpriteZ Starsi,100+lp

; Sprite Draw Mode
; 2= Rotated
; 128 = gouraud shade
  SpriteDrawMode starsi,2+128
  SpriteHandle Starsi,(GetImageWidth(1)/2)*-1,(GetImageHeight(1)/2)*-1
  RotateSprite Starsi,Bubbles(starsI).angle
  ScaleSprite StarsI,0.15+(Rnd(100.0)/100.0) 
  Next starsI
endif


  LightX=mouseX()
  LightY=mouseY()
 
    if Mousebutton()
         Light=off
    else
      Light =on
  endif

     // =================================================================
     // Run Through and update all the sprites positions
     // =================================================================
  For starsI=1 To BubblesN
    x#=Bubbles(starsI).x+Bubbles(starsI).xspeed
    Y#=Bubbles(starsI).y+Bubbles(starsI).Yspeed
 
    If X#<0 Or X#>sw
    Bubbles(starsI).xspeed=Bubbles(starsI).xspeed*-1
    EndIf
 
    If Y#<0 Or Y#>sh
      Bubbles(starsI).yspeed=Bubbles(starsI).yspeed*-1
    EndIf

    TurnSprite Starsi,Bubbles(starsI).rotspeed
    PositionSprite Starsi,X#,Y#
    Bubbles(starsI).x=x#
    Bubbles(starsI).Y=y#


; Check if the Sprite is within the Lights Radius
      if Light =on

    SpriteDrawMode starsi,2+128

   
    if CircleHitSprite(LightX,lighty,LIghtRadius,Starsi)
      SpriteVisible starsi,On

  ; Light Sprites Vertex
 
      x1#=GetSpriteVertexX(Starsi,0)
      y1#=GetSpriteVertexY(Starsi,0)
      x2#=GetSpriteVertexX(Starsi,1)
      y2#=GetSpriteVertexY(Starsi,1)
      x3#=GetSpriteVertexX(Starsi,2)
      y3#=GetSpriteVertexY(Starsi,2)
      x4#=GetSpriteVertexX(Starsi,3)
      y4#=GetSpriteVertexY(Starsi,3)
   
        CalcIntensity(LightX,LightY,LightRadius,StarsI,0,X1#,Y1#)
        CalcIntensity(LightX,LightY,LightRadius,StarsI,1,X2#,Y2#)
        CalcIntensity(LightX,LightY,LightRadius,StarsI,2,X3#,Y3#)
        CalcIntensity(LightX,LightY,LightRadius,StarsI,3,X4#,Y4#)

    else
      SpriteVisible starsi,Off
     
    endif
    else
   
    SpriteDrawMode starsi,2
    endif

  Next starsI


    if Mousebutton()
     For spr=1 To BubblesN
  SpriteImageIntensity spr,0,255
  SpriteImageIntensity spr,1,255
  SpriteImageIntensity spr,2,255
  SpriteImageIntensity spr,3,255
  next
    endif


   DrawallSprites


  circle lightx,lighty,lightradius,0


Text 0,0,"Fps: "+Str$(FPS())+" Sprites:"+str$(BubblesN)
Sync
Loop


Psub CalcIntensity(LightX,LightY,LightRadius,ThisSprite,VertexIndex,VertX#,VertY#)
dist#=getdistance2d(vertx#,verty#,lightx,lighty)
if Dist#>LightRadius
  SpriteImageIntensity ThisSprite,VertexIndex,0
else
  SpriteImageIntensity ThisSprite,VertexIndex,255-((Dist#/LightRadius)*255)
endif 
EndPsub