Rendering pixels from array of RGB values in SDL 1.2?

只谈情不闲聊 提交于 2019-12-18 08:59:33

问题


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.


回答1:


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)



回答2:


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

See:

  • official example: http://hg.libsdl.org/SDL/file/e12c38730512/test/teststreaming.c
  • my derived example which does not require blob data and compares both methods: https://github.com/cirosantilli/cpp-cheat/blob/0607da1236030d2e1ec56256a0d12cadb6924a41/sdl/plot2d.c

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



来源:https://stackoverflow.com/questions/20753726/rendering-pixels-from-array-of-rgb-values-in-sdl-1-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!