--love的window模块比较简单,直接贴代码了
function love.load()
io.stdout:setvbuf("no") -- 设置io为无缓存模式
--获取显示模式
local w1, h1, flags = love.window.getMode()
--[[ flags是一个表,默认属性如下
fullscreen false
resizable false
fsaa 0
fullscreentype normal
vsync true
minwidth 1
centered true
minheight 1
borderless false
display 1
]]
--love.window.setMode( width, height, flags ) --设置显示模式,flags参数同上
--获取显示器的分辨率,参数是第i个显示器
local w2, h2 = love.window.getDesktopDimensions(flags.display)
--获取love2d程序的窗口大小
local w3, h3 = love.window.getWidth( ) ,love.window.getHeight()
local w4, h4 = love.window.getDimensions( )--同上
--窗口标题
love.window.setTitle("测试window模块")
print(love.window.getTitle())
--获取love2d所在显示器的所有显示模式
local modes = love.window.getFullscreenModes(flags.display)
table.sort(modes, function(a, b) return a.width*a.height < b.width*b.height end) --按宽x高结果大小排序
print("显示器模式")
for k,v in pairs(modes) do
print(k,v.width,v.height)
end
-- love.window.getDisplayCount()--获取显示器数量
-- love.window.getFullscreen() --是否全屏
--设置全屏,两种模式 "normal"和desktop",缺省normal模式
--normal会改变屏幕分辨率
--desktop是无边框窗口,窗口自动适应屏幕分辩率,不改变屏幕分辩率大小
--当love2d窗口大小设为800x600可以发现使用desktop鼠标大小不变,而noraml变大
--love.window.setFullscreen( true, "normal" )
--设置图标 任务栏上的图标,图片大小有限制
--love.window.setIcon(love.image.newImageData("1.jpg") )
--love.window.getIcon() --获取图标
--love.window.isCreated() --窗口是否创建
-- scale = love.window.getPixelScale( ) --和mac视网膜显示屏相关,love2d 0.9.1才有这个函数
end
local msg=""
local kfocus,mfocus="",""
function love.draw()
love.graphics.print("press ‘c‘ resize,‘n‘ normal fullscreen,‘d‘ desktop fullscreen",100,50)
love.graphics.print(msg, 100, 100)
love.graphics.print(kfocus, 100, 120)
love.graphics.print(mfocus, 100, 140)
end
function love.update()
--窗口焦点事件,当失去焦点时,应该让游戏暂停
if love.window.hasFocus() then
kfocus="window has keyboard focus"
else
kfocus="window lose keyboard focus"
end
if love.window.hasMouseFocus() then
mfocus="window has mouse focus"
else
mfocus="window lose mouse focus"
end
--窗口是否可见
if love.window.isVisible() then
msg="Window is visible!"
else
msg="Window is not visible!"
end
end
function love.keypressed(key)
if key == "escape" then
love.event.quit()
end
--测试love.resize
if key == "c" then
love.window.setMode(100000,100000)
end
if key== "n" then
love.window.setFullscreen( true, "normal" )
end
if key=="d" then
love.window.setFullscreen( true, "desktop" )
end
end
--love.window.isVisible的回调模式
function love.visible(v)
if v then
print("Window is visible!")
else
print("Window is not visible!")
end
end
--窗口大小改变事件,只有不满足love.window.setMode设置的大小时引发
function love.resize(w, h)
msg=("resize width: %d and height: %d."):format(w, h)
print(msg)
end
--love.window.hasFocus的回调模式
function love.focus(f)
if not f then
text = "UNFOCUSED!!"
print("LOST FOCUS")
else
text = "FOCUSED!"
print("GAINED FOCUS")
end
end
--love.window.hasMouseFocus的回调模式
function love.mousefocus(f)
if not f then
text = "Mouse is not in the window!"
print("LOST MOUSE FOCUS")
else
text = "Mouse is in the window!"
print("GAINED MOUSE FOCUS")
end
end