home *** CD-ROM | disk | FTP | other *** search
- /*
- demograf.c
-
- Copyright (c) 1988, by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- This program demonstrates how C-scape screens can be placed
- on top of graphics images.
-
- This program relies on the compiler supplied graphics library
- to draw the graphic images.
-
- The following graphics libraries are supported:
-
- Microsoft C5: you must define M5 on the compile line.
- Turbo C2: Turbo C defines the Symbol __TURBOC__
-
- Note: You must run the DOS utility GRAPTABL.COM
- before running this program on a CGA.
- GRAPTABL.COM loads the extended ASCII character
- set for use in graphics mode.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
-
- #include <cscape.h>
- #include <popdecl.h>
- #include <framer.h>
-
- #include <pcmode.h>
-
- /*** Graphics Lib headers ***/
-
- #ifdef M5 /* Microsoft 5.x */
- #include <graph.h>
- #endif
-
- #ifdef __TURBOC__ /* Turbo C 2.0 */
- #include <graphics.h>
-
- /* BGI_PATH is directory in which initgraph looks for the
- Borland .BGI files. You may have to change this path
- depending on how you have installed your Turbo C compiler.
- Consult the Turbo C manual under initgraph for more information.
- Note that the backslashes in the path name must quoted
- with a preceding backslash.
- */
-
- #define BGI_PATH "\\TC\\"
-
- #endif
-
- /*** data structure to hold information about the demo ***/
- typedef struct {
-
- int shape; /* current shape type */
- int size; /* current shape size */
- int color; /* current painting color */
-
- int hgt; /* display height in pixels */
- int wid; /* display width in pixels */
- int ncolors; /* number of available colors */
-
- byte reg; /* colors used by menus and popups */
- byte sel;
-
- } dgraf_struct;
-
-
- /*** Macros and definitions ***/
-
- #define nrand(n) (rand() % n)
- #define BLANK 998 /* positive for compat w/ cs31 */
- #define QUIT 999
- #define MIXED 999
-
- /*** Function prototypes ***/
-
- int draw(dgraf_struct *);
-
- int u_erase(VOID *, int); /* User functions connected to pulldown menu */
- int u_shape(VOID *, int);
- int u_size(VOID *, int);
- int u_color(VOID *, int);
- int u_horoscope(VOID *, int);
-
- void dgraf_Clear(void);
- void dgraf_Ellipse(opbox *, int);
- void dgraf_Rectangle(opbox *, int);
- void dgraf_Line(opbox *, int);
- int dgraf_Init(dgraf_struct *);
-
- /*** The pulldown menu definition ***/
-
- struct frame_def main_menu[] = {
-
- { "Erase", u_erase, BLANK},
- { FRAME_END },
- { "Figure", NULL, BLANK},
- { " Mixed ", u_shape, MIXED},
- { " Line ", u_shape, 1},
- { " Circle ", u_shape, 2},
- { " Square ", u_shape, 3},
- { FRAME_END },
- { "Size", NULL, BLANK},
- { " Mixed ", u_size, MIXED},
- { " Small ", u_size, 1},
- { " Medium ", u_size, 2},
- { " Large ", u_size, 3},
- { FRAME_END },
- { "Color", NULL, BLANK},
- { " Mixed ", u_color, MIXED},
- { " Single ", u_color, 1},
- { FRAME_END },
- { "Horoscope", NULL, BLANK},
- { " Fast ", u_horoscope, 5},
- { " Medium ", u_horoscope, 3},
- { " Slow ", u_horoscope, 1},
- { FRAME_END },
- { "Quit", NULL, QUIT},
- { FRAME_END },
- { FRAME_END },
- };
-
-
- void main()
- {
- dgraf_struct dgraf;
- sed_type frame;
- int ret, key, x;
-
- /* Initialize the C-scape device interface:
- def_ModeGraphics selects best available graphics mode
- grwin_Class saves graphic in the background window
- */
-
- if (!disp_Init(def_ModeGraphics, grwin_Class)) {
- printf("demograf: No graphics hardware found\n");
- return;
- }
-
- /* Initialize Graphics Library */
- if ((x = dgraf_Init(&dgraf)) != 0) {
- disp_Close();
- #ifdef __TURBOC__ /* Turbo C 2.0 */
- printf("demograf: Turbo C initgraph error %d\n", x);
- #else
- printf("demograf: unable to initialize graphics library.\n");
- #endif
- }
-
- /* Create the pulldown menu */
- frame = frame_Open(main_menu, bd_1, dgraf.reg, dgraf.sel, dgraf.reg);
- frame_Repaint(frame);
-
- while (TRUE) {
-
- while (!kb_Check()) {
- draw(&dgraf);
- }
-
- /* key pressed, read it and call pulldown menu */
- key = kb_Read();
- if ((ret = frame_Go(frame, key, (VOID *) &dgraf )) == QUIT) {
- /* Quit selected, break out of while loop */
- break;
- }
- /* if first letter search of menu failed, try again */
- else if (ret == 0) {
- if (frame_Go(frame, ' ', (VOID *) &dgraf ) == QUIT) {
- /* Quit selected, break out of while loop */
- break;
- }
- }
- }
-
- /* close down device interface */
- disp_Close();
- }
-
- int draw(dgrafp)
- dgraf_struct *dgrafp;
- /*
- Draws a shape using the current information
- in the dgraf structure.
- */
- {
- int shape, size, color;
- opbox box;
-
- shape = (dgrafp->shape == MIXED) ? nrand(3) + 1 : dgrafp->shape;
- size = (dgrafp->size == MIXED) ? nrand(3) + 1: dgrafp->size;
- color = (dgrafp->color == MIXED) ? nrand(dgrafp->ncolors) : dgrafp->color;
-
- box.ymin = nrand(dgrafp->hgt);
- box.xmin = nrand(dgrafp->wid);
- box.ymax = box.ymin + (dgrafp->hgt)/10 * size;
- box.xmax = box.xmin + (dgrafp->wid)/10 * size;
-
- switch(shape) {
- case 1:
- dgraf_Line(&box, color);
- break;
- case 2:
- dgraf_Ellipse(&box, color);
- break;
- case 3:
- dgraf_Rectangle(&box, color);
- break;
- }
-
- return(TRUE);
- }
-
- /*---------------------------------------------------------------------------*/
- /* User functions: functions called from the pull down menu */
- /* pdata is a pointer to the dgraf struct and is passed via frame_Go */
-
- int u_erase(pdata, idata)
- VOID *pdata;
- int idata;
- /*
- Erase the display
- */
- {
- dgraf_Clear();
-
- return(1);
- }
-
- int u_shape(pdata, idata)
- VOID *pdata;
- int idata;
- /*
- Change the current shape to idata's value
- idata is defined in the menu definition structure.
- */
- {
- dgraf_struct *dgrafp;
-
- dgrafp = (dgraf_struct *) pdata;
-
- dgrafp->shape = idata;
-
- return(1);
- }
-
- int u_size(pdata, idata)
- VOID *pdata;
- int idata;
- /*
- Change the current size to idata's value
- idata is defined in the menu definition structure.
- */
- {
- dgraf_struct *dgrafp;
-
- dgrafp = (dgraf_struct *)pdata;
-
- dgrafp->size = idata;
-
- return(1);
- }
-
- int u_color(pdata, idata)
- VOID *pdata;
- int idata;
- /*
- Change the current color to idata's value
- idata is defined in the menu definition structure.
- */
- {
- dgraf_struct *dgrafp;
-
- dgrafp = (dgraf_struct *)pdata;
-
- dgrafp->color = idata;
-
- return(1);
- }
-
- char *horoscope_msg[] = {
- "AQUARIUS\nYou will find a substanstial amount of cash in your toothpaste.",
- "PISCES\nYou will be the Elvis of the nineties.",
- "ARIES\nA surprise gift of a lawn ornament is not out of the question.",
- "TAURUS\nA dream about cows carries a clue to building a better life for yourself.",
- "GEMINI\nAll your hard work will pay off; aluminum siding is yours for the asking.",
- "CANCER\nA ringing pay phone holds your key to a world of fun.",
- "LEO\nYou are too busy to waste time on horoscopes.",
- "VIRGO\nA warped bowling alley will lead you to a major scientific discovery.",
- "LIBRA\nYou will star in a major motion picture with Garrett Morris.",
- "SCORPIO\nYou need a good change of pace. Purchase some imported cheeses.",
- "SAGITTARIUS\nGood times ahead, despite a boring horoscope.",
- "CAPRICORN\nUnexpected company will be dropping in. Be sure you bathroom is tidy."
- };
-
- #define COUNT (sizeof(horoscope_msg) / sizeof(char *))
-
- int u_horoscope(pdata, idata)
- VOID *pdata;
- int idata;
- /*
- Slides a window across the display.
- idata determines the speed of the horoscope.
- idata comes from the framer definition structure.
- */
- {
- sed_type sed;
- int col;
- dgraf_struct *dgrafp;
-
- dgrafp = (dgraf_struct *)pdata;
-
- /* Create a message window */
- sed = pop_Text(horoscope_msg[nrand(COUNT)], 10, disp_GetWidth(), -1, 22, dgrafp->reg, bd_1);
-
- /* Slide the window across the display */
- for (col = disp_GetWidth(); col > -20; col -= idata) {
- sed_SetPosition(sed, 10, col);
- }
-
- /* Destroy the window */
- sed_Close(sed);
-
- return(TRUE);
- }
-
-
- /*---------------------------------------------------------------------------*/
- /* Portable graphics routines:
- The following routines are defined for the various graphics libraries
- supported.
- */
-
- #ifdef M5 /* Microsoft */
-
- int dgraf_Init(dgrafp)
- dgraf_struct *dgrafp;
- /*
- Initialize the graphics library.
- Initialize demo data.
-
- Returns 0 if successful.
-
- Microsoft version.
- */
- {
- struct videoconfig config; /* Microsoft defined structure */
- int char_height; /* height of characters in pixels */
-
- switch(pc_GetMode()) {
- case 0x13:
- _setvideomode(_MRES256COLOR); /* initialize MicroSoft video mode */
- char_height = 8;
- break;
-
- case 0x12:
- _setvideomode(_VRES16COLOR); /* initialize MicroSoft video mode */
- char_height = 16;
- break;
-
- case 0x11:
- _setvideomode(_VRES2COLOR); /* initialize MicroSoft video mode */
- char_height = 16;
- break;
-
- case 0x10:
- _setvideomode(_ERESCOLOR); /* initialize MicroSoft video mode */
- char_height = 16;
- break;
-
- case 0x0f:
- _setvideomode(_ERESNOCOLOR); /* initialize MicroSoft video mode */
- char_height = 14;
- break;
-
- case 0x0e:
- _setvideomode(_HRES16COLOR); /* initialize MicroSoft video mode */
- char_height = 8;
- break;
-
- case 0x0d:
- _setvideomode(_MRES16COLOR); /* initialize MicroSoft video mode */
- char_height = 8;
- break;
-
- case 0x06:
- _setvideomode(_HRESBW); /* initialize MicroSoft video mode */
- char_height = 8;
- break;
-
- case 0x05:
- _setvideomode(_MRESNOCOLOR); /* initialize MicroSoft video mode */
- char_height = 8;
- break;
- default:
- return(1);
- }
-
- /* initialize config structure */
- _getvideoconfig(&config);
-
- dgraf_Clear();
-
- /* Set clip region to avoid drawing over menu bar */
- _setcliprgn(0, char_height, config.numxpixels - 1, config.numypixels - 1);
-
- /* set up dgraf data */
-
- dgrafp->shape = MIXED;
- dgrafp->size = MIXED;
- dgrafp->color = MIXED;
-
- dgrafp->hgt = config.numypixels;
- dgrafp->wid = config.numxpixels;
- dgrafp->ncolors = config.numcolors;
-
- /* Set up the menu colors */
- if (disp_GetColors() > 2L) {
- dgrafp->reg = 0x34;
- dgrafp->sel = 0x43;
- }
- else {
- dgrafp->reg = 0x10;
- dgrafp->sel = 0x01;
- }
-
- return(0);
- }
-
- void dgraf_Line(opboxp, color)
- opbox *opboxp; /* defined by C-scape */
- int color;
- /*
- Draw a line from
- (opboxp->xmin, opboxp->ymax) to (opboxp->xmax, opboxp->ymax)
- with color color.
-
- Microsoft version.
- */
- {
- _setcolor(color);
- _moveto(opboxp->xmin, opboxp->ymin);
- _lineto(opboxp->xmax, opboxp->ymax);
- }
-
- void dgraf_Rectangle(opboxp, color)
- opbox *opboxp;
- int color;
- /*
- Draw a rectangle with corners
- (opboxp->xmin, opboxp->ymax) (opboxp->xmax, opboxp->ymax)
- with color color.
-
- Microsoft version.
- */
- {
- _setcolor(color);
- _rectangle(_GBORDER, opboxp->xmin, opboxp->ymin, opboxp->xmax, opboxp->ymax);
- }
-
- void dgraf_Ellipse(opboxp, color)
- opbox *opboxp;
- int color;
- /*
- Draw an ellipse bounded by the rectangle with corners
- (opboxp->xmin, opboxp->ymax) (opboxp->xmax, opboxp->ymax)
- with color color.
-
- Microsoft version.
- */
- {
- _setcolor(color);
- _ellipse(_GBORDER, opboxp->xmin, opboxp->ymin, opboxp->xmax, opboxp->ymax);
- }
-
- void dgraf_Clear()
- /*
- Clear the display.
-
- Microsoft version.
- */
- {
- _clearscreen(_GVIEWPORT);
- }
-
- #endif
- /*---------------------------------------------------------------------------*/
-
- #ifdef __TURBOC__ /* Turbo C 2.0 */
-
- int dgraf_Init(dgrafp)
- dgraf_struct *dgrafp;
- /*
- Initialize the graphics library.
- Initialize demo data.
-
- Returns 0 if successful.
-
- Turbo C version.
- */
- {
- int mode, driver;
- int char_height; /* height of characters in pixels */
-
- switch(pc_GetMode()) {
- case 0x13:
- /* Not supported by Turbo C */
- return(FALSE);
-
- case 0x12:
- driver = VGA;
- mode = VGAHI;
- char_height = 16;
- break;
-
- case 0x11:
- driver = MCGA;
- mode = MCGAHI;
- char_height = 16;
- break;
-
- case 0x10:
- driver = EGA;
- mode = EGAHI;
- char_height = 16;
- break;
-
- case 0x0f:
- driver = EGAMONO;
- mode = EGAMONOHI;
- char_height = 14;
- break;
-
- case 0x0e:
- driver = EGA;
- mode = EGALO;
- char_height = 8;
- break;
-
- case 0x0d:
- /* Not supported by Turbo C */
- return(FALSE);
-
- case 0x06:
- driver = CGA;
- mode = CGAHI;
- char_height = 8;
- break;
-
- case 0x05:
- driver = CGA;
- mode = CGAC0;
- char_height = 8;
- break;
-
- default:
- return(FALSE);
- }
-
- initgraph(&driver, &mode, BGI_PATH);
-
- if (driver < 0) {
- /* initgraph failed */
- return(driver);
- }
-
- dgraf_Clear();
-
- /* Set clip region to avoid drawing over menu bar */
- setviewport(0, char_height, getmaxx(), getmaxy(), TRUE);
-
- /* set up dgraf data */
-
- dgrafp->shape = MIXED;
- dgrafp->size = MIXED;
- dgrafp->color = MIXED;
-
- dgrafp->hgt = getmaxy();
- dgrafp->wid = getmaxx();
- dgrafp->ncolors = getmaxcolor() + 1;
-
- /* Set up the menu colors */
- if (disp_GetColors() > 2L) {
- dgrafp->reg = 0x34;
- dgrafp->sel = 0x43;
- }
- else {
- dgrafp->reg = 0x10;
- dgrafp->sel = 0x01;
- }
-
- return(0);
- }
-
- void dgraf_Line(opboxp, color)
- opbox *opboxp; /* defined by C-scape */
- int color;
- /*
- Draw a line from
- (opboxp->xmin, opboxp->ymax) to (opboxp->xmax, opboxp->ymax)
- with color color.
-
- Turbo C version.
- */
- {
- setcolor(color);
- line(opboxp->xmin, opboxp->ymin, opboxp->xmax, opboxp->ymax);
- }
-
- void dgraf_Rectangle(opboxp, color)
- opbox *opboxp;
- int color;
- /*
- Draw a rectangle with corners
- (opboxp->xmin, opboxp->ymax) (opboxp->xmax, opboxp->ymax)
- with color color.
-
- Turbo C version.
- */
- {
- setcolor(color);
- rectangle(opboxp->xmin, opboxp->ymin, opboxp->xmax, opboxp->ymax);
- }
-
- void dgraf_Ellipse(opboxp, color)
- opbox *opboxp;
- int color;
- /*
- Draw an ellipse bounded by the rectangle with corners
- (opboxp->xmin, opboxp->ymax) (opboxp->xmax, opboxp->ymax)
- with color color.
-
- Turbo C version.
-
- Note: This really draws circles not ellipses...
- */
- {
- int rad, x, y;
-
- x = opboxp->xmin + ((opboxp->xmax - opboxp->xmin) / 2);
- y = opboxp->ymin + ((opboxp->ymax - opboxp->ymin) / 2);
- rad = (opboxp->xmax - opboxp->xmin) / 2;
-
- setcolor(color);
- circle(x, y, rad);
- }
-
- void dgraf_Clear()
- /*
- Clear the display.
-
- Turbo C version.
- */
- {
- clearviewport();
- }
-
- #endif
- /*---------------------------------------------------------------------------*/
-
-