getting the pixel value

hi

i use SDL2. and i was testing some functions i took from some internet and some unwanted results happened.

the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Pixel *GetPixel(SDL_Surface *surface, int x, int y)
{
    Uint8 p8, *buf8;
    Uint16 p16, *buf16;
    Uint32 p32, *buf32;

    switch(surface->format->BitsPerPixel)
    {
        case 8:
            buf8=(Uint8 *)surface->pixels;
            p8=buf8[y * surface->w + x];
            p32=p8;
            break;
        case 16:
            buf16=(Uint16 *)surface->pixels;
            p16=buf16[y * surface->w + x];
            p32=p16;
            break;
        default:
            buf32=(Uint32 *)surface->pixels;
            p32=buf32[y * surface->w + x];
            break;
    }

    Uint8 Red, Green, Blue, Alpha;

    SDL_GetRGBA(p32, surface->format, &Red, &Green, &Blue, &Alpha);

    Pixel * tmp = new Pixel(Red, Green, Blue, Alpha);
    if(!tmp)
        Logger("failed to get pixel from surface");

    return tmp;
}


main.cpp
1
2
3
Pixel *px = fge::GetPixel(surf, 0, 0);

std::cout << px << std::endl;


and the output changes time to time.

what do you think of it

EDIT: if you need any more info just tell me
Last edited on
ohhh i find the mistake i did not use it as std::cout << &px << std::endl; and i just got where the value is stored :D

nope still problematic
Last edited on
> and the output changes time to time.
of course, you make another allocation every time you enter that function.
¿why are you using new in the first place?

1
2
3
4
5
6
7
Pixel GetPixel(SDL_Surface *surface, int x, int y){
   //...
   return Pixel(Red, Green, Blue, Alpha);
}

Pixel px = fge::GetPixel(surf, 0, 0);
std::cout << px << std::endl; //¿does it know how to print pixels? 



PS: the strike trough does not appear in http://www.cplusplus.com/forum/general/ , so your post read as "i found the mistake"
Last edited on
yes ne555 thx. as you said it does not know how to print a pixel sth i forgot. thx for the help again
Topic archived. No new replies allowed.