need help with code from a book

Hi guys

I have the Beginning 3D Game Programming book by Tom Miller

In chapter 4 page 62, he says to put code in the OnCreateDevice method in listing 4.2

adding this code causes the program to crash, with the error being Failed to create Direct3D device

public void OnCreateDevice(object sender, DeviceEventArgs e) {
SurfaceDescription desc = e.BackBufferDescription;

ExtendedMaterial[] mtrls;
levelMesh = Mesh.FromFile(MediaPath + "level.x", MeshFlags.Managed, e.Device, out mtrls);
if ((mtrls != null) && (mtrls.Length > 0)) {
levelTextures = new Texture[mtrls.Length];
for (int i = 0; i < mtrls.Length; i++) {
levelTexturesIdea = TextureLoader.FromFile(e.Device, MediaPath + mtrlsIdea.TextureFilename);
}
}
}

The error that it causes seems to be tied to dxmut.cs, because the exception is caught there, around line 2722

try
{
// If the settings dialog exists call its OnCreateDevice() and OnResetDevice()
if (State.Settings != null)
{
State.Settings.OnCreateDevice(device);
State.Settings.OnResetDevice();
}
// Call the dialog resource manager create device method
DialogResourceManager.GetGlobalInstance().OnCreateDevice(device);

// Call the resource cache created function
ResourceCache.GetGlobalInstance().OnCreateDevice(device);

// Call the applications device created callback if it's set
State.IsInsideDeviceCallback = true;
if (DeviceCreated != null)
{
DeviceCreated(this, new DeviceEventArgs(device, State.BackBufferSurfaceDesc));
}
State.IsInsideDeviceCallback = false;
State.AreDeviceObjectsCreated = true;
}
catch (MediaNotFoundException e)
{
DisplayErrorMessage(e);
success = false;
throw;
}
catch (Exception e)
{
DisplayErrorMessage(new CreatingDeviceException(e));
success = false;
throw;
}


Please help me! I'm having the worst time with my independent study this semester. if it wasn't implied by the code, this is using c# and the october 2004 release of the directx sdk


Answer this question

need help with code from a book

  • Remy Baek

    Does the book have any full example projects you can run I can't help but feel that if you've copied this code right, some other previous step must be missing. Assuming that there hasn't been a change to DX since the book came out. You should also check if there are errata on any website.

    I see it came with a CD, is there a full project on there you can try to build and run that uses this code



  • INeedHelp2

    sorry about posting the code like that (please Tom don't sue me for it), the only reason i posted it is because i wrote it following examples from the code so i didn't think it'd be a huge deal because it isn't the full game code. anyway, i apologize for posting it, and i'll remove the zips from my webspace. sorry for any trouble i caused.

    i'll try those suggestions zman. i'll have to get to them after work today, and i'll reply if its resolved or not. thanks for the help

  • Mary Hinge

    The 1st thing to do is to go into Debug/Exceptions and change 'When the exception is thrown' to 'break into the debugger'. The framework from the book (and the one in the SDK) have a lot of try catch statements in them so you *never* see the actual line the error happened on.

    So once you have done that and you run the code you will see that its trying to load the media file. From what I can make out you have hardcoded the location of the media files in app.config to be c:\media\blockers. My guess is that its not finding the media files there because that's not the default place the CD copies them to I copied the media files from the CD into c:\media\blockers and the code works. Well I get a black screen but no exception.

    You should also be very careful posting copyright files without permission. I have no idea if Tom is OK with the code and media being posted. My updates on The ZBuffer not only have permission, but I only post the changed files and no media. I've edited your posts to remove the links. You should probably remove the files.



  • Bratomat

    I had this problem, this forum solved it, indirectly. My problem was in the App.config file, where i missed off the trailing backslash in the media path. Took me hours to find this forum... Anyway, If anyone else wonders, device from the book should be e.Device (or put in a using block suggested on other forums) and if it compiles the exception is probably a missing file. Now i can see my program!! (well Tom's program) =)

    MMatt


  • DQM

    i hope you're not pissed that i posted in both places.

    ok, so here's the deal with his code.  he's trying to show us how to render a mesh with directx from a file.  if you look at his regular code (like the stuff hosted at zbuffer, which i have downloaded just so you know), he implements this differently than he does in the example, because in the full code he has other classes created already for rendering screens and menus and such.  the actual code is executed elsewhere in the program. 

    so that you can see it, i uploaded it to my webspace for download

    [removed link to copyright code]

    if you'd be so kind, download it and see for yourself what's happening.  thanks for your time guys


  • Landale

    {"Error in the application." }

    System.Object {Microsoft.DirectX.Direct3D.Direct3DXException} System.Object
    _className null string
    _COMPlusExceptionCode 0xe0434f4d int
    _exceptionMethod <undefined value> System.Reflection.MethodBase
    _exceptionMethodString null string
    _helpURL null string
    _HResult 0x88760389 int
    _innerException { } System.Exception
    _message "Error in the application." string
    _remoteStackIndex 0x0 int
    _remoteStackTraceString null string
    _source null string
    + _stackTrace {System.Array} System.Object
    _stackTraceString null string
    _xcode 0xe0434f4d int
    _xptrs 0x0 int
    HelpLink null string
    HResult 0x88760389 int
    InnerException { } System.Exception
    Message "Error in the application." string
    Source "Microsoft.DirectX.Direct3DX" string
    StackTrace " at Microsoft.DirectX.Direct3D.Mesh.FromFile(String filename, MeshFlags options, Device device, GraphicsStream& adjacency, ExtendedMaterial[]& materials, EffectInstance[]& effects)\r\n at Microsoft.DirectX.Direct3D.Mesh.FromFile(String filename, MeshFlags options, Device device, ExtendedMaterial[]& materials)\r\n at GameEngine.CreateGraphic(Object sender, DeviceEventArgs e) in c:\\test\\chap3\\gameengine.cs:line 89\r\n at GameEngine.OnCreateDevice(Object sender, DeviceEventArgs e) in c:\\test\\chap3\\gameengine.cs:line 83\r\n at Microsoft.Samples.DirectX.UtilityToolkit.Framework.Initialize3DEnvironment() in c:\\test\\chap3\\framework\\dxmut.cs:line 2722" string
    + TargetSite {System.Reflection.RuntimeMethodInfo} System.Reflection.MethodBase


    let me know if you figure anything out from this, thanks for replying

  • FabioDeSantis

    is this forum not functional in firefox   clicking edit completely blanks out my post

    anyway, the code link is wrong, here it is:

    [link removed - copyright code]

  • vinaypugalia

    I see the CreatingDeviceException wraps the actual exception thrown. If you change put a breakpoint in this line :

    DisplayErrorMessage(new CreatingDeviceException(e));

    and then examine e, what is the error actuall being thrown



  • SteffenRuland

    As mentioned in your duplicate post on gamedev.net. You can download/get the updated code from the ZBuffer. This should give you a boost in the right direction and should provide you with a solid base to work from.

    I hope this helps.


  • Mohammad Iqubal MSFT

    yeah, seems to work just fine now. i guess i had the path wrong, but i copied and pasted it from the explorer bar i thought. oh well, whatever, my mistake. thanks so much for pulling my head out of my butt! :P

  • need help with code from a book