Loading textures from resources...

Started by
22 comments, last by Moogle 22 years, 3 months ago
Yes, how do you do this? I dont like NeHes BMP loading routine. It makes me dizzy. I think resources are the best way of putting BMPs into your program and any help would be greatly appricheated.
-=Moogle=-
Advertisement
I do not know if that is a joke but why should putting the images in a resource file be a good alternative? Only BMPs without alpha channels, pack options, probably more complicated code, ... and does not Windows put some kind of limitations on resource files?
NeHe also has a TGA loader. And if you don''t like that either, use a free image loader library. Myself, I use the Intel JPEG Library and my own TGA loader, but there are some very nice libraries out there that can load any kind of image format.
Dirk =[Scarab]= Gerrits
i''d like to know how to do this also, just so i know.
http://www.lectersoft.com
Moogle: You can use LoadImage() for BMPs, both for files and resources. To access resource data in some other file format, check out FindResource(), LoadResource(), LockResource() and FreeResource(). PaintLib (www.paintlib.de) is a free image library that supports loding images from resources.

Obelix: Why would it be a joke? Reading data from a resource isn''t much more complicated than reading data from a file. Where did you get the idea that only BMPs can be put in a resource?
Try glpng. It''s very easy to use.
In my opinion png is the best format.It is compressed and can store alpha-values.


http://www.wyatt100.freeserve.co.uk/download.htm
quote:I do not know if that is a joke but why should putting the images in a resource file be a good alternative? Only BMPs without alpha channels, pack options, probably more complicated code, ... and does not Windows put some kind of limitations on resource files?


A splash screen image.


Beer - the love catalyst
good ol'' homepage
Beer - the love catalystgood ol' homepage
I do not want to be unfriendly but I can not take the Windows resoure file alternative seriously. If you have a bitmap file can you load it and get the the raw data, width, height and type in one function call.
Doing the same thing with resource files would involve extra steps. Where did the Anonymous Poster get the idea that creating the resource file and calling FindResource(), LoadResource(), LockResource(), FreeResource() and using an extra library for loading images from resources is not much more complicated?
I may of course be wrong about, among other things, the support for bitmaps with alpha channels. I guess that you can have any binary data but how easy is it to use? (creating/loading)

I also think that many libraries helps you in the job of uploading it to the card.

For some reason does Dredge-Master quote me and responds "A splash screen image."
Obelix: sorry if I sounded a bit grumpy yesterday, I had a really bad hangover.
As for resource bitmaps not being a serious alternative, well it is. Trust me.

Oh yeah, login name... maybe I''ll get one of those...
Here is how I do it- Takes a string name to an image and loads it from the resource. You''ll need to heavily modify this code, since I pulled it right from my engine.

  int rcrTexture::LoadRES(LPSTR n){	HBITMAP hBmp;	hBmp = (HBITMAP)LoadImage(_rc_GlobalInterface.lpEngineSettings->hInstance,n,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);	if (hBmp == NULL) 	{		rcEmit("TEXLOAD:: Failed on load of Image %s !\n",n);		return 0;	}	BITMAPINFO BInfo;	ZeroMemory(&BInfo,sizeof(BITMAPINFO));	BInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);	// okay fix0r the bitmap	if (GetDIBits(_rc_GlobalInterface.lpEngineSettings->hDC,hBmp,0,1,NULL,&BInfo,DIB_RGB_COLORS) == 0)	{		rcEmit("TEXLOAD:: Failed on info call #1 [ GetDIBits() ] of Image %s !\n",n);		return 0;	}	if (CreateBitImage(&BInfo,hBmp) == 0) return 0;	DeleteObject(hBmp);	return 1; }  


The image this returns is NOT MAPPED to the screens bitdepth, cause that would suck if it was low- heh. So I wrote my own function to map all bitmaps into 32bit RGBAs, CreateBitImage().

  int rcrTexture::CreateBitImage(BITMAPINFO *p,HBITMAP hBmp){	BITMAPINFO *bmi;	bmi = (BITMAPINFO*)new char[256*4 + sizeof(BITMAPINFOHEADER)];	memcpy (bmi,p,sizeof(BITMAPINFOHEADER));	// ok allocate the bitmap	rcBitmap.dwHeight = abs(p->bmiHeader.biHeight);	rcBitmap.dwWidth = abs(p->bmiHeader.biWidth);	rcBitmap.dwFormat = RCFORMAT_RGBA;	rcBitmap.lpBitmap = new unsigned char[rcBitmap.dwWidth * rcBitmap.dwHeight * 4];	BYTE *pIn;	pIn = new BYTE[rcBitmap.dwWidth * rcBitmap.dwHeight * 4];	if (GetDIBits(_rc_GlobalInterface.lpEngineSettings->hDC,hBmp,0,rcBitmap.dwHeight,pIn,bmi,DIB_RGB_COLORS) == 0)	{		rcEmit("TEXLOAD:: Failed on translation #2 [ GetDIBits() ] of Image!\n");		return 0;	}	// ok loaded the image and pulled the bits- now we need to color adjust	if (bmi->bmiHeader.biBitCount <= 8)	{		// It''s palleted, so do the conversion		int NumColors;		if (bmi->bmiHeader.biBitCount == 1) NumColors = 2;		if (bmi->bmiHeader.biBitCount == 4) NumColors = 16;		if (bmi->bmiHeader.biBitCount == 8) NumColors = 256;		BYTE *pelOut = rcBitmap.lpBitmap, *pelIn = pIn;		DWORD dwPixelCount = rcBitmap.dwWidth * rcBitmap.dwHeight;		switch (NumColors)		{		case 2:			break;			while (dwPixelCount)			{				// ok eight pels per byte- so lets do it				BYTE pos, x;				pos = 1;				while ((pos <= 8) && (dwPixelCount))				{					x = *pelIn & (1 << pos);					pelOut[0] = bmi->bmiColors[x].rgbRed;					pelOut[1] = bmi->bmiColors[x].rgbGreen;					pelOut[2] = bmi->bmiColors[x].rgbBlue;					pelOut[3] = 255;					pelOut += 1;					dwPixelCount--;					pos++;				}				pelIn += 1;			}			break;		case 16:			while (dwPixelCount)			{				// ok two pels per byte- so lets do it				BYTE x;				x = *pelIn & 0xF;				pelOut[0] = bmi->bmiColors[x].rgbRed;				pelOut[1] = bmi->bmiColors[x].rgbGreen;				pelOut[2] = bmi->bmiColors[x].rgbBlue;				pelOut[3] = 255;				pelOut += 4;				dwPixelCount--;				if (dwPixelCount)				{					x = (*pelIn & 0xF0) >> 4;					pelOut[0] = bmi->bmiColors[x].rgbRed;					pelOut[1] = bmi->bmiColors[x].rgbGreen;					pelOut[2] = bmi->bmiColors[x].rgbBlue;					pelOut[3] = 255;					pelOut += 4;					dwPixelCount--;				}				pelIn += 1;			}			break;		case 256:			// ok step through the bytes, filling in the proper color data			while (dwPixelCount)			{				pelOut[0] = bmi->bmiColors[*pelIn].rgbRed;				pelOut[1] = bmi->bmiColors[*pelIn].rgbGreen;				pelOut[2] = bmi->bmiColors[*pelIn].rgbBlue;				pelOut[3] = 255;				pelOut += 4;				pelIn += 1;				dwPixelCount--;			}			break;		}	} else if (p->bmiHeader.biBitCount == 24)	{		// okay we just need to pad the alphas		BYTE *pelOut = rcBitmap.lpBitmap, *pelIn = pIn;		DWORD dwPixelCount = rcBitmap.dwWidth * rcBitmap.dwHeight;		while (dwPixelCount)		{			pelOut[3] = 255;			pelOut[2] = pelIn[0];			pelOut[1] = pelIn[1];			pelOut[0] = pelIn[2];			pelOut += 4;			pelIn += 3;			dwPixelCount--;		}	} else if (p->bmiHeader.biBitCount == 32)	{		// okay we just need to swap the red and blue		BYTE *pelOut = rcBitmap.lpBitmap, *pelIn = pIn;		DWORD dwPixelCount = rcBitmap.dwWidth * rcBitmap.dwHeight;		while (dwPixelCount)		{			pelOut[0] = pelIn[0];			pelOut[1] = pelIn[1];			pelOut[2] = pelIn[2];			pelOut[3] = 255;//pelIn[3];			pelOut += 4;			pelIn += 4;			dwPixelCount--;		}	}	delete [] pIn;	delete [] bmi;	return 1;}  


This topic is closed to new replies.

Advertisement