Sign in to follow this  

Untitled

This topic is 3039 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

Hi i'm having problems compiling this file. Here's the source code the header:
// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>


// include the Direct3D Library file
#ifdef _DEBUG
#   pragma comment(lib, "d3dx9d.lib")
#else
#   pragma comment(lib, "d3dx9.lib")
#endif
#pragma comment(lib, "d3d9.lib")



// global declarations
LPDIRECT3D9			d3d;    // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9	d3ddev;    // the pointer to the device class
LPDIRECT3DSURFACE9	surface;
LPDIRECT3DSURFACE9	backbuffer;

#define SCREEN_WIDTH	800
#define SCREEN_HEIGHT	600

#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

The main cpp file:
#include "BSoD.h"

void initD3D(HWND hWnd);    // sets up and initializes Direct3D
void render_frame(void);    // renders a single frame
void cleanD3D(void);    // closes Direct3D and releases memory

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;
	MSG msg;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "WindowClass";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          "WindowClass",
                          "BSoD",
                          WS_POPUP | WS_EX_TOPMOST | WS_VISIBLE,
                          0, 0,
                          SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D
    initD3D(hWnd);

	D3DXLoadSurfaceFromFile(surface,
							NULL,
							NULL,
							"BSoD.bmp",
							NULL,
							D3DX_DEFAULT,
							0,
							NULL);


    // enter the main loop:

    while(TRUE)
    {
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if(msg.message == WM_QUIT)
            break;

		render_frame();
		if(KEY_DOWN(VK_ESCAPE))
			PostMessage(hWnd, WM_DESTROY, 0, 0);
    }

    // clean up DirectX and COM
    cleanD3D();

    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}


// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);    // create the Direct3D interface

    D3DPRESENT_PARAMETERS d3dpp;    // create a struct to hold various device information

    ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use
    d3dpp.Windowed = FALSE;    
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D


    // create a device class using this information and the info from the d3dpp stuct
    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);

	d3ddev->CreateOffscreenPlainSurface(
			SCREEN_WIDTH, SCREEN_HEIGHT, 
			D3DFMT_A8R8G8B8,
			D3DPOOL_DEFAULT,
			&surface,
			NULL);
}


// this is the function used to render a single frame
void render_frame(void)
{
    // clear the window to a deep blue
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

    d3ddev->BeginScene();    // begins the 3D scene

	d3ddev->GetBackBuffer(0,
						  0,
						  D3DBACKBUFFER_TYPE_MONO,
						  &backbuffer );

	d3ddev->StretchRect(surface,
						NULL,
						backbuffer,
						NULL,
						D3DTEXF_NONE);

    d3ddev->EndScene();    // ends the 3D scene

    d3ddev->Present(NULL, NULL, NULL, NULL);   // displays the created frame on the screen

}


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D
}


The error it gives me is: Unhandled exception at 0x01011202 in BSoD.exe: 0xC0000005: Access violation reading location 0x00000000. the line it breaked on was the last NULL parameter of the d3ddev->CreateOffScreenSurface any idea why?

Share this post


Link to post
Share on other sites
Well, so called tutorials are not general programming lessons, they just show how to achieve a particular goal so too much error checking could make the code less clear. I've often seen books with such disclaiming that OOP, modern c++, proper error checking and exception safety, etc. were left out on purpose for the sake of clarity. Sorry to say that but the real people to be blamed are those who copy and paste some code without understanding what they are doing. Jumping into C++, DirectX and Win32 at the same time without knowing you can't dereference a null or invalid pointer, seriously?

Share this post


Link to post
Share on other sites
ok, my bad. my bad.

So by implementing HRESULT checking, I figured out that the CreateDevice function failed, which resulted in the failing of everything else -.-

So now that I know that the device failed, what can I do about it? what is the most common things that cause it to fail?

Share this post


Link to post
Share on other sites
The debug runtime gave me this:

Direct3D9: (WARN) :HW device not available. GetAdapterCaps fails.

I think this has to do with a hardware problem? odd since I'm compiling this on a fairly new computer

Share this post


Link to post
Share on other sites
I think I might've seen that problem before when trying to run DX apps while a game was running. Other than that, are the graphics drivers up to date?

Share this post


Link to post
Share on other sites
HW device not available can mean that the video card doesn't support the capability you're asking for. However, it can also mean that the device isn't available because you didn't pass it a valid window handle. When you added your error checking did you remember to add checking on the non-DirectX parts of your code? Specifically the window registration and creation?

Share this post


Link to post
Share on other sites
Ok, I really need to start error checking more vigilantly.

Apparently the window never got past the registration stage. -.-

Oh and after cranking some stuff in DirectX Control Panel I got rid of the GetAdapterCaps() thing.

Share this post


Link to post
Share on other sites
I stripped down most of the code and used the the GetLastError() to figure out what's wrong. Odd thing is, the return value was 0. Yet, my error still pops up. I'm not sure if I'm approaching this from the right angle.

Revised code,

Header:
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <fstream>
#include <iostream>


// include the Direct3D Library file
#ifdef _DEBUG
# pragma comment(lib, "d3dx9d.lib")
#else
# pragma comment(lib, "d3dx9.lib")
#endif
#pragma comment(lib, "d3d9.lib")

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

IDirect3D9* d3d;
LPDIRECT3DDEVICE9 d3ddev;
HRESULT hResult;


.cpp:
#include "BSoD.h"

void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

MSG msg;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "WindowClass";

RegisterClassEx(&wc);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed", "Error!",
MB_ICONEXCLAMATION | MB_OK);

GetLastError();
DWORD dw;
dw = GetLastError();

std::ofstream myfile;
myfile.open ("error.txt");
myfile << dw;
myfile.close();
}



hWnd = CreateWindowEx(NULL,
"WindowClass",
"BSoD",
WS_POPUP | WS_EX_TOPMOST | WS_VISIBLE,
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);

if(FAILED(hWnd))
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}


ShowWindow(hWnd, nCmdShow);

initD3D(hWnd);

while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

render_frame();

if(KEY_DOWN(VK_ESCAPE))
PostMessage(hWnd, WM_DESTROY, 0, 0);
}

cleanD3D();

return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

return DefWindowProc (hWnd, message, wParam, lParam);
}
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

if((d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
MessageBox(NULL, "Failed to create Direct3D object", "Error", MB_OK | MB_ICONEXCLAMATION);
}

D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information

ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D



hResult = d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

if(FAILED(hResult))
{
MessageBox(NULL, "Failed to create Direct3D Device", "Error", MB_OK | MB_ICONEXCLAMATION);
}


}

void render_frame(void)
{
// clear the window to a deep blue
hResult = d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

if(FAILED(hResult))
{
MessageBox(NULL, "Failed to clear backbuffer", "Error", MB_OK | MB_ICONEXCLAMATION);
}

hResult = d3ddev->BeginScene(); // begins the 3D scene

if(FAILED(hResult))
{
MessageBox(NULL, "Failed to begin scene", "Error", MB_OK | MB_ICONEXCLAMATION);
}

hResult = d3ddev->EndScene(); // ends the 3D scene

if(FAILED(hResult))
{
MessageBox(NULL, "Failed to end scene", "Error", MB_OK | MB_ICONEXCLAMATION);
}

hResult = d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame on the screen

if(FAILED(hResult))
{
MessageBox(NULL, "Failed to present scene", "Error", MB_OK | MB_ICONEXCLAMATION);
}

}

void cleanD3D(void)
{
hResult = d3ddev->Release(); // close and release the 3D device

if(FAILED(hResult))
{
MessageBox(NULL, "Failed to release Direct3D device", "Error", MB_OK | MB_ICONEXCLAMATION);
}

hResult = d3d->Release(); // close and release Direct3D

if(FAILED(hResult))
{
MessageBox(NULL, "Failed release Direct3D object", "Error", MB_OK | MB_ICONEXCLAMATION);
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by buggy123
I stripped down most of the code and used the the GetLastError() to figure out what's wrong. Odd thing is, the return value was 0. Yet, my error still pops up. I'm not sure if I'm approaching this from the right angle.

Revised code,

Header:
*** Source Snippet Removed ***

.cpp:
*** Source Snippet Removed ***


What's the line that is breaking it now? If it's a pointer reference with access violation at address 0x00000000 that means your using a null pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by buggy123
I stripped down most of the code and used the the GetLastError() to figure out what's wrong. Odd thing is, the return value was 0. Yet, my error still pops up. I'm not sure if I'm approaching this from the right angle.

Revised code,

Header:
*** Source Snippet Removed ***

.cpp:
*** Source Snippet Removed ***



if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed", "Error!",
MB_ICONEXCLAMATION | MB_OK);

GetLastError();
DWORD dw;
dw = GetLastError();

std::ofstream myfile;
myfile.open ("error.txt");
myfile << dw;
myfile.close();
}


You do realize that you don't have to create a file here to output the last error code. If you use a stringstream and write the error code to it before you create the message box, you can actualy display the error code in the message box.

std::stringstream sstr;
sstr << "Window Reg Failed with error code: " << GetLastError();

MessageBoxA(NULL, "Window Registration Failed", sstr.str().c_str(),
MB_ICONEXCLAMATION | MB_OK);


Seems a bit overkill to create a file for just this, or just use:

std::cout << "Window Reg Failed with error code: " << GetLastError() << std::endl;


To write to standard out.

Share this post


Link to post
Share on other sites
2 problems that I see in that code, one SiCrane already pointed out:


RegisterClassEx(&wc);

if(!RegisterClassEx(&wc))
{





and


d3d = Direct3DCreate9(D3D_SDK_VERSION);

if((d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{





I'd try to debug and help you out but I dont have the DX SDK installed on this laptop so proofreading your code is the best I can do.

Edit:
Whats the point of calling GetLastError() twice?


GetLastError();
DWORD dw;
dw = GetLastError();



[Edited by - phear- on August 20, 2009 9:30:39 AM]

Share this post


Link to post
Share on other sites
Thanks for the answers guys. But can someone explain to me why

d3d = Direct3DCreate9(D3D_SDK_VERSION);

if((d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)


is wrong? Is it because I created the Direct3D interface twice?

Share this post


Link to post
Share on other sites
Quote:
Original post by buggy123
Thanks for the answers guys. But can someone explain to me why

d3d = Direct3DCreate9(D3D_SDK_VERSION);

if((d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)


is wrong? Is it because I created the Direct3D interface twice?


Yes. You should check for failure like this:
 if(!d3d) 

Share this post


Link to post
Share on other sites
Thanks guys, I'm glad that there's a dependable noob-tolerant community I can request help from :D

This programming stuff is hard-work, but it's satisfying when the end results is as you intended

Anyways, problem solved (after few hours of flipping through books and cross referencing. xD)

Share this post


Link to post
Share on other sites

This topic is 3039 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.

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