1 #include <SDL.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include "SDL_draw.h" //包含SDL_draw库的头文件 6 int main() 7 { 8 int i; 9 double t; 10 double x,y; 11 double x0=320, y0=240; 12 SDL_Surface *screen; //一个屏幕指针 13 if(SDL_Init(SDL_INIT_VIDEO) < 0){ //初始化视频子系统失败 14 fprintf(stderr,"无法初始化:%s ",SDL_GetError()); //不能用printf(),因为没有包含stdio.h头文件 15 exit(1); 16 } 17 //设置视频模式 18 screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE); 19 if(screen==NULL){ 20 fprintf(stderr,"无法设置视频模式:%s ",SDL_GetError()); 21 exit(1); 22 } 23 atexit(SDL_Quit); //退出 24 25 //画直线,从点(240,180)到点(400,300),颜色为白色 26 Draw_Line(screen,240,180,400,300,SDL_MapRGB(screen->format, 255,255,255)); 27 //画直线,从点(400,180)到点(240,300),颜色为红色 28 Draw_Line(screen,400,180,240,300,SDL_MapRGB(screen->format, 255,0,0)); 29 for(i=0;i<640;i+=2){ 30 // y=240-120*sin(3.14*i/180); 31 x=x0 + 120*sin(3.14*i/180); 32 y=y0 + 120*cos(3.14*i/180); 33 Draw_Pixel(screen,x,y,SDL_MapRGB(screen->format,0,255,0)); 34 // for(t=0; t<500000; t++); 35 SDL_Delay(5); //停留5毫秒 36 SDL_UpdateRect(screen,0,0,0,0); //更新整个屏幕 37 // SDL_Delay(500); //停留5秒 38 } 39 SDL_UpdateRect(screen,0,0,0,0); //更新整个屏幕 40 SDL_Delay(5000); //停留5秒 41 return 0; 42 }
上面的代码做的是动态的画一个圆,然后下面就该设置一下视频背景色了,太懒,我就不整合到一起去了
1 #include<SDL.h> 2 #include<stdlib.h> 3 int main(){ 4 SDL_Surface *screen; 5 Uint32 color; 6 int x; 7 if(SDL_Init(SDL_INIT_VIDEO)<0){ 8 fprintf(stderr, "无法初始化SDL:%s ",SDL_GetError()); 9 exit(1); 10 } 11 screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE); 12 if(screen==NULL){ 13 fprintf(stderr, "无法设置视频%s ", SDL_GetError()); 14 exit(1); 15 } 16 atexit(SDL_Quit); 17 for(x=0;x<=255;x+=1){ 18 color=SDL_MapRGB(screen->format,255,255,x); 19 SDL_FillRect(screen,NULL,color); 20 SDL_UpdateRect(screen,0,0,0,0); 21 } 22 SDL_Delay(6000); 23 return 0; 24 }