home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / mgraph / mgraph.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  12.9 KB  |  140 lines  |  [TEXT/????]

  1. /* Copyright 1994 Ralph Gonzalez */
  2.  
  3. /*
  4. *    FILE:        mgraph.c
  5. *    AUTHOR:        R. Gonzalez
  6. *    CREATED:    March 19, 1990
  7. *    MODIFIED:    March 15, 1994
  8. *                Adapted macgraph.c to agree with xgraph version
  9. *
  10. *   PROJECT CONTENTS:
  11. *   yourfiles.c, mgraph.c, ANSI, MacTraps libraries, MacHeaders option.
  12. *    
  13. *    mgraph.c contains several Macintosh routines to supplement
  14. *    a typical stdio-type application with a graphics window, so that
  15. *    you don't have to deal directly with the Macintosh Toolbox (see
  16. *    below.) Your program must #include "mgraph.h" and must call
  17. *    init_graphics() first.
  18. *    
  19. *    The device coordinate system (usually given in pixels) is norm-
  20. *    alized to -1 to 1 horizontally by default. The vertical limits
  21. *    depend on the screen's aspect ratio. This is used for placing and
  22. *    sizing the graphics window.
  23. *
  24. *    The window's own view coordinate system (which is used for
  25. *    drawing) is also normalized, by default to -1 to 1 horizontally
  26. *    and vertically. 
  27. *
  28. *    (The Macintosh Toolbox is a set of routines in ROM to produce the
  29. *    graphical user interface.  These Toolbox functions are declared in
  30. *    MacHeaders.  They are distinguished from my own functions by
  31. *    beginning with a capital letter.)
  32. */
  33.  
  34. # include    "mgraph.h"
  35. # include    <stdio.h>
  36.  
  37. /*---------------------------------------------------------------------*/
  38. /*    The following #define's may be adjusted to your taste: */
  39. /*---------------------------------------------------------------------*/
  40. # define    TITLE                "\pGraphics"
  41. /* # define    MOVABLE */                    /* use this if your program handles window moves */
  42. # define    BACKGROUND            BLACK    /* this is unused, see background_color(), below */
  43. # define    GRAPH_WIDTH            1.    /* Window sizing & placement in      */
  44. # define    GRAPH_HEIGHT        1.    /* normalized screen coords (below). */
  45. # define    GRAPH_CENTER_X        0.    
  46. # define    GRAPH_CENTER_Y        0.
  47.  
  48. /*---------------------------------------------------------------------*/
  49. /*    You can change these too if you want: */
  50. /*---------------------------------------------------------------------*/
  51. # define    NORM_WIDTH            2.    /* define normalized screen coords */
  52.                                     /* height depends on aspect ratio */
  53. # define    NORM_CENTER_X        0.
  54. # define    NORM_CENTER_Y        0.
  55. # define    VIEW_WIDTH            2.    /* define normalized window coords for drawing */
  56. # define    VIEW_HEIGHT            2.
  57. # define    VIEW_CENTER_X        0.
  58. # define    VIEW_CENTER_Y        0.
  59. /*---------------------------------------------------------------------*/
  60.  
  61. /*    These represent the dimensions of the graphics window given in
  62. *    actual window coordinates. Also the window coords of its center:
  63. */
  64. static double        gGraphWidth,    
  65.                     gGraphHeight,
  66.                     gGraphX,
  67.                     gGraphY;
  68.                     
  69. /*    These represent the dimensions of normalized window coordinate
  70. *    system used for drawing, and coords of its center:
  71. */
  72. static double        gViewWidth,        
  73.                     gViewHeight,
  74.                     gViewX,
  75.                     gViewY;
  76.  
  77. static int            transform_x(double),
  78.                     transform_y(double);
  79.  
  80. /* globals for Macintosh implementation */
  81. static WindowPtr    gGraphWindow,
  82.                     gConsoleWindow;
  83. static CursHandle    gCursor;
  84. static color        gPresentcolor;
  85. # define            NIL_POINTER            0L
  86. # define            MOVE_TO_FRONT        -1L
  87. # define            REMOVE_ALL_EVENTS    0
  88. # define            VISIBLE                1
  89. # define            NO_GO_AWAY            0
  90. # define            NIL_REF_CON            NIL_POINTER
  91.  
  92. /*    These represent the dimensions of the physical display device in
  93. *    device coords, and the device coords of its center:
  94. */
  95. static double        gDeviceWidth,    
  96.                     gDeviceHeight,
  97.                     gDeviceCenterX,
  98.                     gDeviceCenterY;
  99.                     
  100. /*    These represent the dimensions of the normalized device coordinate
  101. *    system, used for window sizing. The desired coords for the center
  102. *    of the device are also given:
  103. */
  104. static double        gNormalDeviceWidth,    
  105.                     gNormalDeviceHeight,
  106.                     gNormalDeviceCenterX,
  107.                     gNormalDeviceCenterY;
  108.  
  109. /************************************************************************
  110. *    You must call init_graphics() at the beginning of main().
  111. *    Likely don't need all these initializations; they are taken from Mark
  112. *    & Reed's "Macintosh Programming Primer", Addison-Wesley, 1989
  113. ************************************************************************/
  114. void    init_graphics(void)
  115. {
  116.     Rect        rectangle;
  117.     double        ratio,
  118.                 temp;
  119.     int            left,
  120.                 top,
  121.                 width,
  122.                 height;
  123.     
  124. /*    Need a stdio function call to produce the generic console environment
  125. *    BEFORE the remaining inits:
  126. */
  127.     printf("\n");
  128.     gConsoleWindow = FrontWindow();
  129.     
  130.     InitGraf(&thePort);
  131.     InitFonts();
  132.     FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
  133.     InitWindows();
  134.     InitMenus();
  135.     TEInit();
  136.     InitDialogs(NIL_POINTER);
  137.     InitCursor();
  138.     
  139.     /* calculate device and normalized screen coord systems */
  140.     /* Note Macs use inverted y axis. This makes gD