• Advertisement

Archived

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

direct draw newbie help

This topic is 6440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering if anyone can write me a simple direct draw program that turns the screen black so I know how to put the code all together. You can post the code here or at my email address: brdn69@hotmail.com It would help me alot considering all the tutorial on the net just show functions and not how to put them together. Please help

Share this post


Link to post
Share on other sites
Advertisement
Hiya,

You may consider picking up Andre Lamothe''s "Tricks of the Windows Game Programming Gurus". It has pretty good descriptions of how to use DirectX. Also, there are many demonstration programs of how everything is put together.

Good luck!

-Krylar

Share this post


Link to post
Share on other sites
Hey,

Another good book is Windows Game Programming for Dummies. It explains how to make Direct X games using Direct Draw and is really well written if you are new to Windows and Direct X programming.

I''ll email you a simple program to turn the screen blank.

Nick

Share this post


Link to post
Share on other sites
is''nt the direct draw screen black by default?
have you read the direc draw readme?
if not you should try it.

//end

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''ll help you out

When you do direct x you basically have 4 very important things to deal with

1. WinMain
2. The windows message loop (which is called from WinMain)
3. The function to update the display (also called from WinMain)
4. A function to create the window, direct draw object, buffers, etc...

Ok, so first here''s a simplified version of my WinMain function


int PASCAL
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// This is the function to create the window,
// direct draw...
if(InitApp(hInstance,nCmdShow)!= DD_OK)
return FALSE;

while (TRUE)
{
// If there is a message waiting...
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage(&msg, NULL, 0, 0))
return msg.wParam;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

//if no message is waiting...
else if (g_bActive)
{
// Update the display
UpdateFrame();

}

// Nothing to do...
else
WaitMessage();

}
}



Now here the function that creates the windows, direct x, etc...

Some things to note:
1. nCmdShow is for the state of the window and hInstance is the handle of the instance of the window (i know, it sounds confusing )

2. This function does not have to return a value, it''s this way because i have it return the return values when creating the direct x stuff. If you don''t want this you''ll have to modify the code below and the code in WinMain.


static HRESULT
InitApp(HINSTANCE hInstance, int nCmdShow)
{

WNDCLASS wc;
HRESULT hRet;

// Set up and register window class
wc.style = CS_HREDRAW / CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;

// This defines the icon for your application
// If you don''t know how to set up an icon yet
// Use "IDI_APPLICATION" for the second parameter
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));

wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NAME;
wc.lpszClassName = NAME;
RegisterClass(&wc);

// Create a window
hWnd = CreateWindowEx(WS_EX_TOPMOST,
NAME,
TITLE,
WS_POPUP,
0,
0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
SetFocus(hWnd);

// Create Direct Draw here, you''ll find tons of tutorials
// on this.

}


Ok now you have WinMain and a function to create the window and whatever else you need to init. Now we get to messages, these are messages (duh ) that windows sends to application. This is what makes multitasking possible, but since (i assume) your creating a game you can ignore most of the messages.



long FAR PASCAL
WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

switch (message)
{
case WM_ACTIVATEAPP:
// Pause if minimized or not the top window
g_bActive = !((BOOL)HIWORD(wParam));
return 0L;

case WM_DESTROY:
// This is where you would release all DX objects
// and do everything you want to do before the
// app ends

// Quit - This is what terminates the app
PostQuitMessage(0);
return 0L;


// If you don''t want to show the cursor use this...
case WM_SETCURSOR:
SetCursor(NULL);
return TRUE;

// Here you can put other stuff, if your just starting
// out you might want to detect key presses, instead
// of using Direct Input
return DefWindowProc(hWnd, message, wParam, lParam);
}





Now your finished with all the windows requirement, and you can actually get to the game (g_pDDSPrimary is my primary buffer)



static void
UpdateFrame(HWND hWnd)
{
// Draw Stuff - This is where a game loop would go
start_game();

while (TRUE)
{
// Flip the Primary and Back Buffer
hRet = g_pDDSPrimary->Flip(NULL, 0);
if (hRet == DD_OK)
break;

if (hRet == DDERR_SURFACELOST)
{
// When surfaces are lost restore them here

}

if (hRet != DDERR_WASSTILLDRAWING)
break;

}

}




That''s it

BTW, to paint the back buffer black, look up the Blt function in the docs, what you have to do is a black colorfill.



+AA_970+

Share this post


Link to post
Share on other sites
Damn, that was me above, here's the same post with the code boxes

I'll help you out

When you do direct x you basically have 4 very important things to deal with

1. WinMain
2. The windows message loop (which is called from WinMain)
3. The function to update the display (also called from WinMain)
4. A function to create the window, direct draw object, buffers, etc...

Ok, so first here's a simplified version of my WinMain function

       
int PASCAL
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// This is the function to create the window,
// direct draw...

if(InitApp(hInstance,nCmdShow)!= DD_OK)
return FALSE;

while (TRUE)
{
// If there is a message waiting...

if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage(&msg, NULL, 0, 0))
return msg.wParam;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

//if no message is waiting...

else if (g_bActive)
{
// Update the display

UpdateFrame();

}

// Nothing to do...

else
WaitMessage();

}
}







Now here the function that creates the windows, direct x, etc...

Some things to note:
1. nCmdShow is for the state of the window and hInstance is the handle of the instance of the window (i know, it sounds confusing <img src="wink.gif" width=15 height=15 align=middle>)

2. This function does not have to return a value, it's this way because i have it return the return values when creating the direct x stuff. If you don't want this you'll have to modify the code below and the code in WinMain.





static HRESULT
InitApp(HINSTANCE hInstance, int nCmdShow)
{

WNDCLASS wc;
HRESULT hRet;

// Set up and register window class

wc.style = CS_HREDRAW / CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;

// This defines the icon for your application

// If you don't know how to set up an icon yet

// Use "IDI_APPLICATION" for the second parameter

wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));

wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NAME;
wc.lpszClassName = NAME;
RegisterClass(&wc);

// Create a window

hWnd = CreateWindowEx(WS_EX_TOPMOST,
NAME,
TITLE,
WS_POPUP,
0,
0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
SetFocus(hWnd);

// Create Direct Draw here, you'll find tons of tutorials
// on this.


}






Ok now you have WinMain and a function to create the window and whatever else you need to init. Now we get to messages, these are messages (duh <img src="wink.gif" width=15 height=15 align=middle>) that windows sends to application. This is what makes multitasking possible, but since (i assume) your creating a game you can ignore most of the messages.






long FAR PASCAL
WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

switch (message)
{
case WM_ACTIVATEAPP:
// Pause if minimized or not the top window

g_bActive = !((BOOL)HIWORD(wParam));
return 0L;

case WM_DESTROY:
// This is where you would release all DX objects

// and do everything you want to do before the

// app ends


// Quit - This is what terminates the app

PostQuitMessage(0);
return 0L;


// If you don't want to show the cursor use this...

case WM_SETCURSOR:
SetCursor(NULL);
return TRUE;

// Here you can put other stuff, if your just starting
// out you might want to detect key presses, instead
// of using Direct Input

return DefWindowProc(hWnd, message, wParam, lParam);
}






Now your finished with all the windows requirement, and you can actually get to the game (g_pDDSPrimary is my primary buffer)





static void
UpdateFrame(HWND hWnd)
{
// Draw Stuff - This is where a game loop would go

start_game();

while (TRUE)
{
// Flip the Primary and Back Buffer

hRet = g_pDDSPrimary->Flip(NULL, 0);
if (hRet == DD_OK)
break;

if (hRet == DDERR_SURFACELOST)
{
// When surfaces are lost restore them here


}

if (hRet != DDERR_WASSTILLDRAWING)
break;

}

}



That's it

BTW, to paint the back buffer black, look up the Blt function in the docs, what you have to do is a black colorfill.



+AA_970+

Edited by - +AA_970+ on July 3, 2000 12:55:46 AM

Edited by - +AA_970+ on July 3, 2000 12:56:49 AM

Share this post


Link to post
Share on other sites
Check out the tutorial on this site called "DirectDraw programming" or something. It contains pretty good source for setting up a basic directdraw program (as long as you have windows initialized).

- Daniel

Share this post


Link to post
Share on other sites

  • Advertisement