你好,游客 登录 注册 搜索
背景:
阅读新闻

SDL Linux下的使用 计算机图形学

[日期:2011-03-12] 来源:Linux社区  作者:yming0221 [字体: ]

使用它来画一条直线

Ultility.h

  1. #ifndef ULTILITY_H_INCLUDED   
  2. #define ULTILITY_H_INCLUDED   
  3. #ifdef __APPLE__   
  4. #include <SDL/SDL.h>   
  5. #else   
  6. #include <SDL.h>   
  7. #endif   
  8. //init the screen   
  9. int init(SDL_Surface *&screen,SDL_Surface *&pic,int w,int h);   
  10. //draw a pixel   
  11. void DrawPixel(SDL_Surface *screen, int x,int y,Uint8 R, Uint8 G, Uint8 B);   
  12. //show a pic and free the resource   
  13. void Load(SDL_Surface *screen,SDL_Surface *pic);   
  14. #endif // DRAWPIXEL_H_INCLUDED   

SDL

Example.h

  1. #ifndef EXAMPLE_H_INCLUDED   
  2. #define EXAMPLE_H_INCLUDED   
  3. #ifdef __APPLE__   
  4. #include <SDL/SDL.h>   
  5. #else   
  6. #include <SDL.h>   
  7. #endif   
  8. //Line   
  9. void DDA_Line(int x0,int y0,int x1,int y1,SDL_Surface *pic);   
  10. void MidPoint_Line(int x0,int y0,int x1,int y1,SDL_Surface *pic);   
  11. //end Line   
  12. #endif // EXAMPLE_H_INCLUDED   

Ultility.cpp

  1. #include "Ultility.h"   
  2. //init the screen   
  3. int init(SDL_Surface *&screen,SDL_Surface *&pic,int w,int h)   
  4. {   
  5.     // initialize SDL video   
  6.     if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )   
  7.     {   
  8.         printf( "Unable to init SDL: %s\n", SDL_GetError() );   
  9.         return 1;   
  10.     }   
  11.     // make sure SDL cleans up before exit   
  12.     atexit(SDL_Quit);   
  13.     // create a new window   
  14.     screen = SDL_SetVideoMode(w, h, 32,SDL_HWSURFACE|SDL_DOUBLEBUF);   
  15.     pic = SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,32, 0, 0,0,0);   
  16. }   
  17. //draw a pixel   
  18. void DrawPixel(SDL_Surface *screen, int x,int y,Uint8 R, Uint8 G, Uint8 B)   
  19. {   
  20.     Uint32 color = SDL_MapRGB(screen->format, R, G, B);   
  21.     if ( SDL_MUSTLOCK(screen) ) {   
  22.         if ( SDL_LockSurface(screen) < 0 ) {   
  23.             return;   
  24.         }   
  25.     }   
  26.     switch (screen->format->BytesPerPixel) {   
  27.         case 1: { /* 假定是8-bpp */  
  28.             Uint8 *bufp;   
  29.             bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;   
  30.             *bufp = color;   
  31.         }   
  32.         break;   
  33.         case 2: { /* 可能是15-bpp 或者 16-bpp */  
  34.             Uint16 *bufp;   
  35.             bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;   
  36.             *bufp = color;   
  37.         }   
  38.         break;   
  39.         case 3: { /* 慢速的24-bpp模式,通常不用 */  
  40.             Uint8 *bufp;   
  41.             bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;   
  42.             *(bufp+screen->format->Rshift/8) = R;   
  43.             *(bufp+screen->format->Gshift/8) = G;   
  44.             *(bufp+screen->format->Bshift/8) = B;   
  45.         }   
  46.         break;   
  47.         case 4: { /* 可能是32-bpp */  
  48.             Uint32 *bufp;   
  49.             bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;   
  50.             *bufp = color;   
  51.         }   
  52.         break;   
  53.     }   
  54.     if ( SDL_MUSTLOCK(screen) ) {   
  55.         SDL_UnlockSurface(screen);   
  56.     }   
  57.     SDL_UpdateRect(screen, x, y, 1, 1);   
  58. }   
  59. void Load(SDL_Surface *screen,SDL_Surface *pic)   
  60. {   
  61.       // centre the bitmap on screen   
  62.     SDL_Rect dstrect;   
  63.     dstrect.x = (screen->w - pic->w) / 2;   
  64.     dstrect.y = (screen->h - pic->h) / 2;   
  65.     // DRAWING STARTS HERE   
  66.     // clear screen   
  67.     SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));   
  68.     // draw bitmap   
  69.     SDL_BlitSurface(pic, 0, screen, &dstrect);   
  70.     // DRAWING ENDS HERE   
  71.     // finally, update the screen :)   
  72.     SDL_Flip(screen);   
  73.     // free loaded bitmap   
  74.     SDL_FreeSurface(pic);   
  75.     // all is well ;)   
  76. }  
linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款