Centering an Image

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
Tyler821
Prole
Posts: 7
Joined: Thu Nov 07, 2013 10:29 pm

Centering an Image

Post by Tyler821 »

Hi. I can't find any way to center an image. :?

I tried:

Code: Select all

love.graphics.draw(map, 0, 0, 0, 1, 1, map:getWidth()/2, map:getWidth()/2)
"map" is the name of the variable for my image. I found that piece of code on the forums, but it didn't work.

PS: I need this urgently since I have a project due tomorrow that involves this. :death:
User avatar
Pebsie
Party member
Posts: 144
Joined: Mon Nov 11, 2013 12:35 am
Location: Lincoln, United Kingdom
Contact:

Re: Centering an Image

Post by Pebsie »

Center the image?
At what position?
The middle of the screen?
Draw it from the middle of the sprite?
Website: http://peb.si
Twitter: @Pebsiee http://twitter.com/pebsiee
Steam: pebsie
Tyler821
Prole
Posts: 7
Joined: Thu Nov 07, 2013 10:29 pm

Re: Centering an Image

Post by Tyler821 »

Pebsie wrote:
I want to center it in the middle of the screen so that the margins on all sides are equal. I'm using my application in fullscreen mode, if that makes a difference. Also, the map takes up most of the screen, but the margins are all weird. If there's a way to center margins, that's what I need.
User avatar
Pebsie
Party member
Posts: 144
Joined: Mon Nov 11, 2013 12:35 am
Location: Lincoln, United Kingdom
Contact:

Re: Centering an Image

Post by Pebsie »

Tyler821 wrote:
Pebsie wrote:
I want to center it in the middle of the screen so that the margins on all sides are equal. I'm using my application in fullscreen mode, if that makes a difference. Also, the map takes up most of the screen, but the margins are all weird. If there's a way to center margins, that's what I need.
I'm still not entirely sure what you mean D:
Post an image :)
Website: http://peb.si
Twitter: @Pebsiee http://twitter.com/pebsiee
Steam: pebsie
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: Centering an Image

Post by DaedalusYoung »

You need the window / screen dimensions. In your code, you center the image, based on its width and height, around position 0,0. That will place the image's centre at the top left.

Do something like this:

Code: Select all

love.graphics.draw(map, love.graphics.getWidth()/2, love.graphics.getHeight()/2, 0, 1, 1, map:getWidth()/2, map:getWidth()/2)
Tyler821
Prole
Posts: 7
Joined: Thu Nov 07, 2013 10:29 pm

Re: Centering an Image

Post by Tyler821 »

DaedalusYoung wrote:You need the window / screen dimensions. In your code, you center the image, based on its width and height, around position 0,0. That will place the image's centre at the top left.

Do something like this:

Code: Select all

love.graphics.draw(map, love.graphics.getWidth()/2, love.graphics.getHeight()/2, 0, 1, 1, map:getWidth()/2, map:getWidth()/2)
Oh, I thought I would only need either offset or position. -facepalm- Thanks! :D

And I'll post an image when I finish coding the game. ;3


EDIT: That didn't work. .-. I just dabbled with the numbers and found something almost centered. The teacher won't know the difference. ;3
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Centering an Image

Post by Germanunkol »

I think you should keep trying. This is something essential which you'll need again and again.
love.graphics.getWidth() is the width of the screen, so love.graphics.getWidth()/2 is the exact middle of the screen, along the x-axis. love.graphics.getHeight()/2 is the exact middle along the y-axis.

So drawing a circle there will center it perfectly:

Code: Select all

function love.draw()
        love.graphics.setColor(255,255,255,255)
        love.graphics.circle("fill", love.graphics.getWidth()/2, love.graphics.getHeight()/2, love.graphics.getHeight()/2)
end
Now, if you want to draw a rectangular image instead, it's origin is usually the upper left corner of the image. So instead of drawing it in the center, you need to draw it a little further to the top and left. By "a little", I mean exactly half the width and height of the image, which is what you were trying to do with the offset variables.
Try:

Code: Select all

function love.draw()
        love.graphics.setColor(255,255,255,255)
        love.graphics.draw(map, love.graphics.getWidth()/2 - map:getWidth()/2, love.graphics.getHeight()/2 - map:getHeight()/2)
end
Of course, love.graphics.getWidth()/2 - map:getWidth()/2 can be rewritten as: (love.graphics.getWidth() - map:getWidth())/2
which saves you a division (speeeed!)

If it's not working, please post more of your code!
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Post Reply

Who is online

Users browsing this forum: No registered users and 69 guests