Answer the question
In order to leave comments, you need to log in
How to properly work with Canvas in Love2D?
I started my acquaintance with Canvas, and the question arose whether it is right to do this, I have a tile map, which I draw once through
canvas:renderTo(function()
--и тут я рисую много тайлов по вертикали и горизонтали
)
Answer the question
In order to leave comments, you need to log in
Canvas, you can find out what it contains by taking a screenshot of the canvas.
GAMEWINDOWCANVAS:newImageData():encode('png', realpath..levelname.. 'x2.png');
substitute the name of your function for gamewindow. in some of the versions love newImageData was renamed, please note.
I would post my code in its entirety. but he's too scary. gave an abbreviated version of the example with all the key commands.
Therefore, I will describe those grains that I learned myself.
I have 2 canvases.
1st atlas. it contains all the drawings that are in the game.
The 2nd canvas is a playing field, the field is "drawn" in it with pieces of the 1st canvas. quads.
an example of how to create an atlas.
function create_atlas()
ATLAS = love.graphics.newCanvas(8192, basetexturesize) is to create an empty picture to fill with a sprite map.
nonetexture="1empty.png";
IMAGES = {};
IMAGES[0]=objs[0+1][3]; -- I have here an appeal to a table with a list of ALL used pictures.
for a0=1,170,1 do -- maximum temporarily 170 (6800 pixels), for 1 byte mode - 256.
IMAGES[a0]=objs[a0][3]; -- load the list of names from the object table.
end
love.graphics.setCanvas(ATLAS)
for i = 0, #IMAGES - 1 do
local img = love.graphics.newImage("Textures/"..IMAGES[i + 1]);
if (img==nil) then love.graphics.newImage("Textures/"..nonetexture); end;
love.graphics.draw(img, i * basetexturesize)
IMAGES[i + 1] = love.graphics.newQuad(i * basetexturesize, 0, basetexturesize, basetexturesize, 8192, basetexturesize)
end
love.graphics.setCanvas()
end
then you can simply draw with the array generated in images
GAMEWINDOWCANVAS = love. graphics.newCanvas(xxxx, yyyy) -- x,y is to create an empty picture to fill with a sprite map.
nonetexture="1empty.png";
IMAGESX={};
love.graphics.setCanvas(GAMEWINDOWCANVAS);
for yy=1,0+visual_mapsize_vertical,1 do -- this loop does the rendering, it should also make an active copy of the map. --5*editor+maximumvertical
for xx=1,0+visual_mapsize_horizontal,1 do
IMAGESX[object_to_rendering]=IMAGES[objectcodenow + 1];
OBJECTPRINTNOW_IMAGESX=IMAGESX[object_to_rendering];
postobjectX=xx*rozmiarznak;
postobjectY=yy*rozmiarznak;
love.graphics.draw(ATLAS,OBJECTPRINTNOW_IMAGESX,postobjectX, postobjectY,0,scaling,scaling);
-- it is this line above that draws everything you want on the playing field.
end;
a=a+1;
end;
a=a+mapsize_horizontal-visual_mapsize_horizontal;
end;
end
love.graphics.setCanvas() -- this line returns the renderer to the playfield. mandatory.
the example is not entirely correct. I didn’t get to the point where I put pictures in 2-3 lines
I'm talking about the experience itself is not enough.
video cards do not support a canvas width of more than 8000 pixels. if that.
And further. Canvas should be formed only if something changes on the field.
If the player stands still and does not move, and his enemies too - - it is not necessary to redraw it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question