SDL_Surface表面

本文详细介绍了 SDL_Surface 结构的各个组成部分,包括其在不同场景下的应用方式,并提供了像素处理的具体示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本结构包含了使用软件方式显示的图片信息。

Uint32

flags

(内部使用)

SDL_PixelFormat*

format

图层的像素格式; 详见 SDL_PixelFormat  (只读)

int

w, h

像素级的宽度和高度 (只读)

int

pitch

一行像素所占的字节数(二位的图像像素会以一维的方式来存储,每一维的长度我们必须知道) (只读)

void*

pixels

指向真实像素的指针;详见讨论 (读写)

void*

userdata

用户可以任意设置的指针 (读写)

int

locked

用于需要锁定的图层 (内部使用)

void*

lock_data

用于需要锁定的图层 (内部使用)

SDL_Rect

clip_rect

SDL_Rect 结构体, 位图传送(blit)时用来裁剪区域 ,可以使用SDL_SetClipRect() 来设置(只读)

SDL_BlitMap*

map

向其他图层进行fast blit的映射信息 (内部使用)

int

refcount

引用计数,可以被程序增加

Uint32

flags

(internal use)

SDL_PixelFormat*

format

the format of the pixels stored in the surface; see SDL_PixelFormat for details (read-only)

int

w, h

the width and height in pixels (read-only)

int

pitch

the length of a row of pixels in bytes (read-only)

void*

pixels

the pointer to the actual pixel data; see Remarks for details (read-write)

void*

userdata

an arbitrary pointer you can set (read-write)

int

locked

used for surfaces that require locking (internal use)

void*

lock_data

used for surfaces that require locking (internal use)

SDL_Rect

clip_rect

an SDL_Rect structure used to clip blits to the surface which can be set by SDL_SetClipRect() (read-only)

SDL_BlitMap*

map

info for fast blit mapping to other surfaces (internal use)

int

refcount

reference count that can be incremented by the application


Pixel 概念。

数字屏幕是一个二维可显示的像素空间,即一个一个点形成的。如果是黑白色,只需要0,1即可表示。但如果这个点有表达多种颜色。
 
SDL_Surface 概念
 
Surface是平面的含意,在SDL中,所有绘制在视频输出都是通过SDL_Sur对face对象来输出。一个图像,一段文字,一个视频都需要转换成SDL_Surface对象来操作,它们可以平铺,堆叠。他们所有数据最终要在一个叠加在表示screen 的SDL_Surface的对象中输出显示。

因此SDL_Surface本质是一个矩形的像素内存,它需要通过专门的绘点函数来输出到不同设备上。
其中SDL_Surface 的坐标系是左上角是原点,向下向左递增.

SDL_Surface的高,宽值保存在结构的w,h成员。(SDL_Surface.w,SDL_Surface.h);
 

SDL_Surface 的底层输出
--------------------------------------
 在桌面平台一般无需处理原始像素输出。在嵌入式平台,如果分辩率不同,要做一些特殊处理。以下就一段通用代码。分别处理 像素数为8,16,32位的情况。这个后面会使用
 
像素处理

 
  1. void DrawPixel(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B) 
  2.     Uint32 color = SDL_MapRGB(screen->format, R, G, B); 
  3.  
  4.     if ( SDL_MUSTLOCK(screen) ) { 
  5.         if ( SDL_LockSurface(screen) < 0 ) { 
  6.             return
  7.         } 
  8.     } 
  9.     switch (screen->format->BytesPerPixel) { 
  10.         case 1: { /* Assuming 8-bpp */ 
  11.             Uint8 *bufp; 
  12.  
  13.             bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; 
  14.             *bufp = color; 
  15.         } 
  16.         break
  17.  
  18.         case 2: { /* Probably 15-bpp or 16-bpp */ 
  19.             Uint16 *bufp; 
  20.  
  21.             bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; 
  22.             *bufp = color; 
  23.         } 
  24.         break
  25.  
  26.         case 3: { /* Slow 24-bpp mode, usually not used */ 
  27.             Uint8 *bufp; 
  28.  
  29.             bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; 
  30.             *(bufp+screen->format->Rshift/8) = R; 
  31.             *(bufp+screen->format->Gshift/8) = G; 
  32.             *(bufp+screen->format->Bshift/8) = B; 
  33.         } 
  34.         break
  35.  
  36.         case 4: { /* Probably 32-bpp */ 
  37.             Uint32 *bufp; 
  38.  
  39.             bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; 
  40.             *bufp = color; 
  41.         } 
  42.         break
  43.     } 
  44.     if ( SDL_MUSTLOCK(screen) ) { 
  45.         SDL_UnlockSurface(screen); 
  46.     } 
  47.     SDL_UpdateRect(screen, x, y, 1, 1); 
蔡军生

跟老菜鸟学C++



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值