Created
June 24, 2012 02:15
-
-
Save benui-dev/2981095 to your computer and use it in GitHub Desktop.
Löve2d code for loading and drawing a tetured polygon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function love.load() | |
| -- Load our repeating texture | |
| local img = love.graphics.newImage("dirt.png") | |
| -- Points for our polygon | |
| local vertices = {12,0, 396,7, 500,375, 195,520, 78,377} | |
| texturedPoly = newTexturedPoly(vertices, img) | |
| end | |
| function newTexturedPoly(vertices, img) | |
| -- We want our images to tile | |
| img:setWrap("repeat", "repeat") | |
| -- We need a quad so the img is repeated | |
| -- The quad width/height should be the max x/y of the poly | |
| local quad = love.graphics.newQuad(0, 0, 500, 520, img:getWidth(), img:getHeight()) | |
| -- Set up and store our clipped canvas once as it's expensive | |
| local canvas = love.graphics.newCanvas() | |
| love.graphics.setCanvas(canvas) | |
| -- Our clipping function, we want to render within a polygon shape | |
| local myStencilFunction = function() | |
| love.graphics.polygon("fill", unpack(vertices)) | |
| end | |
| love.graphics.setStencil(myStencilFunction) | |
| -- Setting to premultiplied means that pixels just get overlaid ignoring | |
| -- their alpha values. Then when we render this canvas object itself, we | |
| -- will use the alpha of the canvas itself | |
| love.graphics.setBlendMode("premultiplied") | |
| -- Draw the repeating image within the quad | |
| love.graphics.drawq(img, quad, 0, 0) | |
| -- Reset everything back to normal | |
| love.graphics.setBlendMode("alpha") | |
| love.graphics.setStencil() | |
| love.graphics.setCanvas() | |
| return canvas | |
| end | |
| function love.draw() | |
| -- Draw our canvas object texturedPoly | |
| love.graphics.draw(texturedPoly, 50, 50) | |
| -- showfps | |
| love.graphics.setColor(255,255,255,200) | |
| love.graphics.print(tostring(love.timer.getFPS()), love.graphics.getWidth()-30, love.graphics.getHeight()-20) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment