faiface / pixel

A hand-crafted 2D game library in Go
MIT License
4.45k stars 246 forks source link

MakePicture methods always create new TargetPicture #178

Open gwttk opened 5 years ago

gwttk commented 5 years ago

Look at batch.go#L82 and canvas.go#L73 All MakePicture methods return new TargetPicture instances. Also every Sprite caches a TargetPicture. So, does that means, for 10k Sprites created from a single Picture, pixel creates 10k TargetPictures? That sounds very inefficient!

faiface commented 5 years ago

Yep, means exactly that! This can be efficient or inefficient based on what target you're using. If you're drawing directly to the window, then yes, this will be rather inefficient, because a new OpenGL texture will be allocated for each picture. However, if you use Batch, it will be very efficient because TargetPicture for Batch is just a struct of two pointers and the Batch only makes one TargetPicture from its own target (e.g. window).

gwttk commented 5 years ago

Hmmm, that makes sense. How about... instead of creating new batchPicture everytime, just reuse it, since every Batch uses only one Picture?

faiface commented 5 years ago

Looking at it, yeah, that could be done. Feel free to make a PR :)