Sign in to follow this  
DragonGeo2

VS2005 + DX9(Aug'06) Problem

Recommended Posts

Okay, so here's the story. I recently (legally) acquired a copy of Visual Studio 2005. I thought that it would be a good idea if I updated my DirectX9 SDK while I was at it, so I did (now I have the August 2006 SDK). I then, opening VS2005 for the first time, with the new SDK installed, opened up a previous project I had built with VS2003 and the October 2005 build of the DXSDK. I hit compile on that program, and none to my amazement, it didn't work. I manually added the d3d9.lib and d3dx9.lib libraries to the "Include Additional Libraries" section in the Project Settings menu, and attempted another recompile. That didn't work either. I then recalled all the bad things I had heard about VS2005 and Unicode / ASCII support, so I disabled certain unicode compile and linking options, and got an old project to build, and run fine. The problem I have is thus: I recently made a new project (source below) and copied-and-pasted most of its source from the source of that project I had made (which works fine on VS2005 with the Aug SDK) and compiled it and set the compiler and linker options exactly the same, and this new project will compile fine, but it will receive a run-time error citing that the LPDIRECT3D9->CreateDevice() method was failing and the program will close. This is quite odd, I have two near-identical programs, one of which works, and another of which does not. Can someone help me figure this out here? I have included the source to the new (and not working) project below, just in case you can spot any critical and silly errors I may have made in copying-and-pasting, and as you can see, it doesn't do much more than set up the Windows section of the program, and initialize DirectX (which my older program had no problem doing). Anyway, thanks for help in advance.
Quote:
#include <stdio.h> #include <windows.h> #include <d3d9.h> #include <d3dx9.h> #include <mmsystem.h> #include <iostream> #include <cstdlib> #include <time.h> #include <string> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd); LRESULT CALLBACK WndProc(HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam); bool InitializeDirect3D(HWND hwnd, bool fullscreen); LPDIRECT3D9 Direct3D_Object = NULL; LPDIRECT3DDEVICE9 D3D_Device = NULL; unsigned int g_Width = 1280; unsigned int g_Height = 1024; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { HWND hwnd; MSG msg; // Message object. WNDCLASSEX windowClass; // Window class object. bool done = false; // True then exit. // This is the Window class. windowClass.cbSize = sizeof(WNDCLASSEX); // size of the WNDCLASSEX structure. windowClass.style = CS_HREDRAW | CS_VREDRAW; // style of the window. windowClass.lpfnWndProc = WndProc; // Address to the windows procedure. windowClass.cbClsExtra = 0; // Extra class information. windowClass.cbWndExtra = 0; // Extra window information. windowClass.hInstance = hInstance; // Handle of application Instance. windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);// Window Icon. windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load mouse cursor. windowClass.hbrBackground = NULL; // Background color. windowClass.lpszMenuName = NULL; // Menu. windowClass.lpszClassName = "TomNoiseDemo"; // Name of the window class. windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);// Minimized window icon. // You must register you class with Windows. if(!RegisterClassEx(&windowClass)) return 0; // Create the window. hwnd = CreateWindowEx(NULL, // The extended style. "TomNoiseDemo", // Window class. "TomNoiseDemo", // Window name. WS_OVERLAPPEDWINDOW | WS_VISIBLE | // Window style. WS_SYSMENU |WS_CLIPCHILDREN | // Window style. WS_CLIPSIBLINGS, // Window style. 0, 0, // X, Y coords. g_Width, g_Height, // Window size. NULL, // Handle to parent window. NULL, // Menu. hInstance, // Handle to app instance. NULL); // Pointer to window. // If there was an error with creating the window, then close the program. if(!hwnd) return 0; ShowWindow(hwnd, SW_SHOW); // Show the window. UpdateWindow(hwnd); // Update its display. done = false; // false = run program, true means stop. // Initialize Direct3D. If fails then we don't want to run the application. if(!InitializeDirect3D(hwnd, true)) done = true; // Application loop. while(!done) { if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { // If a quit message is received then stop rendering and quit the app. if(msg.message == WM_QUIT) { done = true; } TranslateMessage(&msg); // Translate any messages. DispatchMessage(&msg); // Dispatch any messages. } else { // RenderScene(); // Render a frame. } } // Here we unregister the window class with the OS. UnregisterClass("TomNoiseDemo", windowClass.hInstance); return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam) { int width, height; switch(message) { case WM_KEYUP: switch(wParam) { // Close the app if the user presses escape. case VK_ESCAPE: PostQuitMessage(0); break; } break; case WM_SIZE: // re-size message. height = HIWORD(lParam); // This gets the height of the window. width = LOWORD(lParam); // This gets the width of the window. if(height==0) // we don't want it to be possible for a { // height of 0. If it is 0 me make it 1. height = 1; } if(width==0) { width = 1; } g_Width = width; g_Height = height; return 0; break; case WM_CLOSE: case WM_DESTROY: PostQuitMessage(0); break; default: break; } return DefWindowProc( hwnd, message, wParam, lParam ); } bool InitializeDirect3D(HWND hwnd, bool fullscreen) { // This object will allow us to set the display mode of the screen. D3DDISPLAYMODE DisplayMode; // This will allow us to set the parameters of the screen. D3DPRESENT_PARAMETERS Present_Parameters; // This is used to get the capabilities of the hardware. D3DCAPS9 D3DCaps; // It is always a good idea to clear out memory in object although not necessary. ZeroMemory(&Present_Parameters, sizeof(Present_Parameters)); // Create the Direct3D object to get everything started. Direct3D_Object = Direct3DCreate9(D3D_SDK_VERSION); // Error checking. Make sure that it was successful. if(Direct3D_Object == NULL) { MessageBox(NULL, "Error, couldn't initialize DirectX!?!", "DXInit_Error!", MB_OK); return false; } // This function will get the display mode of the device and place it in DisplayMode. if(FAILED(Direct3D_Object->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode))) { MessageBox(NULL, "Error setting the display mode.", "DXDispModeSet_Error!", MB_OK); return false; } // Get the capabilities of the hardware. if(FAILED(Direct3D_Object->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3DCaps))) { return false; } Present_Parameters.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES; // Test which is supported, hardware or software vertex processing. DWORD VertexProcessing = 0; if(D3DCaps.VertexProcessingCaps != 0) VertexProcessing |= D3DCREATE_HARDWARE_VERTEXPROCESSING; else VertexProcessing |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; // Here we are setting the applications parameters... if(fullscreen) { Present_Parameters.Windowed = FALSE; // Window mode (fullscreen). Present_Parameters.BackBufferWidth = g_Width; Present_Parameters.BackBufferHeight = g_Height; } else Present_Parameters.Windowed = TRUE; // Window mode (not fullscreen). Present_Parameters.SwapEffect = D3DSWAPEFFECT_DISCARD; // Dealing with animation (see doc). Present_Parameters.BackBufferFormat = DisplayMode.Format;// Render to the area of the screen. Present_Parameters.BackBufferCount = 1; // Number of back buffers. Present_Parameters.EnableAutoDepthStencil = TRUE; // Check documentation. Present_Parameters.AutoDepthStencilFormat = D3DFMT_D16; // Check documentation. // Now we must create the rendering device. if(FAILED(Direct3D_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, VertexProcessing, &Present_Parameters, &D3D_Device))) { MessageBox(NULL, "CreateDevice() failed! Make sure you have DirectX 9.", "CreateDeviceFail_Error!", MB_OK); return false; } // One last check to be sure. if(D3D_Device == NULL) { MessageBox(NULL, "D3D_Device is equal to NULL!?!", "D3DDeviceIsNull_Error!", MB_OK); return false; } // SetRenderState() will set a parameter for the rendering. Here we turn off lighting. D3D_Device->SetRenderState(D3DRS_LIGHTING, FALSE); // Disable culling. This will render the front and back of the triangles. D3D_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); // Enable depth testing. D3D_Device->SetRenderState(D3DRS_ZENABLE, TRUE); D3D_Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC); D3D_Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC); // This set of commands enables alpha blending - which looks REALLY cool! D3D_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); // Turn off Z write, but still perform Z test D3D_Device->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE); return true; }

Share this post


Link to post
Share on other sites
Rather than posting your source in quote tags, you may wish to try [ c o d e ] or [ s o u r c e ] tags... Remove the spaces :-p It makes it clearer to read.

I am curious about this line
Present_Parameters.BackBufferFormat = DisplayMode.Format;// Render to the area of the screen.

Try checking to see what this value is

Also, I don't know if this will help but I usually stick my hwnd into the present parameters too.

I think the member name is DeviceWindow but I am sure you'll know what I mean.
Present_Parameters.DeviceWindow = hwnd;

Hope this helps you out.

Share this post


Link to post
Share on other sites
It works when initializing in windowed mode. You're not presenting anything, so the window won't be visible though.
Crashing in fullscreen mode most likely depend on some erroneous setting in the present parameters.

Never run an app in fullscreen if you want to debug it. Unless you're remote debugging, or running with several screens, of course.

Share this post


Link to post
Share on other sites
Quote:
Original post by tok_junior
It works when initializing in windowed mode. You're not presenting anything, so the window won't be visible though.
Crashing in fullscreen mode most likely depend on some erroneous setting in the present parameters.

Never run an app in fullscreen if you want to debug it. Unless you're remote debugging, or running with several screens, of course.


This fixed my problem - THANK YOU!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this