killshen

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 3 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

现在就来开始我的第一个windows涂鸦程序

 1 #include <stdlib.h>
 2 #include <sdl.h>
 3 
 4 void DrawPixel(SDL_Surface*,int,int,Uint8,Uint8,Uint8);  
 5 
 6 int main(int argc,char *argv[])
 7 {
 8     SDL_Surface *screen = NULL;
 9     if(SDL_Init(SDL_INIT_VIDEO))
10     {
11         fprintf(stderr,"无法初始化! %s",SDL_GetError());
12         exit(1);
13     }
14     atexit(SDL_Quit);
15     
16     screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
17     if(screen == NULL)
18     {
19         fprintf(stderr,"无法设置800*600分辨率 %s",SDL_GetError());
20         exit(1);
21     }
22     
23     for(int x=0; x<800; x++)
24     {
25         for(int y=0; y<600; y++)
26         {
27             DrawPixel(screen,x,y,x,y,(x+y)/2);
28         }
29     }
30     SDL_Flip(screen);
31     SDL_Event event;
32     int sing = 1;
33     while(sing==1)
34     {
35         while(SDL_PollEvent(&event))
36         {
37             if(event.type==SDL_QUIT)
38                  sing = 0;
39         }    
40     }    
41     SDL_FreeSurface(screen);
42     return 0;
43 }
44 
45 //==============================================================================
46 void DrawPixel(SDL_Surface *screen,int x,int y,Uint8 R,Uint8 G,Uint8 B)
47 {
48     if(SDL_MUSTLOCK(screen))
49     {
50         if(SDL_LockSurface(screen)<0)
51         {
52            return;
53         }
54     }
55     Uint32 color = SDL_MapRGB(screen->format,R,G,B);
56     switch(screen->format->BytesPerPixel)
57     {
58         case 1:
59             {
60                 Uint8 *bufp;
61                 bufp = (Uint8 *)screen->pixels+y*screen->pitch+x;
62                 *bufp = color;
63             }
64             break;
65         case 2:
66             {
67                 Uint16 *bufp;
68                 bufp = (Uint16 *)screen->pixels+y*screen->pitch/2+x;
69                 *bufp = color;
70             }
71             break;
72         case 3:
73             {
74                 Uint8 *bufp;
75                 bufp = (Uint8 *)screen->pixels+y*screen->pitch+x;
76                 *(bufp+screen->format->Rshift/8= R;
77                 *(bufp+screen->format->Gshift/8= G;
78                 *(bufp+screen->format->Gshift/8= B;
79             }
80             break;
81          case 4:
82              {
83                  Uint32 *bufp;
84                  bufp = (Uint32 *)screen->pixels+y*screen->pitch/8+x;
85                  *bufp = color;
86              }
87              break;
88      }                          
89      if(SDL_MUSTLOCK(screen))
90      {
91          SDL_UnlockSurface(screen);
92      }
93 }                                     
好了这段代码是我从网上找资料七拼八凑起来的,在dev-cpp里可以通过编译。运行结果当然是...自己看,
记得编译时要修改工程属性为 windows图形程序 在连接器里加参数 -lmingw32 -lSDLmain -lSDL
在来分析下代码,我承认我很懒从不写注释。
主要介绍几个SDL函数,也加强一下自己的记忆
1。 SDL_Init( )
              原型  
             int SDL_Init(Uint32 flags);
        初始化SDL环境 ,即视频,声音,定时器,CDROM,游戏手柄等等
              flags 取值为以下组合 
                   SDL_INIT_VIDEO     视频
                   SDL_INIT_TIMER      定时器
                   SDL_INIT_AUDIO     声音
                   SDL_INIT_CDROM    CDROM
        成功返回0否则返回-1。
       注意:在程序退出之前要调用 SDL_Quit( ) 在每个退出点前都要调用 SDL_Quit( )
                  为了避免这个麻烦,可以用 atexit(SDL_Quit) 申明一下,这样不管是从哪里退出,系统都会自动  调用 SDL_Quit( )了。                        
       
2。SDL_SetVideoMode( )
         函数原形是 SDL_Surface *SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
    设置屏幕的分辨率,成功的话返回一个 SDL_Surface 的指针,否则返回NULL 
    width和height就是长和宽,bpp是颜色位数。
    flags取值为以下组合
      SDL_SWSURFACE     在内存中创建surface
      SDL_HWSURFACE     在现存中创建surface
      SDL_DOUBLEBUF     启用双缓冲
      SDL_FULLSCREEN    全屏
   还有其他取值,不过这四个是最常用的,通常的组合是 SDL_HWSURFACE|SDL_DOUBLEBUF

  再来说一下SDL_Surface 这个东东,SDL里的所有一切都是画在SDL_Surface上的。     

3。DrawPixel()
   这个自己写的函数是照操SDL官网上的,就是在 x,y处画一个颜色为 (R,G,B)颜色的点,说实话,我也不理解这里面的内容,不过照着用就行了。SDL_Flip(screen)是刷新 screen,即在屏幕上显示出screen中画的所有的点。

4。接下来就是一个非常简陋的消息循环了,SDL_Event 是SDL的消息结构,SDL_PollEvent()不停的从消息队列里取出消息。

5。SDL_FreeSurface(screen) 释放掉screen 记得你创建一个 SDL_Surface 一定要记得释放,否则。。。。

基本代码就这样了。
参考资料  SDL中文教程 http://www.libsdl.org/intro.cn/toc.html
        SDL API参考 http://my.sdlgame.com/sdl/documentation/index.html
          

    

posted on 2006-10-08 22:29 魔剑 阅读(314) 评论(0)  编辑 收藏 引用 所属分类: SDL

只有注册用户登录后才能发表评论。
相关文章:
网站导航:   博客园   博客园最新博文   博问   管理