Jump to content
  • Advertisement
Sign in to follow this  
buggy123

Untitled

This topic is 3251 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
Advertisement
You aren't checking for errors. Make sure that you check to see if all your DirectX calls are succeeding or failing.

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!