Basic Analog Clock [UPDATED]

Share your advanced PureBasic knowledge/code with the community.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Basic Analog Clock [UPDATED]

Post by electrochrisso »

I have put together a very simple analog clock for newbies or old hands at programming to muck around with and perhaps modify and come up with their own version.
If you really want to get into clocks look no further than the master clock coder, netmaestros 'Transparent Clock', which should be easily found by searching on the board under that name.
Makes me drawl at the mouth every time I see it. :lol:

Updated: 24 December 2007
Updated: 25 December 2007
Updated: 27 December 2007

Code: Select all

;
; My Clock By Electrochrisso, 2007, PB4
;

#MiddleX      = 120
#MiddleY      = 120
#Radius       = 110

If CreateImage(0,#MiddleX*2,#MiddleY*2)
  StartDrawing(ImageOutput(0))
    Box(0,0,#MiddleX*2,#MiddleY*2,$999999)
    Circle(#MiddleX,#MiddleY,#MiddleX-2,$D8BC27)
    Circle(120,5,  3,$CCCCCC)
    Circle(178,20, 3,$CCCCCC)
    Circle(219,62, 3,$CCCCCC)
    Circle(235,120,3,$CCCCCC)
    Circle(219,178,3,$CCCCCC)
    Circle(178,219,3,$CCCCCC)
    Circle(120,235,3,$CCCCCC)
    Circle(62, 219,3,$CCCCCC)
    Circle(21, 178,3,$CCCCCC)
    Circle(5,  120,3,$CCCCCC)
    Circle(62, 20, 3,$CCCCCC)
    Circle(21, 62, 3,$CCCCCC)
  StopDrawing()

  If OpenFile(0,"MyClock.ini")
    Win_0X = Val(ReadString(0))
    Win_0Y = Val(ReadString(0))
    Sticky = Val(ReadString(0))
    Win_1X = Val(ReadString(0))
    Win_1Y = Val(ReadString(0))
    AlarmState = Val(ReadString(0))
    AlarmHour = Val(ReadString(0))
    AlarmMin = Val(ReadString(0))
    CloseFile(0)
  EndIf

  If OpenWindow(0,Win_0X,Win_0Y,#MiddleX*2,#MiddleY*2,"My Clock", #PB_Window_SystemMenu) And CreateGadgetList(WindowID(0)) And CreatePopupMenu(0)
    ImageGadget(0,0,0,#MiddleX*2,#MiddleY*2,ImageID(0))
    GadgetToolTip(0,"Right click for options menu")
    MenuItem(1, "Window On Top")
    MenuItem(2, "View Calendar")
    MenuItem(3, "Alarm Settings")
    MenuBar()
    MenuItem(4, "Quit")
    StickyWindow(0,Sticky) : SetMenuItemState(0,1,Sticky)

    Repeat
      Event = WaitWindowEvent(1)
      Wvent = EventWindow()
      If Wvent = 1 And Event = #PB_Event_CloseWindow
        Win_1X = WindowX(1) : Win_1Y = WindowY(1) : CloseWindow(1)
      EndIf
      
      If AlarmState = 1
        If AlarmHour = RealHour And AlarmMin = Minute(Date()) And Second(Date()) < 10
          Delay(100) : Beep_(1000, 100)
        EndIf
      EndIf

      Select Event
        Case #WM_RBUTTONDOWN
          DisplayPopupMenu(0, WindowID(0))
        Case #PB_Event_Menu
          Select EventMenu()
            Case 1
              If GetMenuItemState(0,1) = 0
                StickyWindow(0,1) : SetMenuItemState(0,1,1) : Sticky = 1
              Else
                StickyWindow(0,0) : SetMenuItemState(0,1,0) : Sticky = 0
              EndIf
            Case 2
              If OpenWindow(1, Win_1X, Win_1Y, 220, 200, "Calendar", #PB_Window_SystemMenu)
                If CreateGadgetList(WindowID(1))
                  CalendarGadget(1, 10, 10, 200, 180)
                  StickyWindow(1,1)
                EndIf
              EndIf
            Case 3
              If OpenWindow(2, 0, 0, 220, 90, "Alarm Settings", #PB_Window_SystemMenu | #PB_Window_WindowCentered)
                If CreateGadgetList(WindowID(2))
                  CheckBoxGadget(2, 10, 10, 150, 20, "Activate Alarm")
                  SetGadgetState(2, AlarmState)
                  TextGadget(3, 10, 52, 25, 20, "Hour")
                  SpinGadget(4, 40, 50, 30, 20, 0, 23, #PB_Spin_Numeric) : SetGadgetState(4, 0)
                  SetGadgetState(4, AlarmHour)
                  TextGadget(5, 90, 52, 20, 20, "Min")
                  SpinGadget(6, 115, 50, 30, 20, 0, 59, #PB_Spin_Numeric) : SetGadgetState(6, 0)
                  SetGadgetState(6, AlarmMin)
                  StickyWindow(2,1)
                EndIf
              EndIf
              Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
              AlarmState = GetGadgetState(2) : AlarmHour = GetGadgetState(4) : AlarmMin = GetGadgetState(6)
              CloseWindow(2) : Event = -1
            Case 4 : Break
          EndSelect
      EndSelect

      StartDrawing(ImageOutput(0))
        Circle(#MiddleX,#MiddleY,#MiddleX-8,$9CE060)
        Box(83,48,74,18,0)
        DrawText(85,50,FormatDate("%dd-%mm-%yyyy", Date()),0,$0BD5F4)
        LineXY(#MiddleX,#MiddleY,#MiddleX+Sin(grad_h*(2*3.14159265/360))*98,#MiddleY+Cos(grad_h*(2*3.14159265/360))*98, 0)
        LineXY(#MiddleX,#MiddleY,#MiddleX+Sin(grad_m*(2*3.14159265/360))*107,#MiddleY+Cos(grad_m*(2*3.14159265/360))*107, $0000FF)
        LineXY(#MiddleX,#MiddleY,#MiddleX+Sin(grad_s*(2*3.14159265/360))*110,#MiddleY+Cos(grad_s*(2*3.14159265/360))*110, $FFFFFF)
        Circle(#MiddleX,#MiddleY,4,$FF0000)
      StopDrawing()

      SetGadgetState(0,ImageID(0))
      grad_s = 360-(Second(Date())*6)+180
      grad_m = 360-(Minute(Date())*6)+180
      If GetTimeZoneInformation_(tzi.TIME_ZONE_INFORMATION) <> 1
        RealHour = Hour(Date())-1
        grad_h = 360-((RealHour*30)+180)-(Minute(Date())/2)
      Else
        RealHour = Hour(Date())
        grad_h = 360-((RealHour*30)+180)-(Minute(Date())/2)
      EndIf

    Until Wvent = 0 And IsWindow(1) = 0 And Event = #PB_Event_CloseWindow

    CreateFile(0,"MyClock.ini")
      WriteStringN(0,Str(WindowX(0)))
      WriteStringN(0,Str(WindowY(0)))
      WriteStringN(0,Str(Sticky))
      WriteStringN(0,Str(Win_1X))
      WriteStringN(0,Str(Win_1Y))
      WriteStringN(0,Str(AlarmState))
      WriteStringN(0,Str(AlarmHour))
      WriteStringN(0,Str(AlarmMin))
    CloseFile(0)

  EndIf
EndIf
End
Enjoy :)
Last edited by electrochrisso on Thu Dec 27, 2007 10:36 am, edited 6 times in total.
PureBasic! Purely the best 8)
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post by blueb »

Interesting... I like it! :)

I noticed that your programs time is out by one hour.
e.g: My time... 08:37
your clock displays... 07:37

looking at your program..
grad_h = 360-(((Hour(Date())-1)*30)+180)-(Minute(Date())/2)

I wonder why the -1 in the hour variable?

--blueb
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Post by electrochrisso »

Not sure, might be something to do with daylight saving here in Australia.
Thats what I have to put in to get correct time.

If I do these..
Debug FormatDate("%hh:%ii:%ss", Date())
Debug Hour(Date())
I get the correct time though.

And I have used this on several different computers, so its not just my computers.
A bit of a mystery to me. :roll:
PureBasic! Purely the best 8)
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Hi.

Nice.

I get the same 1 hour difference here. Maybe just chuck the -1 away as it should just go with system clock?
Dare2 cut down to size
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

There was a thread about a mysterious 1 hour difference in the time function. As I recall, not even Fred was sure what caused it.

cheers
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Maybe this addresses it:
http://www.purebasic.fr/english/viewtop ... ght+saving

Then again, maybe it doesn't. But may be worth a look.
Dare2 cut down to size
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Post by electrochrisso »

Thanks for the link I will see what I can work out.
I first noticed this problem when I tried Transparent Clock a while back and had to make a small mod to get it to display correctly.
Australia has silly time zoning 1/2 to 2 hours between states, all over the place. :roll:
Just another politicians hair brained scheme. :?
PureBasic! Purely the best 8)
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Yep!

I am a cane toad (or banana bender, if you prefer). You know, the state that gave you Anna Bligh, Peter Beatie, Joh Bjelke Petersen and now - tah-dah - Kevin O7. (Be afraid. Be very afraid. MuahaHAHAHAHAha).

We in the Sunshine State (and more recently AKA Smart State) refuse to participate in daylight saving because:
  • * It would confuse the cows, they wouldn't know what time to get milked as cows aren't very smart.
    * It will fade the curtains. The Queensland sun is very harsh, and an extra hour could really do some damage.
    * It would be uncomfortable having dinner at 8 pm and it still being daylight
Add to that it would confuse the computers.

But mainly we haven't because the state government wont sent out a pamphlet yet telling us how to set our clocks and watches back one hour. I suspect the reason for this is that:
  • (a) we don't have anyone in government who knows how to adjust a timepiece and so we can't write the bloody pamphlet.
    (b) By the time we got it right we would only have to set them forward again so why bother?
    (c) The government would need to get a scientist in to write another pamphlet explaining how to set them forward, thus wasting taxpayer's money.
Also, about one quarter of us are sure to turn the winder thingo in the wrong direction and about half would just give the pollies the finger so this would lead to three concurrent zones in in our glorious state.

Plus: Given our efforts with the 4-minute water timer (our BIG "save water" scheme) the government is also a little worried about sending out anything that makes us think.

My sand-timer by the way (along with about 1% of them) was blocked in the middle waisty bit and so I can shower for as long as I want. My sister-in-law's timer had no waist. She didn't have time to turn the shower on and off before her time allocation was over. (We are avoiding her for now, until the drought is over, but she has promised to go to the beach and take a dip so maybe this will change).

And - hehe - we cane toads now rule Australia!

heheHEHEHEHEHEHAAAAhaMuaWAAHAHAHAHA!

(Remember, we are the state that gave official sanction and support for the water-driven car).
Dare2 cut down to size
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Post by electrochrisso »

:) :D :lol:
You been eating many pumpkin scones lately. :D
I lived in Toowoomba for 6 years when I was in the army 20 years ago.
It used to be green all year round for the whole time I was there.
I suppose I would have a big shock if I went there now. :shock:
Is the hierarchy just as corrupt up there these days. :?:
Things are getting a lot worse here by the day, hardly any rain anymore.
PureBasic! Purely the best 8)
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

electrochrisso wrote:You been eating many pumpkin scones lately. :D
:lol:
electrochrisso wrote:I lived in Toowoomba for 6 years when I was in the army 20 years ago.
Another ex-soldier!
electrochrisso wrote:It used to be green all year round for the whole time I was there.
We have had some rain recently in the SE, so things are greening up. Dams have got about another 10-12 days supply.
electrochrisso wrote:Is the hierarchy just as corrupt up there these days. :?:
Gone are the days of leaving a brown paper bag on the Premier's desk. We have got sophisticated now!

Actually, nowadays you give a pollie a "loan" and they pay it off if and when they can.
Or you buy a house and sell it to them for $1.
Or you make a donation to their children's trust fund.
Unless you're a millionaire coalminer you're not likely to get caught.

We still wheel trolley-loads of documents through parliament/cabinet.
If you do that they become official secrets (they don't have to be discussed, you just wheel them in and out).
They are then exempt from FOI (Freedom Of Information act).
electrochrisso wrote:Things are getting a lot worse here by the day, hardly any rain anymore.
:(

It's a bugger.
Dare2 cut down to size
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Post by electrochrisso »

Sounds like they are not as blatant these days, but manage to pull the wool over the eyes of the plebs. :lol:
Good to hear you are getting some rain. :)
Lots of talk about building a desalination plant down here soon. (I wonder if they are talking Microsoft minutes when they say soon) :lol:
Anyway back to the programming to set my mind at ease. :wink:
PureBasic! Purely the best 8)
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Post by electrochrisso »

Got a bit off the subject for a few posts.
Looking at the info from the link Dare gave, led me to an API call: GetTimeZoneInformation_(tzi.TIME_ZONE_INFORMATION).
Hopefully it will be as simple as this updated version.

Code: Select all

;
; My Clock By Electrochrisso, 2007, PB4
;

#MiddleX      = 120
#MiddleY      = 120
#Radius       = 110

CreateImage(0,#MiddleX*2,#MiddleY*2)
StartDrawing(ImageOutput(0))
  Box(0,0,#MiddleX*2,#MiddleY*2,$999999)
  Circle(#MiddleX,#MiddleY,#MiddleX-2,$D8BC27)
  Circle(120,5,  3,$CCCCCC)
  Circle(178,20, 3,$CCCCCC)
  Circle(219,62, 3,$CCCCCC)
  Circle(235,120,3,$CCCCCC)
  Circle(219,178,3,$CCCCCC)
  Circle(178,219,3,$CCCCCC)
  Circle(120,235,3,$CCCCCC)
  Circle(62, 219,3,$CCCCCC)
  Circle(21, 178,3,$CCCCCC)
  Circle(5,  120,3,$CCCCCC)
  Circle(62, 20, 3,$CCCCCC)
  Circle(21, 62, 3,$CCCCCC)
StopDrawing()
OpenWindow(0,0,0,#MiddleX*2,#MiddleY*2,"My Clock")
CreateGadgetList(WindowID(0))
ImageGadget(0,0,0,#MiddleX*2,#MiddleY*2,ImageID(0))

Repeat
  Event = WindowEvent()
  StartDrawing(ImageOutput(0))
    Circle(#MiddleX,#MiddleY,#MiddleX-8,$9CE060)
    Box(83,48,74,18,0)
    DrawText(85,50,FormatDate("%dd-%mm-%yyyy", Date()),0,$0BD5F4)
    LineXY(#MiddleX,#MiddleY,#MiddleX+Sin(grad_h*(2*3.14159265/360))*98,#MiddleY+Cos(grad_h*(2*3.14159265/360))*98, 0)
    LineXY(#MiddleX,#MiddleY,#MiddleX+Sin(grad_m*(2*3.14159265/360))*107,#MiddleY+Cos(grad_m*(2*3.14159265/360))*107, $0000FF)
    LineXY(#MiddleX,#MiddleY,#MiddleX+Sin(grad_s*(2*3.14159265/360))*110,#MiddleY+Cos(grad_s*(2*3.14159265/360))*110, $FFFFFF)
    Circle(#MiddleX,#MiddleY,4,$FF0000)
  StopDrawing()
  SetGadgetState(0,ImageID(0))
  grad_s = 360-(Second(Date())*6)+180
  grad_m = 360-(Minute(Date())*6)+180
  If GetTimeZoneInformation_(tzi.TIME_ZONE_INFORMATION) <> 1 ;Check for daylight saving 1=No, 2=Yes and -1=Unknown
    grad_h = 360-(((Hour(Date())-1)*30)+180)-(Minute(Date())/2)
  Else
    grad_h = 360-((Hour(Date())*30)+180)-(Minute(Date())/2)
  EndIf
  Delay(1)
Until Event = #PB_Event_CloseWindow
End
PureBasic! Purely the best 8)
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post by blueb »

Yep... works here. Solves the glitch.

Thanks,
--blueb
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Post by electrochrisso »

Thanks blueb :D

My next task is to add alarms and messages for appointments etc.
PureBasic! Purely the best 8)
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Post by electrochrisso »

Updated it a little bit. See first post.
PureBasic! Purely the best 8)
Post Reply