Difference between revisions of "love.graphics.present"

(Vsync blocking information.)
(Added an example.)
 
Line 17: Line 17:
 
== Notes ==
 
== Notes ==
 
* If [[love.window.setMode]] has <code>vsync</code> equal to <code>true</code>, this function can't run more frequently than the refresh rate (e.g. 60 Hz), and will halt the program until ready if necessary.
 
* If [[love.window.setMode]] has <code>vsync</code> equal to <code>true</code>, this function can't run more frequently than the refresh rate (e.g. 60 Hz), and will halt the program until ready if necessary.
 +
 +
== Examples ==
 +
 +
=== Show a progress bar while the game loads ===
 +
<source lang="lua">
 +
-- Dummy functions for the example.
 +
local function loadSounds()  love.timer.sleep(1) end
 +
local function loadSprites() love.timer.sleep(1) end
 +
local function loadLevels()  love.timer.sleep(1) end
 +
 +
local font = nil
 +
 +
local function drawLoadingScreen(progress, text)
 +
local windowWidth, windowHeight = love.graphics.getDimensions()
 +
 +
-- Draw text.
 +
font = font or love.graphics.newFont(26)
 +
 +
local textX = math.floor((windowWidth - font:getWidth(text)) / 2)
 +
local textY = math.floor(windowHeight/2) - font:getHeight()
 +
 +
love.graphics.setColor(1, 1, 1)
 +
love.graphics.setFont(font)
 +
love.graphics.print(text, textX, textY)
 +
 +
-- Draw progress bar.
 +
local progressWidthFull    = 400
 +
local progressWidthCurrent = progress * progressWidthFull
 +
local progressHeight      = 20
 +
local progressX            = math.floor((windowWidth - progressWidthFull) / 2)
 +
local progressY            = math.floor(windowHeight/2)
 +
 +
love.graphics.setColor(.2, .2, .2)
 +
love.graphics.rectangle("fill", progressX, progressY, progressWidthFull, progressHeight)
 +
love.graphics.setColor(.1, .3, 1)
 +
love.graphics.rectangle("fill", progressX, progressY, progressWidthCurrent, progressHeight)
 +
end
 +
 +
local function presentLoadingScreen(progress, text)
 +
love.graphics.clear()
 +
drawLoadingScreen(progress, text)
 +
love.graphics.present()
 +
end
 +
 +
function love.load()
 +
presentLoadingScreen(0/3, "Loading sprites...") ; loadSprites()
 +
presentLoadingScreen(1/3, "Loading sounds...")  ; loadSounds()
 +
presentLoadingScreen(2/3, "Loading levels...")  ; loadLevels()
 +
end
 +
function love.draw()
 +
drawLoadingScreen(3/3, "Done loading!")
 +
end
 +
</source>
  
 
== See Also ==
 
== See Also ==

Latest revision as of 00:17, 17 July 2021

Displays the results of drawing operations on the screen.

This function is used when writing your own love.run function. It presents all the results of your drawing operations on the screen. See the example in love.run for a typical use of this function.

Function

Synopsis

love.graphics.present( )

Arguments

None.

Returns

Nothing.

Notes

  • If love.window.setMode has vsync equal to true, this function can't run more frequently than the refresh rate (e.g. 60 Hz), and will halt the program until ready if necessary.

Examples

Show a progress bar while the game loads

-- Dummy functions for the example.
local function loadSounds()  love.timer.sleep(1) end
local function loadSprites() love.timer.sleep(1) end
local function loadLevels()  love.timer.sleep(1) end

local font = nil

local function drawLoadingScreen(progress, text)
	local windowWidth, windowHeight = love.graphics.getDimensions()

	-- Draw text.
	font = font or love.graphics.newFont(26)

	local textX = math.floor((windowWidth - font:getWidth(text)) / 2)
	local textY = math.floor(windowHeight/2) - font:getHeight()

	love.graphics.setColor(1, 1, 1)
	love.graphics.setFont(font)
	love.graphics.print(text, textX, textY)

	-- Draw progress bar.
	local progressWidthFull    = 400
	local progressWidthCurrent = progress * progressWidthFull
	local progressHeight       = 20
	local progressX            = math.floor((windowWidth - progressWidthFull) / 2)
	local progressY            = math.floor(windowHeight/2)

	love.graphics.setColor(.2, .2, .2)
	love.graphics.rectangle("fill", progressX, progressY, progressWidthFull, progressHeight)
	love.graphics.setColor(.1, .3, 1)
	love.graphics.rectangle("fill", progressX, progressY, progressWidthCurrent, progressHeight)
end

local function presentLoadingScreen(progress, text)
	love.graphics.clear()
	drawLoadingScreen(progress, text)
	love.graphics.present()
end

function love.load()
	presentLoadingScreen(0/3, "Loading sprites...") ; loadSprites()
	presentLoadingScreen(1/3, "Loading sounds...")  ; loadSounds()
	presentLoadingScreen(2/3, "Loading levels...")  ; loadLevels()
end
function love.draw()
	drawLoadingScreen(3/3, "Done loading!")
end

See Also


Other Languages