Last active
February 23, 2021 14:08
-
-
Save 1bardesign/fac82485d67b8d97f6a38a68f685b5be to your computer and use it in GitHub Desktop.
A little self-contained pomodoro timer for love2d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function love.conf(t) | |
t.window.width, t.window.height = 200, 60 | |
t.window.x, t.window.y = 10, 10 | |
t.window.borderless = true | |
t.window.title = "Pomodoro!" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
little pomodoro timer script to go off on a configurable interval | |
]] | |
local intervals = { | |
{"work", 25}, | |
{"rest", 5}, | |
} | |
local t = 0 | |
local i = 0 | |
local limit = 0 | |
local label = "" | |
love.graphics.setFont(love.graphics.newFont(32)) | |
-- generate beep audio | |
local rate = 44100 | |
local duration = 0.25 | |
local freq_a = 440 | |
local freq_b = 440 * 2 | |
local samples = rate * duration | |
local beep = love.sound.newSoundData(samples, rate, 16, 1 ) | |
for i = 1, samples do | |
local t = i / rate | |
local note = ((1 - (i / samples)) * 2) | |
local note_vol = note % 1 | |
local freq = note < 1 and freq_a or freq_b | |
local a = math.sin(t * freq * math.pi * 2) | |
local b = (a * a) * 2 - 1 | |
local v = a * b * note_vol | |
beep:setSample(i - 1, 1, v) | |
end | |
beep = love.audio.newSource(beep) | |
function love.update(dt) | |
t = t - dt | |
if t <= 0 then | |
--beep | |
beep:seek(0) | |
beep:play() | |
--next interval | |
i = i + 1 | |
if i > #intervals then | |
i = 1 | |
end | |
local cfg = intervals[i] | |
label = cfg[1] | |
limit = cfg[2] * 60 | |
t = limit | |
end | |
end | |
function love.draw() | |
love.graphics.setColor(0.5, 0.5, 0.5, 1.0) | |
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth() * t / limit, love.graphics.getHeight()) | |
love.graphics.setColor(1.0, 1.0, 1.0, 1.0) | |
love.graphics.printf( | |
label, | |
0, 10, | |
love.graphics.getWidth(), | |
"center" | |
) | |
end | |
function love.keypressed(k) | |
if k == "n" or k == "space" then | |
--skip to next | |
t = 0 | |
end | |
if k == "escape" then | |
love.event.quit() | |
end | |
if love.keyboard.isDown("lctrl") then | |
if k == "q" then | |
love.event.quit() | |
elseif k == "r" then | |
love.event.quit("restart") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment