love2d教程2--绘图和文字

love的绘图函数,共有以下7个,以"love.graphics."开头
line 直线
quad 矩形
arc 弧线
point 点
rectangle 多边形
circle 圆
triangle 三角形

drawmode有line和fill两种,分别指轮廓和填充
blendmode有additive alpha subtractive multiplicative premultiplied
colormode有modulate,replace,combine

以上具体的说明请看love的wiki

下面说一下文字。

可以直接使用love.graphics.print()输出文字,其实love2d自带一个ttf字体“love._vera_ttf”(Bitstream Vera Sans)
love可以使用两种字体,ttf和imagefont。下面是imagefont的介绍
The imagefont file is an image file in a format that Löve can load. It can contain transparent pixels, so a PNG file is preferable, and it also needs to contain spacer color that will separate the different font glyphs.
The upper left pixel of the image file is always taken to be the spacer color. All columns that have this color as their uppermost pixel are interpreted as separators of font glyphs. The areas between these separators are interpreted as the actual font glyphs.
The width of the separator areas affect the spacing of the font glyphs. It is possible to have more areas in the image than are required for the font in the love.graphics.newImageFont() call. The extra areas are ignored.
imagefont是一个图像文件的格式,包含透明像素,所以png是可取的,它也需要包含间隔的颜色,将不同的字体字形分开。
左上角像素被作为间隔的颜色。所有列最高部分的像素的颜色被解释为分隔符的字体字形。这些分隔符之间的区域被解释为实际的字体。
分隔符宽度的影响的字体间距,在图像中,间隔区比字体区多会更好,额外的区域将被忽略。仅支持 256 characters。

加载文字会减慢速度,所以建议只使用一种字体,之后对该字体进行操作,如设置大小,颜色等,不要反复加载字体。

注意我在使用中文字体时,发现不论是显示中文还是因为字体大小要17才可,不知怎么回事。(2012.12.07

这个和字体文件有关,用win7雅黑字体可以设置更小的字体。(2013.8.31)

注意下面这段代码会每次加载字体,让速度变慢。(2013.8.31)

 

function love.draw()
    drawGraphics()
    useDefaultFont("hello",210,100,20)
    useImgFont("world",210,140)
    useTTFFont("中文",210,160,18)
    
end

可以简单的加个判断即:
local isInit=false
function love.draw()
  if not isInit then drawGraphics() useDefaultFont("hello",210,100,20) useImgFont("world",210,140) useTTFFont("中文",210,160,18) end end

love2d绘图后,图像依旧是存在的,不需要每次绘制,所以需要时才绘制。

 

main.lua代码如下

function drawGraphics()
    love.graphics.setBlendMode("alpha") --默认混合模式
    love.graphics.setColor(230, 44, 123)
    love.graphics.rectangle("fill", 50, 50, 100, 100)
    
    love.graphics.setColor(12, 100, 230)
    love.graphics.setBlendMode("multiplicative")
    love.graphics.rectangle("fill", 75, 75, 125, 125)

end
--你可以自己动手设置各种属性,看看效果
function useDefaultFont(text,x,y,size)
     love.graphics.setColor(255,0,0)
     love.graphics.setBlendMode("alpha")
     --love.graphics.setColorMode("combine")
    love.graphics.print("default font size is 12",x,y)
    local font = love.graphics.newFont( size )
    love.graphics.setFont(font)
    love.graphics.print(text,x,y+size)
    
end

function useTTFFont(text,x,y,size)
    local font=love.graphics.newFont("assets/mona.ttf",size)
    love.graphics.setFont(font)
    love.graphics.print(text,x,y)
end

--imagefont实际是一副有序的字符图 function useImgFont(text,x,y) local font = love.graphics.newImageFont("assets/imagefont.png", " abcdefghijklmnopqrstuvwxyz" .. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0" .. "123456789.,!?-+/():;%&`'*#=[]\"") love.graphics.setFont(font) love.graphics.print(text, x, y) end function love.load() love.graphics.setBackgroundColor(54, 172, 248) end function love.draw() drawGraphics() useDefaultFont("hello",210,100,20) useImgFont("world",210,140) useTTFFont("中文",210,160,18) end function love.update(dt) end function love.keypressed(key) end

工程文件下载地址http://pan.baidu.com/share/link?shareid=123276&uk=1913510140

 

posted @ 2012-12-07 09:19  半山th  阅读(7454)  评论(29编辑  收藏  举报