首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

!基于MFC的DX8程序用精灵贴图,显示出来的图片大小和原始的不相同

2012-03-09 
求助!基于MFC的DX8程序用精灵贴图,显示出来的图片大小和原始的不相同.程序是用LPDIRECT3DTEXTURE8画图片的

求助!基于MFC的DX8程序用精灵贴图,显示出来的图片大小和原始的不相同.
程序是用LPDIRECT3DTEXTURE8画图片的.
但是,实际显示的大小比原始的大.特求助于大家
不多说,上代码.(为便于大家比较我直接把cpp的代码全部复制出来了.)

C/C++ code
// D3DTestDlg.cpp : implementation file//#include "stdafx.h"#include "D3DTest.h"#include "D3DTestDlg.h"#include "d3d8.h"#include "d3dx8math.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App AboutLPDIRECT3D8            g_pD3D = NULL;LPDIRECT3DDEVICE8    g_pDevice = NULL;LPD3DXSPRITE        g_pSprite = NULL;D3DPRESENT_PARAMETERS g_Present;LPDIRECT3DTEXTURE8    g_pTexture = NULL;class CAboutDlg : public CDialog{public:    CAboutDlg();// Dialog Data    //{{AFX_DATA(CAboutDlg)    enum { IDD = IDD_ABOUTBOX };    //}}AFX_DATA    // ClassWizard generated virtual function overrides    //{{AFX_VIRTUAL(CAboutDlg)    protected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support    //}}AFX_VIRTUAL// Implementationprotected:    //{{AFX_MSG(CAboutDlg)    //}}AFX_MSG    DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){    //{{AFX_DATA_INIT(CAboutDlg)    //}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){    CDialog::DoDataExchange(pDX);    //{{AFX_DATA_MAP(CAboutDlg)    //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)    //{{AFX_MSG_MAP(CAboutDlg)        // No message handlers    //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CD3DTestDlg dialogCD3DTestDlg::CD3DTestDlg(CWnd* pParent /*=NULL*/)    : CDialog(CD3DTestDlg::IDD, pParent){    //{{AFX_DATA_INIT(CD3DTestDlg)        // NOTE: the ClassWizard will add member initialization here    //}}AFX_DATA_INIT    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CD3DTestDlg::DoDataExchange(CDataExchange* pDX){    CDialog::DoDataExchange(pDX);    //{{AFX_DATA_MAP(CD3DTestDlg)    DDX_Control(pDX, IDC_STA_SHOW, m_staShow);    //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CD3DTestDlg, CDialog)    //{{AFX_MSG_MAP(CD3DTestDlg)    ON_WM_SYSCOMMAND()    ON_WM_PAINT()    ON_WM_QUERYDRAGICON()    ON_WM_TIMER()    //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CD3DTestDlg message handlersBOOL CD3DTestDlg::OnInitDialog(){    CDialog::OnInitDialog();    // Add "About..." menu item to system menu.    // IDM_ABOUTBOX must be in the system command range.    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);    ASSERT(IDM_ABOUTBOX < 0xF000);    CMenu* pSysMenu = GetSystemMenu(FALSE);    if (pSysMenu != NULL)    {        CString strAboutMenu;        strAboutMenu.LoadString(IDS_ABOUTBOX);        if (!strAboutMenu.IsEmpty())        {            pSysMenu->AppendMenu(MF_SEPARATOR);            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);        }    }    // Set the icon for this dialog.  The framework does this automatically    //  when the application's main window is not a dialog    SetIcon(m_hIcon, TRUE);            // Set big icon    SetIcon(m_hIcon, FALSE);        // Set small icon        // TODO: Add extra initialization here    this->InitD3D(m_staShow.GetSafeHwnd());    if (FAILED(::D3DXCreateTextureFromFile(g_pDevice, "Test.bmp", &g_pTexture)))    {        int a = 0;        ++a;    }    this->SetTimer(1001, 8, NULL);    return TRUE;  // return TRUE  unless you set the focus to a control}void CD3DTestDlg::OnSysCommand(UINT nID, LPARAM lParam){    if ((nID & 0xFFF0) == IDM_ABOUTBOX)    {        CAboutDlg dlgAbout;        dlgAbout.DoModal();    }    else    {        CDialog::OnSysCommand(nID, lParam);    }}// If you add a minimize button to your dialog, you will need the code below//  to draw the icon.  For MFC applications using the document/view model,//  this is automatically done for you by the framework.void CD3DTestDlg::OnPaint() {    if (IsIconic())    {        CPaintDC dc(this); // device context for painting        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);        // Center icon in client rectangle        int cxIcon = GetSystemMetrics(SM_CXICON);        int cyIcon = GetSystemMetrics(SM_CYICON);        CRect rect;        GetClientRect(&rect);        int x = (rect.Width() - cxIcon + 1) / 2;        int y = (rect.Height() - cyIcon + 1) / 2;        // Draw the icon        dc.DrawIcon(x, y, m_hIcon);    }    else    {        CDialog::OnPaint();    }}// The system calls this to obtain the cursor to display while the user drags//  the minimized window.HCURSOR CD3DTestDlg::OnQueryDragIcon(){    return (HCURSOR) m_hIcon;}bool CD3DTestDlg::InitD3D(HWND hWnd){    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);        HRESULT hr;    D3DDISPLAYMODE d3ddm;    hr = g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);    if(FAILED(hr))        return false;        //D3DPRESENT_PARAMETERS d3dpp;    ZeroMemory(&g_Present, sizeof(g_Present));    g_Present.BackBufferFormat = d3ddm.Format;    g_Present.BackBufferCount = 1;    g_Present.MultiSampleType = D3DMULTISAMPLE_NONE;    g_Present.SwapEffect = D3DSWAPEFFECT_DISCARD;    g_Present.hDeviceWindow = hWnd;    g_Present.Windowed = TRUE;    g_Present.EnableAutoDepthStencil = TRUE;    g_Present.AutoDepthStencilFormat = D3DFMT_D24S8;    g_Present.Flags = 0;    g_Present.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;    //d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;        hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,        D3DDEVTYPE_HAL,        hWnd,        D3DCREATE_SOFTWARE_VERTEXPROCESSING,        &g_Present,        &g_pDevice);        if(FAILED(hr))        return false;        if (FAILED(D3DXCreateSprite(g_pDevice, &g_pSprite)))        return false;    if (FAILED(D3DXCreateSprite(g_pDevice, &g_pSprite)))        return false;        return true;}void CD3DTestDlg::OnTimer(UINT nIDEvent) {    // TODO: Add your message handler code here and/or call default    if (1001 == nIDEvent)    {        this->OnRender();    }    CDialog::OnTimer(nIDEvent);}int CD3DTestDlg::IfDeviceLost(){    HRESULT hr;    if (FAILED(hr = g_pDevice->TestCooperativeLevel()))    {        if (D3DERR_DEVICELOST == hr)            return 1;                if (D3DERR_DEVICENOTRESET)            return 2;    }    return 0;}bool CD3DTestDlg::ResetDevice(){    if (FAILED(g_pDevice->Reset(&g_Present)))    {        return false;    }    DWORD current;    g_pDevice->GetRenderState(D3DRS_AMBIENT, &current);    if (current != D3DCOLOR_ARGB(255, 255, 255, 255))        g_pDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_ARGB(255, 255, 255, 255));        return true;}void CD3DTestDlg::OnRender(){    if (NULL == g_pDevice)    {        return;    }    int nRet = this->IfDeviceLost();    if (nRet == 1)    {        return;    }    else if (nRet == 2)    {        return;    }    g_pDevice->BeginScene();    g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0f, 0);    if (g_pSprite && g_pTexture)    {        D3DXVECTOR2 dvTran(0.0f, 0.0f);        g_pSprite->Draw(g_pTexture, NULL, NULL, NULL, 0.0f, &dvTran, 0xFFFFFFFF);    }    g_pDevice->EndScene();    g_pDevice->Present(NULL, NULL, NULL, NULL);} 



[解决办法]
哈哈,你的显卡是老显卡吧.
大多数的老显卡(比如gf4 mx)都不支持任意长宽的纹理。它们要求纹理的长宽是2的n次方大小。
如果使用任意长宽纹理,它会将纹理拉伸到最接近的2的n次方大小去.

热点排行