Difference between revisions of "love.graphics.present"
m |
(Added an example.) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
Displays the results of drawing operations on the screen. | Displays the results of drawing operations on the screen. | ||
− | This function is used when writing your own [[ | + | 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 == | == Function == | ||
=== Synopsis === | === Synopsis === | ||
Line 7: | Line 8: | ||
love.graphics.present( ) | love.graphics.present( ) | ||
</source> | </source> | ||
+ | |||
=== Arguments === | === Arguments === | ||
None. | None. | ||
+ | |||
=== Returns === | === Returns === | ||
Nothing. | Nothing. | ||
+ | |||
+ | == 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. | ||
+ | |||
+ | == 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 == | ||
* [[love.graphics.clear]] | * [[love.graphics.clear]] |
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.
Contents
Function
Synopsis
love.graphics.present( )
Arguments
None.
Returns
Nothing.
Notes
- If love.window.setMode has
vsync
equal totrue
, 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
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info