LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2461|回复: 7

如何在Linux下用C语言画图?

[复制链接]
发表于 2006-2-17 17:40:19 | 显示全部楼层 |阅读模式
我想画一些函数的图像,Linux下可不可以像Turbo C下仅仅调用一些库函数来完成。
发表于 2006-2-17 21:57:46 | 显示全部楼层
/usr/include/curses.h
回复 支持 反对

使用道具 举报

发表于 2006-2-18 03:55:30 | 显示全部楼层
用这个比较痛苦的
回复 支持 反对

使用道具 举报

发表于 2006-2-18 09:38:56 | 显示全部楼层
LZ不要把Linux当DOS啊
看看framebuffer的东西,可能回有点帮助
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-18 17:02:31 | 显示全部楼层
我只想在Linux下用最简单的办法完成我想要做的事情,不想学太多新的东西。
谢谢楼上的建议。curses.h是处理字符的,画图不太方便。有更好的办法吗?
回复 支持 反对

使用道具 举报

发表于 2006-2-19 13:11:40 | 显示全部楼层
framebuffer 编程
回复 支持 反对

使用道具 举报

发表于 2006-2-19 14:06:35 | 显示全部楼层
好像得学一个图形库的

建议学OpenGL.
回复 支持 反对

使用道具 举报

发表于 2006-2-19 14:30:15 | 显示全部楼层
摘自:http://users.actcom.co.il/%7Echo ... rogramming/events.c
也许对你有帮助。
编绎方法:cc events.c -o event -L/usr/X11R6/lib -lX11
运行方法:在X下
在Debian3下测试通过。
  1. /*
  2. * events.c - demonstrate handling of X events using an events loop.
  3. */
  4. #include <X11/Xlib.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>                /* getenv(), etc. */
  7. /*
  8. * function: create_simple_window. Creates a window with a white background
  9. *           in the given size.
  10. * input:    display, size of the window (in pixels), and location of the window
  11. *           (in pixels).
  12. * output:   the window's ID.
  13. * notes:    window is created with a black border, 2 pixels wide.
  14. *           the window is automatically mapped after its creation.
  15. */
  16. Window
  17. create_simple_window(Display* display, int width, int height, int x, int y)
  18. {
  19.   int screen_num = DefaultScreen(display);
  20.   int win_border_width = 2;
  21.   Window win;
  22.   /* create a simple window, as a direct child of the screen's */
  23.   /* root window. Use the screen's black and white colors as   */
  24.   /* the foreground and background colors of the window,       */
  25.   /* respectively. Place the new window's top-left corner at   */
  26.   /* the given 'x,y' coordinates.                              */
  27.   win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
  28.                             x, y, width, height, win_border_width,
  29.                             BlackPixel(display, screen_num),
  30.                             WhitePixel(display, screen_num));
  31.   /* make the window actually appear on the screen. */
  32.   XMapWindow(display, win);
  33.   /* flush all pending requests to the X server. */
  34.   XFlush(display);
  35.   return win;
  36. }
  37. GC
  38. create_gc(Display* display, Window win, int reverse_video)
  39. {
  40.   GC gc;                                /* handle of newly created GC.  */
  41.   unsigned long valuemask = 0;                /* which values in 'values' to  */
  42.                                         /* check when creating the GC.  */
  43.   XGCValues values;                        /* initial values for the GC.   */
  44.   unsigned int line_width = 2;                /* line width for the GC.       */
  45.   int line_style = LineSolid;                /* style for lines drawing and  */
  46.   int cap_style = CapButt;                /* style of the line's edje and */
  47.   int join_style = JoinBevel;                /*  joined lines.                */
  48.   int screen_num = DefaultScreen(display);
  49.   gc = XCreateGC(display, win, valuemask, &values);
  50.   if (gc < 0) {
  51.         fprintf(stderr, "XCreateGC: \n");
  52.   }
  53.   /* allocate foreground and background colors for this GC. */
  54.   if (reverse_video) {
  55.     XSetForeground(display, gc, WhitePixel(display, screen_num));
  56.     XSetBackground(display, gc, BlackPixel(display, screen_num));
  57.   }
  58.   else {
  59.     XSetForeground(display, gc, BlackPixel(display, screen_num));
  60.     XSetBackground(display, gc, WhitePixel(display, screen_num));
  61.   }
  62.   /* define the style of lines that will be drawn using this GC. */
  63.   XSetLineAttributes(display, gc,
  64.                      line_width, line_style, cap_style, join_style);
  65.   /* define the fill style for the GC. to be 'solid filling'. */
  66.   XSetFillStyle(display, gc, FillSolid);
  67.   return gc;
  68. }
  69. /*
  70. * function: handle_expose. handles an Expose event by redrawing the window.
  71. * input:    display, 2 GCs, XExposeEvent event structure, dimensions of
  72. *           the window, pixels array.
  73. * output:   none.
  74. */
  75. void
  76. handle_expose(Display* display, GC gc, GC rev_gc, XExposeEvent* expose_event,
  77.               unsigned int win_width, unsigned int win_height,
  78.               short pixels[1000][1000])
  79. {
  80.   /* if this is the first in a set of expose events - ignore this event. */
  81.   if (expose_event->count != 0)
  82.     return;
  83.   /* draw the contents of our window. */
  84.   /* draw one pixel near each corner of the window */
  85.   XDrawPoint(display, expose_event->window, gc, 5, 5);
  86.   XDrawPoint(display, expose_event->window, gc, 5, win_height-5);
  87.   XDrawPoint(display, expose_event->window, gc, win_width-5, 5);
  88.   XDrawPoint(display, expose_event->window, gc, win_width-5, win_height-5);
  89.   /* draw two intersecting lines, one horizontal and one vertical, */
  90.   /* which intersect at point "50,100".                            */
  91.   XDrawLine(display, expose_event->window, gc, 50, 0, 50, 200);
  92.   XDrawLine(display, expose_event->window, gc, 0, 100, 200, 100);
  93.   /* now use the XDrawArc() function to draw a circle whose diameter */
  94.   /* is 30 pixels, and whose center is at location '50,100'.         */
  95.   XDrawArc(display, expose_event->window, gc,
  96.            50-(30/2), 100-(30/2), 30, 30, 0, 360*64);
  97.   {
  98.     XPoint points[] = {
  99.       {0, 0},
  100.       {15, 15},
  101.       {0, 15},
  102.       {0, 0}
  103.     };
  104.     int npoints = sizeof(points)/sizeof(XPoint);
  105.     /* draw a small triangle at the top-left corner of the window. */
  106.     /* the triangle is made of a set of consecutive lines, whose   */
  107.     /* end-point pixels are specified in the 'points' array.       */
  108.     XDrawLines(display, expose_event->window, gc,
  109.                points, npoints, CoordModeOrigin);
  110.   }
  111.   /* draw a rectangle whose top-left corner is at '120,150', its width is */
  112.   /* 50 pixels, and height is 60 pixels.                                  */
  113.   XDrawRectangle(display, expose_event->window, gc, 120, 150, 50, 60);
  114.   /* draw a filled rectangle of the same size as above, to the left of the */
  115.   /* previous rectangle.                                                   */
  116.   XFillRectangle(display, expose_event->window, gc, 60, 150, 50, 60);
  117.   /* finally, draw all the pixels in the 'pixels' array. */
  118.   {
  119.     int x, y;
  120.     for (x=0; x<win_width; x++)
  121.       for (y=0; y<win_height; y++)
  122.         switch(pixels[x][y]) {
  123.           case 1: /* draw point. */
  124.             XDrawPoint(display, expose_event->window, gc, x, y);
  125.             break;
  126.           case -1: /* erase point. */
  127.             XDrawPoint(display, expose_event->window, rev_gc, x, y);
  128.             break;
  129.         }
  130.   }
  131. }
  132. /*
  133. * function: handle_drag. handles a Mouse drag event - if the left button
  134. *           is depressed - draws the pixel below the mouse pointer. if the
  135. *           middle button is depressed - erases the pixel below the mouse
  136. *           pointer.
  137. * input:    display, 2 GCs, XButtonEvent event structure, dimensions of
  138. *           the window, pixels array.
  139. * output:   none.
  140. */
  141. void
  142. handle_drag(Display* display, GC gc, GC rev_gc, XButtonEvent* drag_event,
  143.             unsigned int win_width, unsigned int win_height,
  144.             short pixels[1000][1000])
  145. {
  146.   int x, y;
  147.   /* invert the pixel under the mouse. */
  148.   x = drag_event->x;
  149.   y = drag_event->y;
  150.   switch (drag_event->state) {
  151.     case Button1Mask: /* draw the given pixel in black color. */
  152.       XDrawPoint(display, drag_event->window, gc, x, y);
  153.       pixels[x][y] = 1;
  154.       break;
  155.     case Button2Mask: /* draw the given pixel in white color. */
  156.       XDrawPoint(display, drag_event->window, rev_gc, x, y);
  157.       pixels[x][y] = -1;
  158.       break;
  159.   }
  160. }
  161. /*
  162. * function: handle_button_down. handles a Mouse press event - if the left
  163. *           button is depressed - draws the pixel below the mouse pointer.
  164. *           if the middle button is depressed - erases the pixel below the
  165. *           mouse pointer.
  166. * input:    display, 2 GCs, XButtonEvent event structure, dimensions of
  167. *           the window, pixels array.
  168. * output:   none.
  169. */
  170. void
  171. handle_button_down(Display* display, GC gc, GC rev_gc,
  172.                    XButtonEvent* button_event,
  173.                    unsigned int win_width, unsigned int win_height,
  174.                    short pixels[1000][1000])
  175. {
  176.   int x, y;
  177.   /* invert the pixel under the mouse. */
  178.   x = button_event->x;
  179.   y = button_event->y;
  180.   switch (button_event->button) {
  181.     case Button1: /* draw the given pixel in black color. */
  182.       XDrawPoint(display, button_event->window, gc, x, y);
  183.       pixels[x][y] = 1;
  184.       break;
  185.     case Button2: /* draw the given pixel in white color. */
  186.       XDrawPoint(display, button_event->window, rev_gc, x, y);
  187.       pixels[x][y] = -1;
  188.       break;
  189.   }
  190. }
  191. void
  192. main(int argc, char* argv[])
  193. {
  194.   Display* display;                /* pointer to X Display structure.           */
  195.   int screen_num;                /* number of screen to place the window on.  */
  196.   Window win;                        /* pointer to the newly created window.      */
  197.   unsigned int display_width,
  198.                display_height;        /* height and width of the X display.        */
  199.   unsigned int width, height;        /* height and width for the new window.      */
  200.   char *display_name = getenv("DISPLAY");  /* address of the X display.      */
  201.   GC gc, rev_gc;                /* GC (graphics context) used for drawing    */
  202.                                 /*  in our window.                             */
  203.   short pixels[1000][1000];        /* used to store pixels on screen that were  */
  204.                                 /* explicitly drawn or erased by the user.   */
  205.   /* initialize the 'pixels' array to contain 0 values. */
  206.   {
  207.     int x, y;
  208.     for (x=0; x<1000; x++)
  209.       for (y=0; y<1000; y++)
  210.         pixels[x][y] = 0;
  211.   }
  212.   /* open connection with the X server. */
  213.   display = XOpenDisplay(display_name);
  214.   if (display == NULL) {
  215.     fprintf(stderr, "%s: cannot connect to X server '%s'\n",
  216.             argv[0], display_name);
  217.     exit(1);
  218.   }
  219.   /* get the geometry of the default screen for our display. */
  220.   screen_num = DefaultScreen(display);
  221.   display_width = DisplayWidth(display, screen_num);
  222.   display_height = DisplayHeight(display, screen_num);
  223.   /* make the new window occupy 1/9 of the screen's size. */
  224.   width = (display_width / 3);
  225.   height = (display_height / 3);
  226.   printf("window width - '%d'; height - '%d'\n", width, height);
  227.   /* create a simple window, as a direct child of the screen's   */
  228.   /* root window. Use the screen's white color as the background */
  229.   /* color of the window. Place the new window's top-left corner */
  230.   /* at the given 'x,y' coordinates.                             */
  231.   win = create_simple_window(display, width, height, 0, 0);
  232.   /* allocate two new GCs (graphics contexts) for drawing in the window. */
  233.   /* the first is used for drawing black over white, the second is used  */
  234.   /* for drawing white over black.                                       */
  235.   gc = create_gc(display, win, 0);
  236.   rev_gc = create_gc(display, win, 1);
  237.   /* subscribe to the given set of event types. */
  238.   XSelectInput(display, win, ExposureMask | KeyPressMask |
  239.                      ButtonPressMask | Button1MotionMask |
  240.                      Button2MotionMask | StructureNotifyMask);
  241.   /* perform an events loop */
  242.   {
  243.     int done = 0;
  244.     XEvent an_event;
  245.     while (!done) {
  246.       XNextEvent(display, &an_event);
  247.       switch (an_event.type) {
  248.         case Expose:
  249.           /* redraw our window. */
  250.           handle_expose(display, gc, rev_gc, (XExposeEvent*)&an_event.xexpose,
  251.                              width, height, pixels);
  252.           break;
  253.   
  254.         case ConfigureNotify:
  255.           /* update the size of our window, for expose events. */
  256.           width = an_event.xconfigure.width;
  257.           height = an_event.xconfigure.height;
  258.           break;
  259.   
  260.         case ButtonPress:
  261.           /* invert the pixel under the mouse pointer. */
  262.           handle_button_down(display, gc, rev_gc,
  263.                              (XButtonEvent*)&an_event.xbutton,
  264.                              width, height, pixels);
  265.           break;
  266.   
  267.         case MotionNotify:
  268.           /* invert the pixel under the mouse pointer. */
  269.           handle_drag(display, gc, rev_gc,
  270.                       (XButtonEvent*)&an_event.xbutton,
  271.                       width, height, pixels);
  272.           break;
  273.   
  274.         case KeyPress:
  275.           /* exit the application by braking out of the events loop. */
  276.           done = 1;
  277.           break;
  278.   
  279.         default: /* ignore any other event types. */
  280.           break;
  281.       } /* end switch on event type */
  282.     } /* end while events handling */
  283.   }
  284.   /* free the GCs. */
  285.   XFreeGC(display, gc);
  286.   XFreeGC(display, rev_gc);
  287.   /* close the connection to the X server. */
  288.   XCloseDisplay(display);
  289. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表