Original Post
This is wierd code..when I run it, it appears to crash at the 'SetDisplayMode' line, and gives a VC++ debug error, about ESP or something. Here's the code, anyway.
#define WIN32_LEAN_AND_MEAN
#include ddraw.h
#include windows.h
#include windowsx.h
#define INIT_DXSTRUCT(dxstruct) { ZeroMemory(&dxstruct, sizeof(dxstruct)); dxstruct.dwSize = sizeof(dxstruct); }
#define KEYSTATE(key) ((GetAsyncKeyState(key) & 0x8000) ? TRUE : FALSE)
#define RGB_16BIT555(r, g, b) ((r<<10) | (g<<5) | (b))
#define RGB_16BIT565(r, g, b) ((r<<11) | (g<<5) | (b))
LPDIRECTDRAW lpdd7=NULL;
LPDIRECTDRAWSURFACE7 lpddsPrimary=NULL;
LPDIRECTDRAWPALETTE lpddpal = NULL;
DDSURFACEDESC2 ddsd;
PALETTEENTRY palette[256];
int nPixelFormat;
DDPIXELFORMAT pixel;
HWND hwnd;
LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if (msg == WM_DESTROY )
{
PostQuitMessage(0);
return (0);
}
if (msg == WM_CLOSE)
{
if (MessageBox(hwnd,"Do you really want to quit?","Quit?",MB_YESNOCANCEL|MB_ICONINFORMATION)==IDYES)
return(DefWindowProc(hwnd,msg,wparam,lparam));
else
return(0);
}
else
return(DefWindowProc(hwnd,msg,wparam,lparam));
}
int DX_Init()
{
CoInitialize(NULL);
CoCreateInstance(CLSID_DirectDraw,NULL,
CLSCTX_ALL,IID_IDirectDraw7,(void **)&lpdd7);
lpdd7->Initialize(NULL);
lpdd7->SetCooperativeLevel(hwnd,
DDSCL_ALLOWREBOOT|
DDSCL_EXCLUSIVE|
DDSCL_FULLSCREEN);
lpdd7->SetDisplayMode(640,480,16); //Crash line
INIT_DXSTRUCT(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
srand(GetTickCount());
for (int val=0;val < 255; val++)
{
palette[val].peRed = rand() & 0xFF;
palette[val].peBlue = rand() & 0xFF;
palette[val].peGreen = rand() & 0xFF;
palette[val].peFlags= PC_NOCOLLAPSE;
}
lpdd7->CreatePalette(DDPCAPS_8BIT|
DDPCAPS_INITIALIZE,palette,&lpddpal,NULL);
INIT_DXSTRUCT(pixel);
lpddsPrimary->GetPixelFormat(&pixel);
if (pixel.dwGBitMask == 0x07E0)
nPixelFormat = 16;
else
nPixelFormat = 15;
return(0);
}
int ShutDown()
{
lpddsPrimary->Release();
lpddsPrimary = NULL;
lpdd7->Release();
lpdd7 = NULL;
CloseWindow(hwnd);
CoUninitialize();
return(0);
}
inline void PlotPixel(int x,int y, USHORT color,USHORT *buffer)
{
buffer[(y << 9) + (y <<7) +x] = color;
}
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
MSG msg;
WNDCLASSEX myclass;
myclass.cbClsExtra = 0;
myclass.cbSize = sizeof(WNDCLASSEX);
myclass.cbWndExtra = 0;
myclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
myclass.hCursor = LoadCursor(NULL,IDC_ARROW);
myclass.hIcon = LoadIcon(NULL,IDI_WINLOGO);
myclass.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
myclass.hInstance = hinstance;
myclass.lpfnWndProc = MsgHandler;
myclass.lpszClassName = "My Class";
myclass.lpszMenuName = NULL;
myclass.style = CS_HREDRAW|CS_VREDRAW|
CS_DBLCLKS|CS_OWNDC;
RegisterClassEx(&myclass); //Register class
CreateWindowEx(NULL,"My Class","My Window",WS_POPUP|
WS_VISIBLE,0,0,640,480,NULL,NULL,hinstance,NULL);
if (DX_Init())
while(TRUE) //Infite loop!
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_LBUTTONDBLCLK)
break;
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
INIT_DXSTRUCT(ddsd);
lpddsPrimary->Lock(NULL,&ddsd,DDLOCK_WAIT|
DDLOCK_SURFACEMEMORYPTR,NULL);
USHORT* buffer = (USHORT *)ddsd.lpSurface;
int nPitch = (ddsd.lPitch) >> 1;
USHORT color;
if (nPixelFormat = 15)
color=RGB_16BIT555(rand()%32,rand()%32,rand()%32);
else
color=RGB_16BIT565(rand()%32,rand()%64,rand()%32);
if (nPitch = 640)
PlotPixel(rand()%640,rand()%480,color,buffer);
else
lpddsPrimary->Unlock(NULL);
ShutDown();
return(msg.wParam);
}
Any ideas on what could be wrong? Also, any other comments on the code (aside from "Hey, you suck!") are welcome. But please, remember I'm a newbie!
Edited by - DarkAvenger on 9/30/00 9:44:45 PM