在我的SDL系列文章中的上一篇我已经介绍了关于SDL的安装过程及其详细步骤,下面就开始我们的SDL征途吧。
   这一篇我着重为大家讲解如何利用SDL做一个简单的颜色自动变换调色板。

根据SDL官方文档我们很容易就能够获得建立一个屏幕的步骤:
首先我们要初始化一个Video,而后获得一个SDL_Surface;
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
如果函数调用成功则一切OK,
SDL给用户提供了很多画图的API,它们在SDL_gfxPrimitives.h文件中
在这里我们只用其中的一个来实现我们的功能

 
  
  1. boxRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, 
  2.   Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a); 


这个函数就可以实现在dst上显示出我们所要绘制的图案了...是不是感觉很high呀...就这么简单
下面我就把具体的代码贴出来供大家分享下...

 

 
  
  1. #include <SDL.h> 
  2. #include <SDL_gfxPrimitives.h> 
  3. #include <SDL_p_w_picpath.h> 
  4. #include <SDL_rotozoom.h> 
  5. #include <SDL_ttf.h> 
  6. #include <stdio.h> 
  7.  
  8. int main(int argc,char *argv[]) 
  9.  if(SDL_Init(SDL_INIT_VIDEO) < 0 ) 
  10.  { 
  11.   printf("Init error\n"); 
  12.   return -1; 
  13.  } 
  14.  
  15.  
  16.  SDL_Surface *screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE); 
  17.  
  18.  
  19.  if(!screen) 
  20.  { 
  21.   printf("Init video mode error\n"); 
  22.   return -1; 
  23.  } 
  24.  int i = 255; 
  25.  for(; i >= 0; i--) 
  26.  { 
  27.   SDL_FillRect(screen, &screen->clip_rect, 0x0); 
  28.   boxRGBA(screen, 100, 100, 300, 300, 255, 0, 0, i); 
  29.   SDL_Delay(20); 
  30.   SDL_UpdateRects(screen,1,&screen->clip_rect); 
  31.    
  32.  } 
  33.  SDL_Quit(); 
  34.  
  35.  return 0;