Добрый день! Написал тут простенькую программу на ДиректИкс и при запуске ее работа постепенно замедляется. Начало это происходить после добавления текста.
Листинг (функция вывода текста в конце)
Код:
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <MMSystem.h>
#include <d3dx9core.h>
LPDIRECT3D9 pDirect3D = NULL;
LPDIRECT3DDEVICE9 pDirect3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 pBufferVershin = NULL;
LPDIRECT3DINDEXBUFFER9 pBufferIndex = NULL;
LPD3DXFONT pFont = NULL;
RECT Rec;
HFONT hFont;
HRESULT InitialDirect3D(HWND);
LRESULT CALLBACK MainWinProc(HWND, UINT, WPARAM, LPARAM);
VOID RenderingDirect3D();
VOID DeleteDirect3D();
HRESULT InitialObject();
VOID Matrix();
VOID LightMaterial();
VOID DrawMyText(LPCTSTR, int, int, int ,int , D3DCOLOR);
struct CUSTOMVERTEX
{
FLOAT X, Y, Z;
FLOAT nx, ny, nz;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstanse, LPSTR LpCmdLine, int nCmdShow)
{
MSG msg;
HWND hwnd;
WNDCLASSEXW windowsclass;
windowsclass.cbSize = sizeof(WNDCLASSEXW);
windowsclass.style = CS_VREDRAW|CS_HREDRAW|CS_OWNDC|CS_DBLCLKS;
windowsclass.lpfnWndProc = MainWinProc;
windowsclass.cbClsExtra = 0;
windowsclass.cbWndExtra = 0;
windowsclass.hInstance = hinstance;
windowsclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
windowsclass.hCursor = LoadCursor(NULL,IDC_ARROW);
windowsclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
windowsclass.lpszMenuName = NULL;
windowsclass.lpszClassName = L"WINDOWSCLASS";
windowsclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(& windowsclass))
return 0;
if(!(hwnd = CreateWindowEx(NULL, L"WINDOWSCLASS", L"DirectX", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 300, 150, 500, 400, NULL, NULL, hinstance, NULL)))
return(0);
if(SUCCEEDED(InitialDirect3D(hwnd)))
{
if(SUCCEEDED(InitialObject()))
{
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
RenderingDirect3D();
}
}
}
return(msg.wParam);
}
LRESULT CALLBACK MainWinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_DESTROY:
{
DeleteDirect3D();
PostQuitMessage(0);
return 0;
}
case WM_KEYDOWN:
{
if (wparam == VK_ESCAPE)
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hwnd, msg, wparam, lparam));
}
HRESULT InitialDirect3D(HWND hwnd)
{
if(NULL == (pDirect3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
D3DDISPLAYMODE Display;
if(FAILED(pDirect3D -> GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display)))
return E_FAIL;
D3DPRESENT_PARAMETERS Direct3DParametr;
ZeroMemory(&Direct3DParametr, sizeof(Direct3DParametr));
Direct3DParametr.Windowed = FALSE;
Direct3DParametr.SwapEffect = D3DSWAPEFFECT_DISCARD;
Direct3DParametr.BackBufferFormat = Display.Format;
Direct3DParametr.EnableAutoDepthStencil = TRUE;
Direct3DParametr.AutoDepthStencilFormat = D3DFMT_D16;
Direct3DParametr.BackBufferWidth = GetSystemMetrics(SM_CXSCREEN);
Direct3DParametr.BackBufferHeight = GetSystemMetrics(SM_CYSCREEN);
Direct3DParametr.BackBufferCount = 3;
Direct3DParametr.FullScreen_RefreshRateInHz = Display.RefreshRate;
if(FAILED(pDirect3D ->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&Direct3DParametr,&pDirect3DDevice)))
return E_FAIL;
pDirect3DDevice -> SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
pDirect3DDevice->SetRenderState (D3DRS_ZENABLE, D3DZB_TRUE);
return S_OK;
}
VOID RenderingDirect3D()
{
if(pDirect3DDevice == NULL)
return;
pDirect3DDevice -> Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(60, 100, 150), 1.0f, 0);
pDirect3DDevice -> BeginScene();
Matrix();
LightMaterial();
pDirect3DDevice -> SetStreamSource(0, pBufferVershin, 0, sizeof(CUSTOMVERTEX));
pDirect3DDevice -> SetFVF(D3DFVF_CUSTOMVERTEX);
pDirect3DDevice -> SetIndices(pBufferIndex);
pDirect3DDevice -> DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 36, 0 , 12);
DrawMyText(L"MyText", 10, 10, 800, 800, D3DCOLOR_ARGB(250,250,250,0));
pDirect3DDevice -> EndScene();
pDirect3DDevice -> Present(NULL,NULL,NULL,NULL);
}
VOID DeleteDirect3D()
{
if(pDirect3DDevice != NULL)
pDirect3DDevice -> Release();
if(pDirect3D != NULL)
pDirect3D -> Release();
if(pBufferVershin != NULL)
pBufferVershin -> Release();
if(pBufferIndex != NULL)
pBufferIndex->Release();
if (pFont != NULL)
pFont -> Release();
}
HRESULT InitialObject()
{
CUSTOMVERTEX Vershin[] =
{
{ 1.0f,-1.0f,-1.0f, 0.0f, 0.0f,-1.0f, }, //А
{ 1.0f, 1.0f,-1.0f, 0.0f, 0.0f,-1.0f, }, //В
{ -1.0f, 1.0f,-1.0f, 0.0f, 0.0f,-1.0f, }, //С
{ -1.0f,-1.0f,-1.0f, 0.0f, 0.0f,-1.0f, }, //D
{ -1.0f,-1.0f,-1.0f,-1.0f, 0.0f, 0.0f, }, //A2
{ -1.0f, 1.0f,-1.0f,-1.0f, 0.0f, 0.0f, }, //B2
{ -1.0f, 1.0f, 1.0f,-1.0f, 0.0f, 0.0f, }, //C2
{ -1.0f,-1.0f, 1.0f,-1.0f, 0.0f, 0.0f, }, //D2
{ -1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, }, //A3
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, }, //B3
{ 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, }, //C3
{ 1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, }, //D3
{ 1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, }, //A4
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, }, //B4
{ 1.0f, 1.0f,-1.0f, 1.0f, 0.0f, 0.0f, }, //C4
{ 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f, }, //D4
{ 1.0f,-1.0f,-1.0f, 0.0f,-1.0f, 0.0f, }, //A5
{ -1.0f,-1.0f,-1.0f, 0.0f,-1.0f, 0.0f, }, //B5
{ -1.0f,-1.0f, 1.0f, 0.0f,-1.0f, 0.0f, }, //C5
{ 1.0f,-1.0f, 1.0f, 0.0f,-1.0f, 0.0f, }, //D5
{ 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, }, //A6
{ -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, }, //B6
{ -1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 0.0f, }, //C6
{ 1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 0.0f, }, //D6
};
const unsigned short Index[]={
0,1,2, 2,3,0,
4,5,6, 6,7,4,
8,9,10, 10,11,8,
12,13,14, 14,15,12,
16,17,18, 18,19,16,
20,21,22, 22,23,20,
};
// Vertex
if(FAILED(pDirect3DDevice -> CreateVertexBuffer(36*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &pBufferVershin, NULL)))
return E_FAIL;
VOID* pBV;
if(FAILED(pBufferVershin -> Lock(0, sizeof(Vershin), (void**)&pBV, 0)))
return E_FAIL;
memcpy(pBV, Vershin, sizeof(Vershin));
pBufferVershin -> Unlock();
// Index
pDirect3DDevice -> CreateIndexBuffer(36*sizeof(Index), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &pBufferIndex, NULL);
VOID* pBI;
pBufferIndex -> Lock(0, sizeof(Index), (void**)&pBI, 0);
memcpy(pBI, Index, sizeof(Index));
pBufferIndex -> Unlock();
return S_OK;
}
VOID Matrix()
{
D3DXMATRIX MatrixWorld, MatrixWorldX, MatrixWorldY;
D3DXMATRIX MatrixView;
D3DXMATRIX MatrixProjection;
// MatrixWorld
UINT Time = timeGetTime() % 5000;
FLOAT Angle = Time * (2.0f * D3DX_PI) / 5000.0f;
D3DXMatrixRotationX(&MatrixWorldX, Angle);
D3DXMatrixRotationY(&MatrixWorldY, Angle);
D3DXMatrixMultiply( &MatrixWorld, &MatrixWorldX, &MatrixWorldY );
pDirect3DDevice -> SetTransform(D3DTS_WORLD, &MatrixWorld);
// MatrixView
D3DXMatrixLookAtLH(&MatrixView, &D3DXVECTOR3(0.0f, 0.0f, -8.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
pDirect3DDevice -> SetTransform(D3DTS_VIEW, &MatrixView);
// MatrixProjection
D3DXMatrixPerspectiveFovLH(&MatrixProjection, D3DX_PI/4, 1.0f, 1.0f, 100.0f);
pDirect3DDevice -> SetTransform(D3DTS_PROJECTION, &MatrixProjection);
}
VOID LightMaterial()
{
D3DMATERIAL9 Material;
D3DLIGHT9 Light;
ZeroMemory(&Material, sizeof(D3DMATERIAL9));
Material.Diffuse.r = Material.Ambient.r = 1.0f;
Material.Diffuse.g = Material.Ambient.g = 1.0f;
Material.Diffuse.b = Material.Ambient.b = 0.0f;
Material.Diffuse.a = Material.Ambient.a = 1.0f;
pDirect3DDevice -> SetMaterial(&Material);
D3DXVECTOR3 VectorDir;
ZeroMemory(&Light, sizeof(D3DLIGHT9));
Light.Type = D3DLIGHT_DIRECTIONAL;
Light.Diffuse.r = 1.0f;
Light.Diffuse.g = 1.0f;
Light.Diffuse.b = 1.0f;
Light.Range = 100.0f;
VectorDir = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
D3DXVec3Normalize( (D3DXVECTOR3*)&Light.Direction, &VectorDir );
pDirect3DDevice -> SetLight(0, &Light);
pDirect3DDevice -> LightEnable(0, TRUE);
pDirect3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
pDirect3DDevice->SetRenderState( D3DRS_AMBIENT, 0 );
}
VOID DrawMyText(LPCTSTR StrokaTexta, int x, int y, int x1, int y1, D3DCOLOR MyColor)
{
hFont = CreateFont(30, 10, 0, 0, FW_NORMAL, FALSE, FALSE, 0, 1, 0, 0, 0, DEFAULT_PITCH|FF_MODERN, L"Arial");
Rec.left = x;
Rec.top = y;
Rec.right = x1;
Rec.bottom = y1;
D3DXCreateFont(pDirect3DDevice,18,0,400,0,0,RUSSIAN_CHARSET,
OUT_DEFAULT_PRECIS,ANTIALIASED_QUALITY,
DEFAULT_PITCH | FF_MODERN,L"Arial",&pFont);
pFont->DrawText(NULL, StrokaTexta, -1, &Rec, DT_WORDBREAK, MyColor);
}
Что тут не так?