本结构包含了使用软件方式显示的图片信息。
Uint32 | flags | (内部使用) |
format | 图层的像素格式; 详见 SDL_PixelFormat (只读) | |
int | w, h | 像素级的宽度和高度 (只读) |
int | pitch | 一行像素所占的字节数(二位的图像像素会以一维的方式来存储,每一维的长度我们必须知道) (只读) |
void* | pixels | 指向真实像素的指针;详见讨论 (读写) |
void* | userdata | 用户可以任意设置的指针 (读写) |
int | locked | 用于需要锁定的图层 (内部使用) |
void* | lock_data | 用于需要锁定的图层 (内部使用) |
clip_rect | SDL_Rect 结构体, 位图传送(blit)时用来裁剪区域 ,可以使用SDL_SetClipRect() 来设置(只读) | |
SDL_BlitMap* | map | 向其他图层进行fast blit的映射信息 (内部使用) |
int | refcount | 引用计数,可以被程序增加 |
Uint32 | flags | (internal use) |
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) |
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位的情况。这个后面会使用
像素处理
蔡军生
- void DrawPixel(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B)
- {
- Uint32 color = SDL_MapRGB(screen->format, R, G, B);
- if ( SDL_MUSTLOCK(screen) ) {
- if ( SDL_LockSurface(screen) < 0 ) {
- return;
- }
- }
- switch (screen->format->BytesPerPixel) {
- case 1: { /* Assuming 8-bpp */
- Uint8 *bufp;
- bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
- *bufp = color;
- }
- break;
- case 2: { /* Probably 15-bpp or 16-bpp */
- Uint16 *bufp;
- bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
- *bufp = color;
- }
- break;
- case 3: { /* Slow 24-bpp mode, usually not used */
- Uint8 *bufp;
- bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
- *(bufp+screen->format->Rshift/8) = R;
- *(bufp+screen->format->Gshift/8) = G;
- *(bufp+screen->format->Bshift/8) = B;
- }
- break;
- case 4: { /* Probably 32-bpp */
- Uint32 *bufp;
- bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
- *bufp = color;
- }
- break;
- }
- if ( SDL_MUSTLOCK(screen) ) {
- SDL_UnlockSurface(screen);
- }
- SDL_UpdateRect(screen, x, y, 1, 1);
- }