游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1507|回复: 3

为什么我的程序一闪而过.帮忙了.

[复制链接]

1

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2006-3-23 18:00:00 | 显示全部楼层 |阅读模式
# include <windows.h>
//# include <iostream.h>
# include <d3d8.h>
LPDIRECT3D8 pD3D;
LPDIRECT3DDEVICE8 pd3dDevice;
HINSTANCE hInst;
HWND wndHandle;
bool initDirect3D(void);
void render(void);
void cleanUp (void);
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow )
//int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow)
{
        if(! initWindow(hInstance))
                return false;
        if(! initDirect3D())
                return false;
        MSG msg;
        ZeroMemory(&msg,sizeof(msg));
        //if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        //        {
        //        TranslateMessage ( &msg );
        //        DispatchMessage ( &msg );
        //        }
        //        else
        //        {
        //        render( );
        //        }
         BOOL fMessage;
//PeekMessage(&msg,NULL,0U,0U,PM_NOREMOVE);

while(msg.message != WM_QUIT)
{
         fMessage = PeekMessage(&msg,NULL,0U,0U,PM_REMOVE);//对列里没有消息返回真,有返回假
  
                if (fMessage)
                {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
                }

                else
                {
                render();
                }
}
}
bool initWindow(HINSTANCE hInstance)
{
        WNDCLASSEX wcex;
        wcex.cbClsExtra=0;
        wcex.cbSize=sizeof(WNDCLASSEX);
        wcex.cbWndExtra=0;
        wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
        wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
        wcex.hIcon=0;
        wcex.hIconSm=0;
        wcex.hInstance=hInstance;
        wcex.lpfnWndProc=(WNDPROC)WndProc;
        wcex.lpszClassName="DirectxExample";
        wcex.lpszMenuName=NULL;
        wcex.style=CS_HREDRAW|CS_VREDRAW;
        RegisterClassEx(&wcex);
        wndHandle=CreateWindow("DirectxExample","DirectxExample",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,NULL,NULL,hInstance,NULL);
        if(!wndHandle)
                return false;
        ShowWindow(wndHandle,SW_SHOW);
        //UpdataWindow(wndHandle);
        UpdateWindow(wndHandle);
        return true;


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

        }
        return DefWindowProc(hWnd,message,wParam,lParam);
}
bool initDirect3D(void)
{
                pD3D = NULL;
                pd3dDevice = NULL;
                // Create the DirectX object
                if( NULL == ( pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
                {
                return false;
                }
                // Fill the presentation parameters structure
                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,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice ) ) )
                {
                return false;
                }
                return true;
}
void render(void)
{
                // Check to make sure you have a valid Direct3D device
                if( NULL == pd3dDevice )
                return;// Clear the back buffer to a blue color
                pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
                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( );
}

60

主题

1319

帖子

1319

积分

金牌会员

Rank: 6Rank: 6

积分
1319
发表于 2006-3-24 09:44:00 | 显示全部楼层

Re:为什么我的程序一闪而过.帮忙了.

单步调试一下,看看初始化完成了没,或者是在什么时候退出的

0

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2006-3-28 13:35:00 | 显示全部楼层

Re: 为什么我的程序一闪而过.帮忙了.

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

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

里面加上
case WM_PAINT:
     DoSomeThing();
    return 0;
看看

86

主题

2251

帖子

2386

积分

金牌会员

Rank: 6Rank: 6

积分
2386
QQ
发表于 2006-3-28 14:04:00 | 显示全部楼层

Re:为什么我的程序一闪而过.帮忙了.

我调试了一下,CreateDevice()失败,initDirect3D()返回false退出程序的.不了解D3D,楼主自己再看看

  1. // Create a default DirectX device
  2. if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,wndHandle,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice ) ) )
  3. {
  4.     return false;
  5. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-24 01:05

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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