LOVE2D to Android - Power (Energy) and Sleep (Turn off)
Posted: Wed Feb 15, 2017 11:22 pm
Hello my friends, I will provide a simple and useful example to work with android system using LÖVE2D, I LOVE LÖVE and LÖVE LOVE ME LÖVE LOVE LOVE
Code: Select all
sleep_time=0
screen, touch, resize = {}, {}, {}
screen.w, screen.h = love.window.getDesktopDimensions(display)
resize.w, resize.h = screen.w/800 , screen.h/480
touch.x, touch.y = 0, 0
function love.load()
smile = love.graphics.newImage("smile.png")
end
function love.update(dt)
state, percent, seconds = love.system.getPowerInfo()
if percent and percent < 50 then
time=5
else
time=10
end
sleep_time = sleep_time + dt
if math.floor(sleep_time)>=time then
if not love.window.isDisplaySleepEnabled() then
love.window.setDisplaySleepEnabled(true)
end
sleeping=true
return false
else
if love.window.isDisplaySleepEnabled() then
love.window.setDisplaySleepEnabled(false)
end
sleeping=nil
end
end
function love.touchpressed( id, x, y, dx, dy, pressure )
sleep_time=0
touchpress=true
touchreleased=nil
end
function love.touchmoved( id, x, y, dx, dy, pressure )
sleep_time=0
touchmoved=true
touchreleased=nil
end
function love.touchreleased( id, x, y, dx, dy, pressure )
sleep_time=0
touchreleased=true
touchmoved=nil
touchpress=nil
end
function love.draw()
if sleeping then
love.graphics.print("Sleeping "..math.floor(sleep_time).." secs", 10, 10, 0, 3*resize.w, 3*resize.h)
return false
end
for i, id in ipairs(love.touch.getTouches()) do
touch.x, touch.y = love.touch.getPosition(id)
end
--in center and resized
love.graphics.draw(smile, (screen.w/2)-(smile:getWidth()/2), (screen.h/2)-(smile:getHeight()/2), 0 , resize.w, resize.h)
if touchmoved then
status="Moving"
elseif touchpress then
status="Pressing"
elseif touchreleased then
status="Released"
else
status="Sleeping"
end
printer="Touch is "..status.." X="..math.floor(touch.x).." Y="..math.floor(touch.y)
love.graphics.print(printer, 10, 10, 0, 3*resize.w, 3*resize.h)
end