Quite the curious problem with saving surfaces to a file.

Started by
8 comments, last by Roof Top Pew Wee 22 years, 4 months ago
This problem has been troubling me and I have written many posts about it, however, I have narrowed the problem down, but I still don't understand. Here's the code: void savePackage(char* FileName, terrainPackageSurfaces* packageSurfaces) { DDSURFACEDESC2 ddsd; memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ofstream outfile; outfile.open(FileName, ios::app | ios::binary); for(int j = 0; j < 84; j++) { if(packageSurfaces->ifAssigned[j] && packageSurfaces->surfaces[j] != NULL) { packageSurfaces->surfaces[j]->GetSurfaceDesc(&ddsd); // get the description for the width outfile.write( (char*)((void*)packageSurfaces->surfaces[j]) , ddsd.lPitch*ddsd.dwHeight); } } outfile.close(); } A terrainPackageSurfaces object contains 84 LPDIRECTDRAWSURFACE7's. So here's the problem. When I save this, I should be getting rather large files (easily over half a MB and up to 18 MB depending on how many I have loaded). However, the file is usually very small, around 2, 4, 6, or 8 KB. I asked a similar question and found out that if the second argument in the write() function, which happens to be the length in bytes, exceeds the length of the first argument, the result is "unpredictable". So it is likely that I am exceedinging the pointer to the surface. However, I don't see how because I am getting the size directly from the surface each loop and using it to write to the file. My first idea was that maybe I am somehow going over, so I erased ddsd.dwHeight, and replaced it with 50 (this is half of what ddsd.dwHeight is because all of the surfaces are 100 pixels high). I still got the problem. I lowered the number to 30, and again got the problem. I then lowered it to 5, and I finally got a file of appropriate size, but I'm sure that my surfaces are taller than 5 pixels. My second idea was that perhaps the memory on the card is not linear, but I asked around and most people told me that almost all cards have linear memory, especially the popular ones. I have a 32 MB TNT2 RIVA card, so I don't think that could be the problem. This has been stumping me for over 2 weeks now, so if anyone could help, I'd much appreciate it. --Vic-- Edited by - Roof Top Pew Wee on November 23, 2001 1:59:31 PM Edited by - Roof Top Pew Wee on November 23, 2001 2:01:34 PM
Advertisement
ok... can you post the definitions of your data types ? (esp. terrainPackageSurfaces->surfaces );
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
class terrainPackageSurfaces
{public:
int howManyLoaded;
LPDIRECTDRAWSURFACE7 surfaces[84];
bool ifAssigned[84];
DDSURFACEDESC2* ddsd;
terrainPackageSurfaces(){};
terrainPackageSurfaces(DDSURFACEDESC2* DDSD)
{ ddsd = DDSD; // will be used later
for(int i =0; i < 84; i++)
ifAssigned = false;<br> };<br> ~terrainPackageSurfaces()<br> {<br> for(int i = 83; i > -1; i–)<br> if(surfaces)<br> {<br> surfaces->Release();<br> surfaces = NULL;<br> };<br> }<br><br> void makeNew()<br> {<br> howManyLoaded = 0;<br> for(int i = 0; i < 84; i++)<br> {<br> ifAssigned = false;<br> surfaces = NULL;<br> }<br> }<br><br> void fillSurface(int whichToFill, LPDIRECTDRAWSURFACE7 surfaceToUse,<br> RECT* rectToUse = NULL)<br> {<br> surfaces[whichToFill]->Blt(NULL, <br> surfaceToUse, <br> rectToUse, <br> DDBLTFAST_WAIT|DDBLTFAST_NOCOLORKEY,<br> NULL);<br> ifAssigned[whichToFill] = true;<br><br> bool toFill = true;<br> howManyLoaded = 0;<br> for(int i = 0; i < 4; i++)<br> {<br> if(ifAssigned == false)<br> toFill = false;<br> }<br><br> if(toFill)<br> { howManyLoaded++;<br> }<br> else<br> return;<br><br> for(int j = 0; j < 4; j++)<br> {<br> for(int h = 0; h < 20; h++)<br> {<br> if(ifAssigned[4 + h + 20*j] == false)<br> {<br> toFill = false; <br> }// end of if empty<br> }// end of separate surfaces in one texture<br> if(toFill)<br> howManyLoaded++;<br> else<br> return;<br> }// end of each texture<br> }// end of fillSurface function<br><br> LPDIRECTDRAWSURFACE7* assignTexture(int whichToAssign)<br> {<br> if(ifAssigned[whichToAssign])<br> return &surfaces[whichToAssign];<br> else<br> return NULL;<br> }<br>}; // end of terrainPackageSurfaces class<br> <br>I don't think this'll help ya much, because there is also the filling of the surfaces, which is quite lengthy because it involves loading bitmaps, splitting them up and some strange assigning. But the surfaces are filled, and if not, there are tests for them. If they were empty, I would have a file size of 0, because that is all that I'm saving. The only other problem I am thinking of is maybe the terrainPackageSurfaces object gets messed up somewhere because there are about 3 or 4 functions it goes through. to get to this one. But again, I have gone over the code over and over again. I will look again. This post was mainly to see if anyone had any other ideas where I might be having errors, or if there are any evident ones.<br><br>Oh, one more thing. I have seen people have white squares with code in them. It is much neater than putting code here. Can anyone tell me how to do that?<br><br>–Vic–<br><br>Edited by - Roof Top Pew Wee on November 23, 2001 2:34:23 PM<br><br>Edited by - Roof Top Pew Wee on November 23, 2001 2:36:14 PM
LPDIRECTDRAWSURFACE7 objects probably hold a pointer to the surface instead of the surface itself, I''m going to look it up (Need to instal DX first...)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Oh, and you display source code by bracketing source and /source in square brackets ''['' and '']''
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Yeah, LP is long* I believe, but that''s exactly what I want, because I''m using the value in LPDIRECTDRAWSURFACE7 (or the address) as the address of where to look for the memory on the video card. I think this is right. I also checked to see if the surfaces are getting ruined through the different function calls, like if I am messing something up before the function. I moved the save code outside of all of the function calls and tried to write the file with the original object. I know the original object works, because I am blitting from it in my program. I get the same result (a file of about 2.17 kb), so I guess we can assume that the object being passed to the object is correct and the problem is in the function somewhere.

--Vic--
No, what I mean is that LPDIRECTDRAWSURFACE7 itself would be a struct like that

struct DDS7
{
pixel_t* data;
.
.
.
};

which means that when you''re printing the object, you''re not printing the data.

Try using DirectDrawSurface7.Lock to get a pointer to the data.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Genius! I didn''t think of that. I''ll try it and see what happens. Thanks again.

--Vic--
I'm not completely sure, what you exactly want, but if you want
to access the bitmap of a surface you have to call the Lock () method of the surface to get a pointer to the surface data (it id the stored in the DDSURFACEDESC2 structure you have to pass
to Lock ()).

Oops, didn't read all of Fruny's post.

Edited by - VolkerG on November 23, 2001 3:30:18 PM
quote:Original post by Fruny
DirectDrawSurface7.Lock

Fruny, you''re a VB coder?

Yeah, just dumping the pointer would have been writing out a load of COM function pointers, hence it only being small...


Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement