Using 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.
zell2002
Citizen
Posts: 75
Joined: Sun Feb 23, 2014 9:22 pm

Re: Using love.draw

Post by zell2002 »

how come you dont have a table/object/class with variable names and handle the draw call from there?

below example uses middleclass*

Code: Select all

global_objects = {}

function Object:initialize()
	
	self.x = 0
	self.y = 0
	
	self.rot = 0
	
	self.scalex = 1
	self.scaley = 1
	
	self.offsetx = 0
	self.offsety = 0
	
	self.img = love.graphics.newImage("woteva.png")
	self.quad = {} -- quad stuff

	self.color = {255,255,255,255}

	table.insert(global_objects, self)
end

function Object:draw()
	love.graphics.setColor(self.color)
	love.graphics.draw(self.img, self.quad,
		self.x, self.y, self.rot,
		self.scalex, self.scaley,
		self.offsetx, self.offsety)
end


-- in main.lua

function love.draw()
	
	for _, obj in ipairs(global_objects) do
		obj:draw()
	end
end
i prefer this method as it keeps main cleaner - personally
perhaps im missing what youre doing ?
laperen
Prole
Posts: 27
Joined: Tue Jul 26, 2016 1:15 am

Re: Using love.draw

Post by laperen »

zell2002 wrote:how come you dont have a table/object/class with variable names and handle the draw call from there?

below example uses middleclass*

Code: Select all

global_objects = {}

function Object:initialize()
	
	self.x = 0
	self.y = 0
	
	self.rot = 0
	
	self.scalex = 1
	self.scaley = 1
	
	self.offsetx = 0
	self.offsety = 0
	
	self.img = love.graphics.newImage("woteva.png")
	self.quad = {} -- quad stuff

	self.color = {255,255,255,255}

	table.insert(global_objects, self)
end

function Object:draw()
	love.graphics.setColor(self.color)
	love.graphics.draw(self.img, self.quad,
		self.x, self.y, self.rot,
		self.scalex, self.scaley,
		self.offsetx, self.offsety)
end


-- in main.lua

function love.draw()
	
	for _, obj in ipairs(global_objects) do
		obj:draw()
	end
end
i prefer this method as it keeps main cleaner - personally
perhaps im missing what youre doing ?
OP here, I actually was asking about a different method, which seems like exactly(in concept) what you mentioned.
Its in my latest reply which is the post right before yours
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Using love.draw

Post by Positive07 »

So we could think this is the best?

Code: Select all

love.draw = function ()
	for count = 1, #list do
		love.graphics.draw(unpack(list[count]))
	end
end
And yes, I would go with an Object:draw() too, looks cleaner
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
laperen
Prole
Posts: 27
Joined: Tue Jul 26, 2016 1:15 am

Re: Using love.draw

Post by laperen »

Alright then, this is what I'll be going with
Object.lua

Code: Select all

function Object.draw()
	love.graphics.draw(...)
end
Main.lua

Code: Select all

function love.draw()
	for i = 1,#objectList,1 do
		objectList[i].draw()
	end
end
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Using love.draw

Post by bobbyjones »

laperen wrote:Alright then, this is what I'll be going with
Object.lua

Code: Select all

function Object.draw()
	love.graphics.draw(...)
end
Main.lua

Code: Select all

function love.draw()
	for i = 1,#objectList,1 do
		objectList[i].draw()
	end
end
Why not just use ipairs? Lol
laperen
Prole
Posts: 27
Joined: Tue Jul 26, 2016 1:15 am

Re: Using love.draw

Post by laperen »

bobbyjones wrote: Why not just use ipairs? Lol
Test 9 of this link is demonstrating for-loops.
https://springrts.com/wiki/Lua_Performance

Other than that I also prefer the fundamental for-loop, with the exception of the for-each when it's needed.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Using love.draw

Post by bobbyjones »

laperen wrote:
bobbyjones wrote: Why not just use ipairs? Lol
Test 9 of this link is demonstrating for-loops.
https://springrts.com/wiki/Lua_Performance

Other than that I also prefer the fundamental for-loop, with the exception of the for-each when it's needed.
I'm pretty certain in luajit the difference isn't that big and it would be a bit cleaner imo.
laperen
Prole
Posts: 27
Joined: Tue Jul 26, 2016 1:15 am

Re: Using love.draw

Post by laperen »

bobbyjones wrote: I'm pretty certain in luajit the difference isn't that big and it would be a bit cleaner imo.
exactly
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Using love.draw

Post by zorg »

laperen wrote:
bobbyjones wrote: I'm pretty certain in luajit the difference isn't that big and it would be a bit cleaner imo.
exactly
Well, i did test performance between vanilla lua and luaJIT that löve uses, most of those benchmarks are useless with the latter.
Not completely my own opinion, since i did test it out.
No, i can not link you my benchmark code as it was swallowed up in my last HDD crash, but i can do you one better:
I urge you to not believe me, but also to not blindly believe those test results either; code your own benchmarks and decide for yourself.
Some helpful references which you still should not fully trust: :3
LuaJIT Performance Tuning
LuaJIT Optimizations
or just the wiki home page if you feel adventurous. :3

Also, what constitutes clea(n/r)(er) code is, of course, subjective, unless you're in a team following specific style guidelines.
That said, if performance does matter, i would not foresake a bit of ugly for a lot of non-problems in the long run.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests