kmjohnny

Is there a better way to display an image with DirectX7 I'm not practiced DX much yet, but this one is working - only on 16M .bmp.

[Code]lpDDSurf=LoadBitmap("Test.bmp");[/Code]

[Code]LPDIRECTDRAWSURFACE7 LoadBitmap(char *path) {
LPDIRECTDRAWSURFACE7 lpDDPic;
DWORD* lPSurface=NULL;
int lPitch=0;
FILE *fp;
DWORD szine=0,size=0;
unsigned long width,height;
short type,lineend;
unsigned char *sor;


fp=fopen(path,"rb");
if(fp==NULL) return NULL;

fread(&type, sizeof(short), 1, fp);
if (type != 0x4D42)
{
fclose(fp);
return NULL;
}
fread(&size,sizeof(DWORD),1,fp);
fseek(fp, 18, SEEK_SET);
fread(&width, sizeof(long), 1,fp);
fread(&height, sizeof(long), 1,fp);

lineend=(size-54-(width*height*3))/height;

DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN|DDSCAPS_VIDEOMEMORY;
ddsd.dwWidth =width;
ddsd.dwHeight =height;
ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd.ddpfPixelFormat.dwRGBBitCount = 32;
ddsd.ddpfPixelFormat.dwRBitMask = 0x00ff0000;
ddsd.ddpfPixelFormat.dwGBitMask = 0x0000ff00;
ddsd.ddpfPixelFormat.dwBBitMask = 0x000000ff;
ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0x00000000;

hRet = lpDD->CreateSurface(&ddsd, &lpDDPic, NULL);
if (hRet != DD_OK) {
return NULL;
}

ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);

if ((lpDDPic->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL)) != DD_OK)
MessageBox(0, "Failed to lock the surface", "Surface Error", MB_OK);

lPitch = (int)ddsd.lPitch;
lPSurface = (DWORD*)ddsd.lpSurface;

fseek(fp,54,SEEK_SET);
for(int k=height-1; k>=0; k--) {
sor=(unsigned char*)calloc(3*width,sizeof(unsigned char));
fread(sor,width*3*sizeof(char),1,fp);
for(int i=0; i<width; i++) {
szine=(*(sor+i*3+2)<<16)|(*(sor+i*3+1)<<8)|(*(sor+i*3));
lPSurface[i+k*(lPitch>>2)]=szine;
}
free(sor);
if(!feof(fp)) fseek(fp,lineend,SEEK_CUR);
}

fclose(fp);
lpDDPic->Unlock(NULL);


return lpDDPic;
}[/Code]