home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / languages / progs / shell / Other / !ShellEG / c / Main
Encoding:
Text File  |  1994-07-07  |  5.3 KB  |  181 lines

  1. /*
  2. Example program which uses some of the the Shell_* functions.
  3. It displays a times-table in a window, along with various
  4. labels and an animated bar-graph.
  5. */
  6.  
  7.  
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <stdio.h>
  13. #include <math.h>
  14.  
  15.  
  16. #include "Desklib:Error.h"
  17.  
  18. #include "Shell.RectSave.h"    /* For saving of data in rectangles                */
  19. #include "Shell.TextRect.h"    /* For displaying large amounts of text in a rect. in a window    */
  20. #include "Shell.Printf.h"    /* A substitute for printf.                    */
  21. #include "Shell.Array.h"    /* For displaying 2D arrays.                    */
  22. #include "Shell.FontLabel.h"    /* For displaying small 'labels' in an outline font.        */
  23. #include "Shell.Label.h"    /* For displaying small 'labels' in the system font.        */
  24. #include "Shell.BlockRct.h"    /* For displaying blocks which consist of small rectangles.    */
  25. #include "Shell.Extra.h"    /* For various low-level graphics utils.            */
  26. #include "Shell.BarGraph.h"    /* For a bar-graph display.                    */
  27. #include "Shell.PlainRect.h"    /* For saving part of a Shell window as a sprite        */
  28. #include "Shell.BarIcon.h"    /* For an icon on the iconbar, and a default menu.        */
  29.  
  30.  
  31. #define PI 3.14159265
  32.  
  33.  
  34.  
  35.  
  36. char *TimesTableRedrawer( int x, int y, Shell_GeneralArrayInfo *info)
  37.     /* Returns a string which contains the contents of array location (x,y)    */
  38. {
  39. static    char s[64];
  40. sprintf( s, info->format, (x+1)*(y+1));
  41. return s;
  42. }
  43.  
  44.  
  45.  
  46.  
  47. int BlockRedrawer( int x, int y, int maxx, int maxy, void *data)
  48.     /* This plots a 2D gaussian by returning the colour of    */
  49.     /* the block at position (x,y).                */
  50.  
  51. {    double    dx, dy;
  52. dx = (double) x-maxx/2.0;
  53. dy = (double) y-maxy/2.0;
  54.  
  55. return (int) (8 - 7.99 * exp( -0.5 * (dx*dx+dy*dy)/50 ));
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. int main()
  64. {
  65.  
  66. Shell_windblock    *wind;
  67. Shell_rectblock    *textrect, *barrect, *anglerect;
  68. int        i;
  69. clock_t        t = 0;
  70. int        bars[100];
  71.  
  72.  
  73. Shell_Init( /*taskname */ "Shell Demomstration", /*resources*/ "ShellDemo");
  74.     /* This calls Event_Initialise() etc.    */
  75.  
  76. Shell_AddBarIcon( "application");
  77.  
  78. Shell_InitRectSave(            /* This registers a handler for clicks on any shell    */
  79.     "SaveAs",/*template name*/    /* window. If the click is Shift-Menu, and the pointer    */
  80.     3,    /* dragsprite    */    /* is over a savable rectangle, a save box is opened.    */
  81.     0,    /* ok        */    /* There is a template called 'SaveAs' in ShellEG's    */
  82.     1,    /* cancel    */    /* 'Templates' file.                    */
  83.     2    /* filename    */
  84.     );
  85.  
  86. /* TextRectPrintf-ing to a NULL windblock opens a default window    */
  87. /* Shell_Printf is equivalent to Shell_TextRectPrintf( NULL, ...).        */
  88.  
  89. Shell_Printf( "This is the default text-output window.\n");
  90. Shell_Printf( "---------------------------------------\n\n");
  91. Shell_Printf( "Use Shift-Menu to save array/text rectangles...\n\n");
  92.  
  93.  
  94. wind        = Shell_OpenGFXWindow();
  95. textrect    = Shell_AddTextRect( wind, 30, -400, colour_BLACK, colour_YELLOW);
  96.  
  97.  
  98. Shell_TextRectPrintf( textrect, "Some text\n");
  99. Shell_SetWindowTitle( wind->window, "Shell Demonstration");
  100.  
  101. Shell_FontLabel( wind, -100, 20, colour_BLACK, colour_GREY3, "Some anti-aliased text");
  102. Shell_FontLabel( wind, 800, Shell_TEXTYSIZE, colour_BLACK, colour_GREY1, "Times tables...");
  103.  
  104. Shell_AddGeneralArray(
  105.     wind,
  106.     800, 0,                /* Position of top-left of the array            */
  107.     12, 12,             /* x and y size of array.                */
  108.     9,                /* Width of array's columns (in chrs).            */
  109.     "%i",                /* This string is passed to the redrawer.        */
  110.     TimesTableRedrawer,        /* Fn which returns text for any element of the array.    */
  111.     NULL,                /* pointer that is passed to the redraw fn.        */
  112.     colour_BLACK, colour_ORANGE    /* fore/back colour of the array.            */
  113.     );
  114.  
  115. Shell_AddGeneralBlockRect( wind, 100, 100, 20, 20, 8, 8, BlockRedrawer, NULL);
  116. Shell_AddGeneralBlockRect( wind, 500, 100, 20, 20, 8, 8, BlockRedrawer, NULL);
  117.     /* Displays an exponantial-decay square, using 'BlockRedrawer' which returns    */
  118.     /* the brightness of any block in the rectangle.                */
  119.  
  120.  
  121. barrect = Shell_AddBarGraph(
  122.     wind,
  123.     30, -200,             /* Position of bottom-left of bar graph            */
  124.     30,                 /* Number of bars in the graph.                */
  125.     16, 12,             /* Spacing and width of bars.                */
  126.     96,                 /* Maximum height of bars.                */
  127.     bars,                /* Pointer to an array of 30 integers for bar heights    */
  128.     colour_RED, colour_GREY2
  129.     );
  130.     /* 'bars' is an array of the heights of each bar. Later, this array will be changed    */
  131.     /* and the bar-graph rectangle redrawn to give animation.                */
  132.  
  133.  
  134.  
  135. anglerect = Shell_Label( wind, 30, -50, colour_BLACK, colour_GREY3, "Angle =    ");
  136. Shell_SetRectUpdateTime( anglerect, 0.5);
  137.  
  138.  
  139. Shell_MakeRectIcon(
  140.     Shell_AddPlainRect( wind, -150, -250, 550, 300),
  141.     colour_TRANSPARENT, colour_TRANSPARENT,
  142.     "r1"
  143.     );
  144.     /* Make a rect which can be saved as a sprite.    */
  145. Shell_Label( wind, -140, -300, colour_BLACK, colour_GREEN, "Try Shift-Menu inside the rectangle above here...");
  146.  
  147.  
  148.  
  149. /* The next bit animates a bar-graph...    */
  150.  
  151. for (;;) for ( i=0; i<100; i++)    {
  152.  
  153.     int    a;
  154.     double    angle = i/100.0 * 16 * PI;
  155.  
  156.     for ( a=0; a<30; a++)    {        /* Update the array of bar-heights    */
  157.         bars[a] = 48 + (int) (40 *
  158.             (
  159.             sin( angle + a/2.0)  * cos( angle/4 + a/3.0)
  160.             )
  161.             );
  162.         }
  163.  
  164.     Shell_ForceRectUpdate( barrect);    /* Redraw the bargraph imediately without    */
  165.                         /* clearing the rectangle.            */
  166.  
  167.     Shell_ReLabelfSlow( anglerect, "Angle = %g", angle);
  168.         /* Only updates at speed set earlier.    */
  169.  
  170.     if ( clock()/CLOCKS_PER_SEC/10 != t/10)    {
  171.         t = clock()/CLOCKS_PER_SEC;
  172.         Shell_Printf( "\nTime since start is now %i seconds\n\n", t);
  173.         Shell_TextRectPrint( textrect, "Some more text...\n");
  174.         }
  175.  
  176.     Shell_PollSlow();
  177.     }
  178.  
  179. return 0;
  180. }
  181.