Simple Analog Clock in Hollywood

The place for any Hollywood tutorials
Post Reply
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Simple Analog Clock in Hollywood

Post by jalih »

Just a simple example. I hope you like it...

Source available here
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Simple Analog Clock in Hollywood

Post by jalih »

I just updated the download link above... I Fixed some errors in my framework and added more functionality...

Now all it takes to write nice analog clock is this:

Code: Select all

;
; Simple Analog Clock in Hollywood
;
@APPICON { Ic16x16 = "icons/my16x16icon.png",
	   Ic24x24 = "icons/my24x24icon.png",
	   Ic32x32 = "icons/my32x32icon.png",
	   Ic48x48 = "icons/my48x48icon.png",
	   Ic128x128 = "icons/my128x128icon.png",
	   Ic256x256 = "icons/my256x256icon.png",
	   Ic512x512 = "icons/my512x512icon.png"}
	   
@DISPLAY {Width = 200, Height = 200, Borderless = True, Title = "Hollywood Clock"}

; Include game framework
@INCLUDE "hgf/game.hws"


Const #SMALL_DOT_SIZE = 1
Const #LARGE_DOT_SIZE = 4


; Game setup
Function game.load()
	EscapeQuit(True)
	game:setUpdateRate(1)
	time = GetTime(True)
EndFunction
 
 
; Game state "main" draw function
Function game.draw()
	Cls(#WHITE)
	drawClock()
EndFunction


; game state "main" update function
Function game.update(dt)
	time = GetTime(True)
EndFunction



; Draw the analog clock
Function drawClock()
	; Get seconds, minutes and hours.
	Local hours = MidStr(time, 0, 2)
	Local minutes = MidStr(time, 3, 2)
	Local seconds = MidStr(time, 6, 2)

	; Get the angles of the clock hands.
	Local angsec = 90 - seconds * 6
	Local angmin = 90 - minutes * 6
	Local anghour = 90 - (hours * 5 + minutes / 10) * 6
	
	Local cp = point:new( { x = game.buffer.r:dx()/2, y = game.buffer.r:dy()/2 } ) ; Center point
	Local cr = cp.x - 8  ; Clock radius
	Local shr = cp.x - 16  ; Clock's second hand radius
	Local mhr = cp.x - 20  ; Clock's minute hand radius
	Local hhr = cp.x - 32  ; Clock's hour hand radius
	
	SetFillStyle(#FILLNONE)
	game.buffer:drawRect(game.buffer.r, #BLACK) ; Draw outline of the window rectangle
	
	SetFillStyle(#FILLCOLOR)
	
	; Draw minute and second dots.
	For Local i = 0 To 59
		game.buffer:drawCircle(point:new( { x = cp.x - #SMALL_DOT_SIZE, y = cp.y - #SMALL_DOT_SIZE } ):circlept(cr, i * (360 / 60) ), #SMALL_DOT_SIZE, #BLACK)
	Next
	
	; Draw hour dots.
	For Local i = 0 To 11
		game.buffer:drawCircle(point:new( { x = cp.x - #LARGE_DOT_SIZE, y = cp.y - #LARGE_DOT_SIZE } ):circlept(cr, i * (360 / 12) ), #LARGE_DOT_SIZE, #BLACK)
	Next
	
	; Draw clock's second, minute and hour hands.
	game.buffer:drawLine(cp, cp:circlept(shr, angsec), #BLACK, { Thickness = 2 } )
	game.buffer:drawLine(cp, cp:circlept(mhr, angmin), #BLACK, { Thickness = 6 } )
	game.buffer:drawLine(cp, cp:circlept(hhr, anghour), #BLACK, { Thickness = 8 } )
	
EndFunction


; Here we go!
game:go()
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Simple Analog Clock in Hollywood

Post by jalih »

Added a couple of more functions...

Now this works too:

Code: Select all

;
; Simple Analog Clock in Hollywood
;
@APPICON { Ic16x16 = "icons/my16x16icon.png",
	   Ic24x24 = "icons/my24x24icon.png",
	   Ic32x32 = "icons/my32x32icon.png",
	   Ic48x48 = "icons/my48x48icon.png",
	   Ic128x128 = "icons/my128x128icon.png",
	   Ic256x256 = "icons/my256x256icon.png",
	   Ic512x512 = "icons/my512x512icon.png"}
	   
@DISPLAY {Width = 200, Height = 200, Borderless = True, Title = "Hollywood Clock"}

; Include game framework
@INCLUDE "hgf/game.hws"


Const #SMALL_DOT_SIZE = 1
Const #LARGE_DOT_SIZE = 4


; Game setup
Function game.load()
	EscapeQuit(True)
	game:setUpdateRate(1)
	time = GetTime(True)
EndFunction
 
 
; Game state "main" draw function
Function game.draw()
	Cls(#WHITE)
	drawClock()
EndFunction


; game state "main" update function
Function game.update(dt)
	time = GetTime(True)
EndFunction



; Draw the analog clock
Function drawClock()
	; Get seconds, minutes and hours.
	Local hours = MidStr(time, 0, 2)
	Local minutes = MidStr(time, 3, 2)
	Local seconds = MidStr(time, 6, 2)

	; Get the angles of the clock hands.
	Local angsec = 90 - seconds * 6
	Local angmin = 90 - minutes * 6
	Local anghour = 90 - (hours * 5 + minutes / 10) * 6
	
	Local cp = point:new( { x = game.buffer.r:dx()/2, y = game.buffer.r:dy()/2 } ) ; Center point
	Local cr = cp.x - 8  ; Clock radius
	Local shr = cp.x - 16  ; Clock's second hand radius
	Local mhr = cp.x - 20  ; Clock's minute hand radius
	Local hhr = cp.x - 32  ; Clock's hour hand radius
	
	SetFillStyle(#FILLNONE)
	game.buffer:drawRect(game.buffer.r, #BLACK) ; Draw outline of the window rectangle
	
	SetFillStyle(#FILLCOLOR)
	
	; Draw minute and second dots.
	For Local i = 0 To 59
		game.buffer:drawCircle(cp:subxy(#SMALL_DOT_SIZE):circlept(cr, i * (360 / 60) ), #SMALL_DOT_SIZE, #BLACK)
	Next
	
	; Draw hour dots.
	For Local i = 0 To 11
		game.buffer:drawCircle(cp:subxy(#LARGE_DOT_SIZE):circlept(cr, i * (360 / 12) ), #LARGE_DOT_SIZE, #BLACK)
	Next
	
	; Draw clock's second, minute and hour hands.
	game.buffer:drawLine(cp, cp:circlept(shr, angsec), #BLACK, { Thickness = 2 } )
	game.buffer:drawLine(cp, cp:circlept(mhr, angmin), #BLACK, { Thickness = 6 } )
	game.buffer:drawLine(cp, cp:circlept(hhr, anghour), #BLACK, { Thickness = 8 } )
	
EndFunction


; Here we go!
game:go()
tolkien
Posts: 190
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Simple Analog Clock in Hollywood

Post by tolkien »

Hey! Thanks so much! It´s good to learn!
User avatar
Juan Carlos
Posts: 887
Joined: Mon Sep 06, 2010 1:02 pm

Re: Simple Analog Clock in Hollywood

Post by Juan Carlos »

Thank you jalih for your examples, with them this forum seens a good programming forum....
P.D.: I'll like put my own examples but I'm very anarchic programming.
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Simple Analog Clock in Hollywood

Post by jalih »

I just updated the download link...


Modifications:

- I rewrote my frameworks primitives library (primitives.hws). Now it serves as a good example on howto do Object Oriented Programming with Hollywood.
- Some small modifications and cleanups to clock.hws


Take a look and let me know, if I broke anything in the process...
Post Reply