Problem with Colors

Hello !

I have written a method in my Class which sould draw a Pixel on the
Surface !
It gets a X and Y coordination and a color(Uint32).

I am doing following:

void gfx::drawPixel(unsigned int x,unsigned int y,Uint32 color)
{

Uint8* bits,bpp;

Uint32
col=SDL_MapRGB(area()->format,color&0xFF0000,color&0x00FF00,color&0x0000FF);

if(SDL_MUSTLOCK(area()))
{
if(SDL_LockSurface(area()))
return;
}

bpp=area()->format->BytesPerPixel;

bits=((Uint8*) area()->pixels)+yarea()->pitch+xbpp;

// I am in 16Bit color depth

*((Uint16 *)(bits)) = (Uint16)col;
cout << “bpp->2” << endl;

break;
}

So when I call this method in a loop
which should draw Pixels at a random position in a
random color, I only got Blue pixels !

eg:

srand(time(NULL));
	
gfx g;

while(!done)
{
	g->drawPixel(rand()%640,rand()%480,rand());
}

Can anybody explain me why the pixels are all blue ???
What I am doing wrong ???

Thank you and best regrads.
Ciao
Anes

Uuuups I forgot something:

if(SDL_MUSTLOCK(area()))
{
SDL_UnlockSurface(area());
}

SDL_UpdateRect(area(),x,y,1,1);

These lines come right at the end of the drawPixel method !

The area() method give me SDL_Surface
eg:

class gfx
{
private:
SDL_Surface* screen;
};

SDL_Surface* area()
{
return screen;
}

Ciao
Anes

Anes Lihovac wrote:

Hello !

I have written a method in my Class which sould draw a Pixel on the
Surface !
It gets a X and Y coordination and a color(Uint32).

I am doing following:

void gfx::drawPixel(unsigned int x,unsigned int y,Uint32 color)
{

Uint8* bits,bpp;

Uint32
col=SDL_MapRGB(area()->format,color&0xFF0000,color&0x00FF00,color&0x0000FF);

Okay I found it out myself.

I must not do the SDL_MapRGB !

Ciao