Original Post
I have this directdraw program and it runs... kind of :\ it's totaly shambles I know but it's mainly just cause its only purpose right now is to put a pixl on the screen, clean up latter. The problem is that for some reason the pixel seems rather far to the right (like a the 1/5 mark) but a screen shot using print screen shows it in the correct position... I'll take both answers to the question and tips on how to improve the quality of the program :D, besides this the only other thing I've used DD for was reading the screen, luckly for me that actualy works and looks better :)
#include <windows.h>
#include <ddraw.h>
#define WIN32_LEAN_AND_MEAN
HINSTANCE ghinst = 0;
HWND mainwindow = 0;
static DDSCAPS ddscaps;
static LPDIRECTDRAW lpDD = 0;
static LPDIRECTDRAWSURFACE lpDDSPrimary;
static LPDIRECTDRAWSURFACE lpDDSBack;
static DDSURFACEDESC ddsd;
static PAINTSTRUCT ps;
static HDC whdc;
bool CreatePrimarySurface();
bool DirectDrawInit();
void __forceinline updateFrame( void );
void suicide ();
long FAR PASCAL WndProc (HWND myhwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CHAR:
if (wParam == 27)
PostQuitMessage(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return(DefWindowProc(myhwnd,message,wParam,lParam));
}
int FAR PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
WINDOWPLACEMENT status;
WNDCLASS wc;
HWND myhwnd=0;
HDC hdc;
MSG msg;
RECT r;
ghinst = hInst;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = ghinst;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = 0;
wc.lpszMenuName = 0;
wc.hCursor = LoadCursor(0,IDC_ARROW);
wc.hbrBackground = (HBRUSH__ *)GetStockObject(BLACK_BRUSH);
wc.lpszClassName = "DDClass";
if (!RegisterClass(&wc)) return(1);
bool mainloop = 1;
if (!DirectDrawInit())
{
suicide();
exit(0);
}
CreatePrimarySurface();
long* colour = 0;
BYTE setpixel = 1;
while (mainloop)
{
while (PeekMessage(&msg,0,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
{mainloop = 0;}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(setpixel)
{
DDSURFACEDESC DDSurfDesc;
ZeroMemory( &DDSurfDesc, sizeof( DDSurfDesc ));
DDSurfDesc.dwSize = sizeof( DDSurfDesc );
lpDDSBack->Lock( 0, &DDSurfDesc, DDLOCK_WAIT, 0 );
colour =reinterpret_cast<long*>(DDSurfDesc.lpSurface) + 1;
*colour = 0xFF;
lpDDSBack->Unlock( colour );
lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
UpdateWindow(mainwindow);
//lpDDSPrimary->Lock(0,&ddsd,DDLOCK_SURFACEMEMORYPTR,0);
// colour =reinterpret_cast<long*>(ddsd.lpSurface) + 1;
//*colour = 0x00FFFFFF;
//lpDDSPrimary->Unlock(ddsd.lpSurface);
}
}
suicide();
return(1);
}
void suicide ()
{
if (lpDD)
{
if (lpDDSPrimary)
{
lpDDSPrimary->Release();
lpDDSPrimary = 0;
}
lpDD->Release(); lpDD = 0;
}
if (mainwindow)
{
DestroyWindow(mainwindow);
mainwindow = 0;
}
}
bool DirectDrawInit()
{
HDC hdc = GetDC(0);
mainwindow = CreateWindow("DDClass","Menu",WS_VISIBLE | WS_POPUP,0,0,GetDeviceCaps(hdc,HORZRES),GetDeviceCaps(hdc,VERTRES),0,0,ghinst,0);
ReleaseDC(0,hdc);
ShowCursor(0);
HRESULT ddrval;
ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
if( ddrval != DD_OK )
{
return(false);
}
ddrval = lpDD->SetCooperativeLevel( mainwindow, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if( ddrval != DD_OK )
{
lpDD->Release();
return(false);
}
ddrval = lpDD->SetDisplayMode( 800, 600, 32);
if( ddrval != DD_OK )
{
lpDD->Release();
return(false);
}
return(true);
}
bool CreatePrimarySurface()
{
HRESULT ddrval;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
if( ddrval != DD_OK )
{
lpDD->Release();
return(false);
}
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps, &lpDDSBack);
if( ddrval != DD_OK )
{
lpDDSPrimary->Release();
lpDD->Release();
return(false);
}
return true;
}
void __forceinline updateFrame( void )
{
lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
} /* updateFrame */