Page 1 of 1

oscilloscope "look like"

Posted: Wed Dec 27, 2006 7:02 pm
by flaith
Code updated for 5.20+

Hi,

because i'm starting to understand sin and cos, i made this little program just for fun :wink:

Code: Select all

If InitSprite() = 0
  MessageRequester("Erreur", "Impossible d'ouvrir l'écran & l'environnement nécessaire aux sprites !", 0)
  End
EndIf

#width = 320
#height = 240
theta.f = 0.0 : x = 0 : y = 0 : frame.f = 0.0 : pas.f = 45.0 : div = 1 : DoCos = 1

If OpenWindow(0, 0, 0, #width+70, #height, "Oscilloscope...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(0, #width+10, #height-25, 55, 20, "Quitter")
  TextGadget(5, #width+3, 5, 32, 20, "Pas", #PB_Text_Center|#PB_Text_Border)
  TextGadget(6, #width+38, 5, 32, 20, "Haut", #PB_Text_Center|#PB_Text_Border)
  TrackBarGadget(1, #width+5, 27, 20, #height-80, 0, 100, #PB_TrackBar_Vertical)
  SetGadgetState(1, pas)
  
  CheckBoxGadget(7, #width+32, 27, 40, 15, "Cos") : SetGadgetState(7,DoCos)
  
  TrackBarGadget(2, #width+40, 42, 20, #height-80-15, 1, 10, #PB_TrackBar_Vertical)
  SetGadgetState(2, div)
  TextGadget(3, #width+5, #height-50, 30, 20, Str(pas), #PB_Text_Center|#PB_Text_Border)
  TextGadget(4, #width+40, #height-50, 30, 20, Str(div), #PB_Text_Center|#PB_Text_Border)
  
  If OpenWindowedScreen(WindowID(0), 0, 0, #width, #height, 0, 0, 0)
    CreateSprite(0, 4, 4)
    If StartDrawing(SpriteOutput(0))
        Box(0, 0, 4, 4, RGB(255, 0, 255))
        Box(1, 0, 2, 4, RGB(127, 200, 127))
        Box(0, 1, 4, 2, RGB(127, 200, 127))
        Box(1, 1, 2, 2, RGB(0, 255, 0))
      StopDrawing()
    EndIf
    TransparentSpriteColor(0,$FF00FF)
    
    CreateSprite(1, #width, #height)
    If StartDrawing(SpriteOutput(1))
        
        For i = 0 To #width - 1 Step #height/12 ;20
          LineXY(i,0,i,#height-1,$1F1F1F)
        Next i
        
        For i = 0 To #height - 1 Step #height/12 ;20
          LineXY(0,i,#width-1,i,$1F1F1F)
        Next i
        
        LineXY(0,#height/2,#width-1,#height/2,$AFAFAF)
        LineXY(#width/2,0,#width/2,#height,$AFAFAF)
        
        For i = #width/16 To #width - 1 Step #height/12 ;20
          LineXY(i,(#height/2)-5,i,(#height/2)+5,$AFAFAF)
        Next i
        
        For i = #width/16 To #height - 1 Step #height/12 ;20
          LineXY((#width/2)-5,i,(#width/2)+5,i,$AFAFAF)
        Next i
        
      StopDrawing()
    EndIf
    
  Else
    MessageRequester("Erreur", "Impossible d'ouvrir un écran dans la fenêtre!", 0)
    End
  EndIf
EndIf

#RAD_TO_DEG = 57.29577951308232087  ;57.2957795130823208767981548141052    = valeur de la conversion de radian en degré
#DEG_TO_RAD =  0.01745329251994329  ; 0.0174532925199432957692369076848861 = valeur de la conversion de degré en radian

Macro DegCos(val)
  Cos(val / #RAD_TO_DEG)
EndMacro

Macro DegSin(val)
  Sin(val / #RAD_TO_DEG)
EndMacro

Repeat
  Repeat
    Event = WindowEvent()
    
    ; Faire un Random pour le fun
    ;     div = Random(10)
    ;     SetGadgetState(2,div)
    ;     SetGadgetText(4,Str(div))
    
    Select Event 
      Case #PB_Event_Gadget
        If EventGadget() = 0
          End
        EndIf
        
        If EventGadget() = 1
          pas = GetGadgetState(1)
          SetGadgetText(3,Str(pas))
        EndIf
        
        If EventGadget() = 2
          div = GetGadgetState(2)
          SetGadgetText(4,Str(div))
        EndIf
        
        If EventGadget() = 7
          DoCos = 1 - DoCos
          SetGadgetState(7,DoCos)
        EndIf
        
      Case #PB_Event_CloseWindow
        End 
    EndSelect
  Until Event = 0
  
  FlipBuffers()
  ;ClearScreen(RGB(0, 0, 0))
  
  DisplaySprite(1,0,0)
  
  For x = 0 To #width - 1
    theta = x + frame
    Select DoCos
      Case 0
        y = ((#height-4) / 2) - DegSin(theta) * (#height / 2) ;-4 à cause de la hauteur du sprite
      Case 1
        y = ((#height-4) / 2) - DegSin(theta) * (#height / 2) * ( DegCos(theta) / div )
    EndSelect
    
    DisplayTransparentSprite(0,x,y)
  Next
  
  frame = frame + pas               ;vitesse de défilement
  Delay(25)                         ;valeur pour donner du temps au processeur sinon process à 98%
ForEver

Posted: Wed Dec 27, 2006 9:00 pm
by rsts
Very nice and thanks for sharing.

Now to find a use for it in my clipboard prog :?

cheers

Posted: Wed Dec 27, 2006 11:02 pm
by flaith
:D

Re: oscilloscope "look like"

Posted: Thu Dec 28, 2006 1:42 am
by NoahPhense
nice ..

- np

Posted: Thu Dec 28, 2006 11:17 am
by Derek
Nice, had to change the div variable, clashes with inline asm.

Posted: Thu Dec 28, 2006 1:55 pm
by flaith
Derek wrote:Nice,
:D thanks
Derek wrote:had to change the div variable, clashes with inline asm.
could you show me your code ?

Posted: Thu Dec 28, 2006 5:47 pm
by Derek
Your line

Code: Select all

theta.f = 0.0 : x = 0 : y = 0 : frame.f = 0.0 : pas.f = 45.0 : div = 1 : DoCos = 1
My line

Code: Select all

theta.f = 0.0 : x = 0 : y = 0 : frame.f = 0.0 : pas.f = 45.0 : divi = 1 : DoCos = 1
etc.

Just used find/replace to change 'div' in your code to 'divi'.

If you have inline asm turned on in the editor then div is not allowed as a variable name because it is an asm command. No big problem, just had to rename the variable, or turn off inline asm. :D

Posted: Thu Dec 28, 2006 6:17 pm
by flaith
i hadn't understood about the inline asm, sorry
now it's clearer thanks :D

Posted: Thu Dec 28, 2006 6:24 pm
by Derek
No problem, caught me out a long time ago, until someone pointed me in the right direction. :D