Jump to content
  • Advertisement
Sign in to follow this  
pseudomarvin

OpenGL Blit a byte array of pixels to screen in SDL fast

This topic is 669 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 am implementing a software rasterizer for school and I would like to simply pass to SDL a buffer with pixels and tell it the format that they are in (RGB, 24b per pixel). SDL would then render that to screen.

I have tried using SDL_Surface but it seems to only work with images or data with headers since the following fails:

unsigned char pixels[BYTES_PER_PIXEL * SCREEN_WIDTH * SCREEN_HEIGHT];
// ... all pixels are filled with white color

imageSurface = SDL_CreateRGBSurfaceFrom(pixels,
      SCREEN_WIDTH,
      SCREEN_HEIGHT,
      sizeof(unsigned char) * BYTES_PER_PIXEL,       // depth
      SCREEN_WIDTH * BYTES_PER_PIXEL,               // pitch (row length * BPP)
      0x000000ff,                                   // red mask
      0x0000ff00,                                   // green mask
      0x00ff0000,                                   // blue mask
      0);                                           // alpha mask

The error is: SDL_CreateRGBSurfaceFrom failed: Unknown pixel format. What is the fastest way to do it in SDL? Also would there be a speed difference between that approach and drawing to an OpenGL texture which I would then render onto a full screen quad?

Share this post


Link to post
Share on other sites
Advertisement

Ok so I've found a solution, I'm not sure how effective it is but I've realized that it probably doesn't matter since the time it takes will be very small relative to the whole rasterization process.

void setPixel(SDL_Surface* surface, int X, int Y, Uint32 Color) 
{
	Uint8* pixel = (Uint8*)surface->pixels;
	pixel += (Y * surface->pitch) + (X * sizeof(Uint32));
	*((Uint32*)pixel) = Color;
}

//...

for (int y = 0; y < screenSurface->h; ++y)
{
   for (int x = 0; x < screenSurface->w; ++x)
   {
     setPixel(screenSurface, x, y, 0x0000ffff);
   }
}
Edited by pseudomarvin

Share this post


Link to post
Share on other sites

Turn on some optimization, and likely the compiler will fold your setPixel code into the double nested loops, and move constant parts out of the way.

 

In general, make sure it scales nicely, otherwise don't bother for speed unless proven to be a problem :)

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!