LuAllegro clock example.

Clock example is a small script that shows current time in a LuAllegro window. 

Clock hands are transparent bitmaps, drawn using pivot_sprite routine.

Here is the list of files included in clock.zip:

clock.lua         -clock script.

godzina.pcx       -transparent bitmap containing hour hand of a clock.

minuta.pcx        -transparent bitmap containing minute hand of a clock.

sekunda.pcx       -transparent bitmap containing second hand of a clock.

miscellaneous.lua -helper script - it puts all allegro routines directly into root _G level

8x16.pcx          -font used in this example

startup.lua       -Initializes LuAllegro library, loads and calls miscellaneous.lua, then loads font and calls clock.lua.

 

How to run a clock example?

You need muaddib034.exe or above to run this example.

 

Here is the script code:

set_color_depth(32)

if not set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0) 
then 
print("set_gfx_mode failed.Exiting.") 
return;
end
set_display_switch_mode(SWITCH_BACKGROUND)

clearscreen()
local WHITE=makecol(255,255,255)
local YELLOW=makecol(255,255,128)
local GRAY =makecol(128,128,128)
local LIGHTGRAY =makecol(200,200,200)

textout_centre(font8x16,"LuAllegro clock example",320,0,WHITE,0)
textout_centre(font8x16,"Press any key to exit the program.",320,464,WHITE,0)

for x=0,3 do
local v=70+30*x
local col=makecol(v,v,v)
circle(320,240,220-x,col)
circle(320,240,210+x,col)
floodfill(320,25,col)
end

local Pi=3.1415926535

for x=0,59 do
local angle=Pi*x/30
local x1,y1,x2,y2
x1=320+210*sin(angle)
y1=240-210*cos(angle)
x2=320+205*sin(angle)
y2=240-205*cos(angle)
line(x1,y1,x2,y2,GRAY)
end

local function znacznik_godzinowy(k,mk,c,r1,r2)
local x1,y1,x2,y2,x3,y3,x4,y4
x1=320+r1*sin(k-mk)
y1=240-r1*cos(k-mk)
x2=320+r1*sin(k+mk)
y2=240-r1*cos(k+mk)
x3=320+r2*sin(k+mk)
y3=240-r2*cos(k+mk)
x4=320+r2*sin(k-mk)
y4=240-r2*cos(k-mk)
polygon(nil,{x1,y1,x2,y2,x3,y3,x4,y4},c)
end

for i=0,11 do
local angle=Pi*i/6
znacznik_godzinowy(angle,0.016,GRAY,211,190)
znacznik_godzinowy(angle,0.008,LIGHTGRAY,208,192)
end

local sekunda_bmp= load_bitmap("sekunda.pcx")
local minuta_bmp= load_bitmap("minuta.pcx")
local godzina_bmp= load_bitmap("godzina.pcx")

local zegar= create_bitmap(440,440)
local offscreen= create_bitmap(440,440)

--zapamiętaj widok zegara
blit(screen,zegar,320-220,240-220,0,0,440,440)



local wskazowka_sekundowa=function(k)
   pivot_sprite(offscreen,sekunda_bmp,220,220,7,147,k*6)
end

local wskazowka_minutowa=function(k)
   pivot_sprite(offscreen,minuta_bmp,220,220,16,191,k*6-0.4)
end

local wskazowka_godzinowa=function(k)
   pivot_sprite(offscreen,godzina_bmp,220,220,31,141,k*6)
end

local rysuj_zegar=function(godzina,minuta,sekunda)

--narysuj na offscreen widok zegara
blit(zegar,offscreen,0,0,0,0,440,440)

--narysuj wskazowki
wskazowka_godzinowa(godzina*5)
wskazowka_minutowa(minuta)
wskazowka_sekundowa(sekunda)

--narysuj to wszystko na ekranie
blit(offscreen,screen,0,0,100,20,440,440)
end


clear_keybuf()

local t=os.date("*t",os.time())
local second=t.sec;

repeat

--pobierz dane o czasie
t=os.date("*t",os.time())

--odśwież widok zegara, kiedy zmieni się sekunda
if second~=t.sec then
textout(font8x16,os.date("%c",os.time()),0,0,WHITE,0)
rysuj_zegar( t.hour+t.min/60+t.sec/3600 , t.min+t.sec/60 , t.sec ) 
second=t.sec;
end

rest(50)
until keypressed()

clear_keybuf()
clearscreen()
--exits the program
exit()