Page 1 of 1

While learning the ropes One more

Posted: Tue Apr 03, 2007 9:50 pm
by gebe
Code updated for 5.20+

While learning .....

Quick Color values in Hex and Dec.

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)

;- Window Constants
;No need for Help ;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #TrRed
  #TxtRED
  #TrGreen
  #TxGreen
  #TrBlue
  #TxBlue
  #Tcolor
  #TvalDEC
  #TvalHEX
  #TLabelDEC
  #TLabelHex
EndEnumeration
;------------------------------------------------------------------------------------
Procedure Open_Window_0()
  If OpenWindow(#Window_0, 615, 2, 400, 300, "Pure Basic Colors Settings Values Hex and Dec",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
   
    TrackBarGadget(#TrRed  , 10,  70, 130, 20, 0, 255, #PB_TrackBar_Ticks)
    TrackBarGadget(#TrGreen, 10, 120, 130, 20, 0, 255, #PB_TrackBar_Ticks)
    TrackBarGadget(#TrBlue , 10, 170, 130, 20, 0, 255, #PB_TrackBar_Ticks)
   
    TextGadget(#TxtRED , 145,  70, 70, 20, "RED",   #PB_Text_Center | #PB_Text_Border)   
    TextGadget(#TxGreen, 145, 120, 70, 20, "GREEN", #PB_Text_Center | #PB_Text_Border)
    TextGadget(#TxBlue , 145, 170, 70, 20, "BLUE",  #PB_Text_Center | #PB_Text_Border)
   
    TextGadget(#Tcolor,  250, 65, 100, 130, "-",  #PB_Text_Center | #PB_Text_Border)
   
    TextGadget(#TLabelDEC, 110, 210, 110, 20, "Decimal Value", #PB_Text_Center | #PB_Text_Border)
    TextGadget(#TvalDEC, 250, 210, 100, 20, "-", #PB_Text_Center | #PB_Text_Border)
    TextGadget(#TLabelHex, 110, 240, 110, 20, "HexaDecimal Value", #PB_Text_Center | #PB_Text_Border)
    TextGadget(#TvalHEX, 250, 240, 100, 20, "-", #PB_Text_Center | #PB_Text_Border)
  EndIf
EndProcedure
;--------------------------------------------------------------------------------------
#GrMult=256
#BlMult=256*256
Open_Window_0()
Rouge.l=0
Vert.l=0
Bleu.l=0
Couleur.l=0
 SetGadgetColor(#TxtRED,#PB_Gadget_FrontColor,#White)
 SetGadgetColor(#Txgreen,#PB_Gadget_FrontColor,#White)
 SetGadgetColor(#Txblue,#PB_Gadget_FrontColor,#White)
Repeat ; Start of the event loop
 
  Event = WaitWindowEvent() ; This line waits until an event is received from Windows
 
  WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
 
  GadgetID = EventGadget() ; Is it a gadget event?
 
  EventType = EventType() ; The event type
 
  ;You can place code here, and use the result as parameters for the procedures
 
  If Event = #PB_Event_Gadget
   
    If GadgetID = #TrRed
      Rouge = GetGadgetState(#TrRed)
      SetGadgetText(#TxtRED,Str(rouge))
      couleur=rouge+vert*#grmult+bleu*#BlMult
      SetGadgetColor(#Tcolor,#PB_Gadget_BackColor,couleur)
      SetGadgetText(#tvaldec,Str(couleur))
      SetGadgetText(#tvalhex,Hex(couleur))
      SetGadgetColor(#TxtRED,#PB_Gadget_BackColor,ROUGE)

    ElseIf GadgetID = #TrGreen
     
      vert = GetGadgetState(#TrGreen)
      SetGadgetText(#TxGreen,Str(Vert))
      couleur=rouge+vert*#grmult+bleu*#BlMult
      SetGadgetColor(#Tcolor,#PB_Gadget_BackColor,couleur)
      SetGadgetText(#tvaldec,Str(couleur))
      SetGadgetText(#tvalhex,Hex(couleur))
      SetGadgetColor(#Txgreen,#PB_Gadget_BackColor,vert*#grmult)

    ElseIf GadgetID = #TrBlue
      bleu = GetGadgetState(#TrBlue)
      SetGadgetText(#Txblue,Str(bleu))
      couleur=rouge+vert*#grmult+bleu*#BlMult
      SetGadgetColor(#Tcolor,#PB_Gadget_BackColor,couleur)
      SetGadgetText(#tvaldec,Str(couleur))
      SetGadgetText(#tvalhex,Hex(couleur))
      SetGadgetColor(#Txblue,#PB_Gadget_BackColor,bleu*#blmult)

    EndIf   
  EndIf
Until Event = #PB_Event_CloseWindow ; End of the event loop
End
;It was easy even for a beginner

gebe :)



addition



Hi,
While learning to handle LCCwin32 ,I discovered the clock record of the microprocessor.
I am sure it is not new to some chaps but it might have been forgotten ...

gebe

Code: Select all

Procedure.q Mcycles()
  EnableASM
  RDTSC 
;my XP "64"bits Win32 machine does not like the following 2 codes
;I had to cancell those 2 lines in order for it to work


  MOV [ecx+4],edx  ;XP shy************
  MOV [ecx],eax      ;XP shy ***********
  ProcedureReturn ;edx,eax
  
  DisableASM
EndProcedure
a.f=0
OpenConsole()
For j=100 To 1001 Step 100  
  Print (Str(1000000*j)+" loops")
  Print (Chr(10))
  tt0=GetTickCount_()
  t0.q=Mcycles()
  For i.l=0 To 1000000*j
    a=Sqr(i);a+2*4/345 Do something ...
  Next i
  t1.q=Mcycles()
  ttt=GetTickCount_()-tt0
  tc.q=t1-t0
  Print ("Count Start : "+Str(t0))
  Print(Chr(10))
  Print ("Count Stop : "+Str(t1))
  Print(Chr(10))
  Print("Number of Clocks : "+ Str(tc))
  Print(Chr(10))
  Print("Time : "+Str(ttt)+" milliseconds")
  Print(Chr(10))
  Print("Freq =  ")
  Print( Str(tc/ttt/1000)) 
  Print(" Mhz")
  Print (Chr(10))
Next j

Print(Chr(10))
Print(Chr(10)) 
Print ("TEST Ended Press Any key To EXIT")

Repeat
  Delay(10)
Until Inkey()=" "
gebe :)

Posted: Tue Apr 03, 2007 11:42 pm
by rsts
Yeah - it's obvious you have no prior knowledge of programming :D

Nice one :)

cheers

Posted: Wed Apr 04, 2007 9:47 pm
by gebe
Thanks rsts :oops:
Starts at Motorola 6800 eval kit.(70's)
Does not make me any yonger...
I 've been looking to see if there was a lot about QueryPerformancecounter and freq
As it is skimpy I've (while learning Pure Basic)tried to show some of my tests with them
Here goes :(I hope there are not too many mistakes....Please correct me if.)

Code: Select all

; Use of QPFreq & QPCntr as a High resolution timer
;(intead of TimegetTime)
;time overhead(to get in an out)
;2 calls following each other
;1/QueryPerformanceCounter_(@Start.q);
;2/QueryPerformanceCounter_(@Stop.q)
;T.f= (Stop - Start) * 1000000/QPFreq.f;no need for double
;=minimum timing delay
;IncludeFile "FormForSimple.pb"
; PureBasic Visual Designer v3.95 build 1485 (PB4Code)


;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #TextFreq
  #TLabelForFreq
  #TLabelForCorrection
  #TextCorr
  #TLabelForNbLoops
  #TextLoopsToDo
  #TLabelForTotalTime
  #TextTIME
  #TLabelSpare1
  #TLabelSpare2
  #TextSpare1
  #TextSpare2
  #BStart
  #SCLoops
  #Spin_0
EndEnumeration


Procedure Open_Window_0()
  If OpenWindow(#Window_0, 304, 96, 400, 300, "Use of PerformanceCounter(No overhead correction)",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      TextGadget(#TextFreq, 140, 20, 90, 30, "-", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TLabelForFreq, 20, 20, 90, 30, "PCntr Freq.(Hz)", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TLabelForCorrection, 20, 60, 90, 30, "Correction", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TextCorr, 140, 60, 90, 30, "-", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TLabelForNbLoops, 20, 100, 90, 30, "Numb. of Loops", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TextLoopsToDo, 140, 100, 90, 30, "-", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TLabelForTotalTime, 20, 140, 90, 30, "Time (uSec.)", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TextTIME, 140, 140, 90, 30, "-", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TLabelSpare1, 20, 180, 90, 30, "Wk Display1", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TLabelSpare2, 20, 220, 90, 30, "Wk Display2", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TextSpare1, 140, 180, 90, 30, "-", #PB_Text_Center | #PB_Text_Border)
      TextGadget(#TextSpare2, 140, 220, 90, 30, "-", #PB_Text_Center | #PB_Text_Border)
      ButtonGadget(#BStart, 20, 260, 140, 30, "START")
      ScrollBarGadget  (#ScLoops,  250, 1, 25,  300, 1, 1000, 1,  #PB_ScrollBar_Vertical )
      SetGadgetState   (#ScLoops,  50)   ; set 1st scrollbar (ID = 0) to 50 of 100
      SpinGadget(#Spin_0, 183, 260, 50, 30, 1, 1000)
    EndIf
  EndIf
EndProcedure
Global CPC.f
Global Overhead.f
;-----------------------------------------
Procedure.f getOpTime(e.q,b.q)
  ProcedureReturn ((e-b) * cpc)-Overhead
EndProcedure
;-----------------------------------------
Procedure testit(nbl)
test1000=1000
TEST10000=10000
QueryPerformanceCounter_(@Started.q)


  For i=0 To nbl
  ;******************
;Any tests from here
    SetGadgetText(#TextSpare1,"QwErTy")
    SetGadgetText(#TextSpare2,"qWeRtY")
    
    If (i % test1000) = 0
      SetGadgetText(#TextSpare1,"")
      SetGadgetText(#TextSpare2,"")
    ElseIf i % test10000
      SetGadgetText(#TextSpare1,"10000")
      SetGadgetText(#TextSpare2,"10000")
    ;-----.......
    
    ;.......
    EndIf
;any test to here   
 ;******************   
  Next i
QueryPerformanceCounter_(@Stopped.q)
  
  
 SetGadgetText(#TextTIME,Str(Int( getOpTime(stopped,started))))
  
  
EndProcedure
;-----------------------------------------
;==========================================
;OpenConsole()


QueryPerformanceFrequency_(@Qpf.q);Get the frequency(it is a quad)
CPC=1000000/Qpf;Fraction of uSec,Short name as it could be used often
QueryPerformanceCounter_(@StartCnt.q)
QueryPerformanceCounter_(@StopCnt.q)
GoAndBack.q=StopCnt-StartCnt
Overhead=GoAndBack*CPC

Open_Window_0()
SetGadgetText(#TextFreq,StrQ(qpf))
SetGadgetText(#TextCorr,StrD(CPC))

Repeat ; Start of the event loop
  
  Event = WaitWindowEvent() ; This line waits until an event is received from Windows
  
  WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
  
  GadgetID = EventGadget() ; Is it a gadget event?
  
  EventType = EventType() ; The event type
  
  ;You can place code here, and use the result as parameters for the procedures
  
  If Event = #PB_Event_Gadget
    
    If GadgetID = #BStart
    testit(loops)
    ElseIf GadgetID = #SCLoops
      loops.l=GetGadgetState(#SCLoops) * GetGadgetState(#Spin_0)
      SetGadgetText(#TextLoopsToDo,Str(loops))
      ElseIf GadgetID = #Spin_0
       loops=GetGadgetState(#SCLoops) * GetGadgetState(#Spin_0)
       SetGadgetText(#TextLoopsToDo,Str(loops))
       SetGadgetText(#Spin_0,Str(GetGadgetState(#Spin_0)))
    EndIf
    
  EndIf
  
Until Event = #PB_Event_CloseWindow ; End of the event loop

End
;
Anybody intersted in speed tests yet ???

gebe

Posted: Thu Apr 05, 2007 9:28 am
by Derek
There are always speed tests being run on these forums, just do a search for sqr replacement to see a recent one.

Trouble is, there are so many different cpu's out there now and it always seems to be the case that intel's are better than amd's at one thing and amd's are better than intel's at another and then you chuck in multiple cores and the whole debate just breaks down.

Saying that I'm always interested in speed tests. :)

Posted: Thu Apr 05, 2007 6:47 pm
by gebe
Nice to hear you're interested in speed tests.

By the way Derek, I heard from a very reliable source that your worker is going to register a complaint with its union ....
for not letting him rest even on week ends
:twisted:
:D
gebe

Posted: Thu Apr 05, 2007 8:30 pm
by Derek
gebe wrote:By the way Derek, I heard from a very reliable source that your worker is going to register a complaint with its union ....
for not letting him rest even on week ends
:twisted:
:D
You lost me there. :?

Posted: Fri Apr 06, 2007 12:29 am
by gebe
Got you.
I mean the little dancer on your posts.

gebe :D

Posted: Fri Apr 06, 2007 9:20 am
by Derek
Oh, I see what you mean. :)

He's a robot called Bender from a cartoon called 'Futurama'. It's very funny, from the same people that do the Simpsons.

Posted: Fri Apr 06, 2007 6:23 pm
by gebe
He is cute ,I thought he had a familliar homeribartic look 8) :)
I m trying the LCC C++compiler to match it against PB.(correction C compiler it does not have classes..)
It is a challenge (for me)

Cherrio

gebe :)

adding some bits

Code: Select all

;Have I missed them or there are not many formatting routines  
;"floating" around (pun intended)
;Here is one 
Procedure.s FormatSN(fd.d,n)
;Format a float or double to n decimals string
  s.s=StrD(fd)
  i=1
  l=Len(s)
  While (Mid(s,i,1)<>".")And (i<l)
    i+1
  Wend;i=place of "."
  If  i+1+n <=l
    strOut.s=Left(s,i)+ Mid(s,i+1,n)
  Else ;   > L
    nb=i+1+n-L
    strOut.s=Left(s,i)+ Mid(s,i+1,nb)
  EndIf
  
ProcedureReturn strOut 

EndProcedure

;Call + error testing

d.d=12345.67898765432
MessageRequester(StrD(d),"with 2 decimals "+ formatsn(d,2))
MessageRequester(StrD(d),"with 40 decimals "+ formatsn(d,20))
MessageRequester(StrD(d),"with 0 decimals "+ formatsn(d,0))
MessageRequester(StrD(d),"with -2 decimals "+ formatsn(d,-2))

f.f=123.654
MessageRequester(StrF(f),"with 2 decimals "+ formatsn(f,2))
MessageRequester(StrF(f),"with 20 decimals "+ formatsn(f,20))
MessageRequester(StrF(f),"with 0 decimals "+ formatsn(f,0))
MessageRequester(StrF(f),"with -2 decimals "+ formatsn(f,-2))