⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 examp001.c

📁 supplementary source file 1 for The BESTLibrary
💻 C
📖 第 1 页 / 共 2 页
字号:
/*==========================================================================
 *
 *  EXAMP001.C                                    Sunday, September 11, 1994
 *
 *  Sample source code for The BESTLibrary v2.32.  Demonstrates the use of
 *    the various graphics functions.
 *
 *  Intended to give examples of:
 *    _16_c_need_scrn
 *    _16_c_need_wrst
 *    _16_c_save
 *    _16_c_show
 *    _16_copy
 *    _16_i_need
 *    _16_i_save
 *    _16_i_show
 *    _16_p_need
 *    _16_p_save
 *    _16_p_show
 *
 *  Authored independently by George Vanous
 *  Email any comments, compliments, or suggestions to vanous@helix.net
 *
 *==========================================================================*/


/* NOTE  if, when you compile, you receive a linker error that states there
         are two undefined symbols "EGAVGA_driver_far" and "small_font_far",
         you need to perform the following steps:

      1) go into your BGI subdirectory (where EGAVGA.BGI is located)
      2) type BGIOBJ /F EGAVGA
      3) type BGIOBJ /F LITT
      4) copy the two .OBJs created (EGAVGAF.OBJ and LITTF.OBJ)into the
         same subdirectory as all your libraries (usually LIB\)
      5) type TLIB GRAPHICS.LIB +EGAVGAF +LITTF

         this updates your GRAPHICS.LIB file to include the EGAVGA.BGI file
         and the LITT.CHR font (you can do this for all your .BGI and .CHR
         files)
*/

/* ------------------------------------------------------------------------ */
/* ----------------------------  INCLUDE FILES  --------------------------- */

#include <alloc.h>
#include <stdlib.h>
#include <graphics.h>
#include "!bestlib.h"                  /* include !BESTLIB.H in compilation */

/* ------------------------------------------------------------------------ */
/* ------------------------------  CONSTANTS  ----------------------------- */

#define BGCLR BLUE                              /* background color         */
#define TEXTHEIGHT 12                           /* y-height of printed text */
#define BUFFERSPACE 100                         /* byte size of "buffer"    */
#define DASHES printf("\n--------------------------------------------------------------------------------")
#define PRINT(x, y, text) _16_boxfill(x, y+2, textwidth(text), TEXTHEIGHT-2,\
 BLACK, COPY_IMAGE); outtextxy(x, y, text);     /* prints in graphics mode  */
#define ERR01 "\nI require 400,000 bytes (400kb) of free memory\nBut there is only %ld bytes currently available\n\nDo you want me to continue regardless [y/N] ? "
#define MSG01 "\n\nI suggest you try removing any unnecessary TSRs (Terminate but Stay Resident\nprograms) to increase the amount of available memory.\n\n"

/* ------------------------------------------------------------------------ */
/* -------------------------  FUNCTION PROTOTYPES  ------------------------ */

void  animate_images(void);                     /* animate images           */
void  begin_sequence(void);                     /* beginning sequence       */
char *color_translate(int color);   /* convert color from integer to string */
         /* restore the background under the text pointed to by "locations" */
void  erase_text(int locations[][3], int number);
void  exit_sequence(void);                      /* exitting sequence        */
void  restore_background(void);                 /* restore the background   */
void  print_image_properties(int x, int y, imagedata *image, char *image_name,
char *image_command, int locations[][3]);       /* print image properties   */
void  save_background(void);                    /* draw and save background */
void  save_images(void);                        /* draw and save images     */
void  show_images(void);                        /* show images onscreen     */
char *t_or_f(int boolean);      /* converts a number into "TRUE" or "FALSE" */

/* ------------------------------------------------------------------------ */
/* -------------------------  GLOBAL DEFINITIONS  ------------------------- */

/* The following three definition are required by TheBESTLibrary to be
   present, even if they are not all used.  If they are not present, the
   compiler will produce a "linker error". */
asciiscan key;                         /* global structure "key"            */
cursordata cursor;                     /* global structure "cursor"         */
mousedata msdata;                      /* global structure "msdata"         */

int oldmode;                           /* old video mode                    */
byte oldcurx, oldcury;                 /* old cursor coordinates            */
char *buffer;                          /* general-purpose text buffer space */
char message[81];    /* 80 bytes + 1 byte for the NULL-terminating
                        character is sufficient to hold the longest message */

imagedata *bgscrn[4],                  /* storage area for the background   */
          *image_c[4],                 /* define four compress images       */
          *image_i[2],                 /* define two plane images           */
          *image_p[2];                 /* define two pixel images           */

/* ------------------------------------------------------------------------ */


/*----------------------------------------------------------------------------
 * MAIN SUBROUTINE OF EXAMPLE SOURCE CODE 001
 */
void main(void)
{
  begin_sequence();                          /* perform the begin sequence  */
  save_background();                         /* draw and save background    */
  save_images();                             /* draw and save the images    */
  show_images();                             /* show the images onscreen    */

  exit(0);  /* executes the procedure "exit_sequence()" because of the line */
            /* "atexit(exit_sequence)" in procedure "begin_sequence"        */
}

/*----------------------------------------------------------------------------
 * Animate the images.
 */
void animate_images(void)
{
/* restore the background under the right three images on the first row     */
   _16_restore_bg(image_c[1]->x,      image_c[1]->y,
                  image_c[1]->length, image_c[1]->height, bgscrn);
   _16_restore_bg(image_c[2]->x,      image_c[2]->y,
                  image_c[2]->length, image_c[2]->height, bgscrn);
   _16_restore_bg(image_c[3]->x,      image_c[3]->y,
                  image_c[3]->length, image_c[3]->height, bgscrn);

/* now animate the image on the first row that is left                      */

/* restore the background under the right image on the second row           */
   _16_restore_bg(image_p[1]->x,      image_p[1]->y,
                  image_p[1]->length, image_p[1]->height, bgscrn);

/* now animate the image on the second row that is left                     */

/* restore the background under the right image on the third row            */
   _16_restore_bg(image_i[1]->x,      image_i[1]->y,
                  image_i[1]->length, image_i[1]->height, bgscrn);

/* now animate the image on the third row that is left                      */

}

/*----------------------------------------------------------------------------
 * Beginning sequence.
 */
void begin_sequence(void)
{
  int gdrv = VGA, gmod = VGAHI, error; /* help initialize the graphics mode */
  char ch;                             /* generic character holder          */
  byte i, j;                           /* cursor position holders           */

  /* allocate space for a generic string buffer */
  buffer = (char *) malloc(BUFFERSPACE);

  /* check amount of available memory */
  DASHES;                              /* print one line of dashes          */
  if (coreleft() < 400000L) {
    sprintf(buffer, ERR01, coreleft());
    fprintf(stderr, buffer);           /* print low memory warning message  */
    cur_get_coord_abs(&i, &j);         /* get the cursor position           */
    while ((ch = case_up(getchre(i, j))) != 'Y' && ch != 'N' && ch != 13) {
      beep();                          /* signal invalid keypress           */
      txt_chr_erase(i, j, 1);          /* erase character                   */
    }
    if (ch != 'Y') {                   /* if 'Y' chosen                     */
      fprintf(stderr, MSG01);          /*   print suggestion message        */
      exit(1);                         /*   exit to DOS with ERRORLEVEL 1   */
    }                                  /* else continue with program        */
  }

  fade_out();                          /* fade current text screen to black */

  /* initialize The BEST Library and save old video mode
   * set 640x480x16 graphics mode (VGA16) and initialize the mouse (TRUE)   */
  oldmode = bestlib_init(VGA16, TRUE);

  /* store that status of the insert, caps, num, and scroll lock */
  kbd_status_save();

  atexit(exit_sequence);               /* define the exit procedure         */

  /* allocate all necessary memory */
  bgscrn[0] = (imagedata *) malloc(_16_i_need(640, 204));
  bgscrn[1] = (imagedata *) malloc(_16_i_need(640, 204));
  bgscrn[2] = (imagedata *) malloc(_16_i_need(640, 204));
  bgscrn[3] = (imagedata *) malloc(_16_i_need(640, 204));
  image_c[0] = (imagedata *) malloc(_16_c_need_wrst(50, 50));
  image_c[1] = (imagedata *) malloc(_16_c_need_wrst(50, 50));
  image_p[0] = (imagedata *) malloc(_16_p_need(50, 50));
  image_p[1] = (imagedata *) malloc(_16_p_need(50, 50));
  image_i[0] = (imagedata *) malloc(_16_i_need(50, 50));
  if ((image_i[1] = malloc(_16_i_need(50, 50))) == NULL) {
     exit_sequence();                  /* perform the exit sequence         */
     fprintf(stderr, "\nInsufficient free memory to continue\n");
     exit(1);  /* insufficient free memory -- exit to DOS with ERRORLEVEL 1 */
  }    /* only one "if" is necessary because if there is enough memory for
        * the last allocation, there would have been enough for all of the
        * previous allocations                                              */

       /* we did check to see that there was at least 400,000 bytes of
          memory available, so the above memory check is unnecessary; it
          is present for the purpose of example                             */

  /* setup graphics mode */
  if ((error = registerfarbgidriver(EGAVGA_driver_far)) < 0 ||
      (error = registerfarbgifont(small_font_far)) < 0) {
    fprintf(stderr, "\nGraphics error: %s\n", grapherrormsg(error));
    exit(2);               /* error registering a BGI driver -- exit to DOS */
  }
  initgraph(&gdrv, &gmod, "");
  if ((error = graphresult()) != grOk) {
    fprintf(stderr, "\nGraphics error: %s\n", grapherrormsg(error));
    exit(3);            /* error occurred changing to 640x480x16 video mode */
  }

  /* initialize mouse to 640x480x16 graphics mode
   * "mousepresent" = TRUE if mouse is detected, else "mousepresent" = FALSE
  ms_init(VGA16);
  ms_show();                           /* show mouse cursor                 */
   */

  _16_floodallall(BGCLR);              /* flood entire video memory         */
  settextstyle(SMALL_FONT, HORIZ_DIR, 4);   /* set default output text font */
  settextjustify(LEFT_TEXT, TOP_TEXT); /* define how text will be written   */
}

/*----------------------------------------------------------------------------
 * Return the string representation of an integer-represented color.
 *
 * "color" - integer representation of a color.
 *
 * RETURNS:
 * = name of the color
 * = NULL if unknown color

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -