# Having Problems with "The Tower" from Programming Role Playing Games with DirectX 2Ed

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

## Recommended Posts

I tried running this game to get a feel for what the end result of the book would be and I've come across a very frustrating (for me anyway) bug. Everytime I try to start a new game, or quit the game, I get an unhandled exception at 0x7c901230. Since it didn't have the source code readily available from the executable. I decided to put all the info into a project and compile it from there in order to see where the problem is coming from. So I run it in Debug mode and come up to this function
BOOL cVertexBuffer::Unlock()
{
if(m_pVB == NULL)
return FALSE;

if(FAILED(m_pVB->Unlock()))
return FALSE;

m_Locked = FALSE;

return TRUE;
}

where the bold line is the culprit for this error (or at least where the code breaks) My question is, has anyone come across this problem before? And barring that fact, Can anyone help me fix it? I have the most recent DirectX version and my video card drivers are all up to date.

##### Share on other sites
1) your m_pVB object may have been released before you call Unlock().
2) your m_pVB object may not point to the correct object

You should try to find where you release your object, and when you unlock it.

Regards,

##### Share on other sites
Quote:
 Original post by Emmanuel Deloget1) your m_pVB object may have been released before you call Unlock().2) your m_pVB object may not point to the correct objectYou should try to find where you release your object, and when you unlock it. Regards,

Looking in at the code, it's calling unlock during a call to MenuTexture.Free(). Which is handled during the cApp::MenuFrame(...)

It appears to be pointing to the correct type of object (IDirect3DResource9)

The call stack looks like this
	Game.exe!cVertexBuffer::Unlock()  Line 2941	C++ 	Game.exe!cVertexBuffer::Free()  Line 2877	C++ 	Game.exe!cApp::MenuFrame(void * Ptr=0x000edc58, long Purpose=2)  Line 806	C++ 	Game.exe!cStateManager::Pop(void * DataPtr=0x000edc58)  Line 218 + 0xf	C++ 	Game.exe!cApp::MenuFrame(void * Ptr=0x000edc58, long Purpose=3)  Line 843	C++ 	Game.exe!cStateManager::Process(void * DataPtr=0x000edc58)  Line 243 + 0xf	C++ 	Game.exe!cApp::Frame()  Line 244	C++ 	Game.exe!cApplication::Run()  Line 93 + 0xd	C++ 	Game.exe!WinMain(HINSTANCE__ * hInst=0x00400000, HINSTANCE__ * hPrev=0x00000000, char * szCmdLine=0x00141f20, int nCmdShow=1)  Line 628 + 0xb	C++ 	Game.exe!WinMainCRTStartup()  Line 251 + 0x30	C 	kernel32.dll!7c816d4f() 	 	kernel32.dll!7c8399f3()

Where Free() calls Unlock.
MenuFrame creates and frees the VertexBuffer (among other things)

Going backwards through it, I'm not finding where the m_pVB variable would have been lost incorrectly.

Could the problem be that the m_pVB was not created correctly?

##### Share on other sites
Ok, I've found what appears the be the culprit.

The cVertexBuffer::Set Function.

The code logic seems to follow this path

When initializing the menu, it creates a VertexBuffer then sets the Vertices (followed by loading of texture, selection menu, Font and Camera setup)

Which looks to run fine.

Then when you chose an option on the screen (in this case New Game, though Quit and I'd assume Load game would give the same error), It prepares to shutdown the Main Menu (which makes sense). In doing so it goes to Free the Vertex Buffer.

Now the problem appears to be that when you Set the vertexBuffer in the initialization step. You've already freed the Vertex buffer, because this is what the set fuction does.

BOOL cVertexBuffer::Set(unsigned long FirstVertex, unsigned long NumVertices, void *VertexList){  if(m_Graphics == NULL || VertexList == NULL || m_pVB == NULL)    return FALSE;  if(m_Graphics->GetDeviceCOM() == NULL)    return FALSE;  // Lock the vertex buffer  if(Lock(FirstVertex, NumVertices) == FALSE)    return FALSE;  // Copy vertices to vertex buffer  memcpy(m_Ptr, VertexList, NumVertices * m_VertexSize);  // Unlock vertex buffer  if(Unlock() == FALSE)    return FALSE;  return TRUE;}

So inherent in the Set function is a call to lock and then unlock the VB. So when Unlock() is called later in the shutdown phase of the Main Menu it crashes.

So at least I know now what the problem is. I just need to figure out how to fix it. Though if anybody has any thoughts on it I'd appreciate the help.

##### Share on other sites
I am also using this book and I get numerous errors trying to compile the source in most of the chapters. (Most notibly the game core)
I wouldn't recomend it to anyone who can't rework the code..... Just sayin' your not the only one.

##### Share on other sites
ok that's fixed.

Now I have a new problem (sigh).

The CheckIntersect function isn't working and crashing with another access violation.

It's probably something similar. But it's still very frustrating.

##### Share on other sites
Quote:
 Original post by PlasmaroboI am also using this book and I get numerous errors trying to compile the source in most of the chapters. (Most notibly the game core)I wouldn't recomend it to anyone who can't rework the code..... Just sayin' your not the only one.

That's good (yet at the same time bad) to know.

Though should there be an addendum to this book if the code has that many errors?

##### Share on other sites
Quote:
Original post by kontrol
Quote:
 Original post by PlasmaroboI am also using this book and I get numerous errors trying to compile the source in most of the chapters. (Most notibly the game core)I wouldn't recomend it to anyone who can't rework the code..... Just sayin' your not the only one.

That's good (yet at the same time bad) to know.

Though should there be an addendum to this book if the code has that many errors?

I remember I saw a fairly recent code update on the book's web site (see page 814 for the URL). Maybe it correct your own problems.

Regards,

##### Share on other sites
His website is only about the 1st edition, there is nothing about the 2nd edition.
Or do you have another url than http://home.att.net/~rpgbook/ on page 814?

##### Share on other sites
Sadly, no. I can't check the site right now (stupid web security stuff here), so I'll give it a try in a few hours.

##### Share on other sites
Quote:
 Original post by Emmanuel DelogetSadly, no. I can't check the site right now (stupid web security stuff here), so I'll give it a try in a few hours.

It's only for the first edition (DirectX 8) not the second edition (DirectX 9)

##### Share on other sites
So there's no place where we can download a newer version of the source code or a patch?

##### Share on other sites
Hello,
I just wanted to create a thread with a similar topic. My problem is that I've bought the 2nd edition used (over ebay), but it seems that my CD is damaged, none of my computers is able to read it.
Or would someone of you be so kind to email it me (or upload it somewhere and send me the url)?

##### Share on other sites
Quote:
 Original post by extHello,I just wanted to create a thread with a similar topic. My problem is that I've bought the 2nd edition used (over ebay), but it seems that my CD is damaged, none of my computers is able to read it.Is there really no way to download the sourcecode somewhere?Or would someone of you be so kind to email it me (or upload it somewhere and send me the url)?my email addy is ext.sascha googlemail com

Unfortunately, the code, as-is out of the book, needs reworking. It doesn't compile immediately out of the box. I had to make changes just to get it to compile. Most of the current problems I'm having don't seem to be related to my code changes, but I'm not sure. Other than that, I don't know of any place online that has updated source code.

##### Share on other sites
Quote:
Original post by kontrol
Quote:
 Original post by extHello,I just wanted to create a thread with a similar topic. My problem is that I've bought the 2nd edition used (over ebay), but it seems that my CD is damaged, none of my computers is able to read it.Is there really no way to download the sourcecode somewhere?Or would someone of you be so kind to email it me (or upload it somewhere and send me the url)?my email addy is ext.sascha googlemail com

Unfortunately, the code, as-is out of the book, needs reworking. It doesn't compile immediately out of the box. I had to make changes just to get it to compile. Most of the current problems I'm having don't seem to be related to my code changes, but I'm not sure. Other than that, I don't know of any place online that has updated source code.

Would it be possible for you to email me your sources?
At the moment I'm happy with any version of the sources. Fixing it is annoying but not impossible for me.
Thanks.

##### Share on other sites
I'm having the same problems with the book and the fact that it has all those errors discourages me from reading the rest of the book. I wonder why he hasn't created a site for errata in his new book.

##### Share on other sites
Hey i got the same book and the errors keep coming, i got another book ("Programming a multiplayer fps in directx") and it compiles but crashes when you try to run the game. The only way i got through jim adams book was to rewrite everything and just use the ideas instead of the code.

##### Share on other sites
Quote:
Original post by ext
Quote:
Original post by kontrol
Quote:
 Original post by extHello,I just wanted to create a thread with a similar topic. My problem is that I've bought the 2nd edition used (over ebay), but it seems that my CD is damaged, none of my computers is able to read it.Is there really no way to download the sourcecode somewhere?Or would someone of you be so kind to email it me (or upload it somewhere and send me the url)?my email addy is ext.sascha googlemail com

Unfortunately, the code, as-is out of the book, needs reworking. It doesn't compile immediately out of the box. I had to make changes just to get it to compile. Most of the current problems I'm having don't seem to be related to my code changes, but I'm not sure. Other than that, I don't know of any place online that has updated source code.

Would it be possible for you to email me your sources?
At the moment I'm happy with any version of the sources. Fixing it is annoying but not impossible for me.
Thanks.

I'll try and track them down for you today.

I'm wondering if we all should form some sort of collaborative effort in order to address some of these code issues we all keep coming across.

##### Share on other sites
Ok, update on the problem with the Intersect function.

Again it's not the actual Intersect function that's the problem. It's the fact that the initial scene doesn't get loaded correctly.

So I'm putting my Load function out there to see if maybe I did something incorrectly

*note - this is one of the functions I had to change because in using DirectX 9, the parsing code had to be changed up a bit.

These types
IDirectXFile
IDirectXFileEnumObject
IDirectXFileData

ID3DXFile
ID3DXFileEnumObject
ID3DXFileData

and some other functions were altered because of that.

So here's the code I have, hope it makes some sort of sense. The commented (SUCCEEDED(...))section is what the book code had originally.
BOOL cMesh::Load(cGraphics *Graphics, char *Filename, char *TexturePath){  ID3DXFile           *pDXFile = NULL;  ID3DXFileEnumObject *pDXEnum = NULL;  ID3DXFileData       *pDXData = NULL;  sFrame                 *TempFrame, *FramePtr;  sMesh                  *Mesh;  D3DXMATRIX					*Matrix;	  DWORD						i;  // Free prior mesh object data  Free();  // Error checking  if((m_Graphics = Graphics) == NULL || Filename == NULL)    return FALSE;  // Create the file object  if(FAILED(D3DXFileCreate(&pDXFile)))    return FALSE;  // Register the templates  if(FAILED(pDXFile->RegisterTemplates((void*)D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES))) {    pDXFile->Release();    return FALSE;  }  // Create an enumeration object  if(FAILED(pDXFile->CreateEnumObject((void*)Filename, DXFILELOAD_FROMFILE, &pDXEnum))) {    pDXFile->Release();    return FALSE;  }  // Create a temporary frame  TempFrame = new sFrame();  DWORD nChildren = 0;  pDXEnum->GetChildren(&nChildren);  // Loop through all objects looking for the frames and meshes	for (DWORD nChild = 1; nChild<nChildren; ++nChild)	{		pDXEnum->GetChild(nChild,&pDXData);		ParseXFileData(pDXData, TempFrame, TexturePath);		ReleaseCOM(pDXData);	}/*  while(SUCCEEDED(pDXEnum->GetNextDataObject(&pDXData))) {    ParseXFileData(pDXData, TempFrame, TexturePath);    ReleaseCOM(pDXData);  }*/  // Release used COM objects  ReleaseCOM(pDXEnum);  ReleaseCOM(pDXFile);  // See if we should keep the tempframe as root  if(TempFrame->m_MeshList != NULL) {    m_Frames = TempFrame;    m_Frames->m_Name = new char[7];    strcpy(m_Frames->m_Name, "%ROOT%");  } else {    // Assign the root frame and release temporary frame    m_Frames = TempFrame->m_Child;    FramePtr = m_Frames;    while(FramePtr != NULL) {      FramePtr->m_Parent = NULL;      FramePtr = FramePtr->m_Sibling;    }    TempFrame->m_Child = NULL;    delete TempFrame;  }  // Match frames to bones (for matrices)  MapFramesToBones(m_Frames);  	//Generate default mesh (if contains a skin mesh)	if((Mesh = m_Meshes) != NULL) {		while(Mesh != NULL) {			if(Mesh->m_SkinInfo !=NULL) {				Matrix = new D3DXMATRIX[Mesh->m_NumBones];				for(i=0; i<Mesh->m_NumBones;i++)					D3DXMatrixIdentity(&Matrix);				Mesh->m_SkinInfo->UpdateSkinnedMesh(Matrix, NULL, Mesh->m_Mesh, Mesh->m_SkinInfo);								delete [] Matrix;			}			Mesh = Mesh->m_Next;		}	}  // Calculate bounding box and sphere  if((Mesh = m_Meshes) != NULL) {    while(Mesh != NULL) {      m_Min.x = min(m_Min.x, Mesh->m_Min.x);      m_Min.y = min(m_Min.y, Mesh->m_Min.y);      m_Min.z = min(m_Min.z, Mesh->m_Min.z);      m_Max.x = max(m_Max.x, Mesh->m_Max.x);      m_Max.y = max(m_Max.y, Mesh->m_Max.y);      m_Max.z = max(m_Max.z, Mesh->m_Max.z);      m_Radius = max(m_Radius, Mesh->m_Radius);      Mesh = Mesh->m_Next;    }  }  return TRUE;}

##### Share on other sites
I think you misunderstood me (a little), what I need is the source on the companion cd, because my cd is damaged. So I don't have any version/copy of the source code.

##### Share on other sites
hmm, this is interesting, i actually got this book a few months ago and was going to read it over the summer

hearing that the code is wrong makes me sad; is it little things or big things (like the theory is right but when you actually code the thing it fails? or something easier?)

thanks for the update, not sure what i'll do now :/

##### Share on other sites
Hello,
I need the sources, too. Can someone of you upload the book's sources, please?

##### Share on other sites
Stop asking for the code. Such kind of behavior is not endorsed by gamedev.net.

Sharing the code of this book is illegal - the code license states this. The author is a moderator emeritus on these board, and he specifically stated that the code should not be shared. In fact, he don't even want you to post large portions of the code on the site. Unfortunately, he hasn't been seen for a while. It doesn't mean that what he stated should be forgotten.

If you have a problem with your CD, ask a new CD from the book publicher. Since you have the book, you should be able to find the correct address.

If you don't have the book, don't ask for the code. Just buy the book.

Frankly, if you believe this book is a good one, support the author, don't steal his work.

Regards,

##### Share on other sites
I second that I find that rather disgusting, even if it was a piece damaged over ebay, give the guy a bad review if you must, but dont ask for anything illegal.

##### Share on other sites
Understood on the code sharing thing. Though does anybody have any comments on the way I reworked the code. Am I going down the wrong path by thinking I may have done something incorrectly with the modifications?