loading bitmaps, creating sprites
As part of my graphics engine, I am creating several functions to load and display bitmaps into ''Sprite'' objects which will support several frames for animation. These will all be loaded from the same bitmap, so my bitmaps are split up into cells which can be loaded individually. One function is called Create_Sprite and the other is called Draw_Sprite. I seem to be having a problem with two things:
1) My bitmaps only want to work properly if they are 64*64 (I a only use powers of 2). Anything smaller (eg 32*32) squished the image vertically to half of its size. 64*64 and higher works fine.
2) My cell extraction just doesn''t work at all, I get strange data. Below is the code for Create_Sprite. Note that cx and cy are cell positions, and mode determines whether to extract from cells, or the entire image. As you will see, if mode = 0 then bitmap is extracted from cells. I also included the Sprite data structure.
Also, could someone tell me how to put this code into a white box with all of the colors, as many people seem to do it.
/////////////////////////////////////////// sprite structure ///
typedef struct BITMAP_OBJ_TYP
{
int width, height; // width and height
int x_pos, y_pos; // x and y positions
int frames; // number of frames in animation
LPDIRECTDRAWSURFACE spritesurf; // bitmap surface
} BITMAP_OBJ, *BITMAP_OBJ_PTR;
/////////////////////////////////////////////////////////////////
////////////////////////Create_Sprite()//////////////////////////
int Create_Sprite(BITMAP_OBJ_PTR sprite,BITMAP_FILE_PTR spritesurf,int width,int height,int cx,int cy,int mode)
{
UCHAR *source_ptr, *dest_ptr;
sprite->spritesurf = NULL;
// params for creating surface
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwWidth = sprite->width = width;
ddsd.dwHeight = sprite->height = height;
// offscreen surface
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
if (lpdd->CreateSurface(&ddsd,&(sprite->spritesurf),NULL)!=DD_OK)
return(0);
// transparency
DDCOLORKEY color_key;
color_key.dwColorSpaceLowValue = 0;
color_key.dwColorSpaceHighValue = 0;
(sprite->spritesurf)->SetColorKey(DDCKEY_SRCBLT, &color_key);
// if 0, then extaction is cellbased
if (mode==0)
{
// move to specified cell
cx = cx*(sprite->width+1) + 1;
cy = cy*(sprite->height+1) + 1;
}
// get bitmap data
source_ptr = spritesurf->buffer + cy*spritesurf->bitmapinfoheader.biWidth+cx;
// put sprite onto its surface
ddsd.dwSize = sizeof(ddsd);
(sprite->spritesurf)->Lock(NULL,&ddsd,DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,NULL);
dest_ptr = (UCHAR *)ddsd.lpSurface;
// copy the image data into dest_ptr
for (int index_y=0; index_yheight; index_y++)
{
memcpy(dest_ptr, source_ptr,sprite->width);
dest_ptr += sprite->width;
source_ptr += spritesurf->bitmapinfoheader.biWidth;
}
(sprite->spritesurf)->Unlock(ddsd.lpSurface);
return(1);
}
/////////////////////////////////////////////////////////////////
-eat me!-but i''m not a cannibal!
Anyone? I can''t find out what''s going on.
-eat me!-but i''m not a cannibal!
I was looking through it, but I dont know direct x. I think you should post this in the direct x forum. you are in general game programming here.
"I''''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''''Urden
"I''''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''''Urden
------------------------------Put THAT in your smoke and pipe it
Have you checked the parameters you parse into your Create_Sprite() routine? Maybe the problem is in another function.
An alternative to your system is to limit one bitmap file for each sprite, for example if you had say a mario character, put the walk frames on the first row at 0:0, the jump frames on the second at 0:32, and so on... until youve added all the frames. Crop the BMP file to remove any non-used surface area.
When the sprite is requested, have the sprite_load function load the BMP at the same time, and unload the BMP when the sprite is destroyed.
An alternative to your system is to limit one bitmap file for each sprite, for example if you had say a mario character, put the walk frames on the first row at 0:0, the jump frames on the second at 0:32, and so on... until youve added all the frames. Crop the BMP file to remove any non-used surface area.
When the sprite is requested, have the sprite_load function load the BMP at the same time, and unload the BMP when the sprite is destroyed.
Downloads: ZeroOne Realm
Downloads: ZeroOne Realm
As for the ''source'' box, refer to the FAQ (the small faq icon near the top).
Before the code, put the word "source" within square brackets [sour_e].
After the code, put the same thing there but include a foward slash before the ''S'' [/sour_e]
(substitute the _ for ''c'' off course)
Before the code, put the word "source" within square brackets [sour_e].
After the code, put the same thing there but include a foward slash before the ''S'' [/sour_e]
(substitute the _ for ''c'' off course)
Downloads: ZeroOne Realm
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement