2中的RGB值数组渲染像素

2中的RGB值数组渲染像素

本文介绍了从SDL 1.2中的RGB值数组渲染像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在NES模拟器,我无法弄清楚如何渲染像素。我使用一个三维数组来保存每个像素的RGB值。对于256 x 224屏幕大小,数组定义如下:

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];

例如, [0] [0] [0] 保存蓝色值, [0] [0] [1] 保存绿色值, [0] [0] [2 ] 保存屏幕位置 [0] [0] 处的像素的红色值。

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].

当vblank标志变高时,我需要渲染屏幕。当SDL去渲染屏幕时,screenData数组将充满每个像素的RGB值。我能够找到一个名为 SDL_CreateRGBSurfaceFrom 的函数,看起来它可能工作,我想做的。但是,我看到的所有示例使用1维数组的RGB值,而不是一个3维数组。

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.

什么将是最好的方式,像素?这也将是很好,如果功能允许我调整表面某种程度,所以我不必使用256 x 224窗口大小。

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.

推荐答案

您需要将数据存储为一维 char array

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)

这篇关于从SDL 1.2中的RGB值数组渲染像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:05