Thanks for your reply @frob the following is the complete CPP source code:
#define STRICT
#define D3D_OVERLOADS
#include <windows.h>
#include <d3d.h>
#pragma comment(lib, "ddraw.lib")
#pragma comment(lib, "dxguid.lib")
#define WIN_WIDTH 200
#define WIN_HEIGHT 200
struct RENDERDIMENSIONS { DWORD dwWidth; DWORD dwHeight; };
LPDIRECTDRAW7 _dd;
LPDIRECTDRAWSURFACE7 _frontBuffer;
LPDIRECTDRAWSURFACE7 _backBuffer;
LPDIRECT3D7 _d3d;
LPDIRECT3DDEVICE7 _d3dDevice;
LPDIRECT3DVERTEXBUFFER7 v_buffer;
RECT _renderRect;
RENDERDIMENSIONS _renderDimensions;
D3DVERTEX _triangleVertices[3];
void isDXError(HRESULT hRet, char *msg) {
if (FAILED(hRet)) {
MessageBoxA(0, msg, "DirectX Error", MB_ICONERROR);
exit(0);
}
}
void createFrontBuffer() {
HRESULT hRet;
DDSURFACEDESC2 ddsd;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hRet = _dd->CreateSurface(&ddsd, &_frontBuffer, NULL);
isDXError(hRet, "CreateSurface DDSCAPS_PRIMARYSURFACE");
}
void GetRenderDimensions(HWND hwnd) {
GetClientRect(hwnd, &_renderRect);
ClientToScreen(hwnd, (POINT*)&_renderRect.left);
ClientToScreen(hwnd, (POINT*)&_renderRect.right);
_renderDimensions.dwWidth = _renderRect.right - _renderRect.left;
_renderDimensions.dwHeight = _renderRect.bottom - _renderRect.top;
}
void createBackBuffer(HWND hwnd) {
HRESULT hRet;
DDSURFACEDESC2 ddsd;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY;
ddsd.dwWidth = _renderDimensions.dwWidth;
ddsd.dwHeight = _renderDimensions.dwHeight;
hRet = _dd->CreateSurface(&ddsd, &_backBuffer, NULL);
isDXError(hRet, "DDSCAPS_OFFSCREENPLAIN");
}
void createD3D() {
HRESULT hRet;
hRet = _dd->QueryInterface(IID_IDirect3D7, (LPVOID *)&_d3d);
isDXError(hRet, "QueryInterface _d3d");
}
void createD3DDevice() {
HRESULT hRet;
hRet = _d3d->CreateDevice(IID_IDirect3DHALDevice, _backBuffer, &_d3dDevice);
if (FAILED(hRet)) {
hRet = _d3d->CreateDevice(IID_IDirect3DRGBDevice, _backBuffer, &_d3dDevice);
isDXError(hRet, "CreateDevice");
}
}
void createBuffers(HWND hwnd) {
createFrontBuffer();
createBackBuffer(hwnd);
}
void createDirectDraw(HWND hwnd) {
HRESULT hRet;
hRet = DirectDrawCreateEx(NULL, (VOID**)&_dd, IID_IDirectDraw7, NULL);
isDXError(hRet, "DirectDrawCreateEx");
hRet = _dd->SetCooperativeLevel(hwnd, DDSCL_NORMAL);
isDXError(hRet, "SetCooperativeLevel");
}
void createClipper(HWND hwnd) {
HRESULT hRet;
LPDIRECTDRAWCLIPPER clipper;
hRet = _dd->CreateClipper(0, &clipper, NULL);
isDXError(hRet, "CreateClipper");
clipper->SetHWnd(0, hwnd);
_frontBuffer->SetClipper(clipper);
}
void initD3D(HWND hwnd) {
GetRenderDimensions(hwnd);
createDirectDraw(hwnd);
createBuffers(hwnd);
createClipper(hwnd);
createD3D();
createD3DDevice();
}
void render() {
D3DCOLOR backgroundColor = D3DRGBA(.0f, .2f, .4f, 1.0f);
_d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, backgroundColor, 0, 0);
_frontBuffer->Blt(&_renderRect, _backBuffer, NULL, DDBLT_WAIT, NULL);
}
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
RECT getWindowCenter() {
RECT rect;
GetClientRect(GetDesktopWindow(), &rect);
rect.left = (rect.right / 2) - (WIN_WIDTH / 2);
rect.top = (rect.bottom / 2) - (WIN_HEIGHT / 2);
return rect;
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) {
MSG msg;
HWND hwnd;
WNDCLASSW wc;
RECT rect = getWindowCenter();
memset(&wc, 0, sizeof(WNDCLASSW));
memset(&msg, 0, sizeof(MSG));
wc.lpszClassName = L"myWin";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpfnWndProc = WndProc;
RegisterClassW(&wc);
hwnd = CreateWindowW(
wc.lpszClassName,
L"DirectX 7.0 Triangle",
WS_OVERLAPPEDWINDOW,
rect.left,
rect.top,
WIN_WIDTH,
WIN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
initD3D(hwnd);
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
render();
}
return msg.wParam;
}
I'm a C# developer so excuse me if I used an ugly naming convention.
There is nothing fancy or strange here, I am measuring the CPU consumption with the Task Manager, why should I need something else when the code is so simple? why is the equivalent DirectX 9.0 example running without any noticeable increase in CPU usage?
By downloading and checking Microsoft's DXSDK you can see the triangle code sample by yourself and compare the 7.0 version with 9.0, paths are:
Visual C++ 4.0 - ..\dx7sdk-7001\samples\multimedia\d3dim\src\tutorials\triangle
Visual Studio 2005 (Express works as well) - ..\dxsdk_aug2007\Samples\C++\Direct3D\Tutorials\Tut03_Matrices