|
|
小弟我刚学D3D不久,今天在把位图加载并显示在屏幕上时发现图片出现比较严重的锯齿(图片本身没问题)。这在我以前用GDI时是不会有的。请问是怎么回事?以下是我的源码:
其中的位图文件大家随便拿一幅来试试就行了。谢谢!
#include "stdafx.h"
#include <d3d9.h>
#include "d3dx9.h"
#include "stdio.h"
#include "d3dx9tex.h"
#pragma comment(lib,"dxguid.lib")
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
IDirect3D9* d3d_3D ;
IDirect3DDevice9* d3d_Device ;
IDirect3DSurface9* surface;
HWND hWnd;
HINSTANCE hInst;
const int SCREENWIDTH = 1280;
const int SCREENHEIGHT = 960 ;
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
bool initDirect3D(void);
void render(void);
void cleanUp (void);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
render();
}
}
cleanUp ();
return msg.wParam;
}
ATOM MyRegisterClass(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 = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
hWnd = CreateWindow("canvas", "Mydx" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
if ( !initDirect3D( ) )
{
return FALSE;
}
MoveWindow(hWnd,0,0,1280,960,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
bool initDirect3D(void)
{
d3d_3D = NULL;
d3d_Device = NULL;
if( ! ( d3d_3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}
char str[20] = "";
UINT va=d3d_3D->GetAdapterCount();
sprintf(str,"X?%d ",va);
MessageBox(hWnd,str,"sdfd",NULL);
D3DADAPTER_IDENTIFIER9 ident;
d3d_3D->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &ident);
MessageBox(hWnd,ident.Description,"sdfd",NULL);
UINT numModes = d3d_3D->GetAdapterModeCount(D3DADAPTER_DEFAULT,D3DFMT_X8R8G8B8);
sprintf(str,"X?%d ",numModes);
MessageBox(hWnd,str,"sdfd",NULL);
for (UINT i=0; i < numModes; i++)
{
D3DDISPLAYMODE mode;
d3d_3D->EnumAdapterModes(D3DADAPTER_DEFAULT,D3DFMT_X8R8G8B8,i,&mode);
if ( mode.Width == SCREENWIDTH && mode.Height == SCREENHEIGHT )
{
break ;
}
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 1280;
d3dpp.BackBufferWidth = 960;
d3dpp.hDeviceWindow = hWnd;
if( D3D_OK!=( d3d_3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3d_Device ) ) )
{
return false;
}
if(D3D_OK!=(d3d_Device->CreateOffscreenPlainSurface(
1280,
960,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&surface,
NULL))
)
{
return NULL;
}
if(D3D_OK!=(D3DXLoadSurfaceFromFile( surface,
NULL,
NULL,
"Sophia2.bmp",
NULL,
D3DX_DEFAULT,
0,
NULL )))
return NULL;
return true;
}
void render(void)
{
if( NULL == d3d_Device ) return;
d3d_Device->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB( 0,0,255), 1.0f, 0 );
if(SUCCEEDED(d3d_Device->BeginScene()))
{
IDirect3DSurface9* backbuffer = NULL;
d3d_Device->GetBackBuffer( 0,
0,
D3DBACKBUFFER_TYPE_MONO,
&backbuffer );
d3d_Device->StretchRect( surface,NULL,backbuffer,NULL,D3DTEXF_NONE );
if((d3d_Device->UpdateSurface(surface, NULL, backbuffer, NULL)))
d3d_Device->EndScene();
}
d3d_Device-> resent( NULL, NULL, NULL, NULL );
}
void cleanUp (void)
{
if( d3d_Device != NULL )
d3d_Device->Release( );
if( d3d_3D != NULL )
d3d_3D->Release( );
if( surface != NULL )
surface->Release( );
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
} |
|