home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1994 Ralph Gonzalez */
-
- /*
- * FILE: mgraph.c
- * AUTHOR: R. Gonzalez
- * CREATED: March 19, 1990
- * MODIFIED: March 15, 1994
- * Adapted macgraph.c to agree with xgraph version
- *
- * PROJECT CONTENTS:
- * yourfiles.c, mgraph.c, ANSI, MacTraps libraries, MacHeaders option.
- *
- * mgraph.c contains several Macintosh routines to supplement
- * a typical stdio-type application with a graphics window, so that
- * you don't have to deal directly with the Macintosh Toolbox (see
- * below.) Your program must #include "mgraph.h" and must call
- * init_graphics() first.
- *
- * The device coordinate system (usually given in pixels) is norm-
- * alized to -1 to 1 horizontally by default. The vertical limits
- * depend on the screen's aspect ratio. This is used for placing and
- * sizing the graphics window.
- *
- * The window's own view coordinate system (which is used for
- * drawing) is also normalized, by default to -1 to 1 horizontally
- * and vertically.
- *
- * (The Macintosh Toolbox is a set of routines in ROM to produce the
- * graphical user interface. These Toolbox functions are declared in
- * MacHeaders. They are distinguished from my own functions by
- * beginning with a capital letter.)
- */
-
- # include "mgraph.h"
- # include <stdio.h>
-
- /*---------------------------------------------------------------------*/
- /* The following #define's may be adjusted to your taste: */
- /*---------------------------------------------------------------------*/
- # define TITLE "\pGraphics"
- /* # define MOVABLE */ /* use this if your program handles window moves */
- # define BACKGROUND BLACK /* this is unused, see background_color(), below */
- # define GRAPH_WIDTH 1. /* Window sizing & placement in */
- # define GRAPH_HEIGHT 1. /* normalized screen coords (below). */
- # define GRAPH_CENTER_X 0.
- # define GRAPH_CENTER_Y 0.
-
- /*---------------------------------------------------------------------*/
- /* You can change these too if you want: */
- /*---------------------------------------------------------------------*/
- # define NORM_WIDTH 2. /* define normalized screen coords */
- /* height depends on aspect ratio */
- # define NORM_CENTER_X 0.
- # define NORM_CENTER_Y 0.
- # define VIEW_WIDTH 2. /* define normalized window coords for drawing */
- # define VIEW_HEIGHT 2.
- # define VIEW_CENTER_X 0.
- # define VIEW_CENTER_Y 0.
- /*---------------------------------------------------------------------*/
-
- /* These represent the dimensions of the graphics window given in
- * actual window coordinates. Also the window coords of its center:
- */
- static double gGraphWidth,
- gGraphHeight,
- gGraphX,
- gGraphY;
-
- /* These represent the dimensions of normalized window coordinate
- * system used for drawing, and coords of its center:
- */
- static double gViewWidth,
- gViewHeight,
- gViewX,
- gViewY;
-
- static int transform_x(double),
- transform_y(double);
-
- /* globals for Macintosh implementation */
- static WindowPtr gGraphWindow,
- gConsoleWindow;
- static CursHandle gCursor;
- static color gPresentcolor;
- # define NIL_POINTER 0L
- # define MOVE_TO_FRONT -1L
- # define REMOVE_ALL_EVENTS 0
- # define VISIBLE 1
- # define NO_GO_AWAY 0
- # define NIL_REF_CON NIL_POINTER
-
- /* These represent the dimensions of the physical display device in
- * device coords, and the device coords of its center:
- */
- static double gDeviceWidth,
- gDeviceHeight,
- gDeviceCenterX,
- gDeviceCenterY;
-
- /* These represent the dimensions of the normalized device coordinate
- * system, used for window sizing. The desired coords for the center
- * of the device are also given:
- */
- static double gNormalDeviceWidth,
- gNormalDeviceHeight,
- gNormalDeviceCenterX,
- gNormalDeviceCenterY;
-
- /************************************************************************
- * You must call init_graphics() at the beginning of main().
- * Likely don't need all these initializations; they are taken from Mark
- * & Reed's "Macintosh Programming Primer", Addison-Wesley, 1989
- ************************************************************************/
- void init_graphics(void)
- {
- Rect rectangle;
- double ratio,
- temp;
- int left,
- top,
- width,
- height;
-
- /* Need a stdio function call to produce the generic console environment
- * BEFORE the remaining inits:
- */
- printf("\n");
- gConsoleWindow = FrontWindow();
-
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL_POINTER);
- InitCursor();
-
- /* calculate device and normalized screen coord systems */
- /* Note Macs use inverted y axis. This makes gD