IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]SDL_Surface表面

    caimouse发表于 2016-12-05 08:13:07
    love 0

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

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

    跟老菜鸟学C++





沪ICP备19023445号-2号
友情链接