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
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top