From 0729efb0e33f8e962ac15d004a0efc2bb005406b Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 24 Mar 2024 16:26:07 -0600 Subject: [PATCH] First commit, did some stuff --- clock.lua | 35 +++++++++++++++++++++++++ conf.lua | 3 +++ main.lua | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 clock.lua create mode 100644 conf.lua create mode 100644 main.lua diff --git a/clock.lua b/clock.lua new file mode 100644 index 0000000..35ac334 --- /dev/null +++ b/clock.lua @@ -0,0 +1,35 @@ +local MAX_SECONDS = 60 * 60 * 12 + +local Hand = {} +Hand.__index = Hand + +function Hand:new(length, color, sec_per_turn, ticks_per_turn) + local h = { + length = (length or 10), + color = (color or {1,1,1}), + sec_per_turn = (sec_per_turn or 1), + ticks_per_turn = ticks_per_turn + } + return setmetatable(h, self) +end + +local function handTransform(sec_elapsed) + local turns = sec_elapsed / self.sec_per_turn + if self.ticks_per_turn then + turns = math.floor(turns * self.ticks_per_turn) / self.ticks_per_turn + end + return love.math.newTransform(0, 0, 2 * math.pi * turns) +end + +function Hand:draw(sec_elapsed) + local turns = sec_elapsed / self.sec_per_turn + if self.ticks_per_turn then + turns = math.floor(turns * self.ticks_per_turn) / self.ticks_per_turn + end + love.graphics.push("all") + love.graphics.applyTransform(handTransform(sec_elapsed)) + love.graphics.setColor(unpack(self.color)) + love.graphics.rectangle("fill", -10, 10, 20, -self.length) + love.graphics.pop() +end + diff --git a/conf.lua b/conf.lua new file mode 100644 index 0000000..9fda08f --- /dev/null +++ b/conf.lua @@ -0,0 +1,3 @@ +function love.conf(t) + t.window.msaa = 3 +end \ No newline at end of file diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..de10df9 --- /dev/null +++ b/main.lua @@ -0,0 +1,77 @@ +local MAX_SECONDS = 60 * 60 * 12 + +local Hand = {} +Hand.__index = Hand + +function Hand:new(length, color, sec_per_turn, ticks_per_turn) + local h = { + length = (length or 10), + color = (color or {1,1,1}), + sec_per_turn = (sec_per_turn or 1), + ticks_per_turn = ticks_per_turn + } + return setmetatable(h, self) +end + +function Hand:draw(sec_elapsed) + local turns = sec_elapsed / self.sec_per_turn + if self.ticks_per_turn then + turns = math.floor(turns * self.ticks_per_turn) / self.ticks_per_turn + end + love.graphics.push("all") + love.graphics.setColor(unpack(self.color)) + love.graphics.rotate(2 * math.pi * turns) + love.graphics.rectangle("fill", -10, 10, 20, -self.length) + love.graphics.pop() +end + +local clock = {rawseconds = 0, speedup = 120} + +function clock:advance(dt) + self.rawseconds = self.rawseconds + dt * self.speedup + if self.rawseconds >= MAX_SECONDS then + self.rawseconds = self.rawseconds - MAX_SECONDS + end +end + +function clock:getHour() + local h = math.floor(self.rawseconds / 60 / 60 % 12) + if h == 0 then + h = 12 + end + return h +end + +function clock:getMinute() + return math.floor(self.rawseconds / 60 % 60) +end + +function clock:getSecond() + return math.floor(self.rawseconds % 60) +end + +function clock:fmt() + return string.format("%2d:%02d:%02d", self:getHour(), self:getMinute(), self:getSecond()) +end + +local secondhand = Hand:new(300, {0.5,0.5,0}, 60, 60) +local minutehand = Hand:new(240, {1,0,0}, 60*60) +local hourhand = Hand:new(180, {0,0.5,0.5}, 60*60*12) + +function love.load() + love.graphics.setBackgroundColor(0,0,0) +end + +function love.update(dt) + clock:advance(dt) +end + +function love.draw() + love.graphics.print(clock:fmt()) + love.graphics.print(math.floor(clock.rawseconds), 10, 50) + + love.graphics.translate(love.graphics.getWidth()/2, love.graphics.getHeight()/2) + secondhand:draw(clock.rawseconds) + minutehand:draw(clock.rawseconds) + hourhand:draw(clock.rawseconds) +end