游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2654|回复: 7

本人对书本“beginning directX 9”中第二章中的代码遇到麻烦

[复制链接]

3

主题

7

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2005-7-21 00:11:00 | 显示全部楼层 |阅读模式
本人对书本“beginning directX 9”中第二章中的代码遇到麻烦
这段程序我从书上复制下来,可运行时窗口弹出却又马上关闭。没等我看清楚程序就结束了!

请问各位怎样解决?谢谢!(上传了附件)
#include <windows.h>
#include <d3d9.h>

LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
HWND wndHandle;

void render(void);
void cleanUp (void);
bool initDirect3D(void);
bool initWindow( HINSTANCE hInstance);
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow )
{
        MSG msg;
        if ( !initWindow( hInstance) )
                return false;
        if(!initDirect3D())
                return false;
        ZeroMemory( &msg, sizeof( msg ) );
        while(msg.message!=WM_QUIT )
        {
                if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
                {
                        TranslateMessage( &msg );
                        DispatchMessage( &msg );
                }
                else
                        render();
        }
        return msg.wParam;
        cleanUp();
}


bool initWindow( HINSTANCE hInstance)
{
        WNDCLASSEX wcex;
        wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
        wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
        wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
        wcex.cbClsExtra = 0; // extra bytes to allocate for this class
        wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
        wcex.hInstance = hInstance; // handle to the application instance
        wcex.hIcon = 0; // icon to associate with the application
        wcex.hCursor = LoadCursor(NULL, IDC_ARROW);// the default cursor
        wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
        wcex.lpszMenuName  = NULL; // the resource name for the menu
        wcex.lpszClassName = "DirectXExample";
        wcex.hIconSm = 0; // the handle to the small icon
        RegisterClassEx(&wcex);
       

        wndHandle = CreateWindow(
        "DirectXExample",                                                                                       
        "DirectXExample",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, // the starting x coordinate
        CW_USEDEFAULT, // the starting y coordinate
        800, // the pixel width of the window
        600, // the pixel height of the window
        NULL, // the parent window; NULL for desktop
        NULL, // the menu for the application; NULL for none
        hInstance, // the handle to the application instance
        NULL); // no values passed to the window
                   // Make sure that the window handle that is created is valid
        if (!wndHandle)
                return false;
        else
        {
                ShowWindow(wndHandle, SW_SHOWNORMAL);
                UpdateWindow(wndHandle);
                return true;
        }       
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        switch (message)
        {
                case WM_DESTROY:
                        PostQuitMessage(0);
                        return 0;
        }
        return DefWindowProc(hWnd, message, wParam, lParam);
}


bool initDirect3D(void)
{
        pD3D = NULL;
        pd3dDevice = NULL;
       
        if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )       
        {
        return false;
        }
        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory( &d3dpp, sizeof( d3dpp ) );
        d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
        d3dpp.BackBufferCount = 1;
        d3dpp.BackBufferHeight = 480;
        d3dpp.BackBufferWidth = 640;
        d3dpp.hDeviceWindow = wndHandle;
        // Create a default DirectX device
        if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,
                D3DDEVTYPE_REF,
                wndHandle,
                D3DADAPTER_DEFAULT,
                &d3dpp,
                &pd3dDevice ) ) )
        {
                return false;
        }
        return true;
}


void render(void)
{
        // Check to make sure you have a valid Direct3D device
        if( NULL != pd3dDevice )
        {
                pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,                // Clear the back buffer to a blue color
                D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );                                // Present the back buffer contents to the display
                pd3dDevice-&gtresent( NULL, NULL, NULL, NULL );
        }
}

void cleanUp (void)
{
        // Release the device and the Direct3D object
        if( pd3dDevice != NULL )
                pd3dDevice->Release( );
        if( pD3D != NULL )
                pD3D->Release( );
}

sf_200572101036.rar

3.05 KB, 下载次数:

42

主题

418

帖子

418

积分

中级会员

Rank: 3Rank: 3

积分
418
发表于 2005-7-21 09:31:00 | 显示全部楼层

Re:本人对书本“beginning directX 9”中第二章中的代码遇到麻

用单步看是哪里的问题

8

主题

49

帖子

49

积分

注册会员

Rank: 2

积分
49
发表于 2005-7-21 11:31:00 | 显示全部楼层

Re:本人对书本“beginning directX 9”中第二章中的代码遇到麻

您好楼主,能给我一份beginning directX 9吗?
我的邮箱是:YanDong_8212@163.com,谢谢,非常感谢!

3

主题

7

帖子

7

积分

新手上路

Rank: 1

积分
7
 楼主| 发表于 2005-7-21 15:01:00 | 显示全部楼层

Re: Re:本人对书本“beginning directX 9”中第二章中的代码遇

wjk98550328: Re:本人对书本“beginning directX 9”中第二章中的代码遇到麻烦

if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,
                D3DDEVTYPE_REF,
                wndHandle,
                D3DADAPTE...

老兄,还是不行!

3

主题

7

帖子

7

积分

新手上路

Rank: 1

积分
7
 楼主| 发表于 2005-7-21 15:11:00 | 显示全部楼层

Re:本人对书本“beginning directX 9”中第二章中的代码遇到麻

我使用的开发环境是vc++6,而书本上的例子是在。net上运行的。会不会是开发环境不同的
原因?

8

主题

49

帖子

49

积分

注册会员

Rank: 2

积分
49
发表于 2005-7-21 16:57:00 | 显示全部楼层

Re:本人对书本“beginning directX 9”中第二章中的代码遇到麻

谢谢楼主给我资料。
我下午一口气就看到了第5章,真是爽啊。
那个例子是因为你的窗口句柄不对,解决的方法是:将有一句的HWND hWnd;提到最程序开头,变成个全局变量。然后将那个wndHandle,都改为hWnd.

42

主题

418

帖子

418

积分

中级会员

Rank: 3Rank: 3

积分
418
发表于 2005-7-21 17:32:00 | 显示全部楼层

Re:本人对书本“beginning directX 9”中第二章中的代码遇到麻

beginning directX 9 是不是中文版的呀,是的话也给俺一份,谢谢了^_^
laurencehuang@sohu.com

3

主题

7

帖子

7

积分

新手上路

Rank: 1

积分
7
 楼主| 发表于 2005-7-22 16:24:00 | 显示全部楼层

Re: Re:本人对书本“beginning directX 9”中第二章中的代码遇

YanDong_8212: Re:本人对书本“beginning directX 9”中第二章中的代码遇到麻烦

谢谢楼主给我资料。
我下午一口气就看到了第5章,真是爽啊。
那个例子是因为你的窗口句柄不对,解决的方法...

朋友,我的解决方法和你的不一样哦,您说的窗口句柄只是个变量名而已,可以自己命名的呀,只要在程序中不与其他变量混淆就行了。

其实问题不在于句柄的名字及其是否是全局变量,贴出来的程序是做了一点点修改,可这些修改都不影响程序的运行,主要的原因是
CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
wndHandle,
D3DADAPTER_DEFAULT,
&d3dpp,
&pd3dDevice

该函数中的第二个参数设置错误,我的显卡要设置成D3DDEVTYPE_HAL
查了半天才知道是这回事!无奈!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-26 13:30

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表