Getpixel And Putpixel Get/putting Wrong Colors


zacaj

void main()
Joined
Apr 3, 2007
Messages
362
Age
27
Location
NY
Website
zacaj.com
I have these functions for getting and putting pixels, and theyre drawing the wrong colors(grey is red, red is blue, etc). Whats wrong?
CODE
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
if(x>surface->w||x<0||y>surface->h||y<0)
return NULL;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
return *p;

case 2:
return *(Uint16 *)p;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;

case 4:
return *(Uint32 *)p;

default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
if(x>surface->w||x<0||y>surface->h||y<0)
{
}
else
{
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
*p = pixel;
break;

case 2:
*(Uint16 *)p = pixel;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;

case 4:
*(Uint32 *)p = pixel;
break;
}
}
}
 

Exophase

Nothing good will ever come of Exophase.
Joined
Sep 21, 2006
Messages
10,308
Age
38
Location
Cleveland OH
What color depth are you using, and could you post the code you're using to print the pixels which you say are not being printed in the correct color?
 
Q

quasist

Guest
That seems that SDL example was for windows. As I remember 16bit color modes differs in win32 and gp2x builds.
 

zacaj

void main()
Joined
Apr 3, 2007
Messages
362
Age
27
Location
NY
Website
zacaj.com
CODE
putpixel (screen, x, y,getpixel(srf,sx,sy));
screen = SDL_SetVideoMode(320,240,16, SDL_HWSURFACE | SDL_DOUBLEBUF);


What is the correct get/putpixel code for the GP2X?
 

joyrider

Member
Joined
Mar 29, 2006
Messages
589
Age
41
Website
www.willemssoft.Be
hmm i have been using the same functions on the gp2x but didn't encounter such problems yet. I do use SDL_MapRGB(surface->format,R,G, B );

Did you call SDL_DisplayFormat on the surface's you load ? might be that the loaded surface is for example in another format than what your current display format is in and that is giving problems . It's a long shot but worth a try if you didn't do it :)
 
Top