home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / demos / testgl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  5.1 KB  |  224 lines

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <vga.h>
  5. #include <vgagl.h>
  6.  
  7.  
  8. /* The logo was drawn by John Remyn. */
  9. /* Feel free to provide a more beautiful/official/thought provoking/cool */
  10. /* logo to replace it. */
  11. #define LOGOWIDTH 201
  12. #define LOGOHEIGHT 85
  13.  
  14. int VGAMODE;
  15. int VIRTUAL;
  16.  
  17. GraphicsContext *backscreen;
  18. GraphicsContext *physicalscreen;
  19. void *logobitmap;
  20.  
  21.  
  22. void loadbitmap( char *filename, void *buf ) {
  23.     FILE *f;
  24.     void *bufp;
  25.     f = fopen(filename, "rb");
  26.     bufp = buf;
  27.     for (;;) {
  28.         int count = fread(bufp, 1, 8192, f);
  29.         if (count == 0)
  30.             break;
  31.         if (feof(f))
  32.             break;
  33.         bufp += count;
  34.     }
  35.     fclose(f);
  36. }
  37.  
  38.  
  39. void test() {
  40.     int i, j;
  41.     unsigned char *bitmap;
  42.     GraphicsContext *savedcontext;
  43.     
  44.     if (VIRTUAL)
  45.         gl_setcontext(backscreen);
  46.  
  47.     gl_clearscreen(0);
  48.     for (i = 0; i < 5; i++) {
  49.         gl_clearscreen(0);
  50.         for (j = 0; j < 20000; j++)
  51.             gl_setpixel(random() % WIDTH, random() % HEIGHT,
  52.                 random() % COLORS);
  53.     }
  54.  
  55.     if (VIRTUAL)
  56.         gl_copyscreen(physicalscreen);
  57.  
  58.     gl_clearscreen(0);
  59.     for (i = 0; i < 5000; i++) {
  60.         int x, y;
  61.         x = random() % (WIDTH - 1);
  62.         y = random() % (HEIGHT - 1);
  63.         gl_fillbox(x, y, random() % (WIDTH - x), random()
  64.             % (HEIGHT - y),    random() % COLORS);
  65.     }
  66.  
  67.     if (VIRTUAL)
  68.         gl_copyscreen(physicalscreen);
  69.  
  70.     gl_clearscreen(0);
  71.     for (i = 0; i < 4000; i++)
  72.         gl_line(random() % WIDTH, random() % HEIGHT,
  73.             random() % WIDTH, random() % HEIGHT,
  74.             random() % COLORS);
  75.  
  76.     if (VIRTUAL)
  77.         gl_copyscreen(physicalscreen);
  78.  
  79.     /* Create bitmap. */
  80.     bitmap = malloc(64 * 64 * BYTESPERPIXEL);
  81.     /* Create temporary graphics context to create bitmap in. */
  82.     savedcontext = gl_allocatecontext();
  83.     gl_getcontext(savedcontext);
  84.     gl_setcontextvirtual(64, 64, BYTESPERPIXEL, BITSPERPIXEL, bitmap);
  85.     /* The rgb functions can be used to create nice bitmaps easily for */
  86.     /* hicolor/truecolor modes. The 256 color 'emulated' truecolor */
  87.     /* looks less impressive. */
  88.     for (i = 0; i < 32; i++)
  89.         for (j = 0; j < 32; j++) {
  90.             int c;
  91.             c = 255 - (i + j) * 4;
  92.             gl_setpixelrgb(31 - i, 31 - j, c, 0, 0);
  93.             gl_setpixelrgb(32 + i, 31 - j, c, c, 0);
  94.             gl_setpixelrgb(31 - i, 32 + j, c, 0, c);
  95.             gl_setpixelrgb(32 + i, 32 + j, c, c, c);
  96.         }
  97.     /* Restore previous context. */
  98.     gl_setcontext(savedcontext);
  99.  
  100.     gl_clearscreen(0);
  101.     for (i = 0; i < 4000; i++) {
  102.         int x, y;
  103.         x = random() % (WIDTH - 64 + 1);
  104.         y = random() % (HEIGHT - 64 + 1);
  105.         gl_putbox(x, y, 64, 64, bitmap);
  106.     }
  107.  
  108.     free(bitmap);
  109.  
  110.     if (VIRTUAL)
  111.         gl_copyscreen(physicalscreen);
  112. }
  113.  
  114.  
  115. void setcustompalette() {
  116.     /* colors 0-31 are an RGB mix (bits 0 and 1 red, 2 green, 3 and 4 blue) */
  117.     /* 32-63    black to red */
  118.     /* 64-95    black to green */
  119.     /* 96-127   black to yellow */
  120.     /* 128-159  black to blue */
  121.     /* 160-191  black to magenta */
  122.     /* 192-223  black to cyan */
  123.     /* 224-255  black to white */
  124.     Palette pal;
  125.     int i;
  126.     for (i = 0; i < 256; i++) {
  127.         int r, g, b;
  128.         r = g = b = 0;
  129.         if ((i & 32) > 0) r = (i & 31) << 1;
  130.         if ((i & 64) > 0) g = (i & 31) << 1;
  131.         if ((i & 128) > 0) b = (i & 31) << 1;
  132.         if (i < 32) {
  133.             r = (i & 3) << 4;   /* 2 bits */
  134.             g = (i & 4) << 3;   /* 1 bit */
  135.             b = (i & 24) << 1;  /* 2 bits */
  136.         }
  137.         pal.color[i].red = r;
  138.         pal.color[i].green = g;
  139.         pal.color[i].blue = b;
  140.     }
  141.     gl_setpalette(&pal);
  142. }
  143.  
  144.  
  145. void logotest() {
  146.     int h, w;
  147.     void *scaled;
  148.     /* Set logo palette. */
  149.     setcustompalette();
  150.     /* Create logo bitmap */
  151.     logobitmap = alloca(LOGOWIDTH * LOGOHEIGHT);
  152.     loadbitmap("linuxlogo.bmp", logobitmap);
  153.     /* Allocate buffer for scaled bitmap. */
  154.     scaled = alloca(WIDTH * HEIGHT);
  155.     gl_clearscreen(0);
  156.     /* Stretch vertically. */
  157.     for (h = 0; h <= LOGOHEIGHT; h++) {
  158.         gl_scalebox(LOGOWIDTH, LOGOHEIGHT, logobitmap,
  159.             LOGOWIDTH, h, scaled);
  160.         gl_putbox(0, 0, LOGOWIDTH, h, scaled);
  161.         if (VIRTUAL)
  162.             gl_copyscreen(physicalscreen);
  163.     }
  164.     gl_clearscreen(0);
  165.     /* Scale to screen resolution. */
  166.     gl_scalebox(LOGOWIDTH, LOGOHEIGHT, logobitmap, WIDTH, HEIGHT, scaled);
  167.     gl_putbox(0, 0, WIDTH, HEIGHT, scaled);
  168.     gl_copyscreen(physicalscreen);
  169. }
  170.  
  171.  
  172. void main() {
  173.     vga_init();
  174.     VGAMODE = vga_getdefaultmode();
  175.     if (VGAMODE == -1)
  176.         VGAMODE = G320x200x256;        /* Default mode. */
  177.  
  178.     if (!vga_hasmode(VGAMODE)) {
  179.         printf("Mode not available.\n");
  180.         exit(-1);
  181.     }
  182.  
  183.     VIRTUAL = 0;    /* No virtual screen. */
  184.     if (vga_getmodeinfo(VGAMODE)->colors == 16 ||
  185.     (vga_getmodeinfo(VGAMODE)->flags & IS_MODEX))
  186.         /* These modes are supported indirectly by vgagl. */
  187.         VIRTUAL = 1;
  188.  
  189.     if (VIRTUAL) {
  190.         /* Create virtual screen. */
  191.         gl_setcontextvgavirtual(VGAMODE);
  192.         backscreen = gl_allocatecontext();
  193.         gl_getcontext(backscreen);
  194.     }
  195.  
  196.     vga_setmode(VGAMODE);
  197.     gl_setcontextvga(VGAMODE);    /* Physical screen context. */
  198.     physicalscreen = gl_allocatecontext();
  199.     gl_getcontext(physicalscreen);
  200.     if (COLORS == 256)
  201.         gl_setrgbpalette();
  202.  
  203.     test();
  204.  
  205.     /* Now do the same with clipping enabled. */
  206.     gl_clearscreen(0);
  207.     gl_setclippingwindow(WIDTH / 4, HEIGHT / 4, WIDTH - WIDTH / 4 - 1,
  208.         HEIGHT - HEIGHT / 4 - 1);
  209.  
  210.     test();
  211.  
  212.     gl_disableclipping();
  213.     if (COLORS == 256)
  214.         /* Show the logo if using 256 color mode. */
  215.         logotest();
  216.  
  217.     getchar();
  218.  
  219.     if (VIRTUAL)
  220.         gl_freecontext(backscreen);
  221.     vga_setmode(TEXT);
  222.     exit(0);
  223. }
  224.