Large arrays

Posted on 2008-11-14
Medium Priority
Last Modified: 2012-05-05
Hi experts.

I'm creating a game in C++ and I want to make a level editor for it in VB.net, because of the UI.
It's a 2D platformer, which means it has a lot of "screens" one after another, 256x240 resolution.

The entire workspace is around 7000x6000 but because that paints very slowly I've divided that in 5x5 sections, 25 "screens" of 256x240, so a total of 1280x1200 that is visible with the ability to go to the next section of that resolution. Every screen is divided in squares of 16x16, the size of a tile so every screen can have 240 tiles.

When the user paints a tile I have to store some information about that tile, it's location, type and path of the bitmap which is already a threedimensional array. If the user were to fill the visible screen with tiles, that would mean 240 times 25 = 6000 tiles which all need to be stored.
Problem number 2, there are a total of 4 layers on top of each other which would mean 4 threedimensional layers 6000 large.

Sorry lots of text.I assume this would put a strain on the program's memory usage so I was wondering what would be a good way to hold all this data without lowering the performance too much?

Thanks in advance.
Question by:Snapples
  • 2
  • 2
LVL 10

Expert Comment

ID: 22967352
Well arrays are the lowest common denominator here, classes or objects add a lot of overhead so maybe arrays are the quickest, as long as you're not doing a lot of looping through 'looking' for things?
LVL 24

Expert Comment

by:Jeff Certain
ID: 22967463
Classes and objects really don't add much overhead in .NET.

I'd consider an object to store the tile information -- location, type, and bitmap path information. I'd probably consider transitioning the bitmaps to be loaded from classes that use the flyweight pattern so you only keep each one in memory once. (This is the same approach text editors use for the graphics for each letter, to avoid having 3 billion letter "T"s in memory).

This leaves the question of the actual storage mechanism. I'd look at using generics, since they are type-safe and perform well. They also give you baked in functionality for Find (although that can be somewhat tricky to implement, since it requires a delegate, and the base implementation in 2.0 was poorly considered... LINQ helps alleviate many of those issues).

Author Comment

ID: 22969201
Sorry if I wasn't very clear, it was late here and I was kinda tired when I posted that question.

I only know about flyweight classes but I've never used one before. It looks simple enough but I'm wondering, if I store bitmaps as flyweights and give them an id, each time when the user selects a tile in my texture library, then it would have to check if that tile is already stored or not. What if the user has already painted like 1000 different tiles, when he selects a tile from my texture library, wouldn't it take long for the program to check if the selected tile is already a flyweight object or not? Since it's a new tile, I can't think of anything else to compare but the path of the bitmap.
LVL 24

Accepted Solution

Jeff Certain earned 2000 total points
ID: 22971914
Well, you could do something like implementing the flyweight pattern using a Dictionary(Of Integer, Bitmap) or Dictionary(Of String, Bitmap), depending on how you want to reference the bitmap (by name/path or ID).

Then, it becomes as simple as checking Dictionary.Contains to see if the key already exists (i.e. the bitmap is already loaded.)

However, assuming that you have a finite and reasonable number of bitmaps, you could well load them all into memory when you initialize the application. This avoids repetitive (and redundant) checks to see if the bitmap is loaded. Since your bitmaps are stored in the Tile object as a pointer, very little memory is consumed.

In addition, since Dictionary(Of T, K) requires unique keys, you're actually going to be able to do a binary tree search. I've benchmarked this using up to a million records, and it's essentially flat as it scales. (Finding the correct element in a 100-element Dictionary takes ~60ms; finding the correct element in a 1,000,000-element Dictionary takes <100ms.)

Author Comment

ID: 23107791
I'm really sorry, real life issues caused me to forget all about this question.
I won't be able to work on this project for a while.

Sorry again for leaving this open for so long.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question