#include "SDL.h"
SDL中的文档描述SDL_CreateRGBSurface函数如下:
SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
Description
Allocate an empty surface (must be called after SDL_SetVideoMode)
If depth is 8 bits an empty palette is allocated for the surface, otherwise a 'packed-pixel' SDL_PixelFormat is created using the [RGBA]mask's provided (see SDL_PixelFormat). The flags specifies the type of surface that should be created, it is an OR'd combination of the following possible values.
SDL_SWSURFACE | SDL will create the surface in system memory. This improves the performance of pixel level access, however you may not be able to take advantage of some types of hardware blitting. |
SDL_HWSURFACE | SDL will attempt to create the surface in video memory. This will allow SDL to take advantage of Video->Video blits (which are often accelerated). |
SDL_SRCCOLORKEY | This flag turns on colourkeying for blits from this surface. If SDL_HWSURFACE is also specified and colourkeyed blits are hardware-accelerated, then SDL will attempt to place the surface in video memory. Use SDL_SetColorKey to set or clear this flag after surface creation. |
SDL_SRCALPHA | This flag turns on alpha-blending for blits from this surface. If SDL_HWSURFACE is also specified and alpha-blending blits are hardware-accelerated, then the surface will be placed in video memory if possible. Use SDL_SetAlpha to set or clear this flag after surface creation. |
在stackoverflow上描述如下:
They are completely different functions.
SDL_SetVideoMode creates the video surface (a.k.a. application screen) and show it to the user.
SDL_CreateRGBSurface creates an empty surface.
After calling SDL_SetVideoMode, if successful, a screen will be shown to the user and you will have (returned by the function, or by calling SDL_GetVideoSurface) the video surface, the screen surface.
SDL_CreateRGBSurface simply creates an empty surface that you can play with it.
Some usage example would be: your application starts and you initialize the video, then you create an empty surface and manipulate it somehow, and finally you blit it to the video surface and the user will see the surface that you manipulated (remember to flip the screen surface, SDL_Flip).
It's important to know what a SDL_Surface is. Since you don't asked I assume you know
也就是说分配了一个空的surface,但是我们可以在上面操作! 然后通过SDL_BlitSurface函数将SDL_CreateRGBSurface创建的surface拷贝给我们用 SDL_SetVideoMode 创建的surface上。我们可以任意操作 SDL_CreateRGBSurfac创建的surface,最后拷贝给真正能看见的那个surface。这样就很好的避免了我们一直操作显示的那个surface带来的巨大的开销!下面是我写的一个简短的Coding:
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_gfxPrimitives.h"
#define WIDTH 640
#define HEIGTH 480
int main(int argc,char **argv)
{
if((SDL_Init(SDL_INIT_VIDEO)) == -1)
{
fprintf(stderr,"SDL init error:%s",SDL_GetError());
return -1;
}
SDL_Surface *surface = SDL_SetVideoMode(WIDTH , HEIGTH, 32 , SDL_SWSURFACE);
if(surface == NULL)
{
fprintf(stderr,"Could not Creat a Screen!:%s",SDL_GetError());
exit(1);
}
SDL_PixelFormat *f = surface->format;
printf("%d %d %d %d\n" , f->Rmask , f->Gmask, f->Bmask, f->Amask);
SDL_Surface *tempsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGTH , 32, f->Rmask, f->Gmask, f->Bmask, f->Amask);
if(tempsurface == NULL)
{
fprintf(stderr,"Could not Creat a Screen!:%s",SDL_GetError());
exit(1);
}
hlineColor(tempsurface, 100, 200 ,100 , 0xffffffff);
//tempsurface = IMG_Load("worm.jpg");
SDL_BlitSurface(tempsurface, &(tempsurface->clip_rect) , surface, &(surface->clip_rect));
SDL_UpdateRects(surface , 1 , &(surface->clip_rect));
SDL_Delay(10000);
return 0;
}
/*
*cross text:
*
*gcc main.c -o main -I/usr/local/include/SDL -L/usr/local/lib -lSDL -lSDL_image -lSDL_gfx
*
*/