Page 1 of 1

creating low-power apps without animations

Posted: Mon Aug 08, 2022 4:25 am
by Kartik Agaram
I have this idea for an app that draws to the screen as little as possible. So I tried modifying love.run (from https://love2d.org/wiki/love.run) to only draw on events:

Code: Select all

function love.run()
  love.graphics.setBackgroundColor(1,1,1)

  love.graphics.origin()
  love.graphics.clear(love.graphics.getBackgroundColor())
  -- could call love.draw() here
  love.graphics.present()

  -- Main loop time.
  return function()
    -- Process events.
    if love.event then
      love.event.pump()
      for name, a,b,c,d,e,f in love.event.poll() do
        if name == "quit" then
          if not love.quit or not love.quit() then
            return a or 0
          end
        end
        love.handlers[name](a,b,c,d,e,f)
      end

      -- update screen only on events
      love.graphics.clear(love.graphics.getBackgroundColor())                  -- A
      -- could call love.draw() here
      love.graphics.present()
    end

    love.timer.sleep(0.001)
  end
end
As expected, this prints a white screen.

However, if I delete the line labeled A, because I want to not clear the screen on every frame, the screen starts to flicker.

Does anyone understand why this happens?

Re: creating low-power apps without animations

Posted: Mon Aug 08, 2022 6:30 am
by ReFreezed
love.event is the module that lets you handle events, i.e. it's always a table, so the if-condition always passes and love.graphics.present() gets called every frame. You should check if the for-loop runs at least once and only clear+draw+present then.

The flickering if you delete love.graphics.clear() is caused by multiple buffering swapping two buffers back and forth.

Re: creating low-power apps without animations

Posted: Mon Aug 08, 2022 7:28 am
by BrotSagtMist
little as possible> not having zero cpu usage.
I chuckled a little.
What you want is a check value that signals if there actually is something worth of drawing, if there is not, then just ignore everything.

Anyway this double buffer flickering is explained in the link but the TLDR version is that many/most graphic cards have multiple, usually two, picture buffers that are flip flop. So you dont end up with reading/writing at the same time.
The buffer is switched on each love.graphics.present(), imagine it like having two canvases and each time present is called they are switched.
And a canvas actually helps if you want to get rid of the clear() call, you just draw a canvas instead.

The hard part is to solve that if you only want to change a tiny amount of pixels, like a cursor, you gotta have to repaint the whole screen for having 20 pixels blink.

You can turn of your graphics card and use software rendering instead using an environment variable, this wont double buffer, you can paint single pixels without the rest of the screen. This is a better method for desktop applications BUT it performs VERY terrible on anything non static. And just having any draw at all is non static. Works nice with love.event.wait() tho.
Have fun getting a blinking cursor with that.

Re: creating low-power apps without animations

Posted: Mon Aug 08, 2022 8:49 am
by pgimeno
If you don't draw everything and call present() every frame, your application is not going to refresh the window contents when you place another window over the Löve window. There's no window content storage and no exposure notification; the window needs to be redrawn every frame. See viewtopic.php?f=4&t=81204&p=190914#p190914

Re: creating low-power apps without animations

Posted: Mon Aug 08, 2022 9:10 am
by BrotSagtMist
Objection, placing another window on top of löve generates an event with the value nil.
While not being useful for much, it at least can trigger a refresh and you can avoid the frozen program look.

Re: creating low-power apps without animations

Posted: Mon Aug 08, 2022 3:27 pm
by Kartik Agaram
Thanks everyone for the extremely illuminating answers!
Have fun getting a blinking cursor with that.
That is, in fact, the one bit of animation I was hoping to retain :D Which introduces problems of redrawing the character under the cursor as well..

Re: creating low-power apps without animations

Posted: Mon Aug 08, 2022 3:46 pm
by BrotSagtMist
I see little chance of doing that without redrawing all the pixels.

But i solved the amount of refreshes by creating different draw layers.
So the underlayer is refreshed after actual draw operations, the mid layer on events and the top layer, where the cursor is, has a timer.

Re: creating low-power apps without animations

Posted: Tue Aug 09, 2022 9:31 am
by Kartik Agaram
That makes sense. This thread also reminded me of Canvas which might be a better way to save power. Do you turn each of your layers into canvases?

Re: creating low-power apps without animations

Posted: Tue Aug 09, 2022 2:59 pm
by BrotSagtMist
Nope, one canvas.
But still working on these problems myself.