خوب کسی جواب نمیده؟
این یه تسته که تو C++builer نوشتم و برای vs یکمی تفاوت داره در حد یکی دو خط مثلا برای "MHD" یه L به اولش اضافه کردم و یا تابع pow10 رو به شکل pow(10, ...)o نوشتم چون ارور میداد وگرنه بقیش مثل هم هست. سرعتشون تقریبا یکیه نمیشه گفت کدوم سریع تره. برنامه با Direct3D9 هست و یک قوری زرد با بکگراند آبی رو نمایش میده که تو مرکز مختصات داره دور خودش می چرخه. از سیستم نورافشانی دایرکت تریدی استفاده شده.
به هر حال وقتی برنامه اجرا شد پنجره تمام صفحه میشه بعد با alt + f4 برنامه رو ببندید، پنجره برنامه بسته میشه یم پنجره پیغام میاد که یک لیستی از فریم بر ثانیه که تو 25 فریم آخر ذخیره شده رو نمایش میده که اولین عدد از تقسیم کل فریم ها بر زمان کل نمایش بدست میاد و بقیشون (24 تای بعد) هم برای هر فریم جداگانه حساب شده. عدد اول تقریبا به شکل میانگین هست.
کد تو C++builder:
/*****************
* *
* MHD_1391 *
* *
*****************/
#include <windows.h>
#pragma hdrstop
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#include <tchar.h>
IDirect3D9 *xd3d;
IDirect3DDevice9 *xdev;
D3DCAPS9 xcaps;
D3DVIEWPORT9 xvp;
D3DPRESENT_PARAMETERS xpp;
D3DLIGHT9 xlight;
D3DMATERIAL9 xmaterial;
ID3DXMesh *teaput;
HWND DXhwnd = NULL;
//------------------------------------------------------------------------------
#define FPS_LIST_COUNT 25
double CurrentTime, StartTime;
int FPS_Index = -1;
double FPSs[FPS_LIST_COUNT];
int FrameCount = 0;
double GetNow()
{
LARGE_INTEGER lic, lif;
QueryPerformanceCounter(&lic);
QueryPerformanceFrequency(&lif);
return (double)lic.QuadPart / (double)lif.QuadPart;
}
void UpdateFPS()
{
FPS_Index++;
if (FPS_Index >= FPS_LIST_COUNT)
{
FPS_Index = 0;
}
FPSs[FPS_Index] = 1.0L / (GetNow() - CurrentTime);
}
char * DoubleToStr(const double d)
{
char * s = new char[20];
if (d == 0.0L)
{
for (int i = 0; i < 20; i++) {
s[i] = '0';
}
s[19] = NULL;
return s;
}
int exponent = (int)log10(fabsl(d));
double num = fabsl(d) / pow10l((double)exponent);
if (num >= 1.0L)
{
num /= 10.0L;
}
else
exponent--;
num += 0.0000000000001L;
int x;
int i = 0;
if (d < 0.0L)
{
s[0] = '-';
}
else
{
s[0] = '+';
}
s[2] = '.';
while (i < 10)
{
i++;
if (i == 2)
{
i++;
}
num *= 10.0L;
x = num;
s[i] = '0' + x;
num = num - x;
}
if (exponent != 0)
{
i++;
s[i] = 'e';
if (exponent < 0)
{
i++;
s[i] = '-';
exponent = abs(exponent);
}
int expon_10 = log10(exponent);
expon_10 = pow10(expon_10);
while (expon_10 > 0)
{
i++;
s[i] = '0' + (exponent / expon_10);
expon_10 /= 10;
}
}
s[i + 1] = NULL;
return s;
}
void straddstr(char **c1, const char *c2)
{
int i = strlen(*c1),
j = strlen(c2);
char *c3 = (char *)malloc(i + j + 1);
memcpy(c3, *c1, i);
memcpy(c3 + i, c2, j);
c3[i + j] = NULL;
*c1 = c3;
}
void MessageFPS()
{
FPSs[0] = (double)FrameCount / (GetNow() - StartTime);
char nextline[3] = {char(13), char(10), 0};
char *s = "";
char *num;
for (int i = 0; i < FPS_LIST_COUNT; i++)
{
num = DoubleToStr(FPSs[i]);
straddstr(&num, nextline);
if (i == 0)
{
straddstr(&num, nextline);
}
straddstr(&s, num);
}
MessageBox(0, s, "MHD_1391", MB_OK);
}
//------------------------------------------------------------------------------
tagPOINT GetScreenSize()
{
tagPOINT p;
tagRECT r;
GetWindowRect(GetDesktopWindow(), &r);
p.x = r.right;
p.y = r.bottom;
return p;
}
tagPOINT desktop_size = GetScreenSize();
HRESULT DXCreateDevice()
{
xd3d = Direct3DCreate9(D3D_SDK_VERSION);
xd3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &xcaps);
DWORD vp;
if (xcaps.Caps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
xpp.BackBufferWidth = desktop_size.x;
xpp.BackBufferHeight = desktop_size.y;
xpp.BackBufferFormat = D3DFMT_A8R8G8B8;
xpp.BackBufferCount = 0;
xpp.MultiSampleType = D3DMULTISAMPLE_NONE;
xpp.MultiSampleQuality = 0;
xpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
xpp.hDeviceWindow = DXhwnd;
xpp.Windowed = true;
xpp.EnableAutoDepthStencil = true;
xpp.AutoDepthStencilFormat = D3DFMT_D24S8;
xpp.Flags = 0;
xpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
xpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
return xd3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
DXhwnd,
D3DCREATE_FPU_PRESERVE | vp,
&xpp,
&xdev
);
}
HRESULT DXSetup()
{
StartTime = GetNow();
HRESULT out = D3DXCreateTeapot(xdev, &teaput, NULL);
if (FAILED(out)) return out;
//----------------------
xvp.X = 0;
xvp.Y = 0;
xvp.Width = desktop_size.x;
xvp.Height = desktop_size.y;
xvp.MinZ = 0.0f;
xvp.MaxZ = 1.0f;
xdev->SetViewport(&xvp);
//----------------------
xlight.Type = D3DLIGHT_POINT;
xlight.Diffuse = D3DXCOLOR(1.0, 1.0, 1.0f, 1.0f);
xlight.Specular = xlight.Diffuse;
xlight.Ambient = xlight.Diffuse;
xlight.Range = 1000.0f;
xlight.Position = D3DXVECTOR3(10.0f, 10.0f, 10.0f);
xlight.Attenuation0 = 1.0f;
xlight.Attenuation1 = 0.0f;
xlight.Attenuation2 = 0.0f;
xdev->SetLight(0, &xlight);
xdev->LightEnable(0, true);
//----------------------
xmaterial.Diffuse = D3DXCOLOR(1.0, 1.0, 0.0f, 1.0f);
xmaterial.Ambient = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
xmaterial.Specular = D3DXCOLOR(1.0, 1.0, 1.0f, 1.0f);
xmaterial.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
xmaterial.Power = 10.0f;
xdev->SetMaterial(&xmaterial);
//----------------------
xdev->SetRenderState(D3DRS_ZENABLE, true);
xdev->SetRenderState(D3DRS_LIGHTING, true);
xdev->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
xdev->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
xdev->SetRenderState(D3DRS_ZWRITEENABLE, true);
xdev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
return 0;
}
void DXDisplay()
{
FrameCount++;
double now_ = GetNow();
xdev->Clear(
0,
NULL,
D3DCLEAR_TARGET |
D3DCLEAR_STENCIL |
D3DCLEAR_ZBUFFER,
0xff0000ff,
1.0,
0);
//------------------------------------------------------------------------------
xdev->BeginScene();
//-------------------------------------------
D3DXMATRIX m, rx, rz;
D3DXMatrixIdentity(&m);
xdev->SetTransform(D3DTS_WORLD, &m);
//-------------------------------------------
D3DXMatrixPerspectiveFovRH(
&m,
M_PI / 4.0f,
(float)xvp.Width / (float)xvp.Height,
1.0f,
1000.0f
);
xdev->SetTransform(D3DTS_PROJECTION, &m);
//-------------------------------------------
D3DXMatrixRotationX(&rx, M_PI / 4.0f);
D3DXMatrixRotationZ(&rz, M_PI / 4.0f);
m = rx * rz;
D3DXMatrixTranslation(&rx, 0.0f, 0.0f, 5.0f);
m = rx * m;
D3DXMatrixInverse(&m, NULL, &m);
xdev->SetTransform(D3DTS_VIEW, &m);
//-------------------------------------------
D3DXMatrixRotationX(&rx, M_PI / 2.0f);
D3DXMatrixRotationZ(&rz, 10.0f * now_ / M_PI);
m = rx * rz;
xdev->SetTransform(D3DTS_WORLD, &m);
teaput->DrawSubset(0);
xdev->EndScene();
//------------------------------------------------------------------------------
xdev->Present(NULL, NULL, NULL, NULL);
}
//------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
void WindowsFunctions()
{
//------------------------------------------------------------------------------
WNDCLASSEX wcx;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = 0;
wcx.lpfnWndProc = WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = GetModuleHandle(0);
wcx.hIcon = LoadIcon(0,IDI_APPLICATION);
wcx.hCursor = LoadCursor(0,IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOWFRAME);
wcx.lpszMenuName = 0;
wcx.lpszClassName = "classname";
wcx.hIconSm = LoadIcon(0,IDI_APPLICATION);
RegisterClassEx(&wcx);
//------------------------------------------------------------------------------
tagRECT desktop_rect;
GetWindowRect(GetDesktopWindow(), &desktop_rect);
HWND hwnd = CreateWindowEx
(
0 ,// dwExStyle
"classname" ,// lpClassName
"title" ,// lpWindowName
WS_POPUP ,// dwStyle
0 ,// x
0 ,// y
desktop_rect.right ,//CW_USEDEFAULT ,// nWidth
desktop_rect.bottom ,// nHeight
0 ,// hWndParent
0 ,// hMenu
0 ,// hInstance
0 // lpParam
);
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
UpdateWindow(hwnd);
DXhwnd = hwnd;
}
WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
DoubleToStr(100.0);
WindowsFunctions();
if FAILED(DXCreateDevice())
MessageBox(NULL, "FAILED CreateDevice!", "MHD_1391", MB_OK);
if FAILED(DXSetup())
MessageBox(NULL, "FAILED Setup!", "MHD_1391", MB_OK);
//------------------------------------------------------------------------------
MSG Msg;
while (true)
{
if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE) > 0)
{
if (Msg.message == WM_QUIT) {
break;
}
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
CurrentTime = GetNow();
DXDisplay();
UpdateFPS();
}
teaput->Release();
xdev->Release();
xd3d->Release();
MessageFPS();
//------------------------------------------------------------------------------
return 0;
}
کد برای VisualStudio:
/*****************
* *
* MHD_1391 *
* *
*****************/
#define _USE_MATH_DEFINES
#include <math.h>
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma hdrstop
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#include <tchar.h>
IDirect3D9 *xd3d;
IDirect3DDevice9 *xdev;
D3DCAPS9 xcaps;
D3DVIEWPORT9 xvp;
D3DPRESENT_PARAMETERS xpp;
D3DLIGHT9 xlight;
D3DMATERIAL9 xmaterial;
ID3DXMesh *teaput;
HWND DXhwnd = NULL;
//------------------------------------------------------------------------------
#define FPS_LIST_COUNT 25
double CurrentTime, StartTime;
int FPS_Index = -1;
double FPSs[FPS_LIST_COUNT];
int FrameCount = 0;
double GetNow()
{
LARGE_INTEGER lic, lif;
QueryPerformanceCounter(&lic);
QueryPerformanceFrequency(&lif);
return (double)lic.QuadPart / (double)lif.QuadPart;
}
void UpdateFPS()
{
FPS_Index++;
if (FPS_Index >= FPS_LIST_COUNT)
{
FPS_Index = 0;
}
FPSs[FPS_Index] = 1.0L / (GetNow() - CurrentTime);
}
char * DoubleToStr(const double d)
{
char * s = new char[20];
if (d == 0.0L)
{
for (int i = 0; i < 20; i++) {
s[i] = '0';
}
s[19] = NULL;
return s;
}
int exponent = (int)log10(fabsl(d));
double num = fabsl(d) / pow(10, (double)exponent);
if (num >= 1.0L)
{
num /= 10.0L;
}
else
exponent--;
num += 0.0000000000001L;
int x;
int i = 0;
if (d < 0.0L)
{
s[0] = '-';
}
else
{
s[0] = '+';
}
s[2] = '.';
while (i < 10)
{
i++;
if (i == 2)
{
i++;
}
num *= 10.0L;
x = num;
s[i] = '0' + x;
num = num - x;
}
if (exponent != 0)
{
i++;
s[i] = 'e';
if (exponent < 0)
{
i++;
s[i] = '-';
exponent = abs(exponent);
}
int expon_10 = log10((double)exponent);
expon_10 = pow(10, (double)expon_10);
while (expon_10 > 0)
{
i++;
s[i] = '0' + (exponent / expon_10);
expon_10 /= 10;
}
}
s[i + 1] = NULL;
return s;
}
void straddstr(char **c1, const char *c2)
{
int i = strlen(*c1),
j = strlen(c2);
char *c3 = (char *)malloc(i + j + 1);
memcpy(c3, *c1, i);
memcpy(c3 + i, c2, j);
c3[i + j] = NULL;
*c1 = c3;
}
void MessageFPS()
{
FPSs[0] = (double)FrameCount / (GetNow() - StartTime);
char nextline[3] = {char(13), char(10), 0};
char *s = "";
char *num;
for (int i = 0; i < FPS_LIST_COUNT; i++)
{
num = DoubleToStr(FPSs[i]);
straddstr(&num, nextline);
if (i == 0)
{
straddstr(&num, nextline);
}
straddstr(&s, num);
}
MessageBoxA(0, s, "MHD_1391", MB_OK);
}
//------------------------------------------------------------------------------
tagPOINT GetScreenSize()
{
tagPOINT p;
tagRECT r;
GetWindowRect(GetDesktopWindow(), &r);
p.x = r.right;
p.y = r.bottom;
return p;
}
tagPOINT desktop_size = GetScreenSize();
HRESULT DXCreateDevice()
{
xd3d = Direct3DCreate9(D3D_SDK_VERSION);
xd3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &xcaps);
DWORD vp;
if (xcaps.Caps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
xpp.BackBufferWidth = desktop_size.x;
xpp.BackBufferHeight = desktop_size.y;
xpp.BackBufferFormat = D3DFMT_A8R8G8B8;
xpp.BackBufferCount = 0;
xpp.MultiSampleType = D3DMULTISAMPLE_NONE;
xpp.MultiSampleQuality = 0;
xpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
xpp.hDeviceWindow = DXhwnd;
xpp.Windowed = true;
xpp.EnableAutoDepthStencil = true;
xpp.AutoDepthStencilFormat = D3DFMT_D24S8;
xpp.Flags = 0;
xpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
xpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
return xd3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
DXhwnd,
D3DCREATE_FPU_PRESERVE | vp,
&xpp,
&xdev
);
}
HRESULT DXSetup()
{
StartTime = GetNow();
HRESULT out = D3DXCreateTeapot(xdev, &teaput, NULL);
if (FAILED(out)) return out;
//----------------------
xvp.X = 0;
xvp.Y = 0;
xvp.Width = desktop_size.x;
xvp.Height = desktop_size.y;
xvp.MinZ = 0.0f;
xvp.MaxZ = 1.0f;
xdev->SetViewport(&xvp);
//----------------------
xlight.Type = D3DLIGHT_POINT;
xlight.Diffuse = D3DXCOLOR(1.0, 1.0, 1.0f, 1.0f);
xlight.Specular = xlight.Diffuse;
xlight.Ambient = xlight.Diffuse;
xlight.Range = 1000.0f;
xlight.Position = D3DXVECTOR3(10.0f, 10.0f, 10.0f);
xlight.Attenuation0 = 1.0f;
xlight.Attenuation1 = 0.0f;
xlight.Attenuation2 = 0.0f;
xdev->SetLight(0, &xlight);
xdev->LightEnable(0, true);
//----------------------
xmaterial.Diffuse = D3DXCOLOR(1.0, 1.0, 0.0f, 1.0f);
xmaterial.Ambient = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
xmaterial.Specular = D3DXCOLOR(1.0, 1.0, 1.0f, 1.0f);
xmaterial.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
xmaterial.Power = 10.0f;
xdev->SetMaterial(&xmaterial);
//----------------------
xdev->SetRenderState(D3DRS_ZENABLE, true);
xdev->SetRenderState(D3DRS_LIGHTING, true);
xdev->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
xdev->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
xdev->SetRenderState(D3DRS_ZWRITEENABLE, true);
xdev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
}
void DXDisplay()
{
FrameCount++;
double now_ = GetNow();
xdev->Clear(
0,
NULL,
D3DCLEAR_TARGET |
D3DCLEAR_STENCIL |
D3DCLEAR_ZBUFFER,
0xff0000ff,
1.0,
0);
//------------------------------------------------------------------------------
xdev->BeginScene();
//-------------------------------------------
D3DXMATRIX m, rx, rz;
D3DXMatrixIdentity(&m);
xdev->SetTransform(D3DTS_WORLD, &m);
//-------------------------------------------
D3DXMatrixPerspectiveFovRH(
&m,
//M_PI
3.14159265358979f / 4.0f,
(float)xvp.Width / (float)xvp.Height,
1.0f,
1000.0f
);
xdev->SetTransform(D3DTS_PROJECTION, &m);
//-------------------------------------------
D3DXMatrixRotationX(&rx, M_PI / 4.0f);
D3DXMatrixRotationZ(&rz, M_PI / 4.0f);
m = rx * rz;
D3DXMatrixTranslation(&rx, 0.0f, 0.0f, 5.0f);
m = rx * m;
D3DXMatrixInverse(&m, NULL, &m);
xdev->SetTransform(D3DTS_VIEW, &m);
//-------------------------------------------
D3DXMatrixRotationX(&rx, M_PI / 2.0f);
D3DXMatrixRotationZ(&rz, 10.0f * now_ / M_PI);
m = rx * rz;
xdev->SetTransform(D3DTS_WORLD, &m);
teaput->DrawSubset(0);
xdev->EndScene();
//------------------------------------------------------------------------------
xdev->Present(NULL, NULL, NULL, NULL);
}
//------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
void WindowsFunctions()
{
//------------------------------------------------------------------------------
WNDCLASSEX wcx;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = 0;
wcx.lpfnWndProc = WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = GetModuleHandle(0);
wcx.hIcon = LoadIcon(0,IDI_APPLICATION);
wcx.hCursor = LoadCursor(0,IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOWFRAME);
wcx.lpszMenuName = 0;
wcx.lpszClassName = L"classname";
wcx.hIconSm = LoadIcon(0,IDI_APPLICATION);
RegisterClassEx(&wcx);
//------------------------------------------------------------------------------
tagRECT desktop_rect;
GetWindowRect(GetDesktopWindow(), &desktop_rect);
HWND hwnd = CreateWindowEx
(
0 ,// dwExStyle
L"classname" ,// lpClassName
L"title" ,// lpWindowName
WS_POPUP ,// dwStyle
0 ,// x
0 ,// y
desktop_rect.right ,//CW_USEDEFAULT ,// nWidth
desktop_rect.bottom ,// nHeight
0 ,// hWndParent
0 ,// hMenu
0 ,// hInstance
0 // lpParam
);
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
UpdateWindow(hwnd);
DXhwnd = hwnd;
}
//WINAPI
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
DoubleToStr(100.0);
WindowsFunctions();
if FAILED(DXCreateDevice())
MessageBox(NULL, L"FAILED CreateDevice!", L"MHD_1391", MB_OK);
if FAILED(DXSetup())
MessageBox(NULL, L"FAILED Setup!", L"MHD_1391", MB_OK);
//------------------------------------------------------------------------------
MSG Msg;
while (true)
{
if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE) > 0)
{
if (Msg.message == WM_QUIT) {
break;
}
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
CurrentTime = GetNow();
DXDisplay();
UpdateFPS();
}
teaput->Release();
xdev->Release();
xd3d->Release();
MessageFPS();
//------------------------------------------------------------------------------
return 0;
}
تو سیستم من تقریبا 1140 فریم بر ثانیه نمایش میدن هم با vs هم C++builder.