Archived

This topic is now archived and is closed to further replies.

liquid_ice_programmer

point ploting with directdraw 7?

Recommended Posts

i am sorry about the length of code but i have no idea what the problem is. the code compile but is automatically closes and it should plot a point a 320X240. what is wrong: // Include Files #include <windows.h> #include <string.h> #include <ddraw.h> #include <ddrawex.h> // Function Prototypes LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); void RegisterWindowClass(HINSTANCE hInstance); void CreateAppWindow(HINSTANCE hInstance); HRESULT WINAPI DirectDrawCreateEx(GUID FAR *lpGUID, LPVOID *lplpDD, REFIID iid, IUnknown FAR *pUnkOuter); inline void Plot8(int x, int y, UCHAR color, UCHAR *buffer, int mempitch) { buffer[x+y*mempitch] = color; } WPARAM StartMessageLoop(); int CreateDirectDrawWindow(); int CreateColorPalette(); int CreateDirectDrawSurface(); int CreateSurfacePalette(); int LockSurface(); int UnLockSurface(); // Global Variable HWND g_hWnd; LPDIRECTDRAW7 lpDD = NULL; LPDIRECTDRAWPALETTE lpDDPal = NULL; LPDIRECTDRAWSURFACE7 lpddsprimary = NULL; DDSURFACEDESC2 ddsd; PALETTEENTRY palette[256]; int color = 0; ////////////////// // WindMain() ////////////////// INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT) { RegisterWindowClass(hInstance); CreateAppWindow(hInstance); ShowWindow(g_hWnd, SW_SHOWDEFAULT); UpdateWindow(g_hWnd); INT result = StartMessageLoop(); return result; } /////////////// // WndProc /////////////// LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: return 0; case WM_DESTROY: PostQuitMessage( 0 ); return 0; case WM_PAINT: ValidateRect(g_hWnd, NULL); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); } ///////////////////////////// // RegisterWindowClass() ///////////////////////////// void RegisterWindowClass(HINSTANCE hInstance) { WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "Window Appication"; wc.hIconSm = NULL; RegisterClassEx(&wc); } ///////////////////////// // CreateAppWindow() ///////////////////////// void CreateAppWindow(HINSTANCE hInstance) { g_hWnd = CreateWindowEx( NULL, "Window Appication", "Basic Window Appication", WS_POPUP, 100, 100, 800, 600, GetDesktopWindow(), NULL, hInstance, NULL); } ////////////////////////// // StartMessageLoop() ////////////////////////// WPARAM StartMessageLoop() { MSG msg; while(1) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { // Use idle time here CreateDirectDrawWindow(); CreateColorPalette(); CreateDirectDrawSurface(); CreateSurfacePalette(); LockSurface(); Plot8(320, 240, 100, (UCHAR *)ddsd.lpSurface, (int) ddsd.lPitch); UnLockSurface(); } } return msg.wParam; } //////////////////////////////// // CreateDirectDrawWindow() //////////////////////////////// CreateDirectDrawWindow() { DirectDrawCreateEx(NULL, (void **)&lpDD, IID_IDirectDraw7, NULL); lpDD->SetCooperativeLevel(g_hWnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWMODEX | DDSCL_ALLOWREBOOT); lpDD->SetDisplayMode(640, 480, 8, 0, 0); return 0; } //////////////////////// // CreateColorPalette(); //////////////////////// CreateColorPalette() { for (color = 1; color < 255; color++) { palette[color].peRed = rand()%256; palette[color].peGreen = rand()%256; palette[color].peBlue = rand()%256; palette[color].peFlags = PC_NOCOLLAPSE; } palette[0].peRed = 0; palette[0].peGreen = 0; palette[0].peBlue = 0; palette[0].peFlags = PC_NOCOLLAPSE; palette[255].peRed = 255; palette[255].peGreen = 255; palette[255].peBlue = 255; palette[255].peFlags = PC_NOCOLLAPSE; lpDD->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, palette, &lpDDPal, NULL); return 0; } //////////////////////////////// //CreateDirctDrawSurface(); //////////////////////////////// CreateDirectDrawSurface() { memset(&ddsd,0,sizeof(ddsd)); //CreateDirectDrawDesc ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; lpDD->CreateSurface(&ddsd, &lpddsprimary, NULL); return 0; } ////////////////////////////// //CreateSurfacePalette(); ////////////////////////////// CreateSurfacePalette() { lpddsprimary->SetPalette(lpDDPal); return 0; } ///////////////////// //LockSurface(); ///////////////////// LockSurface() { memset(&ddsd,0,sizeof(ddsd)); //CreateDirectDrawDesc ddsd.dwSize = sizeof(ddsd); lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL); return 0; } /////////////////////// //UnLockSurface(); /////////////////////// UnLockSurface() { lpddsprimary->Unlock(NULL); return 0; }

Share this post


Link to post
Share on other sites
Can''t tell for sure but I think calling the following...

CreateDirectDrawWindow();
CreateColorPalette();
CreateDirectDrawSurface();
CreateSurfacePalette();
LockSurface();
Plot8(320, 240, 100, (UCHAR *)ddsd.lpSurface, (int) ddsd.lPitch);
UnLockSurface();

Inside of a loop is a bad idea you only want to call those create functiosn once at the start of your application. Calling them multiple time might be whats causing your crash.

Share this post


Link to post
Share on other sites
Yeah as Ilumini said you shouldnt be creating every frame:
you need to create it BEFORE the message loop so the direct draw interfaces are only created once, then you can use them
this should work:


  
WPARAM StartMessageLoop()
{
MSG msg;

//initialise ddraw interfaces

CreateDirectDrawWindow();
CreateColorPalette();
CreateDirectDrawSurface();
CreateSurfacePalette();

while(1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else //nothing to do - do game

{
//draw the pixels

LockSurface();
Plot8(320, 240, 100, (UCHAR *)ddsd.lpSurface, (int) ddsd.lPitch);
UnLockSurface();
}
}
return msg.wParam;
}



That should fix it

If not you can email me ashley_canning@ntlworld.com

Cya
Ash

Share this post


Link to post
Share on other sites