Jump to content
  • Advertisement
Sign in to follow this  
AhmedCoeia

Clearing framebuffer with color

This topic is 2124 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

Hi all,

 

I'm using SDL to access the framebuffer, and I would like to clear it with a color.

 

I have used the following code:

memset(m_FrameBuffer, 0, sizeof(uint32_t) * m_Height * m_Width);

it works and its cleared with black, but what if I want to clear it with another color ?

 

Share this post


Link to post
Share on other sites
Advertisement

Here memset isn't going to do the job, since it operates on bytes, and regrettably the world has moved on from 256-bit displays tongue.png the only reason it works in this case (and a few others) is because "black" happens to translate to all-zeroes, but in general colors of a modern framebuffer don't fit in a byte.

 

So what you should do is find the size of a pixel, in bytes (probably 4 bytes since you have a uint32_t in your code), and have a loop that goes over every 4-byte block in the m_FrameBuffer (representing a pixel's color) and set it to the proper color code. Your language/framework should have some sort of function to convert an RGB triplet to its 4-byte representation (really, it's just one byte for red, green, blue, and one last byte for alpha or reserved, but the order of those bytes is not standardized, it could be RGBA, BGRA, ARGB, ...). Something like this:

uint32_t color = toRGBA(your_color);
uint32_t *ptr = m_FrameBuffer;

for (size_t y = 0; y < m_Height; ++y)
  for (size_t x = 0; x < m_Width; ++x)
    *(ptr++) = color;

Your language may already come with a block-copy function which does the same thing as the code above, and is safer/more optimized.

 

Or, alternatively, look for an SDL function that does the same thing in a cross-platform manner, because writing to raw pixel memory does not always give the same results everywhere, due to byte order issues, alignment, scanline stride (in case you're dealing with, say, 24-bit framebuffers) and so on.

 

tl;dr there is probably a better way to clear the framebuffer in SDL, but the above answers your immediate question.

Edited by Bacterius

Share this post


Link to post
Share on other sites

How about using std::fill ? How it would be used for clearing the buffer then ?

 

Well, I suppose you'd work out the length of the buffer (height * stride) and then use std::fill to fill it with the value you want.  (?)

 

It doesn't hurt anything to write in the padding area, if that's what you're worried about.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!