• Advertisement

Archived

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

DirectDraw help needed

This topic is 6211 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''m having trouble with DirectDraw. I made a window and initialized DirectDraw and that all worked fine, then I put in code to just fill in a rectangle with color with blt() and that also worked. After I replaced that code with code to blit a bitmap to the primary surface ( the bitmap had been loaded already). When I run the program It crashes everytime, when I remove that code from my main game loop the program runs. So I don''t whats going on.

Share this post


Link to post
Share on other sites
Advertisement
Yeah, i got the same problem. I think it is something to do with DDUtil.cpp but I am not sure. However, I am pretty confident it is because it doesn''t know the filename of the bitmap. Has anyone got a DDLoadBitmap() function with a place to put in the filename of the bitmap you want to load? If you have it would be greatly appreciated if you could post it.
Thank you in advance
Graham Hinchly

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
My code is below if someone could take a look at it that would be helpful. It runs fine if I take out the GameMain() function.


#define INITGUID
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ddutil.h"
#include "recource.h"

#define WINDOW_CLASS_NAME "WINCLASS"
#define WINDOW_TITLE "Game"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 8

LPDIRECTDRAW lpdd = NULL;
LPDIRECTDRAW7 lpdd7 = NULL;
LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;
LPDIRECTDRAWSURFACE7 lpddsback = NULL;
LPDIRECTDRAWSURFACE7 circle = NULL;
DDSURFACEDESC2 ddsd;
LPDIRECTDRAWPALETTE lpddpal = NULL;
PALETTEENTRY palette[256];
HWND main_window_handle = NULL;
HINSTANCE main_instance = NULL;
HDC hdc = NULL;

int GameInit();
int GameMain();
int GameShutdown();

LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{

PAINTSTRUCT ps;
HDC hdc;

switch(msg)
{
case WM_CREATE:
{
return(0);
} break;

case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);

EndPaint(hwnd,&ps);
return(0);
} break;

case WM_DESTROY:
{

PostQuitMessage(0);
return(0);
} break;

default:break;

}


return (DefWindowProc(hwnd, msg, wparam, lparam));

}

int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

WNDCLASS winclass;
HWND hwnd;
MSG msg;
HDC hdc;
PAINTSTRUCT ps;

winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;


if (!RegisterClass(&winclass))
return(0);


if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,
WINDOW_TITLE,
WS_POPUP | WS_VISIBLE,
0,0,
WINDOW_WIDTH,
WINDOW_HEIGHT,
NULL,
NULL,
hinstance,
NULL)))
return(0);

main_window_handle = hwnd;
main_instance = hinstance;
hdc = GetDC(main_window_handle);

GameInit();

while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

TranslateMessage(&msg);

DispatchMessage(&msg);
}
GameMain();
}
GameShutdown();
}
int GameInit()
{
if(FAILED(DirectDrawCreate(NULL ,&lpdd ,NULL)))
{
return(0);
}
if(FAILED(lpdd->QueryInterface(IID_IDirectDraw7, (LPVOID *)&lpdd7)))
{
return(0);
}
if(lpdd)
{
lpdd->Release;
lpdd = NULL;
}
if(FAILED(lpdd7->SetCooperativeLevel(main_window_handle ,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT | DDSCL_ALLOWMODEX )))
{
return(0);
}
if(FAILED(lpdd7->SetDisplayMode(SCREEN_WIDTH ,SCREEN_HEIGHT,SCREEN_BPP ,0 ,0)))
{
return(0);
}
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;

if(FAILED(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL)))
{
return(0);
}
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;

if(FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsback)));
{
return(0);
}

for (int 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;

if(FAILED(lpddsprimary->SetPalette(lpddpal)))
{
return(0);
}
if(FAILED(DDLoadBitmap(lpdd7, MAKEINTRESOURCE(CIRCLE_1))))
{
return(0);
}
}


int GameMain()
{


if (KEYDOWN(VK_ESCAPE))
{
PostMessage(main_window_handle,WM_CLOSE,0,0);
}

RECT source;

source.top = 0;
source.left = 0;
source.bottom = 100;
source.right = 100;

if(FAILED(lpddsback->BltFast(400,400,circle,&source,DDBLTFAST_WAIT)))
{
return(0);
}
while(FAILED(lpddsprimary->Flip(NULL,DDFLIP_WAIT)))
Sleep(500);
return(1);
}

int GameShutdown()
{
if(lpddsback)
{
lpddsback->Release();
lpddsback = NULL;
}

if(lpddpal)
{
lpddpal->Release();
lpddpal = NULL;
}

if(lpddsprimary)
{
lpddsprimary->Release();
lpddsprimary = NULL;
}

if(lpdd7)
{
lpdd7->Release();
lpdd7 = NULL;
}
return(1);
}


Share this post


Link to post
Share on other sites
Well, you''re using 8-bit, so it''s possible that DDLoadBitmap() is making a 16-bit DDsurface for your bitmap. I think the colour depth of the surfaces it creates depends on the actual bitmap file, so check your bitmaps are 8-bit.
Also check the bitmaps are the right size, since BltFast() doesn''t do stretch blitting.

It''s probably a good idea to take the DDLoadBitmap() and DDCopyBitmap() (which DDLoadBitmap needs) functions out of Ddutil.cpp and put them in your source code so you can customize them, because as they stand they aren''t very flexible.

Also do you really need to work in 8-bit?

grahamhinchly/
DDLoadBitmap does accept a bitmap filename.

Share this post


Link to post
Share on other sites
I used blt() instead of bltfast() and the program runs, but the bitmap doesn''t show up on the screen, the screen just stays black. ( I posted before and forgot to sign in)

Share this post


Link to post
Share on other sites
Hmm...well, BltFast fails if the dest and source rects are different sizes, so maybe that was the problem.

As for why it''s black, I dunno, it shouldn''t be black with your random pallette. I presume you checked the bitmap is 8-bit paletted?

Even if you didn''t have this problem, you should still switch the whole thing over to 16-bit. Working with RGB values is way easier than handling a palette. With your random palette, none of your bitmaps would have the right colours anyway.

Share this post


Link to post
Share on other sites
Mabey I''ll try using a 16 bit mode then. But does anyone have code to convert a 8 or 24 bit bitmap to 16 bits?

Share this post


Link to post
Share on other sites
The easiest way to convert your bitmaps to 16 bit is with an art package. Writing a converter yourself isn't necessary. DDLoadBitmap() can load 16-bit bitmaps.

If you don't have an art package that can do this, there are plenty of good art packages available on the net, either shareware or trial versions. Paint Shop Pro is pretty good.

Edited by - simon_brown75 on February 16, 2001 9:40:14 AM

Share this post


Link to post
Share on other sites

  • Advertisement