5

I'm working on a NES emulator right now and I'm having trouble figuring out how to render the pixels. I am using a 3 dimensional array to hold the RGB value of each pixel. The array definition looks like this for the 256 x 224 screen size:

byte screenData[224][256][3];

For example, [0][0][0] holds the blue value, [0][0][1] holds the green values and [0][0][2] holds the red value of the pixel at screen position [0][0].

When the vblank flag goes high, I need to render the screen. When SDL goes to render the screen, the screenData array will be full of the RGB values for each pixel. I was able to find a function named SDL_CreateRGBSurfaceFrom that looked like it may work for what I want to do. However, all of the examples I have seen use 1 dimensional arrays for the RGB values and not a 3 dimensional array.

What would be the best way for me to render my pixels? It would also be nice if the function allowed me to resize the surface somehow so I didn't have to use a 256 x 224 window size.

slava
  • 1,901
  • 6
  • 28
  • 32
Sean K
  • 147
  • 2
  • 12

2 Answers2

4

You need to store the data as an unidimensional char array:

int channels = 3; // for a RGB image
char* pixels = new char[img_width * img_height * channels];

// populate pixels with real data ...

SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)pixels,
                img_width,
                img_height,
                channels * 8,          // bits per pixel = 24
                img_width * channels,  // pitch
                0x0000FF,              // red mask
                0x00FF00,              // green mask
                0xFF0000,              // blue mask
                0);                    // alpha mask (none)
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 1
    I have a window and I have a surface, how do I update the window to use the surface I have? There is not setSurface() or updateSurface() function. – Noobs DeSroobs Apr 25 '16 at 08:41
0

In 2.0, use SDL_Texture + SDL_TEXTUREACCESS_STREAMING + SDL_RenderCopy, it's faster than SDL_RenderPoint.

See:

Related: Why do I get bad performance with SDL2 and SDL_RenderCopy inside a double for loop over all pixels?

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985