Original Post
I''ve posted some code that I''m having a problem with. The surface is getting lost. I''m at my wits end with this one. I can''t see why it''s happening. All this app is suppose to do is plot an animated pixel to the backbuffer and flip(). The source is listed below. Please help if you can.
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#define MSG(msg) MessageBox(NULL, _T(msg), _T("Error"), MB_OK|MB_ICONEXCLAMATION)
#define SAFE_RELEASE(a) { if(a) { (a)->Release(); (a)=NULL; } }
#define RGB565(r, g, b) (WORD)((r << 11) + (g << 5) + (b))
#define RGB555(r, g, b) (WORD)((r << 10) + (g << 5) + (b))
#define RGBA32(r, g, b, a) (DWORD)((a << 32) + (r << 24) + (g << 16) + (b))
void PlotPixel(int x, int y, LONG pitch, DWORD *buffer)
{
buffer[x+y*pitch] = RGBA32(255, 255, 255, 255);
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wc;
HRESULT hr;
HWND hWnd;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = reinterpret_cast(GetStockObject(WHITE_BRUSH));
wc.lpszMenuName = NULL;
wc.lpszClassName = _T("CyberDot");
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
_T("CyberDot"),
_T("CyberDot"),
WS_OVERLAPPEDWINDOW,
100, 100, 20, 20,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
LPDIRECTDRAW7 lpDD = NULL;
LPDIRECTDRAWSURFACE7 lpddsPrimary = NULL;
LPDIRECTDRAWSURFACE7 lpddsBackBuffer = NULL;
int x, y, xvel, yvel;
if( FAILED( hr = DirectDrawCreateEx( NULL, (VOID**)&lpDD, IID_IDirectDraw7, NULL ) ) )
{
MSG("DirectDrawCreateEx");
return E_FAIL;
}
if( FAILED( hr = lpDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT)))
return E_FAIL;
if( FAILED( hr = lpDD->SetDisplayMode(640, 480, 32, 0, 0))) return E_FAIL;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP|DDSCAPS_3DDEVICE;
if(FAILED(lpDD->CreateSurface(&ddsd, &lpddsPrimary, NULL))) return E_FAIL;
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if(FAILED(lpddsPrimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsBackBuffer))) return E_FAIL;
lpddsBackBuffer->AddRef();
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
x = 320; y = 240;
xvel = 5; yvel = 10;
MSG msg;
while(TRUE)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_KEYDOWN)
{
if(msg.wParam == VK_ESCAPE) break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
x += xvel;
y += yvel;
if( ((x<10) || (x>630) || (y<10) || (y>470)) )
{
xvel = -xvel;
yvel = -yvel;
}
if(FAILED(hr = lpddsBackBuffer->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT, NULL)))
{
switch(hr)
{
case DDERR_INVALIDOBJECT:
MSG("DDERR_INVALIDOBJECT: ");
break;
case DDERR_INVALIDPARAMS:
MSG("DDERR_INVALIDPARAMS: ");
break;
case DDERR_OUTOFMEMORY:
MSG("DDERR_OUTOFMEMORY: ");
break;
case DDERR_SURFACEBUSY:
MSG("DDERR_SURFACEBUSY: ");
break;
case DDERR_SURFACELOST:
MSG("DDERR_SURFACELOST: ");
break;
case DDERR_WASSTILLDRAWING:
MSG("DDERR_WASSTILLDRAWING: ");
break;
}
}
DWORD *buffer = (DWORD *)ddsd.lpSurface;
PlotPixel(x, y, ddsd.lPitch, buffer);
if(FAILED(lpddsBackBuffer->Unlock(NULL))) MSG("Unlock-Error");
while(FAILED(lpddsPrimary->Flip(NULL, DDFLIP_WAIT)));
Sleep(500);
}
SAFE_RELEASE(lpddsBackBuffer);
SAFE_RELEASE(lpddsPrimary);
SAFE_RELEASE(lpDD);
return 0;
}