home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / gfx / x11 / Mesa_Amiwin.lha / Mesa-Amiwin / samples / copy.c < prev    next >
C/C++ Source or Header  |  1995-11-17  |  4KB  |  198 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include "gltk.h"
  33.  
  34.  
  35. GLenum doubleBuffer, directRender;
  36. GLint windW, windH;
  37.  
  38. char *fileName = 0;
  39. TK_RGBImageRec *image;
  40. float point[3];
  41. float zoom;
  42. GLint x, y;
  43.  
  44.  
  45. static void Init(void)
  46. {
  47.  
  48.     glClearColor(0.0, 0.0, 0.0, 0.0);
  49.  
  50.     x = 0;
  51.     y = windH;
  52.     zoom = 1.8;
  53. }
  54.  
  55. static void Reshape(int width, int height)
  56. {
  57.  
  58.     windW = (GLint)width;
  59.     windH = (GLint)height;
  60.  
  61.     glViewport(0, 0, windW, windH);
  62.  
  63.     glMatrixMode(GL_PROJECTION);
  64.     glLoadIdentity();
  65.     gluOrtho2D(0, windW, 0, windH);
  66.     glMatrixMode(GL_MODELVIEW);
  67. }
  68.  
  69. static GLenum Key(int key, GLenum mask)
  70. {
  71.  
  72.     switch (key) {
  73.       case TK_ESCAPE:
  74.         tkQuit();
  75.       case TK_Z:
  76.     zoom += 0.2;
  77.     break;
  78.       case TK_z:
  79.     zoom -= 0.2;
  80.     if (zoom < 0.2) {
  81.         zoom = 0.2;
  82.     }
  83.     break;
  84.       default:
  85.     return GL_FALSE;
  86.     }
  87.     return GL_TRUE;
  88. }
  89.  
  90. static GLenum Mouse(int mouseX, int mouseY, GLenum button)
  91. {
  92.  
  93.     x = (GLint)mouseX;
  94.     y = (GLint)mouseY;
  95.     return GL_TRUE;
  96. }
  97.  
  98. static void Draw(void)
  99. {
  100.  
  101.     glClear(GL_COLOR_BUFFER_BIT);
  102.  
  103.     point[0] = (windW / 2) - (image->sizeX / 2);
  104.     point[1] = (windH / 2) - (image->sizeY / 2);
  105.     point[2] = 0;
  106.     glRasterPos3fv(point);
  107.  
  108.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  109.     glPixelZoom(1.0, 1.0);
  110.     glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  111.          image->data);
  112.  
  113.     point[0] = (float)x;
  114.     point[1] = windH - (float)y;
  115.     point[2] = 0.0;
  116.     glRasterPos3fv(point);
  117.  
  118.     glPixelZoom(zoom, zoom);
  119.     glCopyPixels((windW/2)-(image->sizeX/2),
  120.          (windH/2)-(image->sizeY/2),
  121.          image->sizeX, image->sizeY, GL_COLOR);
  122.  
  123.     glFlush();
  124.  
  125.     if (doubleBuffer) {
  126.     tkSwapBuffers();
  127.     }
  128. }
  129.  
  130. static GLenum Args(int argc, char **argv)
  131. {
  132.     GLint i;
  133.  
  134.     doubleBuffer = GL_FALSE;
  135.     directRender = GL_TRUE;
  136.  
  137.     for (i = 1; i < argc; i++) {
  138.     if (strcmp(argv[i], "-sb") == 0) {
  139.         doubleBuffer = GL_FALSE;
  140.     } else if (strcmp(argv[i], "-db") == 0) {
  141.         doubleBuffer = GL_TRUE;
  142.     } else if (strcmp(argv[i], "-dr") == 0) {
  143.         directRender = GL_TRUE;
  144.     } else if (strcmp(argv[i], "-ir") == 0) {
  145.         directRender = GL_FALSE;
  146.     } else if (strcmp(argv[i], "-f") == 0) {
  147.         if (i+1 >= argc || argv[i+1][0] == '-') {
  148.         printf("-f (No file name).\n");
  149.         return GL_FALSE;
  150.         } else {
  151.         fileName = argv[++i];
  152.         }
  153.     } else {
  154.         printf("%s (Bad option).\n", argv[i]);
  155.         return GL_FALSE;
  156.     }
  157.     }
  158.     return GL_TRUE;
  159. }
  160.  
  161. void main(int argc, char **argv)
  162. {
  163.     GLenum type;
  164.  
  165.     if (Args(argc, argv) == GL_FALSE) {
  166.     tkQuit();
  167.     }
  168.  
  169.     if (fileName == 0) {
  170.     printf("No image file.\n");
  171.     tkQuit();
  172.     }
  173.  
  174.     image = tkRGBImageLoad(fileName);
  175.  
  176.     windW = 300;
  177.     windH = 300;
  178.     tkInitPosition(0, 0, windW, windH);
  179.  
  180.     type = TK_RGB;
  181.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  182.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  183.     tkInitDisplayMode(type);
  184.  
  185.     if (tkInitWindow("Copy Test") == GL_FALSE) {
  186.     tkQuit();
  187.     }
  188.  
  189.     Init();
  190.  
  191.     tkExposeFunc(Reshape);
  192.     tkReshapeFunc(Reshape);
  193.     tkKeyDownFunc(Key);
  194.     tkMouseDownFunc(Mouse);
  195.     tkDisplayFunc(Draw);
  196.     tkExec();
  197. }
  198.