Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

NafTif

Map arrays

This topic is 6878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Which is the better way to store map data: Map[Xsize * Ysize] or Map[Ysize][Xsize] I use Map[Xsize * Ysize], but everyone else around here seems to use Map[Ysize][Xsize]. Why? Is it faster or better some other way?

Share this post


Link to post
Share on other sites
Advertisement
It really doesn''t matter, it''s a matter of personal preference. I personally like doing it Map[Ysize][Xsize] because it makes the code nicer to look at (at least I think so ).

You should go with the way you like.

/. Muzzafarath

Share this post


Link to post
Share on other sites
Map[XSize * YSize] is faster because it only has 1 level of offset addressing compared to Map[XSize][YSize] which has 2.
And the Map size can be increased to a 3rd dimension simply by adding Map[XSize * YSize * ZSize]. This doesnt slow down because there is still only 1 level of offset addressing.

Share this post


Link to post
Share on other sites
hrm - as far as I can tell when your code gets assembles using map[x-size][y-size] will basically calculate the offset the same way as if you were to program map[x-size * y-size] so it doesn''t technically matter unless you can save on multiplacations by using only powers of two - but I think that these tiny little things are kinda a waste of time unless you are some kind of evangelist programmer (note -> in my programs I use map[x-size * y-size] but it''s just me).
if I were programming a DOS software engine, or any software engine perhaps these things may be more of concern.
It''s up to you.

Share this post


Link to post
Share on other sites
What you gain in only having 1 level of offset addressing, you lose in the explicit multiplication. I think it''s just a matter of coding style. I prefer [x][y] myself (or [y][x], depending) as it seems more ''correct'' - I am dealing with a 2-dimensional object, therefore a 2-dimensional array seems most appropriate. Also, map[10][20] is not the same as map[20][10], whereas map[20*10] is the same as map[10*20] which I find a little confusing as you''re not enforcing a consistent syntax.

Share this post


Link to post
Share on other sites
If you''re using any compiler made after 1990 or so, there is exactly NO difference using either method. OK, so one probably takes .001 seconds longer to compile, but that would be it.

The machine code (at least on my VC here) is the same.

Share this post


Link to post
Share on other sites
So, I think I''ll stay in my style if there isn''t any significant difference in them.

Thanks for your answers.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just out of curiosity, why not malloc some space for your map data instead of using static arrays? You could then use it sort of like an bitmap. I.e, instead of having each byte refer to a particular color, it would refer to a particular tile.

Share this post


Link to post
Share on other sites

I use malloc() for my mapdata...Easier when you have to load maps into memory of different sizes.



-- There IS something rotten in the state of Denmark --

Share this post


Link to post
Share on other sites
I also allocate my maps dynamically and make it a single dimension array.

I never hard code map sizes. I always use a variable (not a constant) to hold my map width and height. If your maps are fixed in size, then you''re going to have problems with flexibilty and it''ll be inefficient memory management.

Dino M. Gambone

Good judgement is gained through experience. Experience, however, is gained through bad judgement.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!