SDL系列之 - 用画直线的方法来画正弦曲线
线段长度无限短后就成为点,所以,现在让我们用画直线的方法来画正弦曲线吧
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 Uint32 screen_color; 13 SDL_Surface *screen; //一个屏幕指针 14 if(SDL_Init(SDL_INIT_VIDEO) < 0){ //初始化视频子系统失败 15 fprintf(stderr,"无法初始化:%s\n",SDL_GetError()); //不能用printf(),因为没有包含stdio.h头文件 16 exit(1); 17 } 18 //设置视频模式 19 screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE); 20 if(screen==NULL){ 21 fprintf(stderr,"无法设置视频模式:%s\n",SDL_GetError()); 22 exit(1); 23 } 24 atexit(SDL_Quit); //退出 25 26 //画直线,从点(240,180)到点(400,300),颜色为白色 27 Draw_Line(screen,240,180,400,300,SDL_MapRGB(screen->format, 255,255,255)); 28 //画直线,从点(400,180)到点(240,300),颜色为蓝色 29 Draw_Line(screen,400,180,240,300,SDL_MapRGB(screen->format, 0,0,255)); 30 for(i=0;i<640;i+=2){ 31 x=240-120*sin(3.14*i/180); 32 i+=2; 33 y=240-120*sin(3.14*i/180); 34 Draw_Line(screen,i-2,x,i,y,SDL_MapRGB(screen->format, 0,0,255)); 35 SDL_Delay(5); //停留5毫秒 36 SDL_UpdateRect(screen,0,0,0,0); //更新整个屏幕 37 } 38 SDL_UpdateRect(screen,0,0,0,0); //更新整个屏幕 39 SDL_Delay(3000); //停留5秒 40 return 0; 41 }