Jump to content
  • Advertisement
Sign in to follow this  
Zuka

Manipulating surface data directly

This topic is 3505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was going to make a prototype Perlin noise generator, but I've run into trouble. I wanted to have a 2-dimensional array of DWORDs, fill that up with the color data, then blit it to a texture I could render directly on the screen. I have no clue how to get my array or DWORDs into a surface, though. I know I need to lock the surface, but how do I set its data?

Share this post


Link to post
Share on other sites
Advertisement
When you lock the surface, one of the parameters you pass is a pointer to a pointer to void. When the call succeeds, the pointer is modified to point at the start of the "locked memory" of the surface. You can write to the address of the pointer (and up to the size of the surface). When you're done, you unlock.

Share this post


Link to post
Share on other sites
Well, you've also got to take the "pitch" into account. That is a variable which indicates the number of bytes between two consecutive scanlines. In practice this usually means that when a surface's width isn't a multiple of (say) four pixels there's a little bit of scratch space to skip over at the end of each line, but there can be other reasons.

Oh, and keep in mind that it's specified in bytes rather than pixels. Nowadays it's probably safe to assume that the pitch is evenly divisible by the pixel size, but I've been bitten by 24-bit modes in past so to be safe you might want to break out the character pointers and pointer casts.

Now then, here's a simple example of clearing a surface:
char *row_ptr = (char *) surface.lpSurface;

for(y = 0; y < surface.dwHeight; ++y) {
my_pixel_t *column_ptr = (my_pixel_t *) row_ptr;

for(x = 0; x < surface.dwWidth; ++x) {
column_ptr[x] = color;
}

row_ptr += surface.lPitch;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!