home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / samples / font.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  6KB  |  274 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. #define OPENGL_WIDTH 24
  32. #define OPENGL_HEIGHT 13
  33.  
  34.  
  35. char string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz";
  36. GLenum rgb, doubleBuffer, directRender, windType;
  37. GLuint strokeBase, outlineBase, filledBase, bitmapBase;
  38. float angleX = 0.0, angleY = 0.0, angleZ = 0.0;
  39. float scaleX = 1.0, scaleY = 1.0, scaleZ = 1.0;
  40. float shiftX = 0.0, shiftY = 0.0, shiftZ = 0.0;
  41.  
  42.  
  43. static void Init(void)
  44. {
  45.  
  46.     strokeBase = glGenLists(256);
  47.     if (tkCreateStrokeFont(strokeBase) == GL_FALSE) {
  48.     tkQuit();
  49.     }
  50.     outlineBase = glGenLists(256);
  51.     if (tkCreateOutlineFont(outlineBase) == GL_FALSE) {
  52.     tkQuit();
  53.     }
  54.     filledBase = glGenLists(256);
  55.     if (tkCreateFilledFont(filledBase) == GL_FALSE) {
  56.     tkQuit();
  57.     }
  58.     bitmapBase = glGenLists(256);
  59.     if (tkCreateBitmapFont(bitmapBase) == GL_FALSE) {
  60.     tkQuit();
  61.     }
  62.  
  63.     glClearColor(0.0, 0.0, 0.0, 0.0);
  64.     glClearIndex(0.0);
  65. }
  66.  
  67. static void Reshape(int width, int height)
  68. {
  69.  
  70.     glViewport(0, 0, (GLint)width, (GLint)height);
  71.  
  72.     glMatrixMode(GL_PROJECTION);
  73.     glLoadIdentity();
  74.     glOrtho(-400.0, 400.0, -200.0, 200.0, -400.0, 400.0);
  75.     glMatrixMode(GL_MODELVIEW);
  76. }
  77.  
  78. static GLenum Key(int key, GLenum mask)
  79. {
  80.  
  81.     switch (key) {
  82.       case TK_ESCAPE:
  83.         tkQuit();
  84.  
  85.       case TK_LEFT:
  86.     shiftX -= 20.0;
  87.     break;
  88.       case TK_RIGHT:
  89.     shiftX += 20.0;
  90.     break;
  91.       case TK_UP:
  92.     shiftY += 20.0;
  93.     break;
  94.       case TK_DOWN:
  95.     shiftY -= 20.0;
  96.     break;
  97.       case TK_n:
  98.     shiftZ += 20.0;
  99.     break;
  100.       case TK_m:
  101.     shiftZ -= 20.0;
  102.     break;
  103.  
  104.       case TK_q:
  105.     scaleX -= 0.1;
  106.     if (scaleX < 0.1) {
  107.         scaleX = 0.1;
  108.     }
  109.     break;
  110.       case TK_w:
  111.     scaleX += 0.1;
  112.     break;
  113.       case TK_a:
  114.     scaleY -= 0.1;
  115.     if (scaleY < 0.1) {
  116.         scaleY = 0.1;
  117.     }
  118.     break;
  119.       case TK_s:
  120.     scaleY += 0.1;
  121.     break;
  122.       case TK_z:
  123.     scaleZ -= 0.1;
  124.     if (scaleZ < 0.1) {
  125.         scaleZ = 0.1;
  126.     }
  127.     break;
  128.       case TK_x:
  129.     scaleZ += 0.1;
  130.     break;
  131.  
  132.       case TK_e:
  133.     angleX -= 5.0;
  134.     if (angleX < 0.0) {
  135.         angleX = 360.0 + angleX;
  136.     }
  137.     break;
  138.       case TK_r:
  139.     angleX += 5.0;
  140.     if (angleX > 360.0) {
  141.         angleX = angleX - 360.0;
  142.     }
  143.     break;
  144.       case TK_d:
  145.     angleY -= 5.0;
  146.     if (angleY < 0.0) {
  147.         angleY = 360.0 + angleY;
  148.     }
  149.     break;
  150.       case TK_f:
  151.     angleY += 5.0;
  152.     if (angleY > 360.0) {
  153.         angleY = angleY - 360.0;
  154.     }
  155.     break;
  156.       case TK_c:
  157.     angleZ -= 5.0;
  158.     if (angleZ < 0.0) {
  159.         angleZ = 360.0 + angleZ;
  160.     }
  161.     break;
  162.       case TK_v:
  163.     angleZ += 5.0;
  164.     if (angleZ > 360.0) {
  165.         angleZ = angleZ - 360.0;
  166.     }
  167.     break;
  168.  
  169.       default:
  170.     return GL_FALSE;
  171.     }
  172.     return GL_TRUE;
  173. }
  174.  
  175. static void Draw(void)
  176. {
  177.  
  178.     glClear(GL_COLOR_BUFFER_BIT);
  179.  
  180.     TK_SETCOLOR(windType, TK_WHITE);
  181.  
  182.     glPushMatrix();
  183.  
  184.     glTranslatef(shiftX, shiftY, shiftZ);
  185.     glRotatef(angleX, 1.0, 0.0, 0.0);
  186.     glRotatef(angleY, 0.0, 1.0, 0.0);
  187.     glRotatef(angleZ, 0.0, 0.0, 1.0);
  188.     glScalef(scaleX, scaleY, scaleZ);
  189.  
  190.     glPushMatrix();
  191.     glRasterPos2f(-390.5, 0.5);
  192.     tkDrawStr(bitmapBase, string);
  193.     glPopMatrix();
  194.  
  195.     glPushMatrix();
  196.     glTranslatef(-390.5, -30.5, 0.0);
  197.     tkDrawStr(strokeBase, string);
  198.     glPopMatrix();
  199.  
  200.     glPushMatrix();
  201.     glTranslatef(-390.5, -60.5, 0.0);
  202.     tkDrawStr(outlineBase, string);
  203.     glPopMatrix();
  204.  
  205.     glPushMatrix();
  206.     glTranslatef(-390.5, -90.5, 0.0);
  207.     tkDrawStr(filledBase, string);
  208.     glPopMatrix();
  209.  
  210.     glPopMatrix();
  211.  
  212.     glFlush();
  213.  
  214.     if (doubleBuffer) {
  215.     tkSwapBuffers();
  216.     }
  217. }
  218.  
  219. static GLenum Args(int argc, char **argv)
  220. {
  221.     GLint i;
  222.  
  223.     rgb = GL_TRUE;
  224.     doubleBuffer = GL_FALSE;
  225.     directRender = GL_TRUE;
  226.  
  227.     for (i = 1; i < argc; i++) {
  228.     if (strcmp(argv[i], "-ci") == 0) {
  229.         rgb = GL_FALSE;
  230.     } else if (strcmp(argv[i], "-rgb") == 0) {
  231.         rgb = GL_TRUE;
  232.     } else if (strcmp(argv[i], "-sb") == 0) {
  233.         doubleBuffer = GL_FALSE;
  234.     } else if (strcmp(argv[i], "-db") == 0) {
  235.         doubleBuffer = GL_TRUE;
  236.     } else if (strcmp(argv[i], "-dr") == 0) {
  237.         directRender = GL_TRUE;
  238.     } else if (strcmp(argv[i], "-ir") == 0) {
  239.         directRender = GL_FALSE;
  240.     } else {
  241.         printf("%s (Bad option).\n", argv[i]);
  242.         return GL_FALSE;
  243.     }
  244.     }
  245.     return GL_TRUE;
  246. }
  247.  
  248. void main(int argc, char **argv)
  249. {
  250.  
  251.     if (Args(argc, argv) == GL_FALSE) {
  252.     tkQuit();
  253.     }
  254.  
  255.     tkInitPosition(0, 0, 800, 400);
  256.  
  257.     windType = (rgb) ? TK_RGB : TK_INDEX;
  258.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  259.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  260.     tkInitDisplayMode(windType);
  261.  
  262.     if (tkInitWindow("Font Test") == GL_FALSE) {
  263.     tkQuit();
  264.     }
  265.  
  266.     Init();
  267.  
  268.     tkExposeFunc(Reshape);
  269.     tkReshapeFunc(Reshape);
  270.     tkKeyDownFunc(Key);
  271.     tkDisplayFunc(Draw);
  272.     tkExec();
  273. }
  274.