Drawing outside of love.draw

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
IndieKid
Citizen
Posts: 80
Joined: Sat Dec 22, 2012 7:05 pm
Contact:

Re: Drawing outside of love.draw

Post by IndieKid »

I was interested in this some time ago. I didn't find the way to do this. :(
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Drawing outside of love.draw

Post by Ubermann »

IndieKid wrote:I was interested in this some time ago. I didn't find the way to do this. :(
Damn, I'm screwed.

Unless you know a way to draw temporary things without using a table to save them and running through this table in love.draw.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Drawing outside of love.draw

Post by Azhukar »

Ubermann wrote:Is it possible?

How?

Thanks.
You need to override the love.run function, you do this by simply copy-pasting that wiki function into your code.

love.graphics.clear is what clears the frame buffer, i.e. the stuff drawn by all draw calls.
love.graphics.present is what draws the frame buffer on the actual screen.

What you want to do is move love.graphics.clear somewhere else but still before love.graphics.present.
User avatar
IndieKid
Citizen
Posts: 80
Joined: Sat Dec 22, 2012 7:05 pm
Contact:

Re: Drawing outside of love.draw

Post by IndieKid »

Azhukar wrote: You need to override the love.run function, you do this by simply copy-pasting that wiki function into your code.

love.graphics.clear is what clears the frame buffer, i.e. the stuff drawn by all draw calls.
love.graphics.present is what draws the frame buffer on the actual screen.

What you want to do is move love.graphics.clear somewhere else but still before love.graphics.present.
Thanks. This isn't that easy to just draw outside of love.draw()
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Drawing outside of love.draw

Post by Jasoco »

If you must, simply move the clear() call to above update() instead of where it currently is (In between update() and draw().) Then it'll let you draw whether you're in update or draw. By using a custom love.run() function in main.lua.
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Drawing outside of love.draw

Post by Ubermann »

Yes it works.

What I need was to draw in mousepressed() so i ovirrided the run() as follows (only show the update part):

Code: Select all

love.graphics.clear()
if love.mousepressed then love.mousepressed(x,y,button) end
if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
if love.graphics then
    if love.draw then love.draw() end
end

if love.timer then love.timer.sleep(0.001) end
if love.graphics then love.graphics.present() end
Basically, I added mousepressed() before update() and moved clear() before everything.

What I don't understand is why by default, LÖVE doesn't add every LÖVE functions to run() so it's possible to draw from everywhere by default.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Drawing outside of love.draw

Post by Azhukar »

Ubermann wrote:What I don't understand is why by default, LÖVE doesn't add every LÖVE functions to run() so it's possible to draw from everywhere by default.
Mousepressed, same as all other events, is already processed in this part of love.run:

Code: Select all

-- Process events.
if love.event then
	love.event.pump()
	for e,a,b,c,d in love.event.poll() do
		if e == "quit" then
			if not love.quit or not love.quit() then
				if love.audio then
					love.audio.stop()
				end
				return
			end
		end
		love.handlers[e](a,b,c,d)
	end
end
What you did was process mousepressed event TWICE. Once in your condition and once in the above part of love.run.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Drawing outside of love.draw

Post by kikito »

Since no one has mentioned it, I'd like to point out that there's a good reason for not allowing drawing outside love.draw.

LÖVE tries to make things simple, but at the same time it tries to help you not to make mistakes. Wanting to draw outside love.draw is usually a sign that there is something that you are not understanding about LÖVE.
What I need was to draw in mousepressed()
You can store the coordinates in variables, and then use those variables to draw stuff inside love.draw. For example:

Code: Select all

local points = {}
function love.mousepressed(x,y)
  local len = #points
  points[len+1], points[len+2] = x,y
end

function love.draw()
  if #points >= 4 then love.graphics.line(points) end
end
When I write def I mean function.
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Drawing outside of love.draw

Post by Ubermann »

Azhukar wrote: Mousepressed, same as all other events, is already processed in this part of love.run:

Code: Select all

-- Process events.
if love.event then
	love.event.pump()
	for e,a,b,c,d in love.event.poll() do
		if e == "quit" then
			if not love.quit or not love.quit() then
				if love.audio then
					love.audio.stop()
				end
				return
			end
		end
		love.handlers[e](a,b,c,d)
	end
end
What you did was process mousepressed event TWICE. Once in your condition and once in the above part of love.run.
But if I set the love.graphics.clear() before the events and after the while, it doesn't works. If this is what you want to mean.


kikito wrote:Since no one has mentioned it, I'd like to point out that there's a good reason for not allowing drawing outside love.draw.

LÖVE tries to make things simple, but at the same time it tries to help you not to make mistakes. Wanting to draw outside love.draw is usually a sign that there is something that you are not understanding about LÖVE.
What I need was to draw in mousepressed()
You can store the coordinates in variables, and then use those variables to draw stuff inside love.draw. For example:

Code: Select all

local points = {}
function love.mousepressed(x,y)
  local len = #points
  points[len+1], points[len+2] = x,y
end

function love.draw()
  if #points >= 4 then love.graphics.line(points) end
end

And that probably result in a low optimized code since the program will always check several unnecessary if...end's even when there is nothing to draw.

If I want to draw something that appears when I press the mouse and disappears when release the mouse, why would I make the program to check it every game cycle if I can do it only when mouse is released? It is like if the program checks if mouse is released and if XXXX variable is true or false, when I can just save that variable and the associated if...end

And also, if there are things to draw that are not always the same, for example, different images, rectangles and other things that have nothing in common, it would be a pain of multiple variables and unnecessary if...end's
I know what you mean, but for a game that has multiple different things to optionally draw, doing it in love.draw() is not much efficient.

As a real example, I need the game to show a tooltip when the mouse hovers over some specific game elements. The tooltip doesn't always have the same contents: sometimes it may have only plain text, sometimes formatted text and sometimes other things such as images.
If I do this in love.draw() I will need one if..end and one variable for each kind of tooltip contents.

And the same would happen for anything that needs to be momentarily shown. If those momentary things were only one or two, it's ok, but when there are many of those things, it would a really painful.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 33 guests