Getting the center coordinates of the screen?????

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
RBXcpmoderator12345
Citizen
Posts: 59
Joined: Sat Mar 07, 2015 11:53 pm

Getting the center coordinates of the screen?????

Post by RBXcpmoderator12345 »

I am trying to draw a image that stretches to the size of the screen. I can use the getWidth and getHeight and the paramaters of the draw function to do that, but I want it to be at the center of the screen so the image isn't offscreen.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Getting the center coordinates of the screen?????

Post by arampl »

First translate x = - imagewidth / 2, y = -height / 2, then scale, then translate x = screenwidth / 2, y = screenheight / 2.

Sorry. This is wrong. Please give me a moment.
Last edited by arampl on Fri Mar 20, 2015 5:07 am, edited 1 time in total.
RBXcpmoderator12345
Citizen
Posts: 59
Joined: Sat Mar 07, 2015 11:53 pm

Re: Getting the center coordinates of the screen?????

Post by RBXcpmoderator12345 »

Um... what?

This is my code, if it helps:

Code: Select all

function love.draw()
if playing == false then
love.graphics.draw(titlescreen)
love.audio.play(theme)
love.graphics.printf("Press Q to play.",75, 350, 200,"center") -- center your text around x = 200/2 + 100 = 200
elseif playing == true then
love.graphics.draw(background, otherParametersHere) -- A
love.audio.stop(theme)
end
end
yes everything is defined
im just having problems with the line with the comment "a"
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Getting the center coordinates of the screen?????

Post by arampl »

To center text:

Code: Select all

function love.load()
-- some of your loading code goes here...

	offw, offh = love.graphics.getDimensions()
	text = "Press Q to play."
	limit = 75
	font = love.graphics.getFont()
	h = font:getHeight()
	_, lines = font:getWrap(text, limit)
	x, y = offw / 2 - limit / 2, offh / 2 - h * lines / 2

-- rest of your loading code goes here...
end

function love.draw()
	love.graphics.printf(text, x, y, limit,"center")
end
You shouldn't put audio code in love.draw(). Put it somewhere else. See my example in your topic "How do I remove drawn images & printed text?"

Kingdaro already told you how game loop is organized.
Normally (if vsync is on), love.draw() function called 60 times per second! Therefore, you should keep all unneeded stuff outside of it. But you keep calling audio.play / audio.stop 60 times per second. Maybe it's harmless to try to play already plaiyng / stop already stopped music, but CPU cycles wasted nevertheless.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Getting the center coordinates of the screen?????

Post by zorg »

arampl wrote:Normally (if vsync is on), love.draw() function called 60 times per second! Therefore, you should keep all unneeded stuff outside of it.
Correction: It is called depending on the current screen's set refresh rate; that can be anything the monitor supports, but it's usually between 60 and 120; example: mine's 75Hz, so love.draw would be called 75 times a second instead of 60.

By the way, love.update would be called the same amount of times love.draw would (under the default circumstances that is (default love.run)), but your're right, it doesn't really go into love.draw.

Edit: to answer the OP's question:

Code: Select all

love.graphics.draw(background, love.graphics.getWidth() /2 - background:getWidth() /2, love.graphics.getHeight() /2 - background:getHeight() /2) -- untested but should work
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.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Getting the center coordinates of the screen?????

Post by arampl »

zorg wrote:Correction: It is called depending on the current screen's set refresh rate; that can be anything the monitor supports
Yeah, you're absolutely right. My bad.
zorg wrote:Edit: to answer the OP's question:

Code: Select all

love.graphics.draw(background, love.graphics.getWidth() /2 - background:getWidth() /2, love.graphics.getHeight() /2 - background:getHeight() /2) -- untested but should work
It appears that things are much simpler than I've thought. To "draw a image that stretches to the size of the screen" enough would be just:

Code: Select all

love.graphics.draw(background, 0, 0, 0, love.graphics.getWidth() / background:getWidth(), love.graphics.getHeight() / background:getHeight())
RBXcpmoderator12345
Citizen
Posts: 59
Joined: Sat Mar 07, 2015 11:53 pm

Re: Getting the center coordinates of the screen?????

Post by RBXcpmoderator12345 »

I know, I figured that out.
My problem is this;
I am trying to center the picture. Whenever i try to position it, it is either offscreen or partially offscreen.
Yes, it is the full size of the screen because

Code: Select all

width = love.graphics:getWidth()
height = love.graphics:getHeight()
This is my code:

Code: Select all

love.graphics.draw(background, 672, 372, 0, width, height, 672, 372)
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Getting the center coordinates of the screen?????

Post by arampl »

Well, what is the actual size of your background image (and window size, to be sure)?

1.Do you have background image that is smaller than screen size?
2. If that is so, do you want just center it unscaled or scale it to the screen size, retaining its aspect ratio?

This code will stretch image of any aspect ratio to the screen size of any aspect ratio (will be centered and retain image's aspect ratio, of course):

Code: Select all

	w, h = love.graphics.getDimensions()
	bw = background:getWidth()
	bh = background:getHeight()
	if bw / bh > w / h then
		scale = w / bw
		x = w / 2 - bw / 2 * scale
		y = h / 2 - bh / 2 * scale
	else
		scale = h / bh
		x = w / 2 - bw / 2 * scale
		y = h / 2 - bh / 2 * scale
	end
	love.graphics.draw(background, x, y, 0, scale, scale)
User avatar
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Re: Getting the center coordinates of the screen?????

Post by TheOddByte »

If it helps, this is what I use

Code: Select all

local image = love.graphics.newImage( "<path>" )
local w, h = love.window.getDimensions()
local width, height = image:getDimensions()
local scalex, scaley = w/width, h/height

function love.draw()
    love.graphics.draw( image, 0,0,0, scalex, scaley )
end
That code basically stretches an image to fit the whole screen, here's how you can center an image

Code: Select all

local image = love.graphics.newImage( "<path>" )
local w, h = love.window.getDimensions()
local width, height = image:getDimensions()

function love.draw()
    love.graphics.draw( image, (w-width)/2, (h-height)/2 )
end
What's the object-oriented way to get wealthy? Inheritance.
RBXcpmoderator12345
Citizen
Posts: 59
Joined: Sat Mar 07, 2015 11:53 pm

Re: Getting the center coordinates of the screen?????

Post by RBXcpmoderator12345 »

ok thanks
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 228 guests