// Basic Program to Draw a Window
#define WIN32_LEAN_AND_MEAN
#define RGB_16BIT(r, g, b) ((r << 11) | (g << 5) | (b))
#define INIT_DXSTRUCT(dxs) { ZeroMemory(&dxs, sizeof(dxs)); dxs.dwSize = sizeof(dxs); }
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <time.h>
#include <ddraw.h>
bool RunApp = TRUE;
LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if(msg == WM_PAINT)
{
PAINTSTRUCT ps;
EndPaint(hwnd, &ps);
return(0);
}
if(msg == WM_QUIT)
{
RunApp = FALSE;
return(0);
}
if(msg == WM_DESTROY)
{
RunApp = FALSE;
PostQuitMessage(0);
return(0);
}
if(msg == WM_CLOSE)
{
RunApp = FALSE;
PostQuitMessage(0);
return(0);
}
return(DefWindowProc(hwnd, msg, wparam, lparam));
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
srand((unsigned)time(NULL));
MSG msg;
WNDCLASSEX BasicWindow;
BasicWindow.cbSize = sizeof(WNDCLASSEX);
BasicWindow.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
BasicWindow.lpfnWndProc = MsgHandler;
BasicWindow.cbClsExtra = 0;
BasicWindow.cbWndExtra = 0;
BasicWindow.hInstance = hInstance;
BasicWindow.hIcon = LoadIcon(NULL, IDI_WINLOGO);
BasicWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
BasicWindow.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
BasicWindow.lpszMenuName = NULL;
BasicWindow.lpszClassName = "Basic Window";
BasicWindow.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
RegisterClassEx(&BasicWindow);
HWND hwnd;
if(!(hwnd = CreateWindowEx(NULL,
"Basic Window",
"Basic Window",
WS_VISIBLE | WS_CAPTION | WS_TILEDWINDOW,
0,
0,
640,
480,
NULL,
NULL,
hInstance,
NULL)))
{
return(0);
}
DDSURFACEDESC2 ddsd;
DDSURFACEDESC2 ddsdoffscreen;
LPDIRECTDRAW7 lpdd7 = NULL;
LPDIRECTDRAWSURFACE7 lpddsPrimary = NULL;
LPDIRECTDRAWSURFACE7 lpddsBack = NULL;
LPDIRECTDRAWSURFACE7 lpddsOffScreen = NULL;
INIT_DXSTRUCT(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_COMPLEX |
DDSCAPS_FLIP |
DDSCAPS_VIDEOMEMORY;
DirectDrawCreateEx(NULL, (void**)lpdd7, IID_IDirectDraw7, NULL);
lpdd7 -> SetCooperativeLevel((hwnd, DDSCL_ALLOWREBOOT | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
lpdd7 -> SetDisplayMode(640, 480, 16, 0, 0);
if(FAILED(lpdd7 -> CreateSurface(&ddsd, &lpddsPrimary, NULL)))
{
// Error handling code....possibly a message box with the error and exit.
return 0;
}
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if(FAILED(lpddsPrimary -> GetAttachedSurface(&ddsd.ddsCaps, &lpddsBack)))
{
// Error code, another message box.
return 0;
}
INIT_DXSTRUCT(ddsdoffscreen);
ddsdoffscreen.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsdoffscreen.dwWidth = 640;
ddsdoffscreen.dwHeight = 480;
ddsdoffscreen.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN |
DDSCAPS_VIDEOMEMORY;
if(FAILED(lpdd7 -> CreateSurface(&ddsdoffscreen, &lpddsOffScreen, NULL)))
{
// error code, messagebox
return 0;
}
while(RunApp == TRUE)
{
lpddsPrimary -> Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
lpddsPrimary -> Unlock(NULL);
}
return(msg.wParam);
}
Problem With A Run-Time Error
Hi Folks,
I''ve finished with my Windows GDI studies and have a good grasp of the Windows programming model. I just need some help with DirectDraw now. I''m having a some trouble with just initializing the windows and surfaces and I''m hoping someone can help me out. The program compiles and builds correctly, but during run-time I get an error and when I debug is points at the SetCooperativeLevel function. Here is my source:
Also, I am following the Game Programming Genesis series of articles and I am looking to make a 2D scroller, so I would like to use DirectDraw and not Direct3D 8. Thanks. Any help is appreciated.
Mike
You didn''t actually create a DirectDraw7 object.
try this:
DirectDrawCreateEx(NULL, (void**)&lpdd7, IID_IDirectDraw7, NULL);
instead of this:
DirectDrawCreateEx(NULL, (void**)lpdd7, IID_IDirectDraw7, NULL);
Take a look at the function docs 4 more info
Alaa,
try this:
DirectDrawCreateEx(NULL, (void**)&lpdd7, IID_IDirectDraw7, NULL);
instead of this:
DirectDrawCreateEx(NULL, (void**)lpdd7, IID_IDirectDraw7, NULL);
Take a look at the function docs 4 more info
Alaa,
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement