游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1930|回复: 1

求助!基于MFC的DX8程序用LPDIRECT3DTEXTURE8画图片,实际画出来

[复制链接]

1

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2011-11-5 22:01:00 | 显示全部楼层 |阅读模式
实际的大小比原始的大.特求助于大家.
不多说,上代码.


  1. // D3DTestDlg.cpp : implementation file
  2. //

  3. #include "stdafx.h"
  4. #include "D3DTest.h"
  5. #include "D3DTestDlg.h"
  6. #include "d3d8.h"
  7. #include "d3dx8math.h"

  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif

  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About

  15. LPDIRECT3D8                        g_pD3D = NULL;
  16. LPDIRECT3DDEVICE8        g_pDevice = NULL;
  17. LPD3DXSPRITE                g_pSprite = NULL;
  18. D3DPRESENT_PARAMETERS g_Present;
  19. LPDIRECT3DTEXTURE8        g_pTexture = NULL;

  20. class CAboutDlg : public CDialog
  21. {
  22. public:
  23.         CAboutDlg();

  24. // Dialog Data
  25.         //{{AFX_DATA(CAboutDlg)
  26.         enum { IDD = IDD_ABOUTBOX };
  27.         //}}AFX_DATA

  28.         // ClassWizard generated virtual function overrides
  29.         //{{AFX_VIRTUAL(CAboutDlg)
  30.         protected:
  31.         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  32.         //}}AFX_VIRTUAL

  33. // Implementation
  34. protected:
  35.         //{{AFX_MSG(CAboutDlg)
  36.         //}}AFX_MSG
  37.         DECLARE_MESSAGE_MAP()
  38. };

  39. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  40. {
  41.         //{{AFX_DATA_INIT(CAboutDlg)
  42.         //}}AFX_DATA_INIT
  43. }

  44. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  45. {
  46.         CDialog::DoDataExchange(pDX);
  47.         //{{AFX_DATA_MAP(CAboutDlg)
  48.         //}}AFX_DATA_MAP
  49. }

  50. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  51.         //{{AFX_MSG_MAP(CAboutDlg)
  52.                 // No message handlers
  53.         //}}AFX_MSG_MAP
  54. END_MESSAGE_MAP()

  55. /////////////////////////////////////////////////////////////////////////////
  56. // CD3DTestDlg dialog

  57. CD3DTestDlg::CD3DTestDlg(CWnd* pParent /*=NULL*/)
  58.         : CDialog(CD3DTestDlg::IDD, pParent)
  59. {
  60.         //{{AFX_DATA_INIT(CD3DTestDlg)
  61.                 // NOTE: the ClassWizard will add member initialization here
  62.         //}}AFX_DATA_INIT
  63.         // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  64.         m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  65. }

  66. void CD3DTestDlg::DoDataExchange(CDataExchange* pDX)
  67. {
  68.         CDialog::DoDataExchange(pDX);
  69.         //{{AFX_DATA_MAP(CD3DTestDlg)
  70.         DDX_Control(pDX, IDC_STA_SHOW, m_staShow);
  71.         //}}AFX_DATA_MAP
  72. }

  73. BEGIN_MESSAGE_MAP(CD3DTestDlg, CDialog)
  74.         //{{AFX_MSG_MAP(CD3DTestDlg)
  75.         ON_WM_SYSCOMMAND()
  76.         ON_WM_PAINT()
  77.         ON_WM_QUERYDRAGICON()
  78.         ON_WM_TIMER()
  79.         //}}AFX_MSG_MAP
  80. END_MESSAGE_MAP()

  81. /////////////////////////////////////////////////////////////////////////////
  82. // CD3DTestDlg message handlers

  83. BOOL CD3DTestDlg::OnInitDialog()
  84. {
  85.         CDialog::OnInitDialog();

  86.         // Add "About..." menu item to system menu.

  87.         // IDM_ABOUTBOX must be in the system command range.
  88.         ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  89.         ASSERT(IDM_ABOUTBOX < 0xF000);

  90.         CMenu* pSysMenu = GetSystemMenu(FALSE);
  91.         if (pSysMenu != NULL)
  92.         {
  93.                 CString strAboutMenu;
  94.                 strAboutMenu.LoadString(IDS_ABOUTBOX);
  95.                 if (!strAboutMenu.IsEmpty())
  96.                 {
  97.                         pSysMenu->AppendMenu(MF_SEPARATOR);
  98.                         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  99.                 }
  100.         }

  101.         // Set the icon for this dialog.  The framework does this automatically
  102.         //  when the application's main window is not a dialog
  103.         SetIcon(m_hIcon, TRUE);                        // Set big icon
  104.         SetIcon(m_hIcon, FALSE);                // Set small icon
  105.        
  106.         // TODO: Add extra initialization here
  107.         this->InitD3D(m_staShow.GetSafeHwnd());
  108.         if (FAILED(::D3DXCreateTextureFromFile(g_pDevice, "Test.bmp", &g_pTexture)))
  109.         {
  110.                 int a = 0;
  111.                 ++a;
  112.         }

  113.         this->SetTimer(1001, 8, NULL);
  114.         return TRUE;  // return TRUE  unless you set the focus to a control
  115. }

  116. void CD3DTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  117. {
  118.         if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  119.         {
  120.                 CAboutDlg dlgAbout;
  121.                 dlgAbout.DoModal();
  122.         }
  123.         else
  124.         {
  125.                 CDialog::OnSysCommand(nID, lParam);
  126.         }
  127. }

  128. // If you add a minimize button to your dialog, you will need the code below
  129. //  to draw the icon.  For MFC applications using the document/view model,
  130. //  this is automatically done for you by the framework.

  131. void CD3DTestDlg::OnPaint()
  132. {
  133.         if (IsIconic())
  134.         {
  135.                 CPaintDC dc(this); // device context for painting

  136.                 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  137.                 // Center icon in client rectangle
  138.                 int cxIcon = GetSystemMetrics(SM_CXICON);
  139.                 int cyIcon = GetSystemMetrics(SM_CYICON);
  140.                 CRect rect;
  141.                 GetClientRect(&rect);
  142.                 int x = (rect.Width() - cxIcon + 1) / 2;
  143.                 int y = (rect.Height() - cyIcon + 1) / 2;

  144.                 // Draw the icon
  145.                 dc.DrawIcon(x, y, m_hIcon);
  146.         }
  147.         else
  148.         {
  149.                 CDialog::OnPaint();
  150.         }
  151. }

  152. // The system calls this to obtain the cursor to display while the user drags
  153. //  the minimized window.
  154. HCURSOR CD3DTestDlg::OnQueryDragIcon()
  155. {
  156.         return (HCURSOR) m_hIcon;
  157. }

  158. bool CD3DTestDlg::InitD3D(HWND hWnd)
  159. {
  160.         g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
  161.        
  162.         HRESULT hr;
  163.         D3DDISPLAYMODE d3ddm;
  164.         hr = g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
  165.         if(FAILED(hr))
  166.                 return false;
  167.        
  168.         //D3DPRESENT_PARAMETERS d3dpp;
  169.         ZeroMemory(&g_Present, sizeof(g_Present));
  170.         g_Present.BackBufferFormat = d3ddm.Format;
  171.         g_Present.BackBufferCount = 1;
  172.         g_Present.MultiSampleType = D3DMULTISAMPLE_NONE;
  173.         g_Present.SwapEffect = D3DSWAPEFFECT_DISCARD;
  174.         g_Present.hDeviceWindow = hWnd;
  175.         g_Present.Windowed = TRUE;
  176.         g_Present.EnableAutoDepthStencil = TRUE;
  177.         g_Present.AutoDepthStencilFormat = D3DFMT_D24S8;
  178.         g_Present.Flags = 0;
  179.         g_Present.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  180.         //d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  181.        
  182.         hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
  183.                 D3DDEVTYPE_HAL,
  184.                 hWnd,
  185.                 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  186.                 &g_Present,
  187.                 &g_pDevice);
  188.        
  189.         if(FAILED(hr))
  190.                 return false;
  191.        
  192.         if (FAILED(D3DXCreateSprite(g_pDevice, &g_pSprite)))
  193.                 return false;

  194.         if (FAILED(D3DXCreateSprite(g_pDevice, &g_pSprite)))
  195.                 return false;
  196.        
  197.         return true;
  198. }

  199. void CD3DTestDlg::OnTimer(UINT nIDEvent)
  200. {
  201.         // TODO: Add your message handler code here and/or call default
  202.         if (1001 == nIDEvent)
  203.         {
  204.                 this->OnRender();
  205.         }
  206.         CDialog::OnTimer(nIDEvent);
  207. }

  208. int CD3DTestDlg::IfDeviceLost()
  209. {
  210.         HRESULT hr;
  211.         if (FAILED(hr = g_pDevice->TestCooperativeLevel()))
  212.         {
  213.                 if (D3DERR_DEVICELOST == hr)
  214.                         return 1;
  215.                
  216.                 if (D3DERR_DEVICENOTRESET)
  217.                         return 2;
  218.         }
  219.         return 0;
  220. }

  221. bool CD3DTestDlg::ResetDevice()
  222. {
  223.         if (FAILED(g_pDevice->Reset(&g_Present)))
  224.         {
  225.                 return false;
  226.         }
  227.         DWORD current;
  228.         g_pDevice->GetRenderState(D3DRS_AMBIENT, &current);
  229.         if (current != D3DCOLOR_ARGB(255, 255, 255, 255))
  230.                 g_pDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_ARGB(255, 255, 255, 255));
  231.        
  232.         return true;
  233. }

  234. void CD3DTestDlg::OnRender()
  235. {
  236.         if (NULL == g_pDevice)
  237.         {
  238.                 return;
  239.         }

  240.         int nRet = this->IfDeviceLost();
  241.         if (nRet == 1)
  242.         {
  243.                 return;
  244.         }
  245.         else if (nRet == 2)
  246.         {

  247.                 return;
  248.         }

  249.         g_pDevice->BeginScene();
  250.         g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0f, 0);

  251.         if (g_pSprite && g_pTexture)
  252.         {
  253.                 D3DXVECTOR2 dvTran(0.0f, 0.0f);
  254.                 g_pSprite->Draw(g_pTexture, NULL, NULL, NULL, 0.0f, &dvTran, 0xFFFFFFFF);
  255.         }

  256.         g_pDevice->EndScene();
  257.         g_pDevice->Present(NULL, NULL, NULL, NULL);
  258. }
复制代码

9

主题

62

帖子

62

积分

注册会员

Rank: 2

积分
62
发表于 2011-11-8 13:06:00 | 显示全部楼层

Re:求助!基于MFC的DX8程序用LPDIRECT3DTEXTURE8画图片,实际画出

因为图片大小不是2的幂
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-6-8 23:05

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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