How does this code do what it does and why did the coder code it this way(pixel+SDL)

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
Code:
Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
{
    //Convert the pixels to 32 bit
    Uint32 *pixels = (Uint32 *)surface->pixels;
    
    //Get the requested pixel
    return pixels[ ( y * surface->w ) + x ];//<----------------------------------------------------------------------------------------------
}
Here's our functions to get and put pixels. In case you missed the bitmap font tutorial, here's a quick review on how pixel access works:

First thing we do is convert the pixel pointer from type void to 32bit integer so we can properly access them. After all, a surface's pixels are nothing more than an array of 32bit integers. Then we get or set the requested pixel.

You maybe be wondering why I don't just go "return pixels[ x ][ y ]".

The thing is the pixels aren't stored like this: 2d.jpg


They're stored like this: 1d.jpg

in a single dimensional array. It's because different operating systems store 2D arrays differently (At least I think that's why).

So to retrieve the red pixel from the array we multiply the y offset by the width and add the x offset.

These functions only work for 32-bit surfaces. You'll have to make one of your own if you're using a different format.

My problem is how does returning the Y offset times the surface width plus the x offset get the pixel color I need?
 

saw792

Is known to say things. That is all.
Reaction score
280
Continuing the example given in the quote:
If you want the blue pixel stored in row 1, column 1 you are actually requesting the pixel stored at index 6 in the pixel array. This is demonstrated in the pictures in the quote. So every time you go down a row you are actually jumping across in the pixel array by 5. So if you want the second row of the array, you need to jump ahead by (1 * 5) elements and then add the x offset. I'm not sure how to explain this more simply, it seems you don't understand how arrays work.
 

Samael88

Evil always finds a way
Reaction score
181
So if you want the second row of the array, you need to jump ahead by (1 * 5) elements and then add the x offset.
That is a pretty good explanation right there.
It is a simple function that simulates a 2D array using a normal array, it is to make it a bit easier to write programs.

I think however that you should read all lazys tutorials properly, he explains the matter better in his bitmap font tutorial, the principle behind that and the principle behind this are basically the same.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
For better visualization, they're stored in an array. This would be the array's representation of screen pixels of a 6x3 screen:

Code:
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]
[ 6 ] [ 7 ] [ 8 ] [ 9 ] [10 ] [11 ]
[12 ] [13 ] [14 ] [15 ] [16 ] [17 ]


So this example would have a width of 6 (0 thru 5) and a height of 3.
If you have a (x,y) coordinate and you want to get the right pixel you need some formula for it.

(3,1) should return 9.
You get this with:
y*width + x => 1*6 + 3 => 9

Basically the (y*width) part says "I go y rows down the 2D array", because width equals an entire row.
 

Samael88

Evil always finds a way
Reaction score
181
No, they are not stored in a 2D array, that is the whole point of the function in the first place.
They are stored in a 1D array.
In your example (3,1) should return 8, you forgot about the zero.

They are stored in a single row like this:
Code:
[0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15]
The only thing that function does is to let you input the coordinates as if you where working with a 2D array and then it goes into the 1D array and takes out the correct color id of the pixel in the given position and returns it for you.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I haven't made a 2D array. I made a 1D array and broke it into parts. You can see continuous numbering.
Sorry if it sounded like I was talking about a 2D array. I tried to visualize how a 1D array can be looked at if you add a second dimension.
I edited it a bit to make it clear.

However, it is 9 :)

Code:
x   0     1     2     [B][COLOR="#FF0000"]3[/COLOR][/B]     4     5       <- Column
0 [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]
[B][COLOR="#FF0000"]1[/COLOR][/B] [ 6 ] [ 7 ] [ 8 ] [ [B][COLOR="#FF0000"]9[/COLOR][/B] ] [10 ] [11 ]
2 [12 ] [13 ] [14 ] [15 ] [16 ] [17 ]

^
Row
 

Samael88

Evil always finds a way
Reaction score
181
I haven't made a 2D array. I made a 1D array and broke it into parts. You can see continuous numbering.
Sorry if it sounded like I was talking about a 2D array. I tried to visualize how a 1D array can be looked at if you add a second dimension.
I edited it a bit to make it clear.
Ah, now I see what you did there, well, my bad:) I have not programmed in a while:rolleyes:
However, it is 9 :)
Yeah, just don't be a bitch about it:p
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top