Could you explain to me the 2 rule?

owen_dennisowen_dennis Just a guy, you know.Member, PRO Posts: 236

I’ve seen some questions before that were all about the "2 rule". I didn’t fully understand the idea of images uncompressing in RAM, so I’ve been assuming that since my images were small on my hard drive, and that they had lengths and widths that were even numbers, that it would be okay in my game. I’m apparently quite wrong and I was conflating some issues.

So let me just try to wrap my head around this.

The 2 rule, as I understand it now, is this: each image should have a length or width of one of these numbers: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048

Here’s where I get a little confused:

if an image is larger than the nearest number in the 2 rule, will it just round up to whatever the nearest 2 is? So like if I have an image that’s 130 by 60, it will actually take up the amount of space in RAM that a 256 by 64 image would take up? So it would be better to make the image 128 x 64, even though on the 64 side I don’t actually need those extra 4 pixels?

So a 1024 x 768 background image is actually taking up the equivalent of 1024x1024 in RAM? So I could make my background image 1024x1024 just because I might as well be using that space if it’s gonna be taken up anyway?

Comments

  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922

    Yes it goes by the largest number of the dimension. To be honest I don't pay much attention to this and my images always look great. I just tend to do things to the scale I need for it to look right in the layout. I myself have never had an issues with images due to size.

  • SocksSocks London, UK.Member Posts: 12,822
    edited September 2015

    @owen_dennis said:
    I’ve seen some questions before that were all about the "2 rule".

    It's called powers-of-2, it's not a rule as such, just a recommendation for the most efficient use of memory.

    @owen_dennis said:
    The 2 rule, as I understand it now, is this: each image should have a length or width of one of these numbers: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048

    Well . . . . . they don't have to stick to those dimensions, for example, if you had a 480 x 480 pixel image - and you were not prepared to compromise image quality by reducing it to 256 x 256 - then there would also be no point increasing it to 512 x 512 . . . . . so in this situation you would be better off ignoring the powers-of-2 recommendation and just sticking with your 400 x 400 pixel image.

    @owen_dennis said:
    if an image is larger than the nearest number in the 2 rule, will it just round up to whatever the nearest 2 is?

    You 'round up' to the next higher number rather than to the nearest.

    @owen_dennis said:
    So like if I have an image that’s 130 by 60, it will actually take up the amount of space in RAM that a 256 by 64 image would take up?

    Yes !

    @owen_dennis said:
    So it would be better to make the image 128 x 64, even though on the 64 side I don’t actually need those extra 4 pixels?

    What !? Lol :smile: 'Better' is an arbitrary term in a technical discussion. If you wanted to save some memory then reducing the 130 to 128 would halve the memory use (for that dimension), because you would be using 128 (128) rather than 256 (130) . . . . . but increasing 60 to 64 would have no effect on your project / on RAM usage other than making your file size (the size of the App on disk) slightly bigger.

    @owen_dennis said:
    So a 1024 x 768 background image is actually taking up the equivalent of 1024x1024 in RAM?

    Yes !

    @owen_dennis said:
    So I could make my background image 1024x1024 just because I might as well be using that space if it’s gonna be taken up anyway?

    This would make your file size on disk larger, so slower to download, slower to load and so on, but it would have no effect on RAM usage when compared to a 1024 x 768 image.

    Most of these concerns are only really worth bothering about when you have lots of images.

  • owen_dennisowen_dennis Just a guy, you know. Member, PRO Posts: 236
    edited September 2015

    @Lost_Oasis_Games @Socks Thanks!

    Alright cool, I'm getting it. One more quick question related to RAM stuff, if an image isn't used, it never gets loaded into the RAM correct (unless I had preload checked)? However, when I stop using an image, it's still sitting in the RAM waiting to be seen again right? When does it get flushed out, only when I quit the game or when I leave a scene?

    I do have a ton of images and I think it might be part of what's crashing my game right now. My game crashes around when it says the images are using up 230mb of RAM and the engine is using 330mb. And I haven't even gone through and added all my sounds yet!

    So now I'm going through and reducing the amount of frames in animation, seeing what I can do to reduce RAM usage in each image, etc. Would be a lot easier if I could just use a custom font, but my game has a lot of writing in it as well, so those end up being big ol' images too. I'm also thinking instead of making a "unpressed button" and a "pressed button" version of each button, I'll just make the button grow larger when it's pressed or something. Anything to try and cut down on images. :-/

  • SocksSocks London, UK.Member Posts: 12,822

    @Lost_Oasis_Games said:
    Yes it goes by the largest number of the dimension. To be honest I don't pay much attention to this and my images always look great. I just tend to do things to the scale I need for it to look right in the layout. I myself have never had an issues with images due to size.

    Agreed, you can often ignore these recommendations unless you have hundreds of images or lots of very large images.

  • SocksSocks London, UK.Member Posts: 12,822

    @owen_dennis said:
    One more quick question related to RAM stuff, if an image isn't used, it never gets loaded into the RAM correct (unless I had preload checked)? However, when I stop using an image, it's still sitting in the RAM waiting to be seen again right? When does it get flushed out, only when I quit the game or when I leave a scene?

    Yes, images are removed from RAM between scenes.

    @owen_dennis said:
    I do have a ton of images and I think it might be part of what's crashing my game right now. My game crashes around when it says the images are using up 230mb of RAM and the engine is using 330mb. And I haven't even gone through and added all my sounds yet!

    Well then you might be a good candidate for image optimisation.

    @owen_dennis said:
    Would be a lot easier if I could just use a custom font, but my game has a lot of writing in it as well, so those end up being big ol' images too.

    There is no particular reason why font images should be any larger than any other image (which is: x2 the size of the actor they are being applied to), unless you have a uniquely large custom font display ?

    @owen_dennis said:
    I'm also thinking instead of making a "unpressed button" and a "pressed button" version of each button, I'll just make the button grow larger when it's pressed or something. Anything to try and cut down on images. :-/

    I doubt a single depressed button image would have any meaningful effect on how well a game runs, but I suppose as part of an overall purge of images it might help.

Sign In or Register to comment.