home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / LIB / x11r6pl13_lib_v0.6.lzh / X11R6PL13_LIB0.6 / EXAMPLE / xengine.c < prev   
C/C++ Source or Header  |  1995-11-11  |  12KB  |  382 lines

  1. static char     sccsid[] = "@(#)xengine.c 1.9   9/19/90";
  2.  
  3. /*
  4.  * xengine - reciprocating engine for X     :-)
  5.  * 
  6.  * Author: Kazuhiko Shutoh, 1990.
  7.  * 
  8.  * Permission to use, copy, modify and distribute without charge this software,
  9.  * documentation, images, etc. is granted, provided that this comment and the
  10.  * author's name is retained.  The author assumes no responsibility for lost
  11.  * sleep as a consequence of use of this software.
  12.  * 
  13.  * Send any comments, bug reports, etc. to: shutoh@isl.yamaha.JUNET or, for
  14.  * oversea: shutoh%isl.yamaha.JUNET%kddlab@uunet.uu.net
  15.  *
  16.  * Modified 2 May 1994 by David Fiedler, david@infopro.com, to report
  17.  * average RPM after a decent number of runs.
  18.  * 
  19.  */
  20.  
  21. #ifdef _OSK
  22. #include <stdio.h>
  23. #include <os9unix.h>
  24. #include <ux_time.h>
  25. #endif
  26.  
  27. #include <X11/Intrinsic.h>
  28. #include <X11/StringDefs.h>
  29. #include <X11/Xaw/Box.h>
  30. #include <X11/Xaw/Form.h>
  31. #include <X11/Xaw/AsciiText.h>
  32. #include <X11/Xaw/Cardinals.h>
  33. #include <X11/Xos.h>
  34. #include <stdio.h>
  35. #include <math.h>
  36.  
  37. #ifdef _OSK
  38. #define PATCHLEVEL 2
  39. #else
  40. #include "patchlevel.h"
  41. #define PI 3.141592654
  42. #endif
  43.  
  44. #define LOOPS 10
  45.  
  46. XtCallbackProc  redraw_callback();
  47. XtTimerCallbackProc Cycle();
  48.  
  49. Widget          toplevel, base, rpm_info, avg_info, engine;
  50. GC              gcPiston, gcShaft, gcCylinder, gcRoter, gcBack, gcDep, gcPre, gcEngine;
  51. Pixmap          enginePixmap;
  52. int             width, height;
  53.  
  54. String          colors[] = {"gray", "lightseagreen", "dimgrey", "forestgreen", "black", "red", "cyan", "firebrick"};
  55.  
  56. main(argc, argv)
  57.  int             argc;
  58.  char          **argv;
  59. {
  60.  
  61.  Arg             args[10];
  62.  XtTranslations  newTranslations;
  63.  static XtActionsRec redrawActions[] = {
  64.   {"expose", (XtCallbackProc) redraw_callback},
  65.  };
  66.  
  67.  static char    *overrideTranslations =
  68.  "<Expose>: expose() \n\
  69.   <ResReq>: expose()";
  70.  
  71.  XColor          srcColor, dummyColor;
  72.  
  73.  int             i;
  74.  int             count;
  75.  
  76.  XtTimerCallbackProc Cycle();
  77.  
  78.  toplevel = XtInitialize("xengine", "XEngine", NULL, 0, &argc, argv);
  79.  
  80.  for (count = 1; count < argc; count++) {
  81.   if ((strcmp("-piston", argv[count]) == 0) && (count + 1 <= argc))
  82.    strcpy(colors[0], argv[++count]);
  83.   else if ((strcmp("-shaft", argv[count]) == 0) && (count + 1 <= argc))
  84.    strcpy(colors[1], argv[++count]);
  85.   else if ((strcmp("-cylinder", argv[count]) == 0) && (count + 1 <= argc))
  86.    strcpy(colors[2], argv[++count]);
  87.   else if ((strcmp("-roter", argv[count]) == 0) && (count + 1 <= argc))
  88.    strcpy(colors[3], argv[++count]);
  89.   else if ((strcmp("-back", argv[count]) == 0) && (count + 1 <= argc))
  90.    strcpy(colors[4], argv[++count]);
  91.   else if ((strcmp("-dep", argv[count]) == 0) && (count + 1 <= argc))
  92.    strcpy(colors[5], argv[++count]);
  93.   else if ((strcmp("-pre", argv[count]) == 0) && (count + 1 <= argc))
  94.    strcpy(colors[6], argv[++count]);
  95.   else if ((strcmp("-patchlevel", argv[count]) == 0) && (count + 1 <= argc))
  96.     {
  97.     printf("xengine - written by Kazuhiko Shutoh\nSCCS ID : %s\nPatchlevel : %d\n", sccsid, PATCHLEVEL);
  98.     exit(0);
  99.   }
  100.   else if ((strcmp("-mono", argv[count]) == 0) && (count + 1 <= argc)) {
  101.    for (i = 0; i < 8; i++)
  102.     strcpy(colors[i], "white");
  103.    strcpy(colors[4], "black");
  104.   } else {
  105.    fprintf(stderr, "Usage : xengine [Toolkit-Options][-piston piston_color]"
  106.                    "[-shaft shaft_color][-cylinder cylinder_color]"
  107.                    "[-roter roter_color][-back background_color]"
  108.                    "[-dep depression_colore][-pre pression color][-mono][-patchlevel]\n");
  109.    exit(0);
  110.   }
  111.  }
  112.  
  113.  base = XtCreateManagedWidget("base", formWidgetClass, toplevel, NULL, 0);
  114.  
  115.  i = 0;
  116.  XtSetArg(args[i], XtNwidth, 200);
  117.  i++;
  118.  XtSetArg(args[i], XtNheight, 200);
  119.  i++;
  120.  engine = XtCreateManagedWidget("engine", boxWidgetClass, base, args, i);
  121.  width = height = 200;
  122.  
  123.  i = 0;
  124.  XtSetArg(args[i], XtNfromHoriz, engine);
  125.  i++;
  126.  XtSetArg(args[i], XtNheight, 200);
  127.  i++;
  128.  rpm_info = XtCreateManagedWidget("rpm_info", asciiTextWidgetClass, base, args, i);
  129.  avg_info = XtCreateManagedWidget("avg_info", asciiTextWidgetClass, base, args, i);
  130.  
  131.  XtAddActions(redrawActions, XtNumber(redrawActions));
  132.  newTranslations = XtParseTranslationTable(overrideTranslations);
  133.  XtOverrideTranslations(engine, newTranslations);
  134.  
  135.  XtRealizeWidget(toplevel);
  136.  
  137.  /* Get graphic context  */
  138.  
  139.  gcPiston = XCreateGC(XtDisplay(engine), XtWindow(engine), (unsigned long) 0, NULL);
  140.  XAllocNamedColor(XtDisplay(engine), DefaultColormap(XtDisplay(engine), 0), colors[0], &srcColor, &dummyColor);
  141.  
  142.  XSetForeground(XtDisplay(engine), gcPiston, srcColor.pixel);
  143.  
  144.  gcShaft = XCreateGC(XtDisplay(engine), XtWindow(engine), (unsigned long) 0, NULL);
  145.  XAllocNamedColor(XtDisplay(engine), DefaultColormap(XtDisplay(engine), 0), colors[1], &srcColor, &dummyColor);
  146.  
  147.  XSetForeground(XtDisplay(engine), gcShaft, srcColor.pixel);
  148.  XSetLineAttributes(XtDisplay(engine), gcShaft, 2, LineSolid, CapButt, JoinMiter);
  149.  
  150.  gcCylinder = XCreateGC(XtDisplay(engine), XtWindow(engine), (unsigned long) 0, NULL);
  151.  XAllocNamedColor(XtDisplay(engine), DefaultColormap(XtDisplay(engine), 0), colors[2], &srcColor, &dummyColor);
  152.  
  153.  XSetForeground(XtDisplay(engine), gcCylinder, srcColor.pixel);
  154.  
  155.  gcRoter = XCreateGC(XtDisplay(engine), XtWindow(engine), (unsigned long) 0, NULL);
  156.  XAllocNamedColor(XtDisplay(engine), DefaultColormap(XtDisplay(engine), 0), colors[3], &srcColor, &dummyColor);
  157.  
  158.  XSetForeground(XtDisplay(engine), gcRoter, srcColor.pixel);
  159.  
  160.  gcBack = XCreateGC(XtDisplay(engine), XtWindow(engine), (unsigned long) 0, NULL);
  161.  XAllocNamedColor(XtDisplay(engine), DefaultColormap(XtDisplay(engine), 0), colors[4], &srcColor, &dummyColor);
  162.  
  163.  XSetForeground(XtDisplay(engine), gcBack, srcColor.pixel);
  164.  
  165.  gcDep = XCreateGC(XtDisplay(engine), XtWindow(engine), (unsigned long) 0, NULL);
  166.  XAllocNamedColor(XtDisplay(engine), DefaultColormap(XtDisplay(engine), 0), colors[5], &srcColor, &dummyColor);
  167.  
  168.  XSetForeground(XtDisplay(engine), gcDep, srcColor.pixel);
  169.  
  170.  gcPre = XCreateGC(XtDisplay(engine), XtWindow(engine), (unsigned long) 0, NULL);
  171.  XAllocNamedColor(XtDisplay(engine), DefaultColormap(XtDisplay(engine), 0), colors[6], &srcColor, &dummyColor);
  172.  
  173.  XSetForeground(XtDisplay(engine), gcPre, srcColor.pixel);
  174.  
  175.  gcEngine = XCreateGC(XtDisplay(engine), XtWindow(engine), (unsigned long) 0, NULL);
  176.  XAllocNamedColor(XtDisplay(engine), DefaultColormap(XtDisplay(engine), 0), colors[7], &srcColor, &dummyColor);
  177.  
  178.  XSetForeground(XtDisplay(engine), gcEngine, srcColor.pixel);
  179.  
  180.  /* Create engine Pixmap */
  181.  
  182.  enginePixmap = XCreatePixmap(XtDisplay(engine), XtWindow(engine), width, height, DefaultDepth(XtDisplay(engine), 0));
  183.  
  184.  /* Interval timer start  */
  185.  XtAddTimeOut(1, Cycle, NULL);
  186.  
  187.  XtMainLoop();
  188. }
  189.  
  190. XtCallbackProc
  191. redraw_callback(w, event, params, nparams)
  192.  Widget          w;
  193.  XEvent         *event;
  194.  String         *params;
  195.  Cardinal       *nparams;
  196. {
  197.  
  198.  Arg             args[10];
  199.  
  200.  Begin_CallBack_Procedure();
  201.  
  202.  XtSetArg(args[0], XtNwidth, 0);
  203.  XtSetArg(args[1], XtNheight, 0);
  204.  XtGetValues(w, args, 2);
  205.  
  206.  width = args[0].value;
  207.  height = args[1].value;
  208.  
  209.  XFreePixmap(XtDisplay(engine), enginePixmap);
  210.  enginePixmap = XCreatePixmap(XtDisplay(engine), XtWindow(engine), width, height, DefaultDepth(XtDisplay(engine), 0));
  211.  
  212.  End_CallBack_Procedure();
  213. }
  214.  
  215. XtTimerCallbackProc
  216. Cycle(client_data, id)
  217.  caddr_t         client_data;
  218.  XtIntervalId    id;
  219. {
  220.  
  221.  Arg             args[10];
  222.  static char     label[256];
  223.  char            tmp_str[16];
  224.  static int      i = 0;
  225.  static int      flag = 0;
  226.  static int      sample = 0;
  227.  static double   mean = 0.0;
  228.  static double   rpm = 0.0;
  229.  static double total = 0.0;
  230.  double          DrawEngine();
  231.  
  232.  Begin_CallBack_Procedure();
  233.  
  234.  if (i++ > 10)
  235.     {
  236.     rpm = mean / 10.0;
  237.     sprintf(tmp_str, "%4.1f rpm\n", rpm);
  238.     total += rpm;
  239.  
  240.     if (sample++ > LOOPS)
  241.       {
  242.       strcat(label, tmp_str);
  243.       sprintf(tmp_str, "--- Avg %4.1f rpm ---\n", total / sample);
  244.       sample = 0;
  245.       total = 0.0;
  246.       flag = 1;
  247.       }
  248.  
  249.     strcat(label, tmp_str);
  250.     XtSetArg(args[0], XtNstring, label);
  251.     XtSetValues(rpm_info, args, 1);
  252.     i = 0;
  253.     mean = 0.0;
  254.     }
  255.  
  256.  mean += DrawEngine();
  257.  XtAddTimeOut(1, Cycle, NULL);
  258.  
  259.  if (flag)
  260.   {
  261.   system ("sleep 15");
  262.   exit(); 
  263.   }
  264.  
  265.  End_CallBack_Procedure();
  266. }
  267.  
  268. double
  269. DrawEngine()
  270. {
  271.  
  272.  double          cycle;
  273.  static int      sw = 1;
  274.  double          angle, theta;
  275.  double          piston_x1, piston_y1;
  276.  double          piston_x2, piston_y2;
  277.  struct timeval  start, end;
  278.  struct timezone dummy;
  279.  
  280.  sw ^= 1;
  281.  
  282.  XSync(XtDisplay(engine), 0);
  283.  gettimeofday(&start, &dummy);
  284.  for (angle = -180; angle < 180; angle += 18) {
  285.   theta = angle * (PI / 180.0);
  286.   piston_x1 = sin(theta) * (width * 0.1) + (width / 2);
  287.   piston_y1 = cos(theta) * (height * 0.1) + (height * 0.7);
  288.   piston_x2 = width / 2;
  289.   piston_y2 = piston_y1 - (height * 0.3);
  290.  
  291.   /* Crear Window */
  292.  
  293.   XFillRectangle(XtDisplay(engine), enginePixmap, gcBack, 0, 0, width, height);
  294.  
  295.   /* Draw heat sink */
  296.  
  297.   XFillRectangle(XtDisplay(engine), enginePixmap, gcEngine,
  298.           (int) (width * 0.35), (int) (height * 0.3),
  299.           (int) (width * 0.3), (int) (height * 0.02));
  300.  
  301.   XFillRectangle(XtDisplay(engine), enginePixmap, gcEngine,
  302.           (int) (width * 0.35), (int) (height * 0.35),
  303.           (int) (width * 0.3), (int) (height * 0.02));
  304.  
  305.   XFillRectangle(XtDisplay(engine), enginePixmap, gcEngine,
  306.           (int) (width * 0.35), (int) (height * 0.4),
  307.           (int) (width * 0.3), (int) (height * 0.02));
  308.  
  309.   /* Draw Cylinder */
  310.  
  311.   XFillRectangle(XtDisplay(engine), enginePixmap, gcEngine,
  312.           (int) (width * 0.42), (int) (height * 0.24),
  313.           (int) (width * 0.16), (int) (height * 0.5));
  314.  
  315.   XFillArc(XtDisplay(engine), enginePixmap, gcEngine,
  316.     (int) (piston_x2 - (width * 0.15)), (int) ((height * 0.7) - (height * 0.15)), (int) (width * 0.3),
  317.     (int) (height * 0.3), 0 * 64, 360 * 64);
  318.  
  319.   XFillArc(XtDisplay(engine), enginePixmap, gcBack,
  320.     (int) (piston_x2 - (width * 0.14)), (int) ((height * 0.7) - (height * 0.14)), (int) (width * 0.28),
  321.     (int) (height * 0.28), 0 * 64, 360 * 64);
  322.  
  323.   XFillRectangle(XtDisplay(engine), enginePixmap, gcBack,
  324.           (int) (width * 0.43), (int) (height * 0.25),
  325.           (int) (width * 0.14), (int) (height * 0.5));
  326.  
  327.   /* Draw Gas */
  328.  
  329.   if (sw != 0) {
  330.    XFillRectangle(XtDisplay(engine), enginePixmap, gcPre,
  331.           (int) (width * 0.435), (int) (height * 0.25),
  332.            (int) (width * 0.13), (int) (piston_y2 - (height * 0.05) - (height * 0.26) + (height * 0.1)));
  333.   } else {
  334.    XFillRectangle(XtDisplay(engine), enginePixmap, gcDep,
  335.           (int) (width * 0.435), (int) (height * 0.25),
  336.            (int) (width * 0.13), (int) (piston_y2 - (height * 0.05) - (height * 0.26) + (height * 0.1)));
  337.   }
  338.  
  339.   /* Draw Piston arm */
  340.  
  341.   XDrawLine(XtDisplay(engine), enginePixmap, gcShaft,
  342.      (int) piston_x1, (int) piston_y1, (int) piston_x2, (int) piston_y2);
  343.   XDrawLine(XtDisplay(engine), enginePixmap, gcShaft,
  344.      (int) piston_x1, (int) piston_y1, (int) piston_x2, (int) (height * 0.7));
  345.  
  346.  
  347.   /* Draw Piston & ring */
  348.  
  349.   XFillRectangle(XtDisplay(engine), enginePixmap, gcPiston,
  350.    (int) (width * 0.435), (int) (piston_y2 - (height * 0.05)),
  351.           (int) (width * 0.13), (int) (height * 0.1));
  352.  
  353.   XFillRectangle(XtDisplay(engine), enginePixmap, gcShaft,
  354.   (int) (width * 0.435), (int) (piston_y2 - (height * 0.045)),
  355.           (int) (width * 0.13), (int) (height * 0.01));
  356.  
  357.   XFillArc(XtDisplay(engine), enginePixmap, gcShaft,
  358.     (int) (piston_x2 - (width * 0.015)), (int) (piston_y2 - (height * 0.015)), (int) (width * 0.03),
  359.     (int) (height * 0.03), 0, 360 * 64);
  360.  
  361.   /* Draw Roter */
  362.   XFillArc(XtDisplay(engine), enginePixmap, gcRoter,
  363.     (int) (piston_x2 - (width * 0.1)), (int) ((height * 0.7) - (height * 0.1)), (int) (width * 0.2),
  364.     (int) (height * 0.2), (int) (angle + 30) * 64, 120 * 64);
  365.  
  366.   XFillArc(XtDisplay(engine), enginePixmap, gcPiston,
  367.     (int) (piston_x2 - (width * 0.025)), (int) ((height * 0.7) - (height * 0.025)), (int) (width * 0.05),
  368.     (int) (height * 0.05), 0, 360 * 64);
  369.  
  370.   XCopyArea(XtDisplay(engine), enginePixmap, XtWindow(engine), gcCylinder, 0, 0, width, height, 0, 0);
  371.  }
  372.  
  373.  XSync(XtDisplay(engine), 0);
  374.  gettimeofday(&end, &dummy);
  375.  
  376.  cycle = 60 / ((double) ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)) / 1000000);
  377.  
  378.  return (cycle);
  379.  
  380. }
  381.  
  382.