Skip to main content
GameDev.net gamedev.net
🔒 Locked

Casting error... I think!

Started by mrmrcoleman Jan 21, 2005 at 5:34 PM 2 replies 1k views
Original Post
mrmrcoleman
mrmrcoleman
Hello. This section of code...

// Loop through the bits and premultiply the values
for (int y=0;y<db.dsBm.bmHeight;++y)
{
	BYTE *pPixel = db.dsBm.bmBits + db.dsBm.bmWidth * 4 * y;

	for (int x=0;x<db.dsBm.bmWidth;++x)
	{
		pPixel[0]= pPixel[0]*pPixel[3]/255;
		pPixel[1]= pPixel[1]*pPixel[3]/255;
		pPixel[2]= pPixel[2]*pPixel[3]/255;
		pPixel+= 4;
	}
}

// Where db is defined earlier like this
DIBSECTION db;
GetObject(hBmp, sizeof(db), &db);



... creates this error output:

C:\Documents and Settings\Mark\StarChart\Testing\Non-Unicode Particle Test\Screen.cpp(1025) : error C2036: 'void *' : unknown size
C:\Documents and Settings\Mark\StarChart\Testing\Non-Unicode Particle Test\Screen.cpp(1025) : error C2440: 'initializing' : cannot convert from 'unsigned char' to 'unsigned char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast



I think this is an easy one, but I don't know much about casting! If anybody knows of any good resources that will stop me posting more newb questions like this one I am open to suggestions! Thanks in advance. Kind regards. Mark Coleman EDIT: My apologies about the formatting, I have tried to reduce the width of the source windows, but I have had no luck. ;(
mike25025
mike25025
try this

BYTE *pPixel = (BYTE*)((int)db.dsBm.bmBits + db.dsBm.bmWidth * 4 * y);
izzo
izzo
Is this line 1025:
BYTE *pPixel = db.dsBm.bmBits + db.dsBm.bmWidth * 4 * y;

?

db.dsBm.bmBits is an LPVOID; that means it's a pointer to a void. You can't do pointer arithmetic on a void pointer because the compiler doesn't know what size the data that you're pointing to is. When you do pointer arithmetic like the following:
pPixel + 5

the compiler doesn't add 5 to the raw memory address that pPixel references, it adds 5 * sizeof(BYTE).

So what you need to do is typecast bmBits to a BYTE pointer:
pPixel = (BYTE*)db.dsBm.bmBits + db.dsBm.bmWidth * 4 * y;

That should fix things up.

cheers
sam.
mrmrcoleman
mrmrcoleman
Thanks mate, that did the job!

Kind regards.

Mark Coleman

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.