#define WIN32_LEAN_AND_MEAN#include <windows.h>//#include <mmsystem.h>#include <ddraw.h>HWND hWnd; LPDIRECTDRAW7 ddraw;LPDIRECTDRAWSURFACE7 ddsFront;LPDIRECTDRAWSURFACE7 ddsBack;LPDIRECTDRAWCLIPPER clipper;HWND createWindow(HINSTANCE hInstance, int nCmdShow);LRESULT CALLBACK windowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);bool initDD();void render();int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ MSG msg; //DWORD last = timeGetTime(); hWnd = createWindow(hInstance, nCmdShow); if (!hWnd) return 1; if (!initDD()) return 2; while (true) { while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) return msg.wParam; TranslateMessage(&msg); DispatchMessage(&msg); } //if (last != timeGetTime()) //{ render(); // last = timeGetTime(); //} }}HWND createWindow(HINSTANCE hInstance, int nCmdShow){ WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = windowProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(NULL, IDI_WINLOGO); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = "WinTest"; wcex.hIconSm = NULL; RegisterClassEx(&wcex); RECT rWindow = {0, 0, 640, 480}; AdjustWindowRect(&rWindow, WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU, false); HWND ret; ret = CreateWindow("WinTest", "WinTest", WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU, CW_USEDEFAULT, 0, rWindow.right - rWindow.left, rWindow.bottom - rWindow.top, NULL, NULL, hInstance, NULL); if (!ret) return NULL; ShowWindow(ret, nCmdShow); UpdateWindow(ret); return ret;}LRESULT CALLBACK windowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;}bool initDD(){ HRESULT ret; DDSURFACEDESC2 DDSD; RECT rSize; ret = DirectDrawCreateEx(NULL, (VOID **)&ddraw, IID_IDirectDraw7, NULL); if (ret != DD_OK) return false; ret = ddraw->SetCooperativeLevel(hWnd, DDSCL_NORMAL); if (ret != DD_OK) return false; ZeroMemory(&DDSD, sizeof(DDSD)); DDSD.dwSize = sizeof(DDSD); DDSD.dwFlags = DDSD_CAPS; DDSD.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; ret = ddraw->CreateSurface(&DDSD, &ddsFront, NULL); if (ret != DD_OK) return false; GetClientRect(hWnd, &rSize); ZeroMemory(&DDSD, sizeof(DDSD)); DDSD.dwSize = sizeof(DDSD); DDSD.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; DDSD.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; DDSD.dwWidth = rSize.right - rSize.left; DDSD.dwHeight = rSize.bottom - rSize.top; ret = ddraw->CreateSurface(&DDSD, &ddsBack, NULL); if (ret != DD_OK) return false; ret = DirectDrawCreateClipper(0, &clipper, NULL); if (ret != DD_OK) return false; ret = clipper->SetHWnd(0, hWnd); if (ret != DD_OK) return false; ret = ddsFront->SetClipper(clipper); return (ret == DD_OK);}void render(){ HRESULT ret; DDBLTFX FX; RECT rView; POINT point; ZeroMemory(&FX, sizeof(FX)); FX.dwSize = sizeof(FX); ret = ddsBack->Blt(NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, &FX); if (ret != DD_OK) return; GetClientRect(hWnd, &rView); point.x = 0; point.y = 0; ClientToScreen(hWnd, &point); OffsetRect(&rView, point.x, point.y); ddsFront->Blt(&rView, ddsBack, NULL, 0, NULL);}
If I remove the comments ("//") the program works fine (lowers framerate).
Is there something wrong with this code?
[edit]comment messed up #include [/edit]
Mox
Edited by - mtsmox on November 8, 2001 12:16:22 PM