Rendering pixels from array of RGB values in SDL 1.2?
Asked Answered
O

2

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.

Operative answered 24/12, 2013 at 1:40 Comment(0)
P
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)
Precarious answered 24/12, 2013 at 2:7 Comment(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.Cyathus
H
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?

Hurl answered 8/4, 2016 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.