home *** CD-ROM | disk | FTP | other *** search
- *** NOTE the names and structure of the routines used in these examples have
- changed in The Best Library 2.00; the updated examples will be
- released soon
-
- EXAMP001.EXE - Prints a simple, common menu in text mode and waits for user
- input. Only the DOS shell and Exit options perform actual
- tasks. The other two just print messages signifying their
- uselessness.
- EXAMP002.EXE - Prints a common menu in VGA graphics mode and waits for user
- input. The File-Load menu option enables other menu options,
- as it would in a real program. The File-New menu option
- disables the menu options enabled with File-Load, as it would
- in a real program.
- EXAMP003.EXE - The user is prompted for a string to animate around the screen
- and the millisecond delay between each movement of the string.
- If a mouse is detected, the left mouse button positions the
- text and the right button asks for a new delay and string to
- animate.
- EXAMP004.EXE - A little game where the object is to dodge the ever-oncoming
- rocks. The cursor pad can be used for movement, with or
- without NUM LOCK active, or the right hand side of the keyboard
- QWEASDZXC. Each key corresponds respectively to the keypad's
- 789456123. Again, the right hand keys work with or without
- CAPS LOCK active.
- EXAMP005.EXE - A demonstration program showing off the mouse routines and
- text output routines. Continually displays the status of the
- mouse cursor's coordinates and button states while filling the
- blank areas of the screen with progressively larger ASCII
- characters and then progressively smaller, in a circuit.
- EXAMP006.EXE - An animation demonstration, utilizing all four animation
- procedures. The first animation sequence will look strange, as
- it was my first attempt at an animation procedure. The second
- is specifically designed for perfectly square objects; anything
- not perfectly square will have its edges displayed (as in the
- animation). The third sequence animates any object perfectly
- with NO distortion. The disadvantage is the slowdown. The
- fourth animation moves the object one graphic row at a time, so
- it is much quicker, but there is a slight flicker.
-
- ==============================================================================
-
- EXAMP001.EXE - source code listing
-
- #include <stdio.h>
- #include "colors.h"
- #include "!bestlib.h"
-
- /*** NOTE even though this program only uses the "keyp" structure, the
- other three are necessary for the assembler routines in the
- !BESTLIB.LIB library to function properly. C would also
- produce a "linker warning" if you have enabled that warning ***/
- filldata fidata; /* create a "filldata" structure */
- printdata prdata; /* create a "printdata" structure */
- mousedata msdata; /* create a "mousedata" structure */
- asciiscan keyp; /* create an "asciiscan" structure */
-
- void main(void)
- {
- int oldmode;
- char *errmsg;
-
- oldmode = readvideomode(); /* save the current video mode */
- textm(0); /* make sure we are in text mode */
- textmem(3); /* store text video memory */
- cursor(3, 1); /* hide and store cursor position */
- clear(LIGHTGRAY, BLACK); /* clear the screen and set colors */
-
- do {
- printsatxy("Select an option:", 5, 3);
- printsatxy("1) Load", 8, 5);
- printsatxy("2) Save", 8, 6);
- printsatxy("3) Shell to DOS", 8, 7);
- printsatxy("4) Exit program", 8, 8); /* print small sample menu */
-
- getchr(); /* wait for a user response */
- switch(keyp.ascii) {
- case '1': /* load was selected */
- beep(); /* beep the speaker */
- printsatxy("The Load option has not yet been written", 5, 24);
- break; /* exit from switch statement */
- case '2':
- beep(); /* beep the speaker */
- printsatxy("The Save option has not yet been written", 5, 24);
- break; /* exit from switch statement */
- case '3': /* shell to DOS was selected */
- cursor(1, 1); /* show and restore cursor position */
- if ((errmsg = dosshell()) != NULL)
- printsatxy(errmsg, 5, 24); /* print the returned error message */
- else { /* else if no error occurred.. */
- cursor(3, 0); /* hide and store ignore position */
- clear(LIGHTGRAY, BLACK); /* clear the screen and set colors */
- printsatxy("Welcome back!", 5, 24);
- }
- break; /* exit from switch statement */
- case '4': /* exit program was selected */
- changevideomode(oldmode); /* restore the original video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(1); /* restore text video memory */
- cursor(1, 1); /* show and restore cursor position */
- }
- break; /* exit from switch statement */
- default: /* the user pressed an invalid key */
- printsatxy("That is not a valid choice ", 5, 24);
- beep(); /* beep the speaker to denote error */
- }
- } while (keyp.ascii != '4'); /* loop until exit is chosen */
- }
-
- ------------------------------------------------------------------------------
-
- EXAMP002.EXE source code listing:
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <graphics.h>
- #include "colors.h"
- #include "!bestlib.h"
-
- #define FILE 1
- #define SORT 2
- #define PRINT 3
- #define SCREEN 4
- #define DOS 5
-
- #define NEW 1
- #define LOAD 2
- #define SAVE 3
- #define FONTSIZE 1
- #define BLOCK 1
- #define ENTIREDOCUMENT 2
- #define EXTERNALFILE 3
- #define EXITTO 1
- #define SHELLTO 2
-
- #define BGCOLOR DARKGRAY /* background color */
- #define TEXTCOLOR WHITE /* color of text messages */
- #define ABORTCOLOR RED /* color of abort messages */
- #define ERASECOMMANDTEXT boxfill(0, txty, MAXX, txtht, BGCOLOR, COPY_IMAGE);
-
- /*** NOTE even though this program only uses the "keyp" structure, the
- other three are necessary for the assembler routines in the
- !BESTLIB.LIB library to function properly. C would also
- produce a "linker warning" if you have enabled that warning ***/
- filldata fidata; /* create a "filldata" structure */
- printdata prdata; /* create a "printdata" structure */
- mousedata msdata; /* create a "mousedata" structure */
- asciiscan keyp; /* create an "asciiscan" structure */
-
- char *menu1ptr[] = { "FILE", "SORT", "PRINT", "SCREEN", "DOS" }; /* menus */
- char *menu1text[] = { /* submenus */
- (char *)0x001D,
- "NEW", "LOAD", "SAVE",
- "BLOCK", "ENTIRE DOCUMENT", "EXTERNAL FILE",
- "FONT SIZE",
- "EXIT TO", "SHELL TO"
- };
- int menu1data[][3] = { { 3, 1, 0x0007 },
- { 0, 0, 0x0000 },
- { 3, 4, 0x0004 },
- { 1, 7, 0x0001 },
- { 2, 8, 0x0003 }
- };
-
- void main(void)
- {
- int gr_err, gdrv = VGA, gmod = VGAHI, fontsize = 5,
- oldmode, mouse = FALSE, command = FALSE, submenu,
- txtht = 15, txty = 465, menu, menux = MAXX / 3, menuy = 220;
- char *errmsg;
-
- oldmode = readvideomode(); /* save the current video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(3); /* store text video memory */
- cursor(3, 1); /* hide and store cursor position */
- }
- mouse = initms_gr(); /* initialize mouse driver if installed */
- initgraph(&gdrv, &gmod, ""); /* initialize graphics mode */
- if ((gr_err = graphresult()) != grOk) {
- /* if an error occurred in changing to graphics mode, print the error */
- printf("Graphics error: %s\n", grapherrormsg(gr_err));
- exit(1); /* exit to DOS returning errorlevel 1 */
- }
- settextjustify(LEFT_TEXT, TOP_TEXT);
- settextstyle(SMALL_FONT, HORIZ_DIR, fontsize); /* setup font size/style */
- floodall(BGCOLOR); /* create the screen background */
- do {
- command = popupmenu(menux, menuy, 1, 5,
- menu1ptr, menu1data, menu1text, mouse);
- if (command == -1) break; /* if unable to allocate memory, exit */
- submenu = command & 0x000F, menu = command >> 4;
- setcolor(TEXTCOLOR); /* set the appropriate text color */
- switch(menu) {
- case FILE: /* a selection from the file menu */
- switch(submenu) {
- case NEW: /* the new submenu was selected */
- menu1text[0] = (char *)0x001D; /* restore menu restrictions */
- menu1data[2][2] = 0x0004; /* disable submenu restrictions */
- ERASECOMMANDTEXT; /* erase any previous messages */
- outtextxy(0, txty, "The NEW submenu was selected -- some options \
- disabled");
- break;
- case LOAD: /* the load submenu was selected */
- menu1text[0] = (char *)0x001F; /* enable menu restrictions */
- menu1data[2][2] = 0x0007; /* enable submenu restrictions */
- ERASECOMMANDTEXT; /* erase any previous messages */
- outtextxy(0, txty, "The LOAD submenu was selected -- some options\
- enabled");
- break;
- case SAVE: ; /* the save submenu was selected */
- ERASECOMMANDTEXT; /* erase any previous messages */
- outtextxy(0, txty, "The SAVE submenu was selected");
- break;
- }
- break; /* exit from switch statement */
- case SORT: /* the sort menu was selected */
- ERASECOMMANDTEXT; /* erase any previous messages */
- outtextxy(0, txty, "The SORT menu was selected");
- break; /* exit from switch statement */
- case PRINT: /* a selection from the print menu */
- switch(submenu) {
- case BLOCK: /* the block submenu was selected */
- ERASECOMMANDTEXT; /* erase any previous messages */
- outtextxy(0, txty, "The BLOCK submenu was selected");
- break;
- case ENTIREDOCUMENT: /* the entiredocument was selected */
- ERASECOMMANDTEXT; /* erase any previous messages */
- outtextxy(0, txty, "The ENTIRE DOCUMENT submenu was selected");
- break;
- case EXTERNALFILE: ; /* the externalfile was selected */
- ERASECOMMANDTEXT; /* erase any previous messages */
- outtextxy(0, txty, "The EXTERNAL FILE submenu was selected");
- break;
- }
- break; /* exit from switch statement */
- case SCREEN: /* a selection from the fonts menu */
- switch(submenu) {
- case FONTSIZE: /* the select new font submenu was selected */
- textm(0); /* make sure we are in text mode */
- clear(LIGHTGRAY, BLACK); /* clear the screen and set colors */
- printsatxy("What font size would you like to use (1-7)?", 10, 12);
- readnumber(7, 1, 54, 12, &fontsize); /* ask user for font size */
- initgraph(&gdrv, &gmod, ""); /* re-initialize graphics mode */
- settextstyle(SMALL_FONT, HORIZ_DIR, fontsize); /* font size */
- txtht = textheight("A") + 3; /* initialize text height */
- txty = MAXY - txtht; /* initialize text y-coord */
- menux = MAXX - (textwidth(menu1ptr[3])+17 +
- textwidth(menu1text[4])+33 + textwidth(menu1text[5])+33 +
- textwidth(menu1text[6])+33); /* calculate text x-coord */
- if (menux > MAXX / 3) menux = MAXX / 3;
- floodall(BGCOLOR); /* create the screen background */
- }
- break; /* exit from switch statement */
- case DOS: /* a selection from the DOS menu */
- switch(submenu) {
- case EXITTO: break; /* the exit to submenu was selected */
- case SHELLTO: /* the shell to submenu was selected */
- if ((errmsg = dosshell()) != NULL) {
- ERASECOMMANDTEXT; /* erase any previous messages */
- while (textwidth(errmsg) > MAXX) /* entire message must fit */
- settextstyle(SMALL_FONT, HORIZ_DIR, --fontsize);
- outtextxy(0, txty, errmsg); /* print the returned error msg */
- }
- else { /* else if no error occurred.. */
- highstandardvga(); /* get back into 640x480x16 mode VGA */
- settextjustify(LEFT_TEXT, TOP_TEXT);
- settextstyle(SMALL_FONT, HORIZ_DIR, fontsize);
- floodall(BGCOLOR); /* restore the screen background */
- outtextxy(0, txty, "Welcome back!");
- }
- break;
- }
- break;
- case FALSE: /* no selection was registered */
- setcolor(ABORTCOLOR); /* set the appropriate abort color */
- ERASECOMMANDTEXT; /* erase any previous messages */
- outtextxy(0, txty, "Operation aborted");
- setcolor(TEXTCOLOR); /* restore text message color */
- break; /* exit from switch statement */
- }
- } while (menu != DOS || submenu != EXITTO); /* loop until exit selected */
-
- changevideomode(oldmode); /* restore the original video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(1); /* restore text video memory */
- cursor(1, 1); /* show and restore cursor position */
- }
- }
-
- ------------------------------------------------------------------------------
-
- EXAMP003.EXE source code listing:
-
- #include <stdlib.h>
- #include "colors.h"
- #include "!bestlib.h"
-
- /*** NOTE even though this program only uses the "keyp" and "prdata"
- structures, the other two are necessary for the assembler
- routines in the !BESTLIB.LIB library to function properly. C
- would also produce a "linker warning" if you have enabled that
- warning ***/
- filldata fidata; /* create a "filldata" structure */
- printdata prdata = { WHITE, BLUE, -1, 1, 1, "Press any key to exit" };
- /* see "printdata" under GLOBAL VARIABLES for explanation of the values */
- mousedata msdata; /* create a "mousedata" structure */
- asciiscan keyp; /* create an "asciiscan" structure */
-
- void main(void)
- {
- int oldmode, deltax = 1, deltay = 1, mouse, delaytime, z;
- char input[80]; /* enough space for a 79-letter string and NULL */
-
- oldmode = readvideomode(); /* save the current video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(3); /* store text video memory */
- cursor(3, 1); /* hide and store cursor position */
- }
- textm(1); /* change to 80x25x16 color text mode */
- clear(LIGHTGRAY, BLACK); /* clear the screen and set colors */
- printsatxy("What text would you like to be animated? (79 letters maximum)",
- 1, 10);
- z = readstring(input, 79, 0, 11); /* ask user for the text to animate */
- if (z == -1) prdata.string2print = "Demonstration";
- else prdata.string2print = input; /* set up the text for printing */
- printsatxy("Enter movement delay in milliseconds (1-1000): [30]", 1, 13);
- z = readnumber(1000, 1, 53, 13, &delaytime); /* ask user for delay time */
- if (z == FALSE) delaytime = 30; /* if <ENTER> or <ESC>, use default */
- clear(WHITE, BLUE); /* clear the screen and set colors */
- mouse = initms(); /* initialize mouse if installed */
- msec(1); /* initialize CPU-independent delay */
-
- if (mouse == TRUE) /* if the mouse was initialized.. */
- xyminmaxms(9, 4, 70, 20); /* restrict its movement (for show) */
- while (!keyhit()) { /* loop until a key is pressed */
- print(); /* print user-inputted text */
- if (mouse == TRUE) printsatxy("The mouse is just for show", 27, 0);
- printsatxy("Press any key to exit", 29, 24);
- if (mouse == TRUE) showms(); /* restore mouse cursor onto screen */
- msec(delaytime); /* wait 10 milliseconds */
- if (mouse == TRUE) hidems(); /* hide mouse before a screen update */
- if (prdata.x >= 80 - stringlenpr() || prdata.x <= 0)
- /* if the x-coordinate is at an edge */
- deltax = -deltax; /* reverse the x-increment (negate) */
- if (prdata.y >= 24 || prdata.y <= 0) /* if y-coordinate is at an edge */
- deltay = -deltay; /* reverse the y-increment (negate) */
- stringerasepr(); /* erase user inputted text */
- prdata.x += deltax,
- prdata.y += deltay; /* increment the x and y coordinates */
- }
-
- changevideomode(oldmode); /* restore the original video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(1); /* restore text video memory */
- cursor(1, 1); /* show and restore cursor position */
- }
- }
-
- ------------------------------------------------------------------------------
-
- EXAMP004.EXE source code listing:
-
- #include <dos.h>
- #include <stdlib.h>
- #include "colors.h"
- #include "!bestlib.h"
-
- #define ROCK 219 /* ASCII code for the oncoming rocks */
- #define FACEMAN 1 /* ASCII code for the main character */
- #define MAXROCKS 200 /* the maximum number of rocks */
- #define MAXROCKDELAY 6 /* the maximum delay in clock ticks */
- #define SCREENX 80 /* number of characters on x axis */
- #define SCREENY 50 /* number of characters on y axis */
-
- void moverock(void); /* procedure definition to move rock */
- void movefaceman(void); /* procedure definition to move main character */
-
- /*** NOTE even though this program only uses the "keyp" structure, the
- other three are necessary for the assembler routines in the
- !BESTLIB.LIB library to function properly. C would also
- produce a "linker warning" if you have enabled that warning ***/
- filldata fidata; /* create a "filldata" structure */
- printdata prdata; /* create a "printdata" structure */
- mousedata msdata; /* create a "mousedata" structure */
- asciiscan keyp; /* create an "asciiscan" structure */
-
- struct { /* the rock data structure */
- int x; /* the current x-coordinate */
- int y; /* the current y-coordinate */
- int oldx; /* the old x-coordinate */
- int oldy; /* the old y-coordinate */
- int dir; /* the direction */
- } rock[MAXROCKS]; /* create an array for all the rocks */
-
- struct { /* the main character data structure */
- int x; /* the current x-coordinate */
- int y; /* the current y-coordinate */
- int oldx; /* the old x-coordinate */
- int oldy; /* the old y-coordinate */
- int dir; /* the direction */
- } faceman; /* only one main character */
-
- void main(void)
- {
- int oldmode;
- register int i; /* register class speeds up loops */
-
- for (i = 0; i < MAXROCKS; i++) { /* initialize rock structures */
- stopw(i, random(MAXROCKDELAY)); /* initialize stop watch */
- rock[i].x = rock[i].oldx = 0; /* initialize x-coordinate */
- rock[i].y = rock[i].oldy = random(SCREENY); /* random y-coordinate */
- rock[i].dir = 3; /* initialize default direction */
- }
- faceman.dir = 0; /* initialize main character's direction */
- faceman.x = faceman.oldx = 40; /* initialize main character's y-coord */
- faceman.y = faceman.oldy = 13; /* initialize main character's x-coord */
-
- oldmode = readvideomode(); /* save the current video mode */
- textm(2); /* change to 50 line text mode */
- textmem(3); /* store text video memory */
- cursor(3, 1); /* hide and store cursor position */
- clear(LIGHTGREEN, BLUE); /* clear the screen and set colors */
- printcatxy(FACEMAN, faceman.x, faceman.y); /* draw the main character */
-
- do {
- movefaceman(); /* move the main character */
- moverock(); /* move one rock at a time */
- printsatxy("Press <ESC> to exit", 30, 24);
- } while (keyp.ascii != 27); /* loop until <ESC> key is pressed */
-
- changevideomode(oldmode); /* restore the original video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(1); /* restore text video memory */
- cursor(1, 1); /* show and restore cursor position */
- }
- }
-
- /*
- * MOVE ONE ROCK AT A TIME AND RANDOMLY SET THE NEW DELAY BEFORE THE NEXT MOVE
- */
- void moverock(void)
- {
- static int i = 0; /* this variable is never lost */
-
- if (stopw(i, 0) > 0) { /* if it is time to move this rock.. */
- if (++rock[i].x >= SCREENX) { /* if the rock is at the boundary.. */
- rock[i].x = 0; /* reset its x-coordinate */
- rock[i].y = random(SCREENY); /* randomly reset its y-coordinate */
- }
- printcatxy(32, rock[i].oldx, rock[i].oldy); /* erase the old rock */
- printcatxy(ROCK, rock[i].x, rock[i].y); /* draw the new rock */
- rock[i].oldx = rock[i].x; /* modify the old x-coordinate of the rock */
- rock[i].oldy = rock[i].y; /* modify the old y-coordinate of the rock */
- stopw(i, random(MAXROCKDELAY)); /* new random delay for the next move */
- }
- if (++i >= MAXROCKS) i = 0; /* if all rocks have moved, restart from #1 */
- }
-
- /*
- * MOVES THE MAIN CHARACTER ACCORDING TO KEYPRESS, REGARDLESS OF NUM LOCK
- *
- * 1 5 6 5 1 6
- * \|/ \|/ \|/
- * DIRECTIONS: 2--+--3 --+-- 2--+--3
- * /|\ /|\ /|\
- * 4 7 8 7 4 8
- */
- void movefaceman(void)
- {
- register int i;
-
- for (i = 0; i < MAXROCKS; i++) { /* check for a collision with a rock */
- if (rock[i].x == faceman.x && rock[i].y == faceman.y) {
- sound(120); msec(400); /* create a 120 Hz sound for 400 ms */
- sound(40); msec(850); /* create a 40 Hz sound for 850 ms */
- nosound(); /* silence the speaker */
- }
- }
- if (keyhit() == FALSE) return; /* if not keypress ready, return */
- getchr(); /* read the keypress into the "keyp" structure */
- kbclear(); /* clear all other character from the keyboard buffer */
- keyp.ascii = dncase(keyp.ascii); /* convert to lowercase if UPPERCASE */
- if (!keyp.ascii) /* if there is no ASCII code.. */
- keyp.ascii = keyp.scan; /* read the scan code */
- switch(keyp.ascii) {
- case 56 : /* direction 1, NUM LOCK active */
- case 72 : /* direction 1, NUM LOCK inactive */
- case 119: faceman.y--; break; /* direction 1, right letter-pad */
- case 52 : /* direction 2, NUM LOCK active */
- case 75 : /* direction 2, NUM LOCK inactive */
- case 97 : faceman.x--; break; /* direction 2, right letter-pad */
- case 54 : /* direction 3, NUM LOCK active */
- case 77 : /* direction 3, NUM LOCK inactive */
- case 100: faceman.x++; break; /* direction 3, right letter-pad */
- case 50 : /* direction 4, NUM LOCK active */
- case 80 : /* direction 4, NUM LOCK inactive */
- case 120: faceman.y++; break; /* direction 4, right letter-pad */
- case 55 : /* direction 5, NUM LOCK active */
- case 71 : /* direction 5, NUM LOCK inactive */
- case 113: faceman.x--, faceman.y--; break;
- /* direction 5, right letter-pad */
- case 57 : /* direction 6, NUM LOCK active */
- case 73 : /* direction 6, NUM LOCK inactive */
- case 101: faceman.x++, faceman.y--; break;
- /* direction 6, right letter-pad */
- case 49 : /* direction 7, NUM LOCK active */
- case 79 : /* direction 7, NUM LOCK inactive */
- case 122: faceman.x--, faceman.y++; break;
- /* direction 7, right letter-pad */
- case 51 : /* direction 8, NUM LOCK active */
- case 81 : /* direction 8, NUM LOCK inactive */
- case 99 : faceman.x++, faceman.y++; /* direction 8, right letter-pad */
- }
- if (faceman.x < 0) faceman.x = 0; /* movement beyond the screen edge.. */
- if (faceman.y < 0) faceman.y = 0; /* not allowed */
- if (faceman.y > SCREENY - 1) faceman.y = SCREENY - 1;
- if (faceman.x > SCREENX - 1) faceman.x = SCREENX - 1;
- printcatxy(32, faceman.oldx, faceman.oldy); /* erase the main character */
- printcatxy(FACEMAN, faceman.x, faceman.y); /* print the main character */
- faceman.oldx = faceman.x; /* modify old x-coord of the main character */
- faceman.oldy = faceman.y; /* modify old y-coord of the main character */
- }
-
- ------------------------------------------------------------------------------
-
- EXAMP005.EXE - source code listing
-
- #include <stdio.h>
- #include <stdlib.h>
- #include "colors.h"
- #include "!bestlib.h"
-
- #define ERR01 0
- #define MSG01 1
- #define MSG02 2
- #define MSG03 3
- #define MSG04 4
- #define MSG05 5
- #define MSG06 6
- #define MSG07 7
- #define MSG08 8
- #define MSG09 9
- #define MSG0A 10
- #define MSG0B 11
- #define MSG0C 12
- #define LOC1 7
- #define LOC2 11
- #define LOC3 5
- #define LOC4 0
- #define LOC5 24
- #define QUE01 13
- #define QUE02 14
- #define QUE03 15
- #define TXT01 16
-
- void floodtext(void);
- void showtext(void);
-
- filldata fidata = { GREEN, BLACK, 0, 0, 0, 80, 25, 32, 0, "" };
- printdata prdata = { -1, -1, -1, 0, 0, "" };
- mousedata msdata;
- asciiscan keyp;
-
- char buffer[70];
- char *text[] = {
- "!!! ERROR OCCURRED INITIALIZING THE MOUSE DRIVER !!!", /* ERR01 */
- "Demonstration of Microsoft compatible mouse routines", /* MSG01 */
- "Authored independently by George Vanous", /* MSG02 */
- "left button is not pressed", /* MSG03 */
- "left button is pressed ", /* MSG04 */
- "left button was pressed at", /* MSG05 */
- "left button was released at", /* MSG06 */
- "right button is not pressed", /* MSG07 */
- "right button is pressed ", /* MSG08 */
- "right button was pressed at", /* MSG09 */
- "right button was released at", /* MSG0A */
- " cursor is located at", /* MSG0B */
- "press any key to terminate", /* MSG0C */
- "Do you wish to continue regardless?", /* QUE01 */
- "Which ASCII character to use as cursor? [219]", /* QUE02 */
- "What color? [7]", /* QUE03 */
- "ASCII table", /* TXT01 */
- };
-
- void main(void)
- {
- int chart, color, oldmode, z;
-
- if (!initms()) { /* if the mouse could not be found */
- cursorwhere(&prdata.x); /* stores the position of the cursor */
- prdata.string2print = text[ERR01];
- print(); /* print the error message */
- prdata.string2print = text[QUE01];
- prdata.y++;
- print(); /* print the question */
- if (upcase(getchre()) != 'Y') /* if the user response was not Yes */
- exit(1); /* exit to DOS with errorlevel 1 */
- }
- oldmode = readvideomode(); /* save the current video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- textmem(3); /* if old mode was a text mode, store text video memory */
- cursor(3, 1); /* hide and store cursor position */
- }
- textm(1); /* change to 80x25x16 color text mode */
- clear(LIGHTGRAY, BLACK); /* clear the screen and set colors */
-
- prdata.x = 5, prdata.y = 11; /* set the next print position */
- prdata.string2print = text[QUE02];
- print(); /* print the question */
- z = readnumber(255, 1, prdata.x + stringlen(prdata.string2print)+1,
- prdata.y, &chart); /* get the user's input */
- if (z == FALSE) chart = 219; /* if <ENTER> or <ESC>, use default */
- prdata.y += 2; /* set the next print position */
- prdata.string2print = text[QUE03];
- print(); /* print the question */
- z = readnumber(15, 0, prdata.x + stringlen(prdata.string2print)+1,
- prdata.y, &color); /* get the user's input */
- if (z == FALSE) color = 7; /* if <ENTER> or <ESC>, use default */
-
- clear(LIGHTGRAY, BLACK); /* clear the screen and set colors */
- showtext(); /* print up the screen */
- prdata.y = LOC1; /* define the y-coordinate to print */
- prdata.command = -1; /* specify no modifications */
- prdata.string2print = text[MSG03];
- print(); /* print left mouse button status */
- prdata.y = LOC2; /* define the y-coordinate to print */
- prdata.string2print = text[MSG07];
- print(); /* print right mouse button status */
- shapems(chart, color, -1); /* change the mouse cursor and color */
- showms(); /* display the mouse cursor */
- stopw(0, 15); /* start stopwatch 0 */
-
- while (!keyhit()) {
- floodtext(); /* fill the entire screen */
- statms(); /* read the mouse status */
- if (msdata.pos[0] != msdata.npos[0]) {
- msdata.pos[0] = msdata.npos[0];
- msdata.update += 1; /* if the mouse x-position changed.. */
- }
- if (msdata.pos[1] != msdata.npos[1]) {
- msdata.pos[1] = msdata.npos[1];
- msdata.update += 2; /* if the mouse y-position changed.. */
- }
- if (msdata.buts[0] != msdata.nbuts[0]) {
- msdata.buts[0] = ~msdata.buts[0];
- msdata.update += 4; /* if the left mouse button state changed.. */
- }
- if (msdata.buts[1] != msdata.nbuts[1]) {
- msdata.buts[1] = ~msdata.buts[1];
- msdata.update += 8; /* if the right mouse button state changed.. */
- }
- if (msdata.update) { /* if mouse registered activity.. */
- msdata.update = 0; /* reset the mouse activity flag */
- prdata.fgcolor = YELLOW; /* set the foreground text color */
- prdata.command = 0; /* specify text to be centered */
- prdata.x = 0, prdata.y = LOC3; /* define the location to print */
- sprintf((prdata.string2print = buffer), "%s %d, %d ",
- text[MSG0B], msdata.pos[0], msdata.pos[1]);
- hidems(); /* hide mouse cursor, updating screen */
- print(); /* print cursor location */
- prdata.command = -1; /* specify no modifications */
- prdata.y = LOC1; /* define y-coordinate of next print */
- if (!msdata.buts[0]) { /* if left button is not pressed.. */
- prdata.string2print = text[MSG03];
- print(); /* print left button is not pressed */
- sprintf((prdata.string2print = buffer), "%s %d, %d ",
- text[MSG06], msdata.butlr[0], msdata.butlr[1]);
- prdata.y++; /* increment y-coordinate of print */
- }
- else { /* else left button is pressed.. */
- prdata.string2print = text[MSG04];
- print(); /* print left button is pressed */
- sprintf((prdata.string2print = buffer), "%s %d, %d ",
- text[MSG05], msdata.butlp[0], msdata.butlp[1]);
- prdata.y += 2; /* add to y-coordinate of print */
- }
- print(); /* print left button coordinates */
- prdata.y = LOC2; /* define new y-coordinate for print */
- if (!msdata.buts[1]) { /* if right button is not pressed.. */
- prdata.string2print = text[MSG07];
- print(); /* print right button is not pressed */
- sprintf((prdata.string2print = buffer), "%s %d, %d ",
- text[MSG0A], msdata.butrr[0], msdata.butrr[1]);
- prdata.y++; /* increment y-coordinate of print */
- }
- else { /* else right button is pressed.. */
- prdata.string2print = text[MSG08];
- print(); /* print right button is pressed */
- sprintf((prdata.string2print = buffer), "%s %d, %d ",
- text[MSG09], msdata.butrp[0], msdata.butrp[1]);
- prdata.y += 2; /* add to y-coordinate of print */
- }
- print(); /* print right button coordinates */
- showms(); /* show mouse cursor, done updating */
- }
- }
- changevideomode(oldmode); /* restore the original video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(1); /* restore text video memory */
- cursor(1, 1); /* show and restore cursor position */
- }
- }
-
- /*
- * 32 250 249 46 7 176 177 178 219
- * · ∙ . ░ ▒ ▓ █
- */
- void floodtext(void)
- #define FLOODCHARNO 8
- {
- static int cycle = 1, dir = 1,
- data[] = { 32, 250, 249, 46, 7, 176, 177, 178, 219 };
-
- if (stopw(0, 0) == TRUE) {
- fidata.fillchar = data[cycle]; /* set the character to fill with */
- fidata.char2overwrite = data[cycle - dir]; /* set char to overwrite */
- if (cycle == FLOODCHARNO || (cycle == 1 && dir < 0))
- dir = -dir; /* start cycling the fill characters in reverse */
- cycle += dir; /* get the next fill character */
- hidems(); fillarea(); showms(); /* fill screen with the character */
- msdata.update++; /* reprint the mouse status onscreen */
- showtext(); /* reprint the standard text */
- stopw(0, 15); /* reset stopwatch 0 */
- }
- }
-
- /*
- * PRINTS INITIAL TEXT
- */
- void showtext(void)
- {
- prdata.fgcolor = WHITE, prdata.bgcolor = BLUE; /* set printing colors */
- prdata.command = 0; /* center the text to print */
- prdata.x = 0, prdata.y = LOC4; /* define the coordinates to print */
- prdata.string2print = text[MSG01];
- print(); /* print first line of title */
- prdata.y++; /* increment the y-coordinate */
- prdata.string2print = text[MSG02];
- print(); /* print second line of title */
- prdata.y = LOC5; /* define the y-coordinate to print */
- prdata.string2print = text[MSG0C];
- print(); /* print the "any key exits" message */
- }
-
- ------------------------------------------------------------------------------
-
- EXAMP006.EXE - source code listing
-
- #include <alloc.h>
- #include <stdlib.h>
- #include <graphics.h>
- #include "colors.h"
- #include "!bestlib.h"
-
- #define BGCOLOR BLACK /* background color */
-
- /*** NOTE even though this program only uses the "keyp" and "msdata"
- structures, the other two are necessary for the assembler
- routines in the !BESTLIB.LIB library to function properly. C
- would also produce a "linker warning" if you have enabled that
- warning ***/
- filldata fidata; /* create a "filldata" structure */
- printdata prdata; /* create a "printdata" structure */
- mousedata msdata; /* create a "mousedata" structure */
- asciiscan keyp; /* create an "asciiscan" structure */
-
- void main(void)
- {
- typedef struct {
- int x;
- int y;
- int maxx;
- int maxy;
- } graphicobject;
-
- graphicobject *graphic;
- int oldmode, mouse, gdrv = VGA, gmod = VGAHI;
- register int i, ii;
- void *bgscrn[3], *image_or[1], *image[3];
-
- if (coreleft() < 280000L) { /* if we do not have enough memory.. */
- textm(0); /* make sure we are in a text mode */
- printsatcur("Insufficient memory to run EXAMP006.EXE");
- exit(1); /* exit to DOS with errorlevel 1 */
- }
- if (isitega() == FALSE) {
- textm(0); /* make sure we are in a text mode */
- printsatcur("I do not detect an EGA/VGA card, are you sure you have \
- one (Y/N)?"); /* a break is necessary or it wouldn't fit on one line */
- getchr();
- if (upcase(keyp.ascii) != 'Y') /* if the user did not press 'Y'.. */
- exit(2); /* exit to DOS with errorlevel 2 */
- }
- if ((oldmode = readvideomode()) == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(3); /* store text video memory */
- cursor(3, 1); /* hide and store cursor position */
- }
- initgraph(&gdrv, &gmod, ""); /* initialize graphics mode */
- settextjustify(LEFT_TEXT, TOP_TEXT);
- settextstyle(SMALL_FONT, HORIZ_DIR, 4); /* setup font size/style */
-
- mouse = initms_gr(); /* initialize mouse driver if installed */
- floodall(BGCOLOR); /* create the screen background */
-
- for (i = 0; i < 5; i++) /* create the screen border */
- boxoutline(i, i, MAXX - i*2, MAXY - i*2, YELLOW, COPY_IMAGE);
-
- /* create the screen background */
- for (i = BLACK, ii = 5; i <= WHITE; i++, ii += 21)
- boxfill(ii, 5, 21, MAXY - 10, i, COPY_IMAGE);
- for (i = YELLOW; i > BLACK; i--, ii += 21)
- boxfill(ii, 5, 21, MAXY - 10, i, COPY_IMAGE);
-
- /* print messages showing user each animation routine in action */
- boxfill(5, 36, 50, 12, BLACK, COPY_IMAGE);
- outtextxy(5, 36, "animate()");
- boxfill(5, 136, 57, 12, BLACK, COPY_IMAGE);
- outtextxy(5, 136, "animate1()");
- boxfill(5, 236, 78, 12, BLACK, COPY_IMAGE);
- outtextxy(5, 236, "animatepixel()");
- boxfill(5, 336, 85, 12, BLACK, COPY_IMAGE);
- outtextxy(5, 336, "animatebestss()");
-
- /* store the background into memory */
- bgscrn[0] = (void *) malloc(imagememreq(640, 204));
- bgscrn[1] = (void *) malloc(imagememreq(640, 204));
- bgscrn[2] = (void *) malloc(imagememreq(640, 72));
- storeimage(0, 0, 640, 204, bgscrn[0]);
- storeimage(0, 204, 640, 204, bgscrn[1]);
- storeimage(0, 408, 640, 72, bgscrn[2]);
-
- /* allocate memory for graphicobject structure */
- graphic = (graphicobject *) malloc(sizeof(graphicobject));
-
- /* draw the image to animate outside the visible video RAM */
- /* because it is outside the visible range, mouse cursor can stay onscreen */
- #define CLR1 LIGHTBLUE
- #define CLR2 YELLOW /* draw the true-color image */
- boxfill(0, MAXY, 32, 32, BGCOLOR, COPY_IMAGE);
- boxfill(13, MAXY, 7, 32, CLR1, COPY_IMAGE);
- boxfill(0, MAXY+13, 32, 7, CLR1, COPY_IMAGE);
- linex(16, MAXY, 31, MAXY+15, CLR2);
- linex(31, MAXY+15, 15, MAXY+31, CLR2);
- linex(15, MAXY+31, 0, MAXY+16, CLR2);
- linex(0, MAXY+16, 16, MAXY, CLR2);
- boxfill(12, MAXY+12, 9, 9, BLACK, COPY_IMAGE);
-
- image[0] = (void *) malloc(imagememreq(32, 32)); /* for storeimage */
- image_or[0] = (void *) malloc(imagememreq(32, 32)); /* for storeimage */
- image[1] = (void *) malloc(pixelimagememreq(32, 32));
- /* for storepixelimage */
- image[2] = (void *) malloc(pixelrcompressimagememreq(0, MAXY, 32, 32));
- /* for storepixelcompressimage */
- storeimage(0, MAXY, 32, 32, image[0]); /* save image */
- storepixelimage(0, MAXY, 32, 32, BGCOLOR, image[1]); /* save image */
- storepixelcompressimage(0, MAXY, 32, 32, BGCOLOR, image[2]); /* sv image */
-
- /* draw the graphics to animate outside the visible screen area */
- #undef CLR1
- #undef CLR2
- #define CLR1 WHITE
- #define CLR2 WHITE /* draw the pure-white image */
- boxfill(0, MAXY, 32, 32, BGCOLOR, COPY_IMAGE);
- boxfill(13, MAXY, 7, 32, CLR1, COPY_IMAGE);
- boxfill(0, MAXY+13, 32, 7, CLR1, COPY_IMAGE);
- linex(16, MAXY, 31, MAXY+15, CLR2);
- linex(31, MAXY+15, 15, MAXY+31, CLR2);
- linex(15, MAXY+31, 0, MAXY+16, CLR2);
- linex(0, MAXY+16, 16, MAXY, CLR2);
- boxfill(12, MAXY+12, 9, 9, BLACK, COPY_IMAGE);
- storeimage(0, MAXY, 32, 32, image_or[0]); /* save the pure-white image */
-
- /* display all the graphics -- ready to animate */
- writeimage(6, 50, COPY_IMAGE, image[0]);
- writeimage(6, 150, COPY_IMAGE, image[0]);
- writepixelimage(6, 250, image[1]);
- writepixelcompressimage(6, 350, image[2]);
- if (mouse == TRUE) showms(); /* if mouse detected, show ms cursor */
- getchr(); /* wait for a keypress before beginning animation sequences */
- graphic->maxx = graphic->maxy = 32; /* initialize the graphic dimensions */
- graphic->x = 6, graphic->y = 50; /* initialize graphic coordinates */
- if (mouse == TRUE) hidems(); /* if mouse detected, hide ms cursor */
-
- for (i = 6; i < MAXX - 37; graphic->x = i, i += 2) {
- if (keyhit()) { /* if the user hit a key.. */
- getchr(); /* read the key */
- if (keyp.ascii == 27) /* if it was the <ESC> key.. */
- exit(0); /* exit to DOS with errorlevel 0 */
- break; /* else abort this animation sequence */
- }
- animate((int *)graphic, i, 50, 0, image_or, image, bgscrn);
- }
-
- graphic->x = 6, graphic->y = 150;
- for (i = 6; i < MAXX - 37; graphic->x = i, i += 2) {
- if (keyhit()) { /* if the user hit a key.. */
- getchr(); /* read the key */
- if (keyp.ascii == 27) /* if it was the <ESC> key.. */
- exit(0); /* exit to DOS with errorlevel 0 */
- break; /* else abort this animation sequence */
- }
- animate1((int *)graphic, i, 150, 0, image, bgscrn);
- }
-
- graphic->x = 6, graphic->y = 250;
- for (i = 6; i < MAXX - 37; graphic->x = i, i += 2) {
- if (keyhit()) { /* if the user hit a key.. */
- getchr(); /* read the key */
- if (keyp.ascii == 27) /* if it was the <ESC> key.. */
- exit(0); /* exit to DOS with errorlevel 0 */
- break; /* else abort this animation sequence */
- }
- animatepixel((int *)graphic, i, 250, 1, image, bgscrn);
- }
-
- graphic->x = 6;
- for (i = 6; i < MAXX - 37; graphic->x = i, i += 2) {
- if (keyhit()) { /* if the user hit a key.. */
- getchr(); /* read the key */
- if (keyp.ascii == 27) /* if it was the <ESC> key.. */
- exit(0); /* exit to DOS with errorlevel 0 */
- break; /* else abort this animation sequence */
- }
- animatebestss(graphic->x, 350, i, 350, image[2], image[2], bgscrn);
- }
-
- free(image_or[0]); /* free all allocated memory */
- for (i = 0; i < 3; i++) free(image[i]);
- if (mouse == TRUE) showms(); /* if mouse detected, show ms cursor */
- getchr(); /* wait for keypress before exitting */
- if (mouse == TRUE) hidems(); /* if mouse detected, hide ms cursor */
- changevideomode(oldmode); /* restore the original video mode */
- if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
- /* if the old mode was a text mode.. */
- textmem(1); /* restore text video memory */
- cursor(1, 1); /* show and restore cursor position */
- }
- }
-
- ==============================================================================
-
- END OF SOURCE CODE LISTINGS
-