These two programs should do exactly the same, yet the DB program runs at 27 FPS, the directx/c++ only at 9:
// Headers
#include <d3d9.h>
#include <d3dx9.h>
#include <time.h>
// Defines
#define SAFE_RELEASE(x)\
if(NULL != (x))\
{\
(x)->Release();\
(x) = NULL;\
}\
// Functions
HWND CreateMainWindow();
LRESULT CALLBACK MessageHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
BOOL InitDirect3D(HWND hWnd);
void CleanUpDirect3D(void);
void CreateD3DFont(int height);
// globals
LPDIRECT3D9 lpD3D = NULL;
LPDIRECT3DDEVICE9 lpD3DDevice = NULL;
LPD3DXFONT lpD3DFont = NULL;
ID3DXSprite* g_pTextSprite = NULL; // Sprite for batching draw text calls
HWND hWnd = 0;
int screenW, screenH;
// main
int wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
{
// set screenres
screenW = 1024;
screenH = 768;
// set window size
hWnd = CreateMainWindow();
RECT rc; GetClientRect(hWnd, &rc);
SetWindowPos(hWnd,HWND_TOP,0,0,1024 + (1024 - rc.right),768 + (768 - rc.bottom),0);
// InitDirect3D
InitDirect3D(hWnd);
CreateD3DFont(14);
D3DXCreateSprite( lpD3DDevice, &g_pTextSprite );
// setup
MSG msg = {0};
int gFPS = 0, frame = 0;
size_t lasttime = clock();
// main loop
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// handle message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// FPS
frame++;
if (clock() - lasttime > CLOCKS_PER_SEC)
{
gFPS = frame;
frame = 0;
lasttime = clock();
}
// Mouse Position
POINT mousePos; GetCursorPos(&mousePos);
POINT windowPos = {0};
ClientToScreen(hWnd,&windowPos);
mousePos.x -= windowPos.x; mousePos.y -= windowPos.y;
// start text
lpD3DDevice->Clear(0, 0,
D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0, 0, 0),
0, 0);
lpD3DDevice->BeginScene();
g_pTextSprite->Begin( D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_DEPTH_BACKTOFRONT );
// "FPS: "
wchar_t FPStext[512] = {0}; wsprintf(FPStext, L"%d", gFPS);
RECT r = { 0, 0, 0, 0 };
lpD3DFont->DrawText(g_pTextSprite, FPStext, -1, &r, DT_NOCLIP, D3DCOLOR_XRGB(255,255,255));
// H-field
for(int x = 0; x < 140; x++)
{
for(int y = 0; y < 88; y++)
{
RECT r = { x * 12 + mousePos.x, y * 12 + mousePos.y, 0, 0 };
lpD3DFont->DrawText(g_pTextSprite, L"H", -1, &r, DT_NOCLIP, D3DCOLOR_XRGB(255,255,255));
}
}
// end text
g_pTextSprite->End();
lpD3DDevice->EndScene();
// Szene anzeigen
lpD3DDevice->Present(0, 0, 0, 0);
}
}
// Freigeben der Objekte
CleanUpDirect3D();
//LocalFree(args);
return 0;
}
// Fenster erzeugen
HWND CreateMainWindow()
{
WNDCLASSEX wndClass =
{
sizeof(WNDCLASSEX), // Groesse angeben
CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW, // Standardstile
MessageHandler, // Callback-Funktion
0, // Zusaetzliche Angaben
0, // nicht benoetigt
NULL, // Anwendungsinstanz
LoadIcon(NULL, IDI_WINLOGO), // Windows-Logo
LoadCursor(NULL, IDC_ARROW), // Normaler Cursor
(HBRUSH)GetStockObject(WHITE_BRUSH), // Weisser Pinsel
NULL, // kein Menue
L"WindowClass", // Der Name der Klasse
LoadIcon(NULL, IDI_WINLOGO) // Windows Logo
};
// Klasse registrieren
RegisterClassEx(&wndClass);
return CreateWindowEx(NULL,
L"WindowClass",
L"s",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
0, 0, screenW, screenH,
NULL,
NULL,
NULL,
NULL);
}
LRESULT CALLBACK MessageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// testen, um welche Nachticht es sich handelt
switch(msg)
{
// Programm beenden, wenn das Fenster
// geschlossen wird
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYDOWN:
PostQuitMessage(0);
break;
}
// Standardnachrichtenverarbeitung von Windows
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// Direct3D initialisieren
BOOL InitDirect3D(HWND hWnd)
{
// Direct3D-Objekt erzeugen
lpD3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS PParams;
ZeroMemory(&PParams, sizeof(PParams));
PParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
PParams.hDeviceWindow = hWnd;
PParams.Windowed = true;
PParams.BackBufferWidth = screenW;
PParams.BackBufferHeight = screenH;
PParams.BackBufferFormat = D3DFMT_A8R8G8B8;
PParams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
// Direct3D-Geraet anlegen
lpD3D->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&PParams,
&lpD3DDevice);
return TRUE;
}
void CreateD3DFont(int height)
{
D3DXCreateFont( lpD3DDevice, -height, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, NONANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
L"Courier New", &lpD3DFont );
lpD3DFont->PreloadGlyphs(0,255);
}
// Direct3D Objekte freigeben
void CleanUpDirect3D(void)
{
SAFE_RELEASE(lpD3DFont);
SAFE_RELEASE(g_pTextSprite);
SAFE_RELEASE(lpD3DDevice);
SAFE_RELEASE(lpD3D);
}
#constant DEF_WIDTH (140)
#constant DEF_HEIGHT (88)
randomize timer()
sync on:sync rate 60
d3d_init
d3d_font 1,"Courier New",14,1,0,0
shiftX# = 0
shiftY# = 0
hide mouse
do
cls
d3d_starttext
shiftX# = mousex()
shiftY# = mousey()
for x = 0 to DEF_WIDTH - 1
for y = 0 to DEF_HEIGHT - 1
d3d_text 1,x * 12 + shiftX#, y * 12 + shiftY#,0,"H"
next y
next x
d3d_text 1,0,0,0,str$(screen fps())
d3d_endtext
sync
loop