Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble with SDL_GetRGBA
    text
    copied!<p>I'm trying to use SDL_GetRGBA to display the RGBA values of the pixels of an image, however I can't get it to work. Here is my code and output:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;SDL/SDL.h&gt; int main() { SDL_Surface *screen; SDL_Surface *image; SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); image = SDL_LoadBMP("testimg2.bmp"); Uint32 i,j; Uint8 r, g, b, a; Uint8 *p = (Uint8 *) image-&gt;pixels; for (i=0; i &lt; 300; i++) { for (j=0; j &lt; 4; j++) printf("i = %u, image-&gt;pixels[i*4+%u] = %u\n", i, j, p[i*4+j]); SDL_GetRGBA(i, image-&gt;format, &amp;b, &amp;g, &amp;r, &amp;a); printf("i = %u, r = %u, g = %u, b = %u, a = %u\n", i, r, g, b, a); } SDL_BlitSurface(image, NULL, screen, NULL); SDL_Flip(screen); SDL_Delay(2000); SDL_FreeSurface(image); SDL_Quit(); return 0; } i = 0, image-&gt;pixels[i*4+0] = 0 i = 0, image-&gt;pixels[i*4+1] = 0 i = 0, image-&gt;pixels[i*4+2] = 255 i = 0, image-&gt;pixels[i*4+3] = 133 i = 0, r = 0, g = 0, b = 0, a = 255 i = 1, image-&gt;pixels[i*4+0] = 0 i = 1, image-&gt;pixels[i*4+1] = 0 i = 1, image-&gt;pixels[i*4+2] = 255 i = 1, image-&gt;pixels[i*4+3] = 140 i = 1, r = 1, g = 0, b = 0, a = 255 i = 2, image-&gt;pixels[i*4+0] = 0 i = 2, image-&gt;pixels[i*4+1] = 0 i = 2, image-&gt;pixels[i*4+2] = 255 i = 2, image-&gt;pixels[i*4+3] = 132 i = 2, r = 2, g = 0, b = 0, a = 255 i = 3, image-&gt;pixels[i*4+0] = 0 i = 3, image-&gt;pixels[i*4+1] = 0 i = 3, image-&gt;pixels[i*4+2] = 255 i = 3, image-&gt;pixels[i*4+3] = 118 i = 3, r = 3, g = 0, b = 0, a = 255 i = 4, image-&gt;pixels[i*4+0] = 0 i = 4, image-&gt;pixels[i*4+1] = 0 i = 4, image-&gt;pixels[i*4+2] = 255 i = 4, image-&gt;pixels[i*4+3] = 114 i = 4, r = 4, g = 0, b = 0, a = 255 i = 5, image-&gt;pixels[i*4+0] = 0 i = 5, image-&gt;pixels[i*4+1] = 0 i = 5, image-&gt;pixels[i*4+2] = 255 i = 5, image-&gt;pixels[i*4+3] = 115 i = 5, r = 5, g = 0, b = 0, a = 255 </code></pre> <p>So everything seems to be correct when manually going through the pixel array byte by byte, but the SDL_GetRGBA function seems to be returning incorrect values. I'm not sure what I'm doing wrong.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload