Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dynamic Tile maps

Started by jman2050 Oct 6, 2002 at 1:14 PM 4 replies 550+ views
Original Post
jman2050
jman2050
Hello, I am trying to figure out how to make a dynamic 2D tilemap. I''ve read the tutorial on this site, but it didn''t work. my further research showed that it was because C++, the language I''m using, doesn''t allow allocating void mamory locations (or something to that effect) since it affects the way constructors of classes work. i tried the new and delete operators, but I got the exact same eroor upon compiling. Please help.
tinak
tinak
I use the following to specify dynamic arrays and clear them. They should help you also..

(type)* allocate(int count)
{
return ((type)*)malloc(sizeof((type)) * count);
}

(type) can be anything from a normal C variable type to a struct specified by yourself.

The above can be used for single dimension arrays such as tiles[10].

For a multi-dimension array such as tiles[10][10] I do the following to setup memory for them:

varname = ((type)**)malloc(sizeof((type)*) * MAXROWS);
for (l=0;l{
varname[l] = ((type)*)malloc(sizeof((type)) * MAXCOLS);
}

Whether or not I am correct in doing this (as I also had problems using delete etc) but I simply set the created variable to NULL.

varname = NULL;
for example I use to essentially reinitialise the double array as an empty variable.

Hopefully that will help you.

Tina

[edited by - tinak on October 6, 2002 3:00:27 PM]
jman2050
jman2050
the point is, i used that exact same method, but C++ wouldn''t let me. C would, but C++ would not let me do it. I remember it being about problems that an occur by allocating memory in that fashion in constructors (which is what I am trying to do). Thanks for trying anyways .
Dynaguy
Dynaguy
Code please?
jman2050
jman2050
well, i got my question answered elsewhere. i was told to use STl vectors, but I would still like to know if there was some other method I can use. basically, I am trying to code something like this:


class MapData
{
private:
int width, height;
int tileReference; //this can be set dynamically at the constructor so this is not my concern
}

class CurrentMap
{
private:
int width, height
Tile tiles; //this is the part. i need the size of this to be changed dynamically during runtime. I wasn''t specific enough before.
MapData map;
//blah blah
}


Hope this gives you a better understanding of what I''m trying to do.
Dynaguy
Dynaguy
The way I do this is using linked lists. Basically you have an array of pointers. Each of these point to the first tile at (x,y). I resize during runtime like this:

array = new Object *[dwSize];
ZeroMemory(array, dwSize * sizeof(Object *));

When I want to know the first tile at (10,5) I get the pointer from the array like so: array[5 * width + 10].

Every object has 2 pointers, prev and next. The first object which the array points to has prev = NULL obviously, the last object has next = NULL.

You can write functions to add objects, and sort them right away by adding them between the right objects in the list.

When cycling through the objects at a specific location, you can use an easy for loop:

for (Object *obj = GetObject(x, z); obj; obj = obj->next) { }

Have fun.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.