GrabImage() question
Monkey Forums/Monkey Programming/GrabImage() question
| ||
Local atlas:Image = LoadImage ("atlas.png") Global image1:Image = atlas.GrabImage (0,0,32,32) Global Image2:Image = atlas.GrabImage (32,32,32,32) Considering the images I am grabbing are always global, do I have to keep a direct reference to the variable 'atlas' too? |
| ||
Hi Shinkiro, Only if you need to keep grabbing parts of the atlas in other moments of your application. If you only do the grabbing once, perhaphs there is no need. Still, I would encourage you not to use Globals unless you really don't have other choice. Once they start to grow (the amount of globals you have) you'll have a hard time mantaining your code. You could instead isolate your atlas (and the grabbed images) to a Singleton in your app and keep things organized: Call ImageResources.GetInstance().yourImage anywere in your app to access the resources in a controlled way. Just my two cents. |
| ||
Thanks for your answer, yes globals would be a really bad idea (I just wanted to illustrate my problem, in fact they are fields). By now my system is really cool. I can now do this: UploadImageAtlas ("atlas1.png") playerSprite.SetImage ("atlas1.png/player.png") When the whole system is more mature maybe I should publish it. (oh my ... not another framework ^^) So is GrabImage as efficient as DrawImageRect()? |
| ||
So is GrabImage as efficient as DrawImageRect()? Well, I'm not sure how DrawImageRect works. GrabImage will take the portion of the image and copy it to the new image. DrawImageRect will probably do some clipping. Since grab image is expected to be done only once, what you really are comparing is DrawImage vs DrawImageRect... Can I assume that DrawImage is slightly faster than DrawImageRect? Probably yes. |
| ||
I don't think there's much speed difference, it's just easier to treat the grabbed image as an image. Here's my implementation of an atlas loader: http://monkeycoder.co.nz/Community/posts.php?topic=1216 There are quite a few out there, but there's always room for more. |
| ||
A grabbed image and its source image share the same surface texture in mojo, so there's not really any *copying* going on. It simply creates a lightweight object that points to the existing surface and stores some offsets. Edit (since I was posting from the bus): GrabImage will be as fast as creating a new object. DrawImage and DrawImageRect should be pretty much identical in terms of performance. DrawImageRect just uses different UV coordinates. |
| ||
Ok, did a small test to see if GrabImage really improves performance that much. I did all the tests for html on a 21,5' iMac 2011. The test was to load 10 different images and render them all over the canvas so it's completely filled, somtetimes overlapping. Nearly every image had dimensions like:120x120. Without GrabImage: Firefox: ~27fps Safari: ~27fps Chrome: ~18fps With GrabImage (all 10 images in one big picture as atlas): Firefox: 60-80fps Safari: ~107fps Chrome: ~115fps WHOOO!!! I was shocked when i saw Chrome's fps counter skyrocket. This is just amazing, it should be typed bold in the docs! So, always use GrabImage, as it may increase your performance from 2x up to 6x (see Chrome). |
| ||
Using GrabImage is essentially the same as DrawImageRect, except it stores the offsets for you. Using an atlas is usually common practice since it doesn't have to do any image context switching. And yeah, HTML5 support in Firefox is still abysmal. :-) |
| ||
Shinkiro1 - was your test with Atlas and without atlas? Or with atlas but drawing via drawimagerect vs grabimage? I believe that it was the first case, so no surprise here. |
| ||
@anawiki Yes, it was atlas vs without atlas. |
| ||
And what is the best with an Atlas: DrawImageRect or DrawImage from a GrabImage ? |
| ||
And what is the best with an Atlas: DrawImageRect or DrawImage from a GrabImage ? They're essentially the same thing, so personal preference. GrabImage is probably more convenient since you can forget about calculating offsets, etc. |
| ||
could you please post the test source code, so we can do our timings and test? |