not really a tip... but kinda interesting 'shadow'

Share your advanced PureBasic knowledge/code with the community.
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

not really a tip... but kinda interesting 'shadow'

Post by dracflamloc »

Its basically just a little hack that almost gets a kind of 2d dynamic shadow in pb. Its slow though so its really only fun for messing around. I was bored in a networking class today. Its not even fully functional since ideally you'd only draw the two outermost border lines so the whole shadow would get filled.

Code: Select all

InitSprite()

OpenWindow(0,0,0,640,480,"Shadow Test",#PB_Window_SizeGadget|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,640,480,1,0,0)

Global bx=150
Global by=150
Global bx2=200
Global by2=250
Global bw=50
Global bh=100

lx=200
ly=380
lr=250
lc=RGB(255,255,255)

Global sc=RGB(1,1,1)


Procedure.b InBox(x,y)
  If x>bx And x<bx2 And y>by And y<by2
    ProcedureReturn 1
  EndIf 
  
  ProcedureReturn 0
  
EndProcedure

Procedure DrawShadowLine(dx,dy,px,py)
  ;dx=dx/10
  ;dy=dy/10
 
  If dx<>0 Or dy<>0
    tmpx=px
    tmpy=py
    
    Repeat
      tmpx + dx
      tmpy + dy
;       If InBox(tmpx,tmpy)
;         ProcedureReturn
;       EndIf 
    Until tmpx < 0 Or tmpx > 640 Or tmpy < 0 Or tmpy > 480
    
    LineXY(px,py,tmpx,tmpy,sc)
  EndIf
EndProcedure

Repeat
  InitKeyboard()
  
  event=WindowEvent()
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Left)
    lx - 2
  ElseIf KeyboardPushed(#PB_Key_Right)
    lx + 2
  ElseIf KeyboardPushed(#PB_Key_Up)
    ly - 2
  ElseIf KeyboardPushed(#PB_Key_Down)
    ly + 2
  ElseIf KeyboardPushed(#PB_Key_Escape)
    End 
  EndIf
  
  FlipBuffers()
  ClearScreen(RGB(0,0,120))
  
  StartDrawing(ScreenOutput())
  
  wavertime.f=wavertime+0.5
  If wavertime>6
    wavertime=0
  EndIf 
  wamt=Sin(wavertime)
  lxt=lx+wamt
  wamt=Cos(wavertime/2)
  lyt=ly+wamt
  
  ;draw lights
  Circle(lxt,lyt,lr,lc)
  Circle(lxt,lyt,10,RGB(255,255,0))
    
  ;draw geometry  
  Box(bx,by,bx2-bx,by2-by,RGB(0,255,0))
  DrawingMode(#PB_2DDrawing_Outlined)
  Box(bx,by,bx2-bx,by2-by,sc)
  Box(1,1,638,478,sc)
  DrawingMode(0)
  
  ;draw shadow border
  dx=bx-lxt
  dy=by-lyt
  DrawShadowLine(dx,dy,bx,by)
  
  dx=bx-lxt
  dy=by2-lyt
  DrawShadowLine(dx,dy,bx,by2)
  
  dx=bx2-lxt
  dy=by-lyt
  DrawShadowLine(dx,dy,bx2,by)
  
  dx=bx2-lxt
  dy=by2-lyt
  DrawShadowLine(dx,dy,bx2,by2)
  
  ;fill shadow
  bcx=bx+bw/2
  bcy=by+bh/2
  dx=bcx-lxt
  dy=bcy-lyt
  ;dx=dx  ;choose sufficiently small dx, but not too small
  ;dy=dy  ;choose sufficiently small dy, but not too small
  tmpx=bcx
  tmpy=bcy
  Repeat 
    tmpx+dx
    tmpy+dy
  Until InBox(tmpx,tmpy)=0
  
  FillArea(tmpx,tmpy,sc,sc)
  
  ;tempfix, draw light point again
  Circle(tmpx,tmpy,1,RGB(251,0,0))
  
  StopDrawing()  

  Delay(16)
Until event=#PB_Event_CloseWindow

End
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Try moving the InitKeyboard() command to before the Repeat loop ;)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

:lol: oops
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

What's the record on the worm game?
Think I'm about to beat it ;)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
Post Reply