Jump to content
  • Advertisement
Sign in to follow this  
Prads

Small problem with my code. Help!

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I have wrote this code to generate floor for my game. Here's the code:


void d3dLoadGraphics::gPlainTerrain(IDirect3DVertexBuffer9 *vBuff, IDirect3DIndexBuffer9 *iBuff,
unsigned short int width, unsigned short int depth) {
TDVertices *roadV;

try {
roadV = new TDVertices [width * depth];
} catch (std::exception& e) {
MessageBox(NULL, e.what(), "Error", MB_ICONERROR);
}

unsigned int rIndex = 0;
bool u = 0, v = 0;
for(unsigned short int z = 0; z < depth; z++) {
for(unsigned short int x = 0; x < width; x++) {
roadV[rIndex].x = (float(x) * 15) - (float(width) * 15 / 2);
roadV[rIndex].y = 0;
roadV[rIndex].z = (float(z) * (-15)) + (float(width) * 15 / 2);
roadV[rIndex].nx = 0;
roadV[rIndex].ny = 1;
roadV[rIndex].nz = 0;
roadV[rIndex].u = u;
roadV[rIndex].v = v;
(u == 0) ? u = 1 : u = 0;
rIndex++;
}
u = 0;
(v == 0) ? v = 1 : v = 0;
}

void *pVData;
vBuff->Lock(0, 0, &pVData, 0);
memcpy(pVData, roadV, sizeof(roadV));
vBuff->Unlock();

delPointer<TDVertices*>(roadV);

rIndex = 0;
WORD *pIData;
iBuff->Lock(0, 0, (void**)&pIData, 0);

for(unsigned short int z = 0; z < depth - 1; z++) {
for(unsigned short int x = 0; x < width - 1; x++) {
pIData[rIndex] = (z * width) + x;
pIData[rIndex + 1] = pIData[rIndex] + 1;
pIData[rIndex + 2] = (z + 1) * width + x + 1;
pIData[rIndex + 3] = pIData[rIndex + 2];
pIData[rIndex + 4] = pIData[rIndex + 2] - 1;
pIData[rIndex + 5] = pIData[rIndex];
rIndex += 6;
}
}
iBuff->Unlock();
}



Problem is with dynamic allocation of TDVertices structure. If I give say,


TDVertices roadV[900];



then the code works perfectly. But I need to dynamically allocate the struct. It won't give any exception either.
Isn't 'int a[100]' same as 'int *roadV = new a[10 * 10]'? If yes, then why is my code not working?
Can anyone help me please? Thanks!!!

Share this post


Link to post
Share on other sites
Advertisement
I don't know if this is what you are looking for, but let me show a basic example of dynamic array allocation and see if you can make it work for what you do.

int* a = NULL;
int x = 5; //or user input, or whatever number is changed to at the time of execution
a = new int[x];

then you can use 'a' like an array

a[0] = 5;
a[1] = 2;

etc..

since it's now an int pointer, make sure to delete it when you're done.

Share this post


Link to post
Share on other sites
Is there any reason why you wouldn't use a standard link-list based data type like a c++ vector? That would be much easier to deal with.

Unless this is something that happens on every frame (since vectors will have more overhead than an array will I assume). If this only happens occasionally, I don't think it would hurt...

Share this post


Link to post
Share on other sites
Delete the try-catch statements. Initialize roadV to NULL. Set a breakpoint at the new TDVertices[..] line. Run your app and, when you get the debug break, check the value of width and depth (they need to be non-zero) before you execute the "new" line. If width and depth are satisfactory, proceed to the next line and check that the value of roadV has changed.

If I'm remember correctly, you can't catch a "new" exception in any case.

TDVertices *roadV = NULL;
roadV = new TDVertices[width*depth]; // set breakpoint at this line and check width/depth.

Quote:
Nope no error and no exceptions either. It just won't create the array.

Something must be happening or you'd get an error as soon as you set a roadV[..] value. That sort of indicates width and depth are 0.

Share this post


Link to post
Share on other sites
I just tried this with my code:


TowerLightning* test;

unsigned short int width = 50;
unsigned short int height = 50;

test = new TowerLightning[width*height];

test[0].x = 500;
test[100].x = 100;

GS::wout << test[0].x << endl; //outputs 500 to a console window I use for debugging

GS::wout << test[100].x << endl; //outputs 100


and it works fine for me.. I even tried it with your "try and catch" statement and it worked fine.

only thing I can think of... is your TDVertices designed to only use a parametrized constructor? or will the default constructor you are using work fine?

Share this post


Link to post
Share on other sites
Buckeye: Nope, value of width and depth were both non-zero. About exception, exception of type bad_alloc is thrown when the allocation fails. Because bad_alloc is derived from the standard base class exception, we can handle that same exception by catching references to the exception class.

Ok, I rewrote the thing and it works now:


void d3dLoadGraphics::gPlainTerrain(IDirect3DVertexBuffer9 *vBuff, IDirect3DIndexBuffer9 *iBuff,
unsigned short int width, unsigned short int depth) {

unsigned int rIndex = 0;
bool u = 0, v = 0;
TDVertices* roadV;
vBuff->Lock(0, 0, (void**)&roadV, 0);
for(unsigned short int z = 0; z < depth; z++) {
for(unsigned short int x = 0; x < width; x++) {
roadV[rIndex].x = (float(x) * 15) - (float(width) * 15 / 2);
roadV[rIndex].y = 0;
roadV[rIndex].z = (float(z) * (-15)) + (float(width) * 15 / 2);
roadV[rIndex].nx = 0;
roadV[rIndex].ny = 1;
roadV[rIndex].nz = 0;
roadV[rIndex].u = u;
roadV[rIndex].v = v;
(u == 0) ? u = 1 : u = 0;
rIndex++;
}
u = 0;
(v == 0) ? v = 1 : v = 0;
}
vBuff->Unlock();

rIndex = 0;
WORD *pIData;
iBuff->Lock(0, 0, (void**)&pIData, 0);

for(unsigned short int z = 0; z < depth - 1; z++) {
for(unsigned short int x = 0; x < width - 1; x++) {
pIData[rIndex] = (z * width) + x;
pIData[rIndex + 1] = pIData[rIndex] + 1;
pIData[rIndex + 2] = (z + 1) * width + x + 1;
pIData[rIndex + 3] = pIData[rIndex + 2];
pIData[rIndex + 4] = pIData[rIndex + 2] - 1;
pIData[rIndex + 5] = pIData[rIndex];
rIndex += 6;
}
}
iBuff->Unlock();
}



Seems like for some "mysterious" reason, memory allocation wasn't working. ???
Thanks GhostFromTexas and Buckeye!!!

Share this post


Link to post
Share on other sites
Glad you got it fixed.

In your original post, I did notice:

memcpy(pVData, roadV, sizeof(roadV));

roadV is a pointer and sizeof(roadv)==4, the size of the variable, not the size of the allocation.

That line should've been:

memcpy(pVData, roadV, width*depth*sizeof(TDVertices));

Might that have been the original problem? (that's a little late now, huh?)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!