home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / COPY.C < prev    next >
Text File  |  1995-03-04  |  3KB  |  177 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. //#include <unistd.h>
  8. #include "tk.h"
  9.  
  10.  
  11. GLenum doubleBuffer, directRender;
  12. GLint windW, windH;
  13.  
  14. char *fileName = 0;
  15. TK_RGBImageRec *image;
  16. float point[3];
  17. float zoom;
  18. GLint x, y;
  19.  
  20.  
  21. static void Init(void)
  22. {
  23.  
  24.     glClearColor(0.0, 0.0, 0.0, 0.0);
  25.  
  26.     x = 0;
  27.     y = windH;
  28.     zoom = 1.8;
  29. }
  30.  
  31. static void Reshape(int width, int height)
  32. {
  33.  
  34.     windW = (GLint)width;
  35.     windH = (GLint)height;
  36.  
  37.     glViewport(0, 0, windW, windH);
  38.  
  39.     glMatrixMode(GL_PROJECTION);
  40.     glLoadIdentity();
  41.     gluOrtho2D(0.,(float) windW, 0., (float)windH);
  42.     glMatrixMode(GL_MODELVIEW);
  43. }
  44.  
  45. static GLenum Key(int key, GLenum mask)
  46. {
  47.  
  48.     switch (key) {
  49.       case TK_B:
  50.     tkClipBoard();
  51.     break;
  52.       case TK_ESCAPE:
  53.         tkQuit();
  54.       case TK_Z:
  55.     zoom += 0.2;
  56.     break;
  57.       case TK_z:
  58.     zoom -= 0.2;
  59.     if (zoom < 0.2) {
  60.         zoom = 0.2;
  61.     }
  62.     break;
  63.       default:
  64.     return GL_FALSE;
  65.     }
  66.     return GL_TRUE;
  67. }
  68.  
  69. static GLenum Mouse(int mouseX, int mouseY, GLenum button)
  70. {
  71.  
  72.     x = (GLint)mouseX;
  73.     y = (GLint)mouseY;
  74.     return GL_TRUE;
  75. }
  76.  
  77. static void Draw(void)
  78. {
  79.  
  80.     glClear(GL_COLOR_BUFFER_BIT);
  81.  
  82.     point[0] = (windW / 2) - (image->sizeX / 2);
  83.     point[1] = (windH / 2) - (image->sizeY / 2);
  84.     point[2] = 0;
  85.     glRasterPos3fv(point);
  86.  
  87.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  88.     glPixelZoom(1.0, 1.0);
  89.     glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  90.          image->data);
  91.  
  92.     point[0] = (float)x;
  93.     point[1] = windH - (float)y;
  94.     point[2] = 0.0;
  95.     glRasterPos3fv(point);
  96.  
  97.     glPixelZoom(zoom, zoom);
  98.     glCopyPixels((windW/2)-(image->sizeX/2),
  99.          (windH/2)-(image->sizeY/2),
  100.          image->sizeX, image->sizeY, GL_COLOR);
  101.  
  102.     glFlush();
  103.  
  104.     if (doubleBuffer) {
  105.     tkSwapBuffers();
  106.     }
  107. }
  108.  
  109. static GLenum Args(int argc, char **argv)
  110. {
  111.     GLint i;
  112.  
  113.     doubleBuffer = GL_FALSE;
  114.     directRender = GL_TRUE;
  115.  
  116.     for (i = 1; i < argc; i++) {
  117.     if (strcmp(argv[i], "-sb") == 0) {
  118.         doubleBuffer = GL_FALSE;
  119.     } else if (strcmp(argv[i], "-db") == 0) {
  120.         doubleBuffer = GL_TRUE;
  121.     } else if (strcmp(argv[i], "-dr") == 0) {
  122.         directRender = GL_TRUE;
  123.     } else if (strcmp(argv[i], "-ir") == 0) {
  124.         directRender = GL_FALSE;
  125.     } else if (strcmp(argv[i], "-f") == 0) {
  126.         if (i+1 >= argc || argv[i+1][0] == '-') {
  127.         printf("-f (No file name).\n");
  128.         return GL_FALSE;
  129.         } else {
  130.         fileName = argv[++i];
  131.         }
  132.     } else {
  133.         printf("%s (Bad option).\n", argv[i]);
  134.         return GL_FALSE;
  135.     }
  136.     }
  137.     return GL_TRUE;
  138. }
  139.  
  140. void main(int argc, char **argv)
  141. {
  142.     GLenum type;
  143.  
  144.     if (Args(argc, argv) == GL_FALSE) {
  145.     tkQuit();
  146.     }
  147.  
  148.     if (fileName == 0) {
  149.     printf("No image file.\n");
  150.     tkQuit();
  151.     }
  152.  
  153.     image = tkRGBImageLoad(fileName);
  154.  
  155.     windW = 300;
  156.     windH = 300;
  157.     tkInitPosition(0, 0, windW, windH);
  158.  
  159.     type = TK_RGB;
  160.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  161.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  162.     tkInitDisplayMode(type);
  163.  
  164.     if (tkInitWindow("Copy Test") == GL_FALSE) {
  165.     tkQuit();
  166.     }
  167.  
  168.     Init();
  169.  
  170.     tkExposeFunc(Reshape);
  171.     tkReshapeFunc(Reshape);
  172.     tkKeyDownFunc(Key);
  173.     tkMouseDownFunc(Mouse);
  174.     tkDisplayFunc(Draw);
  175.     tkExec();
  176. }
  177.