Help with pixels in a texture


Im trying to change the rgba of a pixel in a texture. The problem is that that i want to change the pixel under my mouse position but i dont know exactly how to do it. I have all the pixels of the texture stored in an array. Using the equation texturewidht * y +x i can go from a 2dimensional array to a one dimensional array but in the array of pixels the pixels are stored in sets of rgba. So the first pixel is stored in the first 4 cells of the array…
Pixels[0] = r of pixel 1
Pixels[1] = g of pixel 1
Pixels[2] = b of pixel 1
Pixels[3] = a of pixel 1
Pixels[4] = r of pixel 2
Pixels[5] = g of pixel 2
Pixels[6] = b of pixel 2
Pixels[7] = a of pixel 2

How can i access to the pixels that is under my mouseposition? (Im still learning about textures, veam, surfaces, etc)

You shouldn’t normally work with raw pixels. Not sure what you want to do exactly but manipulating textures pixel by pixel is really slow and should be avoided whenever possible. If you really need to SDL provides helper functions SDL_GetRGBA(), SDL_GetRGB() for reading and SDL_MapRGB(), SDL_MapRGBA() for writing, SDL_PixelFormatEnumToMasks() for finding out how many bitsperpixel there are for the format you are using (you will need to jump this many bits/bytes in your array to get to the next pixel.

For your example above, as you described it, format would be SDL_PIXELFORMAT_RGBA8888, pixels should be stored as Uint32, and so you could get to a specific pixel by writing Pixels[texturewidht * y +x] as you described or as Uint8 in which case Pixels[4 * texturewidht * y +x] would give you the r of the pixel (+1 the g etc). However this is not a given, unless you specifically set the format.

But you really should use SDL_Renderer for rendering since you get hardware acceleration there, and work with the draw functions provided by it.