jakubg1/OpenSMCE

Sprite atlases

Opened this issue · 0 comments

Currently, the sphere drawing routine is drawing each sphere in order of their color. This allows us to reduce the drawcall count to the amount of colors on the screen.
However, the order in which the spheres are drawn may not be important now, but it will be important later, when 3D paths will get introduced. Plus, the drawcall count will still increment when there are many different sphere colors/types on the board, and that decreases the overall performance.

The solution is to place all sphere sprites onto a single texture, so there are no switches in between - and in order to keep the resource files readable and the solution itself to be flexible, I've decided to do it programatically.
The idea is to define texture atlases. These would be created automatically during resource loading, and any drawing action executed on a sprite which is a part of an atlas would redirect that action to draw a texture from said atlas.

Key insights to make this work:

  • love.graphics.newCanvas(w, h) - Creates an instance of Canvas, which will store the atlas texture part.
  • love.graphics.setCanvas(canvas) ---> iterate through every sprite: love.graphics.draw(image, x, y) ---> love.graphics.setCanvas() - Prepare the canvas: fill it with texture data.
  • You can use such prepared Canvas like an Image with love.graphics.draw(...).
  • The sprites should be stacked vertically: the width of the Canvas should be the maximum width of all Sprites, and the height - the sum of all Sprites' heights.
  • Calculate the new frame sizes, if needed.