Help a n00b with DirectDraw D;

Started by
9 comments, last by yochai_vb9 20 years ago
Hi there! I was reading Game Programming Genesis tutorial and started to make my own code. I've tried EVERYTHING Including copying / pasting / changing major stuff, using different methods and even going line by line on the code. Nothing helps. I wanna get an output of my image to the Front Buffer. Sounds diffcult? sure not. BUT ofcourse when you're trying to do some cool thing it fails. Basically, the program is supposed to use Front/Back buffer with flip method and I got an Offscreen buffer which I am trying to Blt onto the Back buffer but something goes wrong unfortunatly. If you will PLEASE check the code and you may find any error / mistake / other way / a miracle, please tell me. I've been trying to figure this out for 4 days now. Thanks! The code I've written is:

//Main Include files

#include <windows.h>
#include <ddraw.h>

//Macros

#define INIT_DXSTRUCT(dxs) {ZeroMemory(&dxs, sizeof(dxs)); dxs.dwSize = sizeof(dxs);}

//Global common variables

LPDIRECTDRAW7 g_dd7 = NULL;
DDSURFACEDESC2 g_sd;
LPDIRECTDRAWSURFACE7 g_sPrimary = NULL;  
LPDIRECTDRAWSURFACE7 g_sBack = NULL;  
LPDIRECTDRAWSURFACE7 g_sBitmap1 = NULL;  

long bx,by = 0;

//Function Prototypes

int Init_DDraw(HWND); 
int Create_Surfaces();
int Show_Bitmap();
int Show_Text();
void ReleaseALL();
bool p_Active = true;

void ReleaseALL()
{
	g_sPrimary->Release();
	g_sPrimary = NULL;
	g_dd7->Release(); 
    g_dd7 = NULL; 
	ShowCursor(true);
	KillTimer(NULL,1);
	exit(1);
}

int Init_DDraw(HWND t_hwnd)
{
	if (FAILED(DirectDrawCreateEx(NULL, (LPVOID*)&g_dd7, IID_IDirectDraw7, NULL)))
		return (false);

	if (FAILED(g_dd7->SetCooperativeLevel(t_hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
		return (false);

	if (FAILED(g_dd7->SetDisplayMode(1024,768,32,0,0)))
		return (false);

	return (true);
}

int Create_Surfaces()
{
	//Primary Buffer

	INIT_DXSTRUCT(g_sd);                              
	g_sd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;  
	g_sd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;   
	g_sd.dwBackBufferCount = 1; 
        
	if (FAILED(g_dd7->CreateSurface(&g_sd, &g_sPrimary, NULL)))
		return (false);

	//Back Buffer

	INIT_DXSTRUCT(g_sd);
	g_sd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
	if (FAILED(g_sPrimary->GetAttachedSurface(&g_sd.ddsCaps, &g_sBack)))
		return (false);		

	//Bitmap 1

	INIT_DXSTRUCT(g_sd);
	g_sd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; 
	g_sd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; 
	g_sd.dwHeight = 480; 
	g_sd.dwWidth = 640; 
	g_sd.ddckCKSrcBlt.dwColorSpaceLowValue  = RGB(0,0,0);
	g_sd.ddckCKSrcBlt.dwColorSpaceHighValue = RGB(0,0,0);
	if (FAILED(g_dd7->CreateSurface(&g_sd, &g_sBitmap1, NULL)))
		return (false);

	return (true);
}

int Show_Bitmap(LPDIRECTDRAWSURFACE7 g_sBitRes, LPDIRECTDRAWSURFACE7 g_sB, LPDIRECTDRAWSURFACE7 g_sP, int xDest, int yDest, LPCTSTR bFile)
{
	HDC hSrcDC;         
	HDC hDestDC;       
	HBITMAP hbitmap;    
	BITMAP bmp;      
	int nHeight, nWidth; 
/*	DDBLTFX fx;
	
	INIT_DXSTRUCT(fx);
	fx.dwFillColor = 0; 
	g_sBack->Blt(NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, &fx);*/

	if ((hbitmap = (HBITMAP)LoadImage(NULL, bFile, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE)) == NULL)
		return (false);


	if ((hSrcDC = CreateCompatibleDC(NULL)) == NULL)
		return (false);

 
	if (SelectObject(hSrcDC, hbitmap) == NULL)
	{
		DeleteDC(hSrcDC);
		return (false);
	}


	if (GetObject(hbitmap, sizeof(BITMAP), &bmp) == 0)
	{
		DeleteDC(hSrcDC);
		return (false);
	}

	nWidth = bmp.bmWidth;
	nHeight = bmp.bmHeight;

	if (FAILED(g_sBitRes->GetDC(&hDestDC)))
	{
		DeleteDC(hSrcDC);
		return (false);
	}

	if (BitBlt(hDestDC, xDest, yDest, nWidth, nHeight, hSrcDC, 0, 0, SRCCOPY) == NULL)
	{
		g_sBitRes->ReleaseDC(hDestDC);
		DeleteDC(hSrcDC);
		return (false);
	}

	g_sB->BltFast(0, 0, g_sBitRes, NULL, DDBLTFAST_SRCCOLORKEY);
	g_sP->Flip(g_sB,0);

	g_sBitRes->ReleaseDC(hDestDC);
	DeleteDC(hSrcDC);

  return (true);
}

int Show_Text(LPCTSTR uMessage, LPDIRECTDRAWSURFACE7 lpdds, COLORREF BackColor, COLORREF txtColor, int xPos, int yPos)
{      
	HDC hDestDC;       

	if (FAILED(lpdds->GetDC(&hDestDC)))
		return (false);

	SetBkColor(hDestDC, BackColor); 
    SetTextColor(hDestDC, txtColor); 
    TextOut(hDestDC, xPos, yPos, uMessage, lstrlen(uMessage)); 
    
	lpdds->ReleaseDC(hDestDC); 

  return (true);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "DirectDraw First";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        0,
        "DirectDraw First",
        "DirectDraw First",
        WS_POPUP| WS_VISIBLE,
        512 / 2, 384 / 2, 640, 480,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

	if (NULL == Init_DDraw(hwnd))
	{
		MessageBox(hwnd, "Intializing DirectDraw Failed!", "Failed!", MB_OK);
		ReleaseALL();
	}

	ShowCursor(false);

	if (NULL == Create_Surfaces())
	{
		MessageBox(hwnd, "Creating surfaces Failed!", "Failed!", MB_OK);
		ReleaseALL();
	}	
	
	MessageBox (hwnd, "All the process has gone well", "Done", MB_OK);

	while (p_Active)
	{
		if (GetMessage(&Msg, NULL, 0, 0) > 0)
		{
			if (Msg.wParam == VK_ESCAPE)
				p_Active = false;
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}

		if (NULL == (Show_Bitmap(g_sBitmap1,g_sBack,g_sPrimary,0 ,0,"Energy.bmp")))
		{
			MessageBox(hwnd, "Showing bitmap Failed!", "Failed!", MB_OK);
			ReleaseALL();
		}

		/*if (NULL == Show_Text("Hello, DirectDraw World!",g_sBack,RGB(0,0,255),RGB(0,255,255),0,25))
		{
			MessageBox(hwnd, "Showing text Failed!", "Failed!", MB_OK);
			ReleaseALL();
		}*/
	}
	ReleaseALL();
    return Msg.wParam;
}

I am getting an output of starnge lines on a black background, as if some mobile phone was dialing and disturbing the screen. [edited by - yochai_vb9 on April 22, 2004 1:42:01 PM]
After the Brood War expansion the three sides in StarCraft were balanced so perfectly you could use them to align pictures on your wall.
Advertisement
Actually, noob is a very commom way to spell newbie if you''ve ever played any online games. You may want to check your grammar as well. You asked a question and put a period at the end. Your sentence "Learn to spell then come ask for help." is grammatically incorrect also. What''s the point of pointing this out I wonder? NOTHING. Now for a serious post...

It''s been a while since I''ve seen DirectDraw, and it was an older version than this, so I loaded the images from file manually and copied byte by byte to the buffers. Have you thought about moving to Direct3D and using the D3DXSPRITE class?
I need to use DirectDraw. I don't want to go into 3D now.

Oh, and I'm really sorry for not being the English Academy >

[edited by - yochai_vb9 on April 23, 2004 3:27:11 AM]
After the Brood War expansion the three sides in StarCraft were balanced so perfectly you could use them to align pictures on your wall.
quote:Original post by Anonymous Poster
Actually, noob is a very commom way to spell newbie if you''ve ever played any online games. You may want to check your grammar as well. You asked a question and put a period at the end. Your sentence "Learn to spell then come ask for help." is grammatically incorrect also. What''s the point of pointing this out I wonder? NOTHING. Now for a serious post...


By the way, if anyone''s wondering what this is about, there was a flame post that I deleted while this was being written. Apologies for any confusion.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

quote:Original post by superpig
quote:Original post by Anonymous Poster
Actually, noob is a very commom way to spell newbie if you''ve ever played any online games. You may want to check your grammar as well. You asked a question and put a period at the end. Your sentence "Learn to spell then come ask for help." is grammatically incorrect also. What''s the point of pointing this out I wonder? NOTHING. Now for a serious post...


By the way, if anyone''s wondering what this is about, there was a flame post that I deleted while this was being written. Apologies for any confusion.


O_o Huh?
After the Brood War expansion the three sides in StarCraft were balanced so perfectly you could use them to align pictures on your wall.
I haven''t used DirectDraw in a while, but if I used to get dodgy lines etc when the surface I was trying to blit wasn''t initialised properly. Make sure you are correctly initialising structures before loading the bitmap surface. Also a good idea might be to load a ddraw surface with the bitmap, and then blit the surface every frame. I see you load and blit the bitmap every frame, that has to be inefficient and COULD be the cause of your problem
u learning rasterizers for its own sake or for the sake of mobile games ?? I reckon use a dx9 or 9 textured quad filling the screen and use the calls texture->Lock(...) and use the pointer to pixels that way.

I''m waiting for the radeon phone ... its gonna be real hot ... maybe it will even start up a new cancer scare???
I''m sorry, but what exactly do you want me to do with Direct3D9? Isn''t it better to first learn the basics like direct draw and make a tile game instead of jumping straight to direct draw? Or am I just mistaken?

Even though, I want to solve this.
After the Brood War expansion the three sides in StarCraft were balanced so perfectly you could use them to align pictures on your wall.
The other poster was suggesting you use Direct3D because you can do 2D with Direct3D plus you get all the benefits of using the 3D hardware and it saves you learning something which is deprecated.

Check out the tutorials at 32bits.co.uk or elsewhere on the net for more help.

Jx
There''s really no reason to learn DirectDraw if you intend to eventually move on to Direct3D; short of some basic graphics concepts, the two have nearly no overlap in what you learn from them. Furthermore, DDraw is deprecated on new hardware, one of the reasons it was finally merged with D3D in DirectX version 8.

If you want to 2D engines or things, I''d advise you to look into using the ID3DXSprite interface of Direct3D 9, or look up orthogonal projection mode to prevent the effects of perspective.

This topic is closed to new replies.

Advertisement