0
\$\begingroup\$

I am using a texture atlas for my tiles. Of course I need to get the sub-images to draw them on the canvas as separate tiles. Right now I can think of two ways to implement this.

  1. Divide the texture atlas into separate Image instances. Then draw them onto the canvas accordingly.

  2. Use a single image, and just draw part of it by adding extra parameters to CanvasRenderingContext2D's method `drawImage.

I'm not sure which is better. My thoughts were that the first option would be more manageable and flexible, since each tile is separated. What would be better? What are the ups and downs of each?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The ups of the "storing texture in atlas" would be, that you can assign textures dinamically. E.g. you hve your dungeon brick tile on the 0;0 coordinates on your image. If ypu put the 0 id on the array where the tile goes, then instead of harcoding it in, you can just do

texX = (i * texSize) % imageWidth

texY = Math.floor(i * texSize / image.width) * texSize

i is the id of the tile, texX and texY are the x and y positions, texSize is 1 textures width.

So for example, you have your texture at (5; 3), and it has 8 textures across, then you set it's id to 29.

\$\endgroup\$
5
  • \$\begingroup\$ Also, the second option would just be a waste of memory. \$\endgroup\$
    – Bálint
    Commented Feb 11, 2016 at 7:43
  • \$\begingroup\$ why would the second option be a waste of memory? I'm just drawing part of the image. Isn't the first option using more memory? \$\endgroup\$
    – userx01
    Commented Feb 11, 2016 at 12:19
  • \$\begingroup\$ @userx01sorry, the first one. You create unnecessary objects. \$\endgroup\$
    – Bálint
    Commented Feb 11, 2016 at 13:16
  • \$\begingroup\$ The option to blit a portion of an atlas multiple times is more efficient, the GPU would only load one texture and copy data from it. Multiple virtual images from an atlas would take initial load and create needless objects in memory. The GPU should still only load one image asset to perform the calculations. \$\endgroup\$
    – Jared
    Commented Feb 11, 2016 at 23:35
  • \$\begingroup\$ @Jared yeah,I said the same \$\endgroup\$
    – Bálint
    Commented Feb 12, 2016 at 6:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .