SimpleClock: My first try with MacOS

Mac OSX specific forum
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

SimpleClock: My first try with MacOS

Post by netmaestro »

Hello,

I made a try at a MacOS program. I'm not very familiar with the OSX environment yet so please be patient with my noobish approach to everything. (in particular I didn't spend a lot of time on the rotation angles so if the thing suddenly goes berserk or takes on a mind of its own don't be too shocked :mrgreen: )

You will need this image: http://lloydsplace.com/clockface-blue.png

And then make it available to this code:

Code: Select all

;///////////////////////////////////////////////////////////////
; Program:            SimpleClock
; Author:             Lloyd Gallant (netmaestro)
; Date:               February 28, 2016
; Target OS:          Mac OSX (not sure the version req'd)
; Compiler Used:      PureBasic 5.42 LTS Beta 4
; License:            Unlimited, do as you like with it
;///////////////////////////////////////////////////////////////
;
;===================================
;           DECLARATIONS
;===================================
Declare RightClickHandler()
Declare MenuHandler()
Declare InitializeEvents()
Declare InitializeWindow()
Declare UpdateClock()

;===================================
;            CONSTANTS
;===================================
Enumeration
  #Output
  #Face
  #Hour
  #Minute
  #Second
EndEnumeration

;===================================
;             GLOBALS
;===================================
Global UserRequest_Quit=0

;===================================
;            RESOURCES
;===================================
DataSection
  clockface:
  IncludeBinary "clockface-blue.png"
EndDataSection

UsePNGImageDecoder()
CatchImage(#Face, ?clockface)

CreateImage(#Hour, 128, 128, 32, #PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(#Hour))
  MovePathCursor(63,73)
  VectorSourceColor(RGBA(230, 230, 230, 255))
  AddPathLine(63, 42)
  StrokePath(5, #PB_Path_RoundEnd)
  FillPath()
StopVectorDrawing()

CreateImage(#Minute, 128, 128, 32, #PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(#Minute))
  MovePathCursor(63,73)
  VectorSourceColor(RGBA(180, 180, 180, 255))
  AddPathLine(63, 25)
  StrokePath(5, #PB_Path_RoundEnd)
  FillPath()
StopVectorDrawing()

CreateImage(#Second, 128, 128, 32, #PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(#Second))
  MovePathCursor(63,73)
  VectorSourceColor(RGBA(255, 0, 0, 255))
  AddPathLine(63, 20)
  StrokePath(1.5, #PB_Path_RoundEnd)
  FillPath()
StopVectorDrawing()

;===================================
;           MAIN LOOP
;===================================
InitializeWindow()
Repeat : EventID = WaitWindowEvent() : Until UserRequest_Quit

;===================================
;        CALLED PROCEDURES
;===================================
Procedure UpdateClock()
  Static LastSecond.i, ms.i, Delta.i
  Protected Hour.i, Minute.i, Second.i, Angle_Hour.d, Angle_Minute.d, Angle_Second.d
  Hour = Hour(Date()):If Hour>12:Hour-12:EndIf
  Minute = Minute(Date())
  Second = Second(Date())
  If Second<>LastSecond
    LastSecond=Second
    ms=ElapsedMilliseconds()
    Delta=0
  Else
    delta=ElapsedMilliseconds()-ms
  EndIf
  Angle_Minute = (Minute / 60) * 360
  Angle_Hour = (Hour / 12) * 360 + (Angle_Minute/360)*(360/12)
  Angle_Second = ((Second+(Delta/1000)) / 60) * 360
  CopyImage(#Face, #Output)
  StartVectorDrawing(ImageVectorOutput(#Output))
    ResetCoordinates()
    RotateCoordinates(63,63,Angle_Hour)
    MovePathCursor(0,0)
    DrawVectorImage(ImageID(#Hour))    
    ResetCoordinates()
    RotateCoordinates(63,63,Angle_Minute)
    MovePathCursor(0,0)
    DrawVectorImage(ImageID(#Minute))       
    ResetCoordinates()
    RotateCoordinates(63,63,Angle_Second)
    MovePathCursor(0,0)
    DrawVectorImage(ImageID(#Second))       
  StopVectorDrawing()
  CocoaMessage(0, WindowID(0), "setBackgroundColor:", CocoaMessage(0, 0, "NSColor colorWithPatternImage:", ImageID(#Output)))
EndProcedure

Procedure InitializeEvents()
  BindEvent(#PB_Event_Timer, @UpdateClock())
  BindEvent(#PB_Event_RightClick, @RightClickHandler())
  BindEvent(#PB_Event_Menu, @MenuHandler())
EndProcedure

Procedure InitializeWindow()
  OpenWindow(0, 0, 0, 128, 128, "", #PB_Window_BorderLess|#PB_Window_ScreenCentered|#PB_Window_Invisible)
  CocoaMessage(0, WindowID(0), "setOpaque:", #NO)
  CocoaMessage(0, WindowID(0), "setMovableByWindowBackground:", #YES)
  CocoaMessage(0, WindowID(0), "setHasShadow:", #YES)
  UpdateClock()
  CreatePopupMenu(0)
  MenuItem(9, "Close the clock")
  MenuItem(10, "Cancel")
  InitializeEvents()
  HideWindow(0, 0)
  AddWindowTimer(0, 1, 50)
EndProcedure

Procedure RightClickHandler()
  DisplayPopupMenu(0, WindowID(0))
EndProcedure

Procedure MenuHandler()
  Select EventMenu()
    Case 9
      UserRequest_Quit = #True
  EndSelect
EndProcedure
As always, critique and suggestions are welcome, I hope you have some fun with it!
BERESHEIT
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: SimpleClock: My first try with MacOS

Post by wilbert »

Beautiful :)
Very good for a first try !
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: SimpleClock: My first try with MacOS

Post by netmaestro »

Thanks wilbert! Making this I was struck by how much can be accomplished with a small amount of code (and effort) with the modern version of PureBasic in the OSX environment. I made something similar to this a few years back on the Windows platform and it required far more API programming to achieve the same result. With the arrival of BindEvent() threads can be dispensed with, the vector drawing lib makes calling OS libraries unnecessary for things like antialias and rotations, and using custom-shaped images for a window canvas is just a couple of calls to Cocoa. Gdiplus on Windows is such a pain with all the prototypes/imports required and it's so nice to just leave that all behind and use native PureBasic for the heavy lifting. Fred and Freak are real-life heroes for the work they've done on PB and the selfless way they've made it available so inexpensively. The gift that keeps on giving!
BERESHEIT
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: SimpleClock: My first try with MacOS

Post by Keya »

congrats on taking the plunge with OSX! its both liberating and empowering not to be stuck in one OS and being able to offer your software to more people huh!? i wouldnt have taken that step without Purebasic either :) special thanks also to wilbert for all his work behind the scenes that allowed Fred to implement CocoaMessage!!
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: SimpleClock: My first try with MacOS

Post by WilliamL »

Very smooth! ..and and example of using vector graphics. :D
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SimpleClock: My first try with MacOS

Post by Fred »

Now, I just booted my mac just to see the newest netmeastro creation ! :twisted:

Nice, as always
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: SimpleClock: My first try with MacOS

Post by davido »

Excellent, as usual.
Thank you for sharing.
DE AA EB
normeus
Enthusiast
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: SimpleClock: My first try with MacOS

Post by normeus »

Currently using xcode for Apple Dev. Thanks to netmaestro's example; I will give PureBasic OSX a try!

Thank you.
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2058
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: SimpleClock: My first try with MacOS

Post by Andre »

Very nice example! :D
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: SimpleClock: My first try with MacOS

Post by Keya »

normeus wrote:Currently using xcode for Apple Dev. Thanks to netmaestro's example; I will give PureBasic OSX a try!
very surprised to see this! I wouldve assumed everyone here that has OSX would already be using Purebasic OSX, but you haven't even tried it yet!? :)

I'm new to OSX and Linux myself, I only tried them because Purebasic itself supports them, but Purebasic for OSX and Linux is very much the same so the learning curve is very small! actually I found most of the learning curve to be in the OS itself, not PB, so if you know the OS well already what are you waiting for!? It's great how most code runs with very few modifications on OSX and Linux, rarely having to use CompilerIf #PB_Compiler_OS statements but able to get the job done perfectly with them when required. Happy days! :)
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: SimpleClock: My first try with MacOS

Post by TI-994A »

Never thought that I'd be impressed by a clock demo. Simply lovely! :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: SimpleClock: My first try with MacOS

Post by fsw »

Keya wrote:
normeus wrote:Currently using xcode for Apple Dev. Thanks to netmaestro's example; I will give PureBasic OSX a try!
very surprised to see this! I wouldve assumed everyone here that has OSX would already be using Purebasic OSX, but you haven't even tried it yet!? :)
... so if you know the OS well already what are you waiting for!?
One example: waiting for drag&drop functionality to work well on OS X...

I am to provide the public with beneficial shocks.
Alfred Hitshock
Post Reply