Loaded BMP screwing up... yes I am doing the padding :)

Started by
1 comment, last by elis-cool 21 years, 10 months ago
it like appears twice, on one side its half the size of the bmp - horizontaly... and then the top appears at twice the size over the top of it... the image is 200x200 as is the back surface...
   
#define RGB32(R, G, B) ((0 <<32) | (R << 16) | (G << 8) | (B))


LPDIRECTDRAWSURFACE4 LoadGameBitmap(char *filename)
{   
	BITMAPINFOHEADER infoheader;  
	DDSURFACEDESC2   surfaceDesc; 
	LPDIRECTDRAWSURFACE4 NewSurface;
	DWORD             *bitmapData;  
	DWORD             *bitmapDone;   
	FILE             *bitmapFile;   
	BYTE             red, green, blue; 
	int padding;
	
	bitmapFile = fopen(filename, "rb");
	fseek(bitmapFile, sizeof(BITMAPFILEHEADER), SEEK_SET);  
	fread(&infoheader, sizeof(BITMAPINFOHEADER), 1, bitmapFile); 
	// get the padding at the end of the bitmap   

	padding = 4 - ((infoheader.biWidth * 3) % 4);   
	if(padding == 4) padding = 0;
	
	ZeroMemory(&surfaceDesc, sizeof(surfaceDesc));  
	surfaceDesc.dwSize = sizeof(surfaceDesc);  
	surfaceDesc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;  
	surfaceDesc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;  
	surfaceDesc.dwWidth = infoheader.biWidth;
	surfaceDesc.dwHeight = infoheader.biHeight;

	if(FAILED(g_pDDraw4->CreateSurface(&surfaceDesc, &NewSurface, NULL)))
	{
		MessageBox(g_hwnd, "NewSurface Failed", "ERROR", MB_ICONSTOP | MB_OK);
		Log("NewSurface Failed");
		return false;
	}

	ZeroMemory(&surfaceDesc, sizeof(surfaceDesc));  
	surfaceDesc.dwSize = sizeof(surfaceDesc);  
	NewSurface->Lock(NULL, &surfaceDesc, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL); 
	bitmapData = new DWORD[infoheader.biWidth * infoheader.biHeight];  
	bitmapDone = (DWORD *)surfaceDesc.lpSurface;   
	for( int y=0; y<infoheader.biHeight; ++y)  
	{     
		for( int x=0; x<infoheader.biWidth; ++x)     
		{        
			fread(&blue, sizeof(BYTE), 1, bitmapFile);     
			fread(&green, sizeof(BYTE), 1, bitmapFile);        
			fread(&red, sizeof(BYTE), 1, bitmapFile);      
			bitmapData[y*infoheader.biWidth + x] = RGB32(red, green, blue);   
		}   
		// skip past the padding in the file      

		fseek(bitmapFile, padding, SEEK_CUR);
	}   
	
	int heightIndex = 0;	
	for( y=infoheader.biHeight-1;  y>=0; --y)   
	{      
		for( int x=0; x<infoheader.biWidth; ++x)         
			bitmapDone[heightIndex*(surfaceDesc.lPitch/2) + x] = bitmapData[y*infoheader.biWidth + x];       
			++heightIndex;   
	}   
	delete bitmapData;
	NewSurface->Unlock(NULL);
	fclose(bitmapFile);
	return NewSurface;
}

g_pBack->Blt(0, g_pSkin, 0, DDBLT_WAIT, &ddbltfx);
  
CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Advertisement
Quickly breezing through, shouldn''t:

quote:
bitmapDone[heightIndex*(surfaceDesc.lPitch/2) + x] = bitmapData[y*infoheader.biWidth + x];


be:

quote:
bitmapDone[heightIndex*(surfaceDesc.lPitch/4) + x] = bitmapData[y*infoheader.biWidth + x];


If your surface is 32bit that is.

Jim Adams


Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX
Thanks man your my hero! I had nearly given up there after spending the last hour and a half tinkering with every other peice of code I had... I thought I had narrowed it down to me having the size of the surface wrong as windows was over taking shit and so it would have been stretching it smaller... but O well!! I thought I had gotten to everything in that function... I guess it was sorta hard to see...

CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]

This topic is closed to new replies.

Advertisement