DirectDraw help needed

Started by
6 comments, last by Crono99 23 years, 2 months ago
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.
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
***We were all beginners once, don't forget that...***
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.peRed = rand()%256;<br> palette.peGreen = rand()%256;<br> palette.peBlue = rand()%256;<br><br> palette.peFlags = PC_NOCOLLAPSE;<br> }<br><br> palette[0].peRed = 0;<br> palette[0].peGreen = 0;<br> palette[0].peBlue = 0;<br> palette[0].peFlags = PC_NOCOLLAPSE;<br> <br> palette[255].peRed = 255;<br> palette[255].peGreen = 255;<br> palette[255].peBlue = 255;<br> palette[255].peFlags = PC_NOCOLLAPSE;<br><br> if(FAILED(lpddsprimary->SetPalette(lpddpal)))<br> {<br> return(0);<br> }<br> if(FAILED(DDLoadBitmap(lpdd7, MAKEINTRESOURCE(CIRCLE_1))))<br> {<br> return(0);<br> }<br>}<br><br><br>int GameMain()<br>{<br> <br><br> if (KEYDOWN(VK_ESCAPE))<br> {<br> PostMessage(main_window_handle,WM_CLOSE,0,0);<br> }<br> <br> RECT source;<br><br> source.top = 0;<br> source.left = 0;<br> source.bottom = 100;<br> source.right = 100;<br><br> if(FAILED(lpddsback->BltFast(400,400,circle,&source,DDBLTFAST_WAIT)))<br> {<br> return(0);<br> }<br> while(FAILED(lpddsprimary->Flip(NULL,DDFLIP_WAIT)))<br> Sleep(500);<br> return(1);<br>}<br><br>int GameShutdown()<br>{<br> if(lpddsback)<br> {<br> lpddsback->Release();<br> lpddsback = NULL;<br> }<br><br> if(lpddpal)<br> {<br> lpddpal->Release();<br> lpddpal = NULL;<br> }<br><br> if(lpddsprimary)<br> {<br> lpddsprimary->Release();<br> lpddsprimary = NULL;<br> }<br><br> if(lpdd7)<br> {<br> lpdd7->Release();<br> lpdd7 = NULL;<br> }<br> return(1);<br>}<br><br><br>
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.

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)
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.
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?
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

This topic is closed to new replies.

Advertisement