nou*_*fal 5 c c++ graphics sdl
我需要从此页面使用以下功能。该SDL_Surface结构被定义为
typedef struct SDL_Surface {
Uint32 flags; /* Read-only */
SDL_PixelFormat *format; /* Read-only */
int w, h; /* Read-only */
Uint16 pitch; /* Read-only */
void *pixels; /* Read-write */
SDL_Rect clip_rect; /* Read-only */
int refcount; /* Read-mostly */
} SDL_Surface;
Run Code Online (Sandbox Code Playgroud)
该函数是:
void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
Uint8 *target_pixel = (Uint8 *)surface->pixels + y * surface->pitch + x * 4;
*(Uint32 *)target_pixel = pixel;
}
Run Code Online (Sandbox Code Playgroud)
在这里我几乎没有疑问,可能是由于缺乏真实的画面。
surface->pitch用y,并x通过4?target_pixel为后续8-bit integer pointer,有什么必要32-bit integer pointer?target_pixel保留pixel值set_pixel?Uint32-valued 像素),但计算是在Uint8. 该4是丑陋的,见下文。计算必须以字节为单位,因为表面的pitch字段以字节为单位。
这是一个(没有我最初的尝试那么激进)重写:
void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) surface->pixels
+ y * surface->pitch
+ x * surface->format->BytesPerPixel);
*target_pixel = pixel;
}
Run Code Online (Sandbox Code Playgroud)
请注意我们如何surface->format->BytesPerPixel用来分解4. 魔术常数不是一个好主意。另请注意,以上假设表面确实使用 32 位像素。
您可以使用以下代码:
unsigned char* pixels = (unsigned char*)surface -> pixels;
pixels[4 * (y * surface -> w + x) + c] = 255;
x是您想要的点的 x,是该点y的 y 并c显示您想要的信息:
if c == 0 >>> blue
if c == 1 >>> green
if c == 2 >>> red
if c == 3 >>> alpha(不透明度)
| 归档时间: |
|
| 查看次数: |
9664 次 |
| 最近记录: |