游戏开发论坛

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

检查了很多遍了,为什么这个程序还是有错呢??

[复制链接]

12

主题

42

帖子

46

积分

注册会员

Rank: 2

积分
46
发表于 2009-8-4 14:53:00 | 显示全部楼层 |阅读模式
是一个简单的粒子系统的程序,后面的几个函数我放到另外一个程序,只改设备接口,能运行,可见不是函数的问题,课主体我检查很多遍了,为什么还是有错,麻烦各位帮我看看!!谢谢!!


#include<d3d9.h>
#include<d3dx9.h>
#include<windows.h>

#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"winmm.lib")

//
// Globals
//
HINSTANCE hInst;                                                        // application instance
HWND wndHandle;                                                                // application window handle
LPDIRECT3D9 pD3D = 0;
LPDIRECT3DDEVICE9 pd3dDevice = 0;
LPD3DXMESH mesh;
const int Width=640;
const int Height=480;

LPDIRECT3DVERTEXBUFFER9 VB;


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

struct Vertex
{
        float x,y,z;
        Vertex(float _x,float _y,float _z)
        {
                x=_x;y=_y;z=_z;
        }
};
#define FVF D3DFVF_XYZ

//
// Framework functions
//

int WINAPI WinMain(HINSTANCE hInstance,
                                   HINSTANCE hPrevInstance,
                                   LPTSTR lpCmdLine, int nCmdShow)
{
        if (!initWindow(hInstance))
        {
                MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
                return false;
        }

        if (!initDirect3D())
        {
                MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
                return false;
        }

        // Main message loop:
    MSG msg;
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
                        TranslateMessage( &msg );
            DispatchMessage( &msg );
        }               
                else
                        render();
                 
    }

        // release the device and the direct3D object
        if( pd3dDevice != NULL)
        pd3dDevice->Release();

    if( pD3D != NULL)
        pD3D->Release();
               
        return (int) msg.wParam;
}

/*********************************************************************
* initWindow
*********************************************************************/
bool initWindow(HINSTANCE hInstance)
{
        WNDCLASSEX wcex;

        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style                        = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc        = (WNDPROC)WndProc;
        wcex.cbClsExtra                = 0;
        wcex.cbWndExtra                = 0;
        wcex.hInstance                = hInstance;
        wcex.hIcon                        = 0;
        wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName        = NULL;
        wcex.lpszClassName        = "DirectXExample";
        wcex.hIconSm                = 0;
        RegisterClassEx(&wcex);

        // create the window
        wndHandle = CreateWindow("DirectXExample",
                                                         "DirectXExample",
                                                         WS_OVERLAPPEDWINDOW,
                                                         0,
                                                         0,
                                                         640,
                                                         480,
                                                         NULL,
                                                         NULL,
                                                         hInstance,
                                                         NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   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);
}

/*********************************************************************
* initDirect3D
* initializes direct3D
*********************************************************************/
bool initDirect3D()
{
        pD3D = NULL;
        pd3dDevice = NULL;

        // create the directX object
        if( NULL == ( pD3D = Direct3DCreate9( 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.hDeviceWindow    = wndHandle;
        d3dpp.EnableAutoDepthStencil=true;
        d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8;

        // create a default directx device
    if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wndHandle,
                                      D3DCREATE_HARDWARE_VERTEXPROCESSING,
                                      &d3dpp, &pd3dDevice ) ) )
    {
        return false;
    }

       
        return true;
}
bool Setup()
{

        pd3dDevice->CreateVertexBuffer(3*sizeof(Vertex),D3DUSAGE_WRITEONLY,FVF,D3DPOOL_MANAGED,&VB,0);
        Vertex* v;
        VB->Lock(0,0,(void**)&v,0);
        v[0]=Vertex(0.0f,1.0f,5.0f);
        v[1]=Vertex(1.0f,0.0f,5.0f);
        v[2]=Vertex(-1.0f,0.0f,5.0f);
        VB->Unlock();

        D3DXCreateTeapot(pd3dDevice,&mesh,0);

        //
        // Set camera.
        //

        D3DXVECTOR3 pos(0.0f, 0.0f, -5.0f);
        D3DXVECTOR3 target(0.0f,0.0f, 0.0f);
        D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);

        D3DXMATRIX V;
        D3DXMatrixLookAtLH(
                &V,
                &pos,
                &target,
                &up);

        pd3dDevice->SetTransform(D3DTS_VIEW, &V);

        //
        // Set projection matrix.
        //

        D3DXMATRIX proj;
        D3DXMatrixPerspectiveFovLH(
                        &proj,
                        D3DX_PI * 0.5f, // 90 - degree
                        (float)Width / (float)Height,
                        0.0f,
                        1000.0f);
        pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
       
        return true;
}



void render(void)
{

                D3DXMATRIX m;
                D3DXMatrixTranslation(&m,1,1,1);
                pd3dDevice->SetTransform(D3DTS_WORLD,&m);

                pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
                pd3dDevice->BeginScene();

                pd3dDevice->SetStreamSource(0,VB,0,sizeof(Vertex));
                pd3dDevice->SetFVF(FVF);
                pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);

                mesh->DrawSubset(0);
                pd3dDevice->EndScene();
                pd3dDevice-&gtresent(0, 0, 0, 0);
       
}

0

主题

10

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2009-8-4 17:28:00 | 显示全部楼层

Re:检查了很多遍了,为什么这个程序还是有错呢??

你的setup函数没调用嘛。。。

5

主题

755

帖子

757

积分

高级会员

Rank: 4

积分
757
发表于 2009-8-4 18:00:00 | 显示全部楼层

Re:检查了很多遍了,为什么这个程序还是有错呢??

错在哪里。堆栈信息。还是直接就没编译过?

12

主题

42

帖子

46

积分

注册会员

Rank: 2

积分
46
 楼主| 发表于 2009-8-4 21:01:00 | 显示全部楼层

Re: 检查了很多遍了,为什么这个程序还是有错呢??

谢谢2楼,我运行成功那一刻真的湿了,大湿!!

问下你们编程都有个初始化的模板吗?是用的DX自己带的还是自己编的??
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-18 22:42

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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