home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 099 / TGE101.ZIP / TGEDEMO.C < prev    next >
C/C++ Source or Header  |  1993-02-04  |  8KB  |  292 lines

  1. #include <alloc.h>
  2. #include <conio.h>
  3. #include <dir.h>
  4. #include <process.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include "tge.h"
  10.  
  11. #ifndef __TURBOC__
  12. #define random(num)        (rand() % num)
  13. #define randomize()         srand((unsigned)time(NULL))
  14. #endif
  15.  
  16. #define ESC    27
  17.  
  18. void lineDemo(void);
  19. void putPixelDemo(void);
  20. void putImageDemo(void);
  21. void drawRectDemo(void);
  22. void filledRectDemo(void);
  23. void paletteDemo(void);
  24. void cycleColour(int *colour, int *increment);
  25. void forceExtension(char *filename, char *extension);
  26. void signOff(void);
  27.  
  28. unsigned char tgeLogo[] = {
  29.   24,0,9,0,
  30.   0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,
  31.   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  32.   0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,
  33.   0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,
  34.   0,0,1,1,0,0,0,0,1,1,0,0,1,1,1,0,1,1,1,1,1,0,0,0,
  35.   0,0,1,1,0,0,0,0,1,1,0,0,0,1,1,0,1,1,1,1,0,0,0,0,
  36.   0,1,1,0,0,0,0,1,1,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,
  37.   0,1,1,0,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,0,0,
  38.   1,1,0,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,0,0,0
  39. };
  40.  
  41. int count;
  42. int colour;
  43. int maxx, maxy;
  44. int colours;
  45. void far *bground;
  46.  
  47.  
  48. void main(int argc, char *argv[])
  49. {
  50.   if (argc < 2)                /* Parse command line */
  51.   {
  52.     printf("TGEDEMO v1.1  Copyright (c) 1993 by Matthew Hildebrand\n\n"
  53.        "    Usage:  TGEDEMO driver\n\n");
  54.     exit(1);
  55.   }
  56.  
  57.   forceExtension(argv[1], ".DRV");    /* Load the specified driver */
  58.   if (!loadGraphDriver(argv[1]))
  59.   {
  60.     printf("Error loading \"%s\".\n\n", argv[1]);
  61.     exit(1);
  62.   }
  63.  
  64.   maxx = _grSystemDrv->maxx;        /* set up variables */
  65.   maxx++;
  66.   maxy = _grSystemDrv->maxy;
  67.   maxy++;
  68.   colours = _grSystemDrv->colours;
  69.   colours++;
  70.  
  71.   bground = farmalloc(imageSize(0,0,23,8));
  72.  
  73.   if (!initGraphics())            /* initialize graphics mode */
  74.   {
  75.     printf("Unable to initialize graphics hardware.\n\n");
  76.     exit(1);
  77.   }
  78.  
  79.   lineDemo();                /* run the demos */
  80.   putPixelDemo();
  81.   putImageDemo();
  82.   drawRectDemo();
  83.   filledRectDemo();
  84.   paletteDemo();
  85.  
  86.   deInitGraphics();            /* restore text and quit */
  87.   unloadGraphDriver();
  88.   signOff();
  89. }
  90.  
  91. void lineDemo(void)
  92. {
  93.   randomize();
  94.   for (; !kbhit();)
  95.     line(random(maxx), random(maxy), random(maxx), random(maxy), random(colours));
  96.  
  97.   if (getch() == ESC)
  98.   {
  99.     deInitGraphics();
  100.     exit(0);
  101.   }
  102.  
  103.   colour = random(colours);
  104.   for (count=maxx-1; count>-1; count-=2) {
  105.     line(0, 0, maxx-count-1, maxy-1, colour);
  106.     line(maxx-1, maxy-1, count, 0, colour);
  107.   }
  108.   clearGraphics(random(colours));
  109. }
  110.  
  111. void putPixelDemo(void)
  112. {
  113.   randomize();
  114.   for (; !kbhit();)
  115.   {
  116.     for (count=0; count<2000; count++)
  117.       putPixel(random(maxx), random(maxy), random(colours));
  118.   }
  119.  
  120.   if (getch() == ESC)
  121.   {
  122.     deInitGraphics();
  123.     exit(0);
  124.   }
  125. }
  126.  
  127. void putImageDemo(void)
  128. {
  129.   int x, y;
  130.  
  131.   x = maxx / 2;
  132.   y = maxy / 2;
  133.  
  134.   randomize();
  135.   while (!kbhit())
  136.   {
  137.     getImage(x, y, x+23, y+8, bground);
  138.     putImage(x, y, tgeLogo);
  139.     putImage(x, y, bground);
  140.  
  141.     if (random(201) >= 100)
  142.       x = x<maxx-1 ? x+1 : x;
  143.     else
  144.       x = x ? x-1 : x;
  145.  
  146.     if (random(201) >= 100)
  147.       y = y<maxy-1 ? y+1 : y;
  148.     else
  149.       y = y ? y-1 : y;
  150.   }
  151.  
  152.   if (getch() == ESC)
  153.   {
  154.     deInitGraphics();
  155.     exit(0);
  156.   }
  157.  
  158.   clearGraphics(random(colours));
  159. }
  160.  
  161. void drawRectDemo(void)
  162. {
  163.   randomize();
  164.   for (; !kbhit();)
  165.     drawRect(random(maxx), random(maxy), random(maxx), random(maxy), random(colours));
  166.  
  167.   if (getch() == ESC)
  168.   {
  169.     deInitGraphics();
  170.     exit(0);
  171.   }
  172.  
  173.   clearGraphics(random(colours));
  174. }
  175.  
  176. void filledRectDemo(void)
  177. {
  178.   randomize();
  179.   for (; !kbhit();)
  180.     filledRect(random(maxx), random(maxy), random(maxx), random(maxy), random(colours));
  181.  
  182.   if (getch() == ESC)
  183.   {
  184.     deInitGraphics();
  185.     exit(0);
  186.   }
  187.  
  188.   clearGraphics(0);
  189. }
  190.  
  191.  
  192. #define MAXINC    7
  193.  
  194. void paletteDemo(void)
  195. {
  196.   int redInc, greenInc, blueInc;
  197.   int red=0, green=0, blue=0;
  198.   colour = 1;
  199.  
  200.   redInc = random(MAXINC);
  201.   redInc++;
  202.   greenInc = random(MAXINC);
  203.   greenInc++;
  204.   blueInc = random(MAXINC);
  205.   blueInc++;
  206.  
  207.   randomize();
  208.   for (count=maxx-1; count>-1; count-=2) {
  209.     line(0, 0, maxx-count-1, maxy-1, colour);
  210.     line(maxx-1, maxy-1, count, 0, colour);
  211.     cycleColour(&red, &redInc);
  212.     cycleColour(&green, &greenInc);
  213.     cycleColour(&blue, &blueInc);
  214.     setPaletteReg(0, red, green, blue);
  215.     if (kbhit())
  216.       break;
  217.   }
  218.  
  219.   if (kbhit())
  220.     getch();
  221. }
  222.  
  223. void cycleColour(int *colour, int *increment)
  224. {
  225.   *colour += *increment;
  226.  
  227.   if (*colour < 0)
  228.   {
  229.     *colour = 0;
  230.     *increment = random(MAXINC) + 1;
  231.   }
  232.   else if (*colour > 255)
  233.   {
  234.     *colour = 255;
  235.     *increment *= -1;
  236.   }
  237. }
  238.  
  239. void forceExtension(char *filename, char *extention)
  240. {
  241.   char fileDrive[MAXDRIVE];
  242.   char fileDir[MAXDIR];
  243.   char fileFile[MAXFILE];
  244.   char fileExt[MAXEXT];
  245.  
  246.   fnsplit(filename, fileDrive, fileDir, fileFile, fileExt);
  247.  
  248.   if (strcmpi(fileExt, extention))
  249.     fnmerge(filename, fileDrive, fileDir, fileFile, extention);
  250. }
  251.  
  252. void signOff(void)
  253. {
  254. #ifndef __POWERC
  255.   static char notice[] = {
  256.     "┌──────────────────────────────────────────────────────────────────────────────┐"
  257.     "│ The Graphics Engine v1.01 Demo  Copyright (c) 1993 by Matthew Hildebrand     │"
  258.     "╞══════════════════════════════════════════════════════════════════════════════╡"
  259.     "│ Although this demo is not particularly gripping, the power of The Graphics   │"
  260.     "│ Engine can be seen when it is run with different drivers:  the demo uses the │"
  261.     "│ same code to access different graphics modes.                                │"
  262.     "│                                                                              │"
  263.     "│ Even if you don't want device-independence, but only one graphics mode, The  │"
  264.     "│ Graphics Engine's optimized assembler routines may be used.                  │"
  265.     "│                                                                              │"
  266.     "│ The Graphics Engine costs a mere $20; paid users are entitled to use any or  │"
  267.     "│ all of the code, without royalties.  Refer to TGE.DOC for details.           │"
  268.     "╞══════════════════════════════════════════════════════════════════════════════╡"
  269.     "│ Matthew Hildebrand, 4 College St., St. Catharines, Ontario, Canada, L2R 2W7  │"
  270.     "└──────────────────────────────────────────────────────────────────────────────┘"
  271.     "\n"
  272.   };
  273.  
  274.   printf(notice);
  275. #else
  276.   printf("┌──────────────────────────────────────────────────────────────────────────────┐");
  277.   printf("│ The Graphics Engine v1.01 Demo  Copyright (c) 1993 by Matthew Hildebrand     │");
  278.   printf("╞══════════════════════════════════════════════════════════════════════════════╡");
  279.   printf("│ Although this demo is not particularly gripping, the power of The Graphics   │");
  280.   printf("│ Engine can be seen when it is run with different drivers:  the demo uses the │");
  281.   printf("│ same code to access different graphics modes.                                │");
  282.   printf("│                                                                              │");
  283.   printf("│ Even if you don't want device-independence, but only one graphics mode, The  │");
  284.   printf("│ Graphics Engine's optimized assembler routines may be used.                  │");
  285.   printf("│                                                                              │");
  286.   printf("│ The Graphics Engine costs a mere $20; paid users are entitled to use any or  │");
  287.   printf("│ all of the code, without royalties.  Refer to TGE.DOC for details.           │");
  288.   printf("╞══════════════════════════════════════════════════════════════════════════════╡");
  289.   printf("│ Matthew Hildebrand, 4 College St., St. Catharines, Ontario, Canada, L2R 2W7  │");
  290.   printf("└──────────────────────────────────────────────────────────────────────────────┘\n");
  291. #endif
  292. }