Guest User

Analog Clock

a guest
Mar 30th, 2012
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. function round(num, idp)
  2.   local mult = 10^(idp or 0)
  3.   return math.floor(num * mult + 0.5) / mult
  4. end
  5.  
  6.  
  7. print("       Analog Clock      ")
  8. sleep(3.0)
  9.  
  10. gpuCount = 0
  11. gpus = {}
  12. gpuSide = ""
  13.  
  14. --detecting gpus
  15.  
  16. sides = {"back","front","left","right","top"}
  17. for i = 1, 5, 1 do
  18.   if peripheral.isPresent(sides[i]) then
  19.     if peripheral.getType(sides[i]) == "gpu" then
  20.       gpuCount = gpuCount + 1                             --increasing gpu count
  21.       table.insert(gpus, (#gpus + 1), sides[i]) --adding gpu to list
  22.     end
  23.   end
  24. end
  25.  
  26. --tell User
  27. exit = false
  28.  
  29. if gpuCount == 0 then
  30.   print("No gpu found!")
  31.   exit = true
  32. elseif (gpuCount > 1) then
  33.   print("More than 1 gpu found!")
  34.   print()
  35.   local reading = true
  36.   while reading do
  37.     print("Please choose:")
  38.     for i = 1, #gpus, 1 do
  39.       print("GPU at side: " .. gpus[i])
  40.     end
  41.     print()
  42.     gpuSide = read()
  43.    
  44.     for i = 1, #gpus, 1 do
  45.  
  46.       if gpuSide == gpus[i] then
  47.         reading = false
  48.       end
  49.     end
  50.    
  51.     if reading == true then
  52.       print("Invaild input!! Please repeat")
  53.       sleep(1.5)
  54.     end
  55.   end
  56.  
  57. elseif gpuCount == 1 then
  58.   gpuSide = gpus[1]
  59. end
  60.  
  61. if not exit then
  62.   print("GPU at side " .. gpuSide .. " selected")
  63.    
  64.   print("Hold [Ctrl] + [T] to terminate!")
  65.   sleep(2)
  66.   print("Starting Clock")
  67.  
  68.   while true do
  69.     time = os.time()
  70.    
  71.     hours = math.floor(time)
  72.     minutes = math.floor((time-hours)*60)
  73.    
  74.    
  75.     --clocCode
  76.       clockCode = "glColor3f(0,0.3,1) glBegin(GL_POINTS)"
  77.      
  78.       for i = 0,360,1 do
  79.        clockCode = clockCode .. " glVertex2f(".. round(math.sin( math.rad(i) )*-63 + 63) .. ",".. round(math.cos( math.rad(i) )*-63 + 64)..")"    
  80.       end
  81.       clockCode = clockCode .. " glEnd()"
  82.    
  83.     -- hourHand
  84.    
  85.       hourHand = " glColor3f(1,1,1) glBegin(GL_TRIANGLES)"
  86.      
  87.       hourHand = hourHand .. "  glVertex2f(".. round(math.sin( math.rad((hours % 12) * 30 - 2) )*63 + 63) .. ",".. round(math.cos( math.rad((hours % 12) * 30 - 2) )*-63 + 64) .. ")"
  88.       hourHand = hourHand .. "  glVertex2f(".. round(math.sin( math.rad((hours % 12) * 30 + 2) )*63 + 63) .. ",".. round(math.cos( math.rad((hours % 12) * 30 + 2) )*-63 + 64) ..")"
  89.       hourHand = hourHand .. "  glVertex2f(63,64)"
  90.      
  91.       hourHand = hourHand .. "  glEnd()"
  92.      
  93.     -- minuteHand
  94.    
  95.       minuteHand = " glColor3f(0.8,0,0.1) glBegin(GL_TRIANGLES)"
  96.            
  97.       minuteHand = minuteHand .. "  glVertex2f(".. round(math.sin( math.rad((minutes) * 6 - 1) )*63 + 63) .. ",".. round(math.cos( math.rad((minutes) * 6 - 1) )*-63 + 64) .. ")"
  98.       minuteHand = minuteHand .. "  glVertex2f(".. round(math.sin( math.rad((minutes) * 6 + 1) )*63 + 63) .. ",".. round(math.cos( math.rad((minutes) * 6 + 1) )*-63 + 64) ..")"
  99.       minuteHand = minuteHand .. "  glVertex2f(63,64)"
  100.            
  101.       minuteHand = minuteHand .. "  glEnd()"
  102.        
  103.    
  104.     --EOF creating OPENGL code
  105.    
  106.     gpu.setRenderCode(gpuSide, [[
  107.    
  108.       gpuBindOutputBuffer(1)
  109.       gpuClearBuffer()
  110.    
  111.       ]] .. clockCode .. " " .. hourHand .. " " .. minuteHand)
  112.    
  113.     sleep(0.001)
  114.   end
  115.  
  116. end
  117.  
  118. print("Going down..")
Advertisement
Add Comment
Please, Sign In to add comment