Uint32 GetTexPixel(SDL_Surface *surface, int offset)
{
//slock(surface);
int bpp = surface->format->BytesPerPixel/8;
This is wrong, it you want bit per pixel, you must multiply by 8.
(anyway after reading the switch code, I saw you want byte per pixel,
so, no /8)
Uint8 *p = (Uint8 *)offset;
This is wrong, wrong and wrong!
Learn to use pointer.
int *p = &offset;
or if you wanna copy data
int *p;
*p = offset;
Anyway, Uint8 is not int and int is not int *. (not same lenght, not
even same signess)
//sunlock(surface);
switch(bpp)
{
case 1: return *p;
You return a 8 bit int, and your function is declared to return
32bit, in this context, you will get the right value, but this is
dangerous.
case 2: return *(Uint16 *)p;
Do you at least know what casting a pointer does?
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;
Dang, p is a 8it int (at least how you declared it), it will handle
8bit and not more.
case 4: return *(Uint32 *)p;
default: return 0;
}
}
You should really code in a cleaner way. C allows many things, even
very wrong.
For example, if you do
int **test;
…
addSomething()
use test[0] or test[1] will mostly works, but only because your are
"lucky". This is wrong. A and B must be static for it to work.
void addSomething() {
test = malloc(2 * sizeof(int*));
int a = 3;
int b = 5;
test[0] = &a;
test[1] = &b;
}
So, it’s not because it works that it is right.
What you do is dangerous, because:
return p[0] << 16 | p[1] << 8 | p[2]
Should not even works, but maybe it does.
p[n] will move the pointer of 8 bit because *p is Uint8
But as p[n] is 8 bit, you cannot shift it by 8 or even by 16.
I did not read what the code is supposed to do, because in this case,
you must look at your code error before thinking of the logic.
Hope this will help you.
Regards–
Kuon
CEO - Goyman.com SA
http://www.goyman.com/
“Computers should not stop working when the users’ brain does.”
-------------- next part --------------
A non-text attachment was scrubbed…
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2434 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20061207/a26e27f3/attachment.bin