Archived

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

Roof Top Pew Wee

How would I make the .dwWidth a constant?

Recommended Posts

I have to create an array of WORDS to copy the contents of a surface to so that I can save it since surfaces are just pointers to memory on the graphics card. To do this, with a surface, I'd have to make the array of size [height * ddsurfacedesc2.dwWidth]. I have a set max height that isn't very large, but the dwWidth could change. Is there a way to get this value into a constant, and if not, what would be a good estimate for the dwWidth? This is assuming that the surface is a perfect square, so the width is equal to the height. --Vic-- Edited by - Roof Top Pew Wee on November 15, 2001 1:49:52 AM

Share this post


Link to post
Share on other sites
If you have a set maximum height, and the surface is a perfect square - you at least have a maximum width?

If you want to be more accurate, dynamically allocate an array using the new operator. This way you can dictate at run-time exactly how big your array will be based on the current width and height of the surface.

Share this post


Link to post
Share on other sites
My knowledge of new says this. Say I want to allocate however many . ..automobiles I need. I have to do myCar[0] = new automobile. And keep doing so. But don''t I have to start out by setting a max amount of cars by doing this: automobile* myCar[MAXAMOUNT]? And I cannot exceed MAXAMOUNT? If this is the case, you didn''t answer much of my question. I already knew to use the new, but I don''t know what the max should be.

--Vic--

Share this post


Link to post
Share on other sites

WORD *buffer = new WORD [height * ddsurfacedesc2.dwWidth];


allocates height * ddsurfacedesc2.dwWidth WORDs and assigns the address to buffer. When you are done:


delete [] buffer;
buffer = NULL; //No necessary, but usually safer


Share this post


Link to post
Share on other sites
Injective:
Your method works, but it doesn''t solve the original problem. See, the buffer is a pointer. When this is part of an object, and the object is saved, only the pointer address is saved, not what it points to. So here, I''m filling up memory, but only the pointer to the memory is being saved. Not very useful for saving graphics. Is there anyway to use the new so that I can dynamically say how many WORDS I want to have, but actually save what is inside and not the addresses? I have a function to save a surface to a bitmap, and I could use this, but I''d rather not because this would make a smaller file. Thanks

--Vic--

Share this post


Link to post
Share on other sites
quote:
Original post by Roof Top Pew Wee
But don''t I have to start out by setting a max amount of cars by doing this: automobile* myCar[MAXAMOUNT]?

No. You can do this:

automobile **myCar;
// initialize outer array:
myCar = new automobile *[MAXAMOUNT];
for(int idx = 0; idx < MAXAMOUNT; ++idx)
myCar[idx] = new auomobile[number_per_array];
// deallocation is done in two steps:
// 1.) deallocate each inner array
for(int idx = 0; idx < MAXAMOUNT; ++idx)
delete [] myCar[idx];
// 2.) deallocate outer array
delete [] myCar;
myCar = NULL;

This is extensible to an ifinite number of dimensions.

Share this post


Link to post
Share on other sites
quote:

only the pointer address is saved, not what it points to



This is just a matter of dereferecing the pointer:

  
bufferLength = height * ddsurfacedesc2.dwWidth;
WORD *buffer = new WORD [ bufferLength ];
memcpy ((void*)buffer, (void*)videoBuffer, sizeof (WORD) * bufferLength);
std::ofstream outFile;
outFile.open ("output.raw", std::ios::binary);
outFile.write ((char*) buffer, sizeof (WORD) * bufferLength);
outFile.close ();
delete [] buffer;


Share this post


Link to post
Share on other sites