' A Direct X Clock 
' GWS - April 2016

def run,region:int
def key,pos,pic,pic1,pic2:int
def wW,wH,sW,sH:int
def sprm,sprh:int
def sprm1,sprm2,sprh1,sprh2:int
def mins,secs,hrs:int

def dmins,dhrs,hrad,mrad:float
def rad60,rad12,pi:float
def sprmx,sprmy,sprhx,sprhy:float

def b$:string

def w:window

autodefine "OFF"

setid "NOSIZE",1
setid "NOMOVE",2
setid "TOPMOST",-1
setid "WM_NCLBUTTONDOWN",0xA1
setid "HTCAPTION",2

declare "user32",SetWindowPos(hwnd:int, Z_order:int, x:int, y:int, width:int, height:int, wFlags:uint),int
declare "gdi32", CreateEllipticRgn(rleft:int, rtop:int, rright:int, rbottom:int),int 
declare "user32",SetWindowRgn(hWnd:window, hRgn:int, bRedraw:int),int


wW = 100 : wH = 100
openwindow w,-wW,0,wW,wH,@nocaption|@noautodraw ,0,"Clock",&messages					:' @nocaption
setwindowcolor w, rgb(0,100,200)

pos = SetWindowPos(w, @TOPMOST, 0, 0, 0, 0, @NOSIZE|@NOMOVE)   					:' always on top

if CREATESCREEN(w,wW,wH) < 0																			:' create a DirectX screen ...
	MESSAGEBOX w, "Could not create DirectX screen","Error"
	run = 0
	end
endif

pic1 = loadimage(getstartpath + "clock1.jpg",@IMGSCALABLE)				:' normal clock
pic2 = loadimage(getstartpath + "clock2.jpg",@IMGSCALABLE)				:' green clock
pic = pic1

pi = 4 * atan(1)

' radians per 60 rotation units (mins or seconds) ...
' (ie. rad60 = (2 * pi / 60) = 0.104719755)
rad60 = pi / 30.0

' radians per 12 rotation units (hours) ...
' (ie. rad12 = (2 * pi / 12) = 0.523598775)
rad12 = pi / 6.0

getscreensize sW,sH
setsize w,(sW-wW-40),40,wW,wH						:'place the clock at top right of the screen 

' load the minutes hand sprites ...
sprm1 = DXSPRITE(w,GETSTARTPATH + "minhand1.bmp",8,38,0,0x0)
if (sprm1 = 0)
	MESSAGEBOX w, "Could not load the minutes hand sprite 1","Error"
	run = 0
	end
endif
DXSETSPRITEDATA sprm1,@SDBLTTYPE,@BLTtransrotozoom

sprm2 = DXSPRITE(w,GETSTARTPATH + "minhand2.bmp",8,38,0,0x0)
if (sprm2 = 0)
	MESSAGEBOX w, "Could not load the minutes hand sprite 2","Error"
	run = 0
	end
endif
DXSETSPRITEDATA sprm2,@SDBLTTYPE,@BLTtransrotozoom

sprm = sprm1

' load the hour hand sprites ...
sprh1 = DXSPRITE(w,GETSTARTPATH + "hourhand1.bmp",8,30,0,0x0)
if (sprh1 = 0)
	MESSAGEBOX w, "Could not load the hour hand sprite 1","Error"
	run = 0
	end
endif
DXSETSPRITEDATA sprh1,@SDBLTTYPE,@BLTtransrotozoom

sprh2 = DXSPRITE(w,GETSTARTPATH + "hourhand2.bmp",8,30,0,0x0)
if (sprh2 = 0)
	MESSAGEBOX w, "Could not load the hour hand sprite 2","Error"
	run = 0
	end
endif
DXSETSPRITEDATA sprh2,@SDBLTTYPE,@BLTtransrotozoom

sprh = sprh1

SetRegion


run = 1

waituntil run = 0
deleteimage pic1,@IMGSCALABLE
deleteimage pic2,@IMGSCALABLE
closewindow w

END

sub messages
' Message handler for the window
select @CLASS
	case @IDCLOSEWINDOW
		run = 0

	case @IDCHAR
		key = @CODE
		select key
			case 27																				:' ESC or Q keys quit the game
			case asc("Q")
			case asc("q")
				run = 0	
		endselect

	case @IDLBUTTONDN
		SENDMESSAGE w, @WM_NCLBUTTONDOWN,@HTCAPTION,0   :' to enable the window to be moved by dragging it

	case @IDDXUPDATE
		Update

	case @IDRBUTTONDN
		contextmenu w,@mousex,@mousey,"I,Clock 1,0,1", "I,Clock 2,0,2",  "I,Close,0,3"

  case @IDMENUPICK
		if (@menunum = 3) then run = 0
		if (@menunum = 1)
			pic = pic1
			sprm = sprm1
			sprh = sprh1
		endif
    if (@menunum = 2) 
			pic = pic2
			sprm = sprm2
			sprh = sprh2
		endif
endselect
return
endsub

sub SetRegion
' Create a circular window
	region = CreateEllipticRgn(0,0,wW,wH)
	SetWindowRgn(w,region,1)
return
endsub

sub Update
' update the DX screen ..
'draw the background
showimage w,pic,@IMGSCALABLE,0,-1,wW,wH									:' the -1 value adjusts the clockface vertical centre

b$=time$
'b$ = "04:45:00"

hrs = val(mid$(b$,1,2))
mins = val(mid$(b$,4,2))
secs = val(mid$(b$,7,2))
dmins = mins + secs/60.0
dhrs = hrs + dmins/60.0
if (dhrs > 12.0) then dhrs = dhrs - 12.0
mrad = dmins * rad60
hrad = dhrs * rad12

DXSETSPRITEDATA sprm,@SDangle,-mrad
DXSETSPRITEDATA sprh,@SDangle,-hrad

sprmx = 50 + 19 * sin(mrad) - 2
sprmy = 50 - 19 * cos(mrad) - 19

sprhx = 50 + 15 * sin(hrad) - 2
sprhy = 50 - 15 * cos(hrad) - 15

DXMOVESPRITE sprm,sprmx,sprmy
DXDRAWSPRITE w,sprm

DXMOVESPRITE sprh,sprhx,sprhy
DXDRAWSPRITE w,sprh

DXFLIP w																:' show the changes

return
endsub


