What is the best way?

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.
Post Reply
User avatar
ellipticaldoor
Prole
Posts: 6
Joined: Sun May 05, 2013 2:56 pm

What is the best way?

Post by ellipticaldoor »

I want to optimize this code but I don´t know very much lua, what I should do? :roll:

Code: Select all

function background.draw()
    if player.actual_level == 0 then
        love.graphics.setColor(52,73,94,255)
        love.graphics.rectangle("fill", 0, roof, 1280, 720)
        love.graphics.setColor(243, 156, 18, background.alpha)
        love.graphics.circle("fill", background.moon_x, 300 + roof, 300)
    end

    if player.actual_level == 1 then
        love.graphics.setColor(255,255,255,255)
        love.graphics.draw(bg, background.posx , roof)
        love.graphics.draw(bg, background.posx - 1280 , roof)
    end

    if player.actual_level == 2 then
        love.graphics.setColor(52,152,219,255)
        love.graphics.rectangle("fill", 0, roof, 1280, 720)
        love.graphics.setColor(230,126,34, background.alpha)
        love.graphics.rectangle("fill", 0, roof, 1280, 720)
        love.graphics.setColor(255,255,255,255)
        love.graphics.draw(bg_2a, 0 , roof)
        love.graphics.setColor(multiplyChannels(255, 255, 255, alphabg_2a))
        love.graphics.draw(bg_2b, 0, roof)
    end

    if player.actual_level == 3 then
        
    end
end
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: What is the best way?

Post by Plu »

What are you trying to optimise? Do you want it more readible, more easily expandable, or just more efficient? There's a different solution for each and I'm not really clear on what you're trying to accomplish.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: What is the best way?

Post by Kingdaro »

If it's not lagging, don't try to optimize it. It's usually only good to start optimizing your code once things start slowing down.

In terms of shortness/readability, it looks fine. There's not much room for any repetitions or functions, since all of the drawing operations are pretty different from one another.
User avatar
ellipticaldoor
Prole
Posts: 6
Joined: Sun May 05, 2013 2:56 pm

Re: What is the best way?

Post by ellipticaldoor »

I want to reduce the number of needed lines and use a switch statement, I don't know how to make it in LUA... :o:
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: What is the best way?

Post by markgo »

Well you can try:

Code: Select all

switch = {...}

function blah(...)
 switch[player.actual_level](...)
end
User avatar
ellipticaldoor
Prole
Posts: 6
Joined: Sun May 05, 2013 2:56 pm

Re: What is the best way?

Post by ellipticaldoor »

thanks!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What is the best way?

Post by Robin »

You'll want to ask each level to draw themselves. So they each gets a function. It's basically what markgo suggested, but switch as a variable name is awfully generic.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 6 guests