Public Group

# Multi Array Directx C++

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

## Recommended Posts

I have structure that called [font="Courier New"]OURCUSTOMVERTEX for the vertices of the terrain and now
I create the terrain :
OURCUSTOMVERTEX *cv_Vertices = new OURCUSTOMVERTEX[WIDTH*HEIGHT];

but i try to do this:
OURCUSTOMVERTEX *cv_Vertices = new OURCUSTOMVERTEX[HEIGHT][WIDTH];

and its give me errors.

##### Share on other sites
Try this:

OURCUSTOMVERTEX **cv_Vertices; cv_Vertices = new OURCOSTOMVERTEX* [ HEIGHT ]; for (int i = 0; i < HEIGHT; i++) cv_Vertices [ i ] = new OURCUSTOMVERTEX [ WIDTH ];

This code will give you a HEIGHT x WIDTH array; but if you want WIDTH x HEIGHT one, just swap HEIGHT and WIDTH in the code above.

Or simply, use STL vectors. They're much simpler and faster.

hth.
-R

##### Share on other sites
Post the errors.

Its pretty easy to write a 2D array class that uses std::vector. Here is an untested one:
 template<class T> class vector2d { public: vector2d(int width, int height) : width_(width), storage(width * height) { } T &operator()(int x, int y) { return storage[index(x,y)]; } const T &operator()(int x, int y) const { return storage[index(x,y)]; } int width() const { return width_; } int height() const { return storage.size() / width_; } private: typename std::vector<T>::size_type index(int x, int y) { // range check here if you want // the vector usually does this for us in debug anyway return y * width_ + x; } int width_; std::vector<T> storage; }; 
You should be able to use it like this:
 vector2d<OURCUSTOMVERTEX> vertices(WIDTH, HEIGHT); [font="'Courier New"] [/font] [font="'Courier New"]// Fill vertices(x,y) = ...[/font] [font="'Courier New"] [/font] [font="'Courier New"]// Use foo = vertices(x,y)[/font] 

##### Share on other sites
Moving this thread to For Beginners, since it's no DirectX specific, and arrays are a reasonably fundamental C++ concept.

##### Share on other sites

I have structure that called [font="Courier New"]OURCUSTOMVERTEX for the vertices of the terrain and now
I create the terrain :
OURCUSTOMVERTEX *cv_Vertices = new OURCUSTOMVERTEX[WIDTH*HEIGHT];

but i try to do this:
OURCUSTOMVERTEX *cv_Vertices = new OURCUSTOMVERTEX[HEIGHT][WIDTH];

and its give me errors.

If this dynamic declaration of a new two dimensional array is within a function this is fine. however, if cv_Vertices is part of a class, you can not declare a two dimensional array like this, a[X][Y]. To be honest, I am not totally sure why, but I have encountered the same problem. However, there is no difference between a single dimensional array and a two dimensional array (or even an Nth dimentional array).

[font="'Courier New"]In memory [/font]OURCUSTOMVERTEX[HEIGHT][WIDTH] is the same as OURCUSTOMVERTEX[WIDTH*HEIGHT]. It is easier to visualise it and understand it when written like this, a[1][1]. All OURCUSTOMVERTEX[HEIGHT][WIDTH] is doing is accessing the the array like so, cv_Vertices[i * WIDTH + HEIGHT]. so for example if you want an array of vertices that is 10 x 10 you can declare it like this: OURCUSTOMVERTEX *cv_Vertices = new OURCUSTOMVERTEX[10*10]; and then iterate over the array like so:

for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
[font="'Courier New"] cv_Vertices[(i * 10) + j] = { /* vertex info */ };[/font]
[font="'Courier New"]
[/font]
[font="'Courier New"]if for ease of use you want to access your array of vertices like a two dimensional array you could write a wrapper class that allows you to index into your array like a two dimensional array:[/font]
[font="'Courier New"]
[/font]
[font="'Courier New"]class Table // 2 D arrays are often visualised like a table -- Columns and Rows[/font]
[font="'Courier New"]{[/font]
[font="'Courier New"]public:[/font]
[font="'Courier New"] Table(int width, int height);[/font]
[font="'Courier New"] ~Table;[/font]
[font="'Courier New"] [/font]
[font="'Courier New"] OURCUSTOMVERTEX& operator[][] (int i, int j);[/font]
[font="'Courier New"] .......[/font]
[font="'Courier New"]private:[/font]
[font="'Courier New"] OURCUSTOMVERTEX* cv_Vertices;[/font]
[font="'Courier New"]};[/font]
[font="'Courier New"]
[/font]
[font="'Courier New"]Hope that helps[/font]

##### Share on other sites
You have no speed or storage advantage in not using a contiguous block of memory. Simply wrap it with accessors that return [x + y*width] and you'll be easily be able to access it with x and y... I don't see why you need a 2D array.

• 11
• 20
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999847
×