bitmap loading/blting troubles

Started by
8 comments, last by utr0cks 20 years, 5 months ago
Ok, i apologize for this extremely vague question up front... I made a little 2d game engine using directx, one problem it has is that if I try to load bitmaps that are not 640x480, or a multiple of 640x480, and then blt them to the screen, they look all messed up, This also happens if i load a 640x480 bitmap and then try to blt it to a space that is not a multiple of 640x480. any ideas on what could be causing this? thanks
Advertisement
try making thebitmap bigger by one or two pixels

PROGAMERS









I don't have to do nothing but live and die, everything else i choose to do- I don't know, but my dad says it alot
do you mean just change the actual .bmp and then leave my code the same? or change the size I load into my program, Ive played around with it for a few minutes and it doesnt seem to be helping.
How are you blitting and how does it look messed up? Is it simply stretching it across the whole screen or do you, literally, end up with garbage?
Are you using the DirectDraw Blitting routines? (Blt and BltFast)?? If you are writing the blit functions yourself, make sure you are locking the surfaces (and unlocking) and that you take into account the surface pitch (width). The pitch wont always be the same as your screen width. I think for 640x480 it is 640 pixels. This may be your problem.

Denny.
i am trying to draw a bitmap in the form of a rectangle, 100x250 pixels, it looks fine, except the bottom 50 pixels or so just looks like static




//here are the main functions that i am using

struct pic
{
int x;
int y;
int width;
int height;
LPDIRECTDRAWSURFACE img;
};

bool draw2(LPDIRECTDRAWSURFACE target, pic *mypic, int w, int h)
{
//formated drawing to scale

RECT tar;
tar.top = mypic->y;
tar.left = mypic->x;
tar.bottom = tar.top + h;
tar.right = tar.left + w;

RECT src;
src.left = 0;
src.top =0;
src.right = mypic->width - 3;
src.bottom = mypic->height - 3;

target->Blt(&tar, mypic->img, &src, DDBLT_WAIT | DDBLT_KEYSRC, NULL);

return true;
}

bool load(pic *mypic, char *filename)
{
//for some reason this only loads well
//with bmps of easy sizes
//you kinda have to guess what an easy size is
//factors of 640 x 480 seem to work well

BITMAP_FILE bmpfile;
if( Load_Bitmap_File(&bmpfile, filename) == 0)
return false;

//pallette
lpddpal->SetEntries(0,0,256, bmpfile.palette);
// mypic->img->SetPalette(lpddpal);
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);

(mypic->img)->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);

video_buffer = (UCHAR *)ddsd.lpSurface;
memset(video_buffer, 0, mypic->height * mypic->width);

memcpy(video_buffer, bmpfile.buffer, mypic->height * mypic->width);

(mypic->img)->Unlock(video_buffer);

Unload_Bitmap_File(&bmpfile);

return true;
}

bool setup(LPDIRECTDRAW main, pic *mypic, int w, int h)
{
//setup the pic
//initialize vars and such
//mypic = the pic to set up
//w = width
//h = height

mypic->height = h;
mypic->width = w;
mypic->x = 0;
mypic->y = 0;


memset(&ddsd,0,sizeof(ddsd));

ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwHeight = h;
ddsd.dwWidth = w;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;

if(main->CreateSurface(&ddsd, &(mypic->img), NULL) != DD_OK)
return false;

DDCOLORKEY col;
col.dwColorSpaceHighValue=0;
col.dwColorSpaceLowValue=0;

(mypic->img)->SetColorKey(DDCKEY_SRCBLT, &col);

return true;
}
I had this sort of trouble and I eventually figured out that this would only happen to bitmaps that didn''t have a length and width of factors of 8. I was using 16-bit color, and I assume I had these problems because I was copying the image data in chunks of 8 bits and so, if I had any image that did not have sides a factor of 8, then I would be copying some extra (or lack of) information at the end of each line, thus causing my image to be slanted, or completely full of garbage.

Now, I''m not sure if that''s entirely the reason to my troubles, but it''s something like that. Anyways, I just fixed it by always making my bitmaps have dimensions that are %8.

*shrug*

HellspawnXIII
"There''s a lot of stuff in this world that needs stepping on."
HellspawnXIII"There's a lot of stuff in this world that needs stepping on."
Your problem is that you are using the width of your image to do the blitting. If the surface resides in video memory there is no gaurantee that the width of the surface is the same as the width of the bitmap. What you have to use is ddsd.lPitch instead of mypic->width when copying the memory from one location to the other. For the blitting rectangles you should use the width though.
-YoshiXGXCX ''99
For the bitmap loading problems with uneven width.
BMP lines are padded to 32bits. So if the width of a line (in bytes) is not divisible by 4 it fill be filled with zeroes until it is (only inside the file, do not add those to your image in memory).

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Try reading this. Your problem is the padding (as the people before me said). That post may help you understand it better. Read the whole thing.

This topic is closed to new replies.

Advertisement