RADAR from CodArch adapting from PB4 to PB5.2x [SOLVED]

Everything related to 3D programming
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

RADAR from CodArch adapting from PB4 to PB5.2x [SOLVED]

Post by LiK137 »

I have adapted Radar.pb from CodArchive to run on PB5.2x - it runs but unfortunately window itself hangs. Neither delay(1) nor (nothing) helps.

belaw the code:

Code: Select all

;>------------------------------------ 
; 
; by Danilo, 20.11.2003 - german forum 
; extends by zomtex2003, 23.11.2003
;>------------------------------------ 

;#Width  = 1600 
;#Height = 1200 
#Width  = 640 
#Height = 480 
;#Width  = 1024 
;#Height = 768 

;#Width  = 800 
;#Height = 600 

PI.f = 3.141593 

#x_0 = #Width / 2
#y_0 = #Height / 2

#rotator_speed = 3 
#max_objects = 10 
CurrentAngle = 180

Structure _OBJ 
  x.l 
  y.l 
  Transparency.l 
  Degree.f 
EndStructure 


Procedure.f winkel(x1.f,y1.f,x2.f,y2.f) 
  a.f = x2-x1 
  b.f = y2-y1 
  c.f = Sqr(a*a+b*b) 
  winkel.f = ACos(a/c)*57.29577 
  If y1 < y2 : winkel=360-winkel : EndIf 
  ProcedureReturn winkel 
EndProcedure

Procedure.f GSin(winkel.f) 
   ProcedureReturn Sin(winkel*(2*3.14159265/360)) 
EndProcedure 

Procedure.f GCos(winkel.f) 
   ProcedureReturn Cos(winkel*(2*3.14159265/360)) 
EndProcedure 

Procedure ObjectSound(x) 
  PlaySound(0)
EndProcedure 

Procedure MakeSprites() 

  h = #Height-100 

  CreateSprite(1,512,512,#PB_Sprite_PixelCollision)
  CreateSprite(2,512,512,#PB_Sprite_PixelCollision)
  CreateSprite(3,h,h,#PB_Sprite_PixelCollision)
  CreateSprite(4, 140, 20)
  
  ; Radar rotator (green > black) 
  StartDrawing(SpriteOutput(1)) 
    angle.f=90 
    Repeat 
      Line(255,255,GSin(angle)*255.0,GCos(angle)*255,RGB(0, Int(angle*2.8), 0)) 
      angle.f - 0.001 
    Until angle <=0 
  StopDrawing() 

  ; Radar objects 
  StartDrawing(SpriteOutput(2)) 
    For a = 127 To 0 Step -1 
      Circle(127,127,a,RGB(255-2*a,255-2*a,255-2*a)) 
    Next a 
  StopDrawing() 
  ZoomSprite(2,32,32) 

  ; Radar chassis 
  StartDrawing(SpriteOutput(3)) 
    Circle(h/2,h/2,h/2,RGB($40,$40,$40)) 
    Circle(h/2,h/2,h/2-10,RGB($00,$00,$00)) 
    DrawingMode(4) 
    For a = 0 To 5 
      Circle(h/2,h/2,h/10*a,RGB($40,$40,$40)) 
    Next a 
    For a = -2 To 2 
      Line(0,h/2+a,h,0,RGB($40,$40,$40)) 
    Next a 
    For a = -2 To 2 
      Line(h/2+a,0,0,h,RGB($40,$40,$40)) 
    Next a 
    For a = 30 To 330 Step 30 
      Line(h/2,h/2,GSin(a)*h/2,GCos(a)*h/2,RGB($40,$40,$40)) 
    Next a 
    For i = 0 To #Width Step 32
      LineXY(0, i, #Width, i, RGB(0, 80, 0))
    Next i
    For i = 0 To #Width Step 32
      LineXY(i, 0, i, #Width, RGB(0, 80, 0))
    Next i
  StopDrawing() 
  
EndProcedure 

Dim Objects._OBJ(#max_objects) 



OpenWindow(0,100,100,640,480,"Radar",#PB_Window_BorderLess)

InitKeyboard()
InitSprite()
InitSound()

OpenWindowedScreen(WindowID(0),0,0,640,480,0,0, #PB_Screen_SmartSynchronization)


Define pd3d.IDirect3DDevice9

EnableASM
!extrn _PB_Screen_Direct3DDevice
!MOV dword EAX, [_PB_Screen_Direct3DDevice]
!MOV dword [v_pd3d],EAX
DisableASM
pd3d\SetRenderState(22,1)
pd3d\SetRenderState(7,0)



LoadSound(0, "sonar2.wav")
LoadSound(2, "background.wav")
SoundVolume(0, 50)
SoundVolume(2, 60)
PlaySound(2, 1)
MakeSprites() 

;- Init objects 

xKoo =  18
yKoo =  22
Objects(1)\x      = xKoo
Objects(1)\y      = yKoo

If Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) > 360
 Objects(1)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) - 360
Else
 Objects(1)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1)
EndIf

xKoo = 0
yKoo = 0
Objects(2)\x      = xKoo
Objects(2)\y      = yKoo 
If Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) > 360
 Objects(2)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) - 360
Else
 Objects(2)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1)
EndIf

xKoo = 20
yKoo = -5
Objects(3)\x      = xKoo
Objects(3)\y      = yKoo 
If Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) > 360
 Objects(3)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) - 360
Else
 Objects(3)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1)
EndIf

;- Main loop 
Repeat
  ; ** buffer timer of loop start
  timer = ElapsedMilliseconds()
  time = timeGetTime_() 
  ExamineKeyboard() 
  If IsScreenActive() And ScreenDeactivated = #False 
    Angle.f + #rotator_speed 
    If Angle > 360:Angle-360
    EndIf    
    If CurrentAngle = 0 : ObjectSound(0) 
    EndIf
    ClearScreen(RGB(0,0,0)) 
    SpriteQuality(#PB_Sprite_BilinearFiltering) 
    ZoomSprite(1,#Height-100,#Height-100) 
      
    RotateSprite(1,Angle,#PB_Absolute)
      
    CurrentAngle + #rotator_speed
    If CurrentAngle >= 180 And Angle = 180: CurrentAngle = 0
    EndIf
    DisplaySprite(1,(#Width-#Height)/2+50,50) 
    DisplayTransparentSprite(3,(#Width-#Height)/2+50,50)

;      SpriteBlendingMode(5,2) 
      ; Objects 

    For a = 0 To #max_objects 
      
      StartDrawing(SpriteOutput(4))
            DrawingMode(4)
            Box(0, 0, 140, 20, RGB(0, 255, 0)) 
            DrawingMode(4)
            Box(1, 1, 138, 17, RGB(0, 0, 0))
            FrontColor(RGB(0, 255, 0))
            DrawText(5, 2,"OB:" + Str(a) + " - " + Str(Objects(a)\Degree) + "° - d: " + Str(Sqr(Pow(Objects(a)\x, 2) + Pow(Objects(a)\y, 2))))
      StopDrawing()
          
      If Objects(a)\x Or Objects(a)\y 
        If (CurrentAngle + 180) < Objects(a)\Degree + 180 And (CurrentAngle + 180) > Objects(a)\Degree +175 And Objects(a)\Transparency<200 
          Objects(a)\Transparency=255 
        EndIf 
      EndIf 
      If Objects(a)>0 
        Objects(a)\Transparency-#rotator_speed 
        If Objects(a)\Transparency<0 
          Objects(a)\Transparency=0 
        EndIf 
      EndIf 
      If Objects(a)\x Or Objects(a)\y 
          DisplayTransparentSprite(2, (#x_0 - 16) + Objects(a)\x, (#y_0 - 16) - Objects(a)\y, Objects(a)\Transparency) 
          DisplayTransparentSprite(4, (#x_0 - 16) + Objects(a)\x + 15, (#y_0 - 16) - Objects(a)\y - 5, 255)
      EndIf 
    Next a 


    g + 1
    If g = 50
      For a = 1 To 3
      
        Select(a)
          Case 1
            Objects(a)\x - 3 
            Objects(a)\y + 2
          Case 2
            Objects(a)\x + 1 
            Objects(a)\y + 0        
          Case 3
            Objects(a)\x + 2 
            Objects(a)\y - 1  
        EndSelect

        If Round(winkel(0, 0, 12, 12) - (winkel(0, 0, Objects(a)\x, Objects(a)\y) - winkel(0, 0, 12, 12)), 1) > 360
          Objects(a)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, Objects(a)\x, Objects(a)\y) - winkel(0, 0, 12, 12)), 1) - 360
        Else
          Objects(a)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, Objects(a)\x, Objects(a)\y) - winkel(0, 0, 12, 12)), 1)
        EndIf
      Next a
      g = 0
    EndIf

;  ElseIf ScreenDeactivated = #False 
;      ScreenDeactivated = #True 
;  ElseIf IsScreenActive() And ScreenDeactivated = #True 
;      ScreenDeactivated = #False 
;      FlipBuffers() 
;      MakeSprites() 
  EndIf 
  
  FlipBuffers() 
  Repeat:Delay(1):Until timeGetTime_()-20>=time 
  
Until KeyboardPushed(#PB_Key_Escape) 
;- End
Last edited by LiK137 on Tue Jan 10, 2017 11:05 am, edited 1 time in total.
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: RADAR from CodArch adapting from PB4 to PB5.2x

Post by Comtois »

Add this in main loop

Code: Select all

  While WindowEvent()
  Wend
Please correct my english
http://purebasic.developpez.com/
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: RADAR from CodArch adapting from PB4 to PB5.2x

Post by DK_PETER »

Code: Select all

;>------------------------------------
;
; by Danilo, 20.11.2003 - german forum
; extends by zomtex2003, 23.11.2003
;>------------------------------------

#Width  = 1600
#Height = 1200
;#Width  = 640
;#Height = 480
;#Width  = 1024
;#Height = 768

;#Width  = 800
;#Height = 600

PI.f = 3.141593

#x_0 = #Width / 2
#y_0 = #Height / 2

#rotator_speed = 3
#max_objects = 10
CurrentAngle = 180

Structure _OBJ
  x.l
  y.l
  Transparency.l
  Degree.f
EndStructure


Procedure.f winkel(x1.f,y1.f,x2.f,y2.f)
  a.f = x2-x1
  b.f = y2-y1
  c.f = Sqr(a*a+b*b)
  winkel.f = ACos(a/c)*57.29577
  If y1 < y2 : winkel=360-winkel : EndIf
  ProcedureReturn winkel
EndProcedure

Procedure.f GSin(winkel.f)
   ProcedureReturn Sin(winkel*(2*3.14159265/360))
EndProcedure

Procedure.f GCos(winkel.f)
   ProcedureReturn Cos(winkel*(2*3.14159265/360))
EndProcedure

Procedure ObjectSound(x)
  ;PlaySound(0)
EndProcedure

Procedure MakeSprites()

  h = #Height-100

  CreateSprite(1,512,512,#PB_Sprite_PixelCollision)
  CreateSprite(2,512,512,#PB_Sprite_PixelCollision)
  CreateSprite(3,h,h,#PB_Sprite_PixelCollision)
  CreateSprite(4, 140, 20)
 
  ; Radar rotator (green > black)
  StartDrawing(SpriteOutput(1))
    angle.f=90
    Repeat
      Line(255,255,GSin(angle)*255.0,GCos(angle)*255,RGB(0, Int(255-angle*2.8), 0))
      angle.f - 0.001
    Until angle <=0
  StopDrawing()

  ; Radar objects
  StartDrawing(SpriteOutput(2))
    For a = 127 To 0 Step -1
      Circle(127,127,a,RGB(255-2*a,255-2*a,255-2*a))
    Next a
  StopDrawing()
  ZoomSprite(2,32,32)

  ; Radar chassis
  StartDrawing(SpriteOutput(3))
    Circle(h/2,h/2,h/2,RGB($40,$40,$40))
    Circle(h/2,h/2,h/2-10,RGB($00,$00,$00))
    DrawingMode(4)
    For a = 0 To 5
      Circle(h/2,h/2,h/10*a,RGB($40,$40,$40))
    Next a
    For a = -2 To 2
      Line(0,h/2+a,h,0,RGB($40,$40,$40))
    Next a
    For a = -2 To 2
      Line(h/2+a,0,0,h,RGB($40,$40,$40))
    Next a
    For a = 30 To 330 Step 30
      Line(h/2,h/2,GSin(a)*h/2,GCos(a)*h/2,RGB($40,$40,$40))
    Next a
    For i = 0 To #Width Step 32
      LineXY(0, i, #Width, i, RGB(0, 80, 0))
    Next i
    For i = 0 To #Width Step 32
      LineXY(i, 0, i, #Width, RGB(0, 80, 0))
    Next i
  StopDrawing()
 
EndProcedure

Dim Objects._OBJ(#max_objects)



OpenWindow(0,100,100,#Width,#Height,"Radar",#PB_Window_BorderLess|#PB_Window_ScreenCentered)

InitKeyboard()
InitSprite()
InitSound()

OpenWindowedScreen(WindowID(0),0,0,#Width,#Height,0,0, #PB_Screen_SmartSynchronization)


; LoadSound(0, "sonar2.wav")
; LoadSound(2, "background.wav")
; SoundVolume(0, 50)
; SoundVolume(2, 60)
; PlaySound(2, 1)
MakeSprites()

;- Init objects

xKoo =  18
yKoo =  22
Objects(1)\x      = xKoo
Objects(1)\y      = yKoo

If Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) > 360
 Objects(1)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) - 360
Else
 Objects(1)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1)
EndIf

xKoo = 0
yKoo = 0
Objects(2)\x      = xKoo
Objects(2)\y      = yKoo
If Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) > 360
 Objects(2)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) - 360
Else
 Objects(2)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1)
EndIf

xKoo = 20
yKoo = -5
Objects(3)\x      = xKoo
Objects(3)\y      = yKoo
If Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) > 360
 Objects(3)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1) - 360
Else
 Objects(3)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, xKoo, yKoo) - winkel(0, 0, 12, 12)), 1)
EndIf

;- Main loop
Repeat
  
  Repeat:ev=WindowEvent():Until ev=0
  ; ** buffer timer of loop start
  timer = ElapsedMilliseconds()
  time = timeGetTime_()
  ExamineKeyboard()
  If IsScreenActive() And ScreenDeactivated = #False
    Angle.f + #rotator_speed
    If Angle > 360:Angle-360
    EndIf   
    If CurrentAngle = 0 : ObjectSound(0)
    EndIf
    ClearScreen(RGB(0,0,0))
    SpriteQuality(#PB_Sprite_BilinearFiltering)
    ZoomSprite(1,#Height-100,#Height-100)
     
    RotateSprite(1,Angle,#PB_Absolute)
     
    CurrentAngle + #rotator_speed
    If CurrentAngle >= 180 And Angle = 180: CurrentAngle = 0
    EndIf
    DisplaySprite(1,(#Width-#Height)/2+50,50)
    DisplayTransparentSprite(3,(#Width-#Height)/2+50,50)

;      SpriteBlendingMode(5,2)
      ; Objects

    For a = 0 To #max_objects
     
      StartDrawing(SpriteOutput(4))
            DrawingMode(4)
            Box(0, 0, 140, 20, RGB(0, 255, 0))
            DrawingMode(4)
            Box(1, 1, 138, 17, RGB(0, 0, 0))
            FrontColor(RGB(0, 255, 0))
            DrawText(5, 2,"OB:" + Str(a) + " - " + Str(Objects(a)\Degree) + "° - d: " + Str(Sqr(Pow(Objects(a)\x, 2) + Pow(Objects(a)\y, 2))))
      StopDrawing()
         
      If Objects(a)\x Or Objects(a)\y
        If (CurrentAngle + 180) < Objects(a)\Degree + 180 And (CurrentAngle + 180) > Objects(a)\Degree +175 And Objects(a)\Transparency<200
          Objects(a)\Transparency=255
        EndIf
      EndIf
      If Objects(a)>0
        Objects(a)\Transparency-#rotator_speed
        If Objects(a)\Transparency<0
          Objects(a)\Transparency=0
        EndIf
      EndIf
      If Objects(a)\x Or Objects(a)\y
          DisplayTransparentSprite(2, (#x_0 - 16) + Objects(a)\x, (#y_0 - 16) - Objects(a)\y, Objects(a)\Transparency)
          DisplayTransparentSprite(4, (#x_0 - 16) + Objects(a)\x + 15, (#y_0 - 16) - Objects(a)\y - 5, 255)
      EndIf
    Next a


    g + 1
    If g = 50
      For a = 1 To 3
     
        Select(a)
          Case 1
            Objects(a)\x - 3
            Objects(a)\y + 2
          Case 2
            Objects(a)\x + 1
            Objects(a)\y + 0       
          Case 3
            Objects(a)\x + 2
            Objects(a)\y - 1 
        EndSelect

        If Round(winkel(0, 0, 12, 12) - (winkel(0, 0, Objects(a)\x, Objects(a)\y) - winkel(0, 0, 12, 12)), 1) > 360
          Objects(a)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, Objects(a)\x, Objects(a)\y) - winkel(0, 0, 12, 12)), 1) - 360
        Else
          Objects(a)\Degree = Round(winkel(0, 0, 12, 12) - (winkel(0, 0, Objects(a)\x, Objects(a)\y) - winkel(0, 0, 12, 12)), 1)
        EndIf
      Next a
      g = 0
    EndIf

;  ElseIf ScreenDeactivated = #False
;      ScreenDeactivated = #True
;  ElseIf IsScreenActive() And ScreenDeactivated = #True
;      ScreenDeactivated = #False
;      FlipBuffers()
;      MakeSprites()
  EndIf
 
  FlipBuffers()
  Repeat:Delay(1):Until timeGetTime_()-20>=time
 
Until KeyboardPushed(#PB_Key_Escape)
;- End
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: RADAR from CodArch adapting from PB4 to PB5.2x

Post by LiK137 »

ThanQ very much Comtois
Perfect DK_PETER, Thanx
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: RADAR from CodArch adapting from PB4 to PB5.2x

Post by davido »

@DK_PETER

Thank you for updating. Looks great. :D
DE AA EB
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: RADAR from CodArch adapting from PB4 to PB5.2x

Post by Bananenfreak »

Looks cool :) I like Radar :D
Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: RADAR from CodArch adapting from PB4 to PB5.2x

Post by applePi »

add to the example the sound (sonar2.wav + background.wav) and it is incredible, like the Dr.(NO) submarine radar with its motors sound background
http://www.2shared.com/file/CRBXV3e7/sonar.html
(click on the smaller download icon at the bottom and not the big one)
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: RADAR from CodArch adapting from PB4 to PB5.2x

Post by LiK137 »

ThanQ, Thats main ingridiend
Post Reply