home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / samples / cursor.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  4KB  |  199 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 "gltk.h"
  29.  
  30.  
  31. GLenum rgb, doubleBuffer, directRender, windType;
  32. int windX, windY;
  33. int cursor;
  34. GLubyte shape[] = {
  35.     0x00, 0x00,
  36.     0x7F, 0xFE,
  37.     0x40, 0x02,
  38.     0x40, 0x02,
  39.     0x40, 0x02,
  40.     0x40, 0x02,
  41.     0x40, 0x02,
  42.     0x40, 0x02,
  43.     0x40, 0x02,
  44.     0x40, 0x02,
  45.     0x40, 0x02,
  46.     0x40, 0x02,
  47.     0x40, 0x02,
  48.     0x40, 0x02,
  49.     0x7F, 0xFE,
  50.     0x00, 0x00
  51. };
  52. GLubyte mask[] = {
  53.     0xFF, 0xFF,
  54.     0xFF, 0xFF,
  55.     0xFF, 0xFF,
  56.     0xE0, 0x07,
  57.     0xE0, 0x07,
  58.     0xE0, 0x07,
  59.     0xE0, 0x07,
  60.     0xE0, 0x07,
  61.     0xE0, 0x07,
  62.     0xE0, 0x07,
  63.     0xE0, 0x07,
  64.     0xE0, 0x07,
  65.     0xE0, 0x07,
  66.     0xFF, 0xFF,
  67.     0xFF, 0xFF,
  68.     0xFF, 0xFF
  69. };
  70.  
  71.  
  72. static void Init(void)
  73. {
  74.     int i;
  75.  
  76.     for (i = TK_BLACK; i <= TK_WHITE; i++) {
  77.     tkNewCursor(i, shape, mask, i, TK_WHITE, 0, 0);
  78.     }
  79.     for (i = TK_BLACK; i <= TK_WHITE; i++) {
  80.     tkNewCursor(i+TK_WHITE, shape, mask, TK_WHITE, i, 0, 0);
  81.     }
  82.     cursor = TK_BLACK;
  83.     tkSetCursor(cursor);
  84.     glClearColor(0.0, 0.0, 0.0, 0.0);
  85.     glClearIndex(0.0);
  86. }
  87.  
  88. static void Reshape(int width, int height)
  89. {
  90.  
  91.     windX = width;
  92.     windY = height;
  93.     glViewport(0, 0, windX, windY);
  94.  
  95.     glMatrixMode(GL_PROJECTION);
  96.     glLoadIdentity();
  97.     gluOrtho2D(0, windX, 0, windY);
  98.     glMatrixMode(GL_MODELVIEW);
  99. }
  100.  
  101. static GLenum Key(int key, GLenum mask)
  102. {
  103.  
  104.     switch (key) {
  105.       case TK_ESCAPE:
  106.         tkQuit();
  107.       case TK_SPACE:
  108.     cursor++;
  109.     if (cursor > TK_WHITE*2) {
  110.         cursor = TK_BLACK;
  111.     }
  112.     tkSetCursor(cursor);
  113.       default:
  114.     return GL_FALSE;
  115.     }
  116.     return GL_TRUE;
  117. }
  118.  
  119. static void Draw(void)
  120. {
  121.  
  122.     glClear(GL_COLOR_BUFFER_BIT);
  123.  
  124.     glBegin(GL_POLYGON);
  125.     TK_SETCOLOR(windType, TK_BLACK);
  126.     glVertex2i(0, 0);
  127.     TK_SETCOLOR(windType, TK_RED);
  128.     glVertex2i(windX, 0);
  129.     TK_SETCOLOR(windType, TK_GREEN);
  130.     glVertex2i(windX, windY);
  131.     TK_SETCOLOR(windType, TK_BLUE);
  132.     glVertex2i(0, windY);
  133.     glEnd();
  134.  
  135.     glFlush();
  136.  
  137.     if (doubleBuffer) {
  138.     tkSwapBuffers();
  139.     }
  140. }
  141.  
  142. static GLenum Args(int argc, char **argv)
  143. {
  144.     GLint i;
  145.  
  146.     rgb = GL_TRUE;
  147.     doubleBuffer = GL_FALSE;
  148.     directRender = GL_TRUE;
  149.  
  150.     for (i = 1; i < argc; i++) {
  151.     if (strcmp(argv[i], "-ci") == 0) {
  152.         rgb = GL_FALSE;
  153.     } else if (strcmp(argv[i], "-rgb") == 0) {
  154.         rgb = GL_TRUE;
  155.     } else if (strcmp(argv[i], "-sb") == 0) {
  156.         doubleBuffer = GL_FALSE;
  157.     } else if (strcmp(argv[i], "-db") == 0) {
  158.         doubleBuffer = GL_TRUE;
  159.     } else if (strcmp(argv[i], "-dr") == 0) {
  160.         directRender = GL_TRUE;
  161.     } else if (strcmp(argv[i], "-ir") == 0) {
  162.         directRender = GL_FALSE;
  163.     } else {
  164.         printf("%s (Bad option).\n", argv[i]);
  165.         return GL_FALSE;
  166.     }
  167.     }
  168.     return GL_TRUE;
  169. }
  170.  
  171. void main(int argc, char **argv)
  172. {
  173.  
  174.     if (Args(argc, argv) == GL_FALSE) {
  175.     tkQuit();
  176.     }
  177.  
  178.     windX = 300;
  179.     windY = 300;
  180.     tkInitPosition(0, 0, windX, windY);
  181.  
  182.     windType = (rgb) ? TK_RGB : TK_INDEX;
  183.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  184.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  185.     tkInitDisplayMode(windType);
  186.  
  187.     if (tkInitWindow("Cursor Test") == GL_FALSE) {
  188.     tkQuit();
  189.     }
  190.  
  191.     Init();
  192.  
  193.     tkExposeFunc(Reshape);
  194.     tkReshapeFunc(Reshape);
  195.     tkKeyDownFunc(Key);
  196.     tkDisplayFunc(Draw);
  197.     tkExec();
  198. }
  199.