home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #2 / amigaacscoverdisc1998-021998.iso / utilities / shareware / dev / libx11 / graphics_examples / class7.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  5.6 KB  |  270 lines

  1. /*
  2.   Program Seven
  3.   Bert Nelson
  4.   Copyright 1992 Bert Nelson
  5.  
  6.   Purpose:     display a simple animation 
  7.  
  8.  
  9.   Compile Instructions:  cc file.c -lX11 -lm -o file
  10.  
  11.   Run Instructions:  type 'file' after compiling and click a button
  12.              on the mouse to start the animation.
  13.                  Press 'q' to quit.
  14.  
  15.  
  16.    Permission to use, copy, modify, and distribute this software and its
  17.    documentation for any purpose and without fee is hereby granted,
  18.    provided that the above copyright notice appear in all copies and that
  19.    both that copyright notice and this permission notice appear in
  20.    supporting documentation.  It is provided "as is" without any express or
  21.    implied warranty.
  22.  
  23.    Bert Nelson DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  24.    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  25.    Bert Nelson BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  26.    ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  27.    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  28.    ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  29.    SOFTWARE.
  30.  
  31.     
  32.   */
  33.  
  34.  
  35.  
  36. #include <X11/Xlib.h>
  37. #include <X11/Xutil.h>
  38.  
  39.  
  40. /* set strings as global */
  41.  
  42. char hello[] = "Animation by Bert Nelson";
  43. unsigned long white,black;
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char **argv;
  48. {
  49.     Display *mydisplay;
  50.     Window  mywindow;
  51.     GC mygc;
  52.     XEvent myevent;
  53.     KeySym mykey;
  54.     XSizeHints myhint;
  55.  
  56.     int myscreen;
  57.     unsigned long myforeground, mybackground;
  58.     int i;
  59.     char text[10];
  60.     int done;
  61.  
  62.     /* set up display and foreground/background */
  63.  
  64.     mydisplay = XOpenDisplay("");
  65.  
  66.     myscreen = DefaultScreen (mydisplay);
  67.     white = mybackground = WhitePixel (mydisplay, myscreen);
  68.     black = myforeground = BlackPixel (mydisplay, myscreen);
  69.  
  70.     /* create a window with a width of 1000 x 700 */
  71.  
  72.     myhint.x = 0;
  73.     myhint.y = 0;
  74.     myhint.width = 1000;
  75.     myhint.height = 700;
  76.     myhint.flags = PPosition | PSize;
  77.     mywindow = XCreateSimpleWindow (mydisplay,
  78.         DefaultRootWindow (mydisplay),
  79.         myhint.x, myhint.y, myhint.width, myhint.height,
  80.         5, myforeground, mybackground);
  81.  
  82.  
  83.  
  84.     XSetStandardProperties (mydisplay, mywindow, hello, hello,
  85.         None, argv, argc, &myhint);
  86.  
  87.     mygc = XCreateGC (mydisplay, mywindow, 0, 0);
  88.  
  89.     XSetBackground (mydisplay, mygc, mybackground);
  90.     XSetForeground (mydisplay, mygc, myforeground);
  91.  
  92.     XSelectInput (mydisplay, mywindow,
  93.         ButtonPressMask | KeyPressMask | ExposureMask);
  94.  
  95.     /* map window to the screen */
  96.  
  97.     XMapRaised (mydisplay, mywindow);
  98.  
  99.  
  100.     /* Loop through until a q is press when the cursor is in
  101.            the window, which will cause the application to quit */
  102.  
  103.     done = 0;
  104.     while (done == 0) {
  105.         XNextEvent (mydisplay, &myevent );
  106.  
  107.  
  108.         switch (myevent.type) {
  109.  
  110.         case Expose:
  111.             if (myevent.xexpose.count == 0)
  112.  
  113.                 break;
  114.  
  115.  
  116.         case MappingNotify:
  117.             XRefreshKeyboardMapping (&myevent);
  118.             break;
  119.  
  120.  
  121.     /* When there is a mouse click fire up the graphics subroutines */
  122.  
  123.         case ButtonPress:
  124.  
  125.             animate(mydisplay,mywindow,mygc);
  126.             break;
  127.  
  128.             /* If a 'q' was pressed while the cursor is within
  129.            the window set done to 1, which causes the loop
  130.            to be broken and the program finishes */
  131.  
  132.  
  133.         case KeyPress:
  134.             i = XLookupString (&myevent, text, 10, &mykey, 0);
  135.             if (i == 1 && text[0] == 'q')
  136.                 done = 1;
  137.             break;
  138.  
  139.  
  140.         } /* switch */
  141.  
  142.  
  143.     } /* while (done == 0) */
  144.  
  145.  
  146.     /* Free up and clean up the window created */
  147.  
  148.  
  149.     XFreeGC (mydisplay, mygc);
  150.     XDestroyWindow (mydisplay, mywindow);
  151.     XCloseDisplay (mydisplay);
  152.  
  153.     exit(0);
  154.  
  155. } /* main */
  156.  
  157. /* The purpose of animate is to show a simple animation of two
  158.    black and white triangles coming at each other and crashing */
  159.  
  160.  
  161. animate(the_display,the_window,the_gc)
  162. Display *the_display;
  163. Drawable the_window;
  164. GC the_gc;
  165.  
  166.  
  167. {
  168.     int k;
  169.     int i;
  170.     int j;
  171.     float speed;
  172.     XPoint figure2[4];
  173.     XPoint figure1[4];
  174.  
  175.     /* set up the two triangles at opposite edges of the screen */
  176.  
  177.     figure1[0].x = 100;
  178.     figure1[0].y = 350;
  179.     figure1[1].x = 200;
  180.     figure1[1].y = 350;
  181.     figure1[2].x = 100;
  182.     figure1[2].y = 250;
  183.     figure1[3].x = 100;
  184.     figure1[3].y = 350;
  185.     figure2[0].x = 800;
  186.     figure2[0].y = 350;
  187.     figure2[1].x = 900;
  188.     figure2[1].y = 350;
  189.     figure2[2].x = 900;
  190.     figure2[2].y = 250;
  191.     figure2[3].x = 800;
  192.     figure2[3].y = 350;
  193.  
  194.  
  195.     XClearWindow(the_display,the_window);
  196.  
  197.     /* set up initial speed */
  198.  
  199.     speed = 4;
  200.  
  201.     /* draw the two figures */
  202.  
  203.  
  204.     for (i=0; i<14; i++)
  205.  
  206.     {
  207.         /* draw the two triangles */
  208.         XDrawLines(the_display,the_window,the_gc,
  209.             figure2,4,CoordModeOrigin);
  210.         XDrawLines(the_display,the_window,the_gc,
  211.             figure1,4,CoordModeOrigin);
  212.  
  213.  
  214.         /* add to the speed, which will make it culmulative and give
  215.            the appeareance that the triangles are speeding up as they
  216.            get closer and closer */
  217.  
  218.         speed = speed + 2.5;
  219.  
  220.  
  221.         /* move the figures over an increment/decement of speed */
  222.  
  223.         for (j=0; j<4; j++)
  224.         {
  225.             figure1[j].x = figure1[j].x + speed ;
  226.             figure2[j].x = figure2[j].x  - speed ;
  227.         }
  228.  
  229.         /* clear the window for the next frame */
  230.  
  231.         XClearWindow(the_display,the_window);
  232.  
  233.     }
  234.  
  235.  
  236.     /* do the crash by stretching and pulling the triangles apart */
  237.  
  238.     for (i=0; i<25; i= i + 1)
  239.     {
  240.         /* draw the triangles */
  241.  
  242.         XDrawLines(the_display,the_window,the_gc,
  243.             figure1,4,CoordModeOrigin);
  244.         XDrawLines(the_display,the_window,the_gc,
  245.             figure2,4,CoordModeOrigin);
  246.  
  247.         /* stretch the triangles by moving the points around */
  248.  
  249.         for (j=0; j<4; j++)
  250.         {
  251.             figure1[j].y = figure1[j].y - 4 * (j);
  252.             figure1[j].x = figure1[j].x - 4 * (j) ;
  253.             figure2[j].y = figure2[j].y + 4 * (j);
  254.             figure2[j].x = figure2[j].x + 4 * (j) ;
  255.  
  256.         }
  257.  
  258.         /* clear the screen for the next frame 
  259.             to give a animation effect */
  260.  
  261.         XClearWindow(the_display,the_window);
  262.  
  263.     }
  264.  
  265.  
  266. }
  267.  
  268.  
  269.  
  270.