home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 March / Chip_2000-03_cd.bin / zkuste / linux / opengl / Terry / OpenGL / Examples / Events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-24  |  3.8 KB  |  165 lines

  1. // OpenGL Tutorial
  2. // Events.c
  3.  
  4. /*************************************************************************
  5. This example extends Hello_World.c to accept and handle events.  The
  6. user should try produce each of the following events: key press, mouse
  7. movement, mouse button press, mouse button release, reshape window, and
  8. expose window.
  9. *************************************************************************/
  10.  
  11. // gcc -o Events  Events.c -lX11 -lMesaGL -lMesaGLU -lMesatk -lm
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include <gltk.h>
  17.  
  18. void expose(int width, int height) {
  19.  
  20.     printf("Got an Expose Event");
  21.     printf(":\t width = %d height = %d\n", width, height);
  22.  
  23.     // Clear the window
  24.     glClear(GL_COLOR_BUFFER_BIT);
  25. }
  26.  
  27. void reshape(int width, int height) {
  28.  
  29.     printf("Got a Reshape Event");
  30.     printf(":\t width = %d height = %d\n", width, height);
  31.  
  32.     // Set the new viewport size
  33.     glViewport(0, 0, (GLint)width, (GLint)height);
  34.  
  35.     // Clear the window
  36.     glClear(GL_COLOR_BUFFER_BIT);
  37. }
  38.  
  39. GLenum key_down(int key, GLenum state) {
  40.  
  41.     printf("Got a Key Down Event");
  42.     printf(":\t key = %d, state = %d\n", key, state);
  43.  
  44.     if ((key == TK_ESCAPE) || (key == TK_q) || (key == TK_Q))
  45.         tkQuit();
  46. }
  47.  
  48. GLenum mouse_down(int x, int y, GLenum state) {
  49.  
  50.     printf("Got a Mouse Down Event");
  51.     printf(":\t x = %d, y = %d, button = ", x, y);
  52.  
  53.     switch (state) {
  54.         case TK_LEFTBUTTON : printf("left\n");
  55.             break;
  56.         case TK_RIGHTBUTTON : printf("right\n");
  57.             break;
  58.         case TK_MIDDLEBUTTON : printf("middle\n");
  59.             break;
  60.         default : printf("none\n");
  61.             break;
  62.     }
  63. }
  64.  
  65. GLenum mouse_up(int x, int y, GLenum state) {
  66.  
  67.     printf("Got a Mouse Up Event");
  68.     printf(":\t x = %d, y = %d, button = ", x, y);
  69.  
  70.     switch (state) {
  71.         case TK_LEFTBUTTON : printf("left\n");
  72.             break;
  73.         case TK_RIGHTBUTTON : printf("right\n");
  74.             break;
  75.         case TK_MIDDLEBUTTON : printf("middle\n");
  76.             break;
  77.         default : printf("none\n");
  78.             break;
  79.     }
  80. }
  81.  
  82. GLenum mouse_move(int x, int y, GLenum state) {
  83.  
  84.     printf("Got a Mouse Move Event");
  85.     printf(":\t x = %d, y = %d, button = ", x, y);
  86.  
  87.     switch (state) {
  88.         case TK_LEFTBUTTON : printf("left\n");
  89.             break;
  90.         case TK_RIGHTBUTTON : printf("right\n");
  91.             break;
  92.         case TK_MIDDLEBUTTON : printf("middle\n");
  93.             break;
  94.         default : printf("none\n");
  95.             break;
  96.     }
  97. }
  98.  
  99. void draw(void) {
  100.  
  101.     printf("Got a Display Event\n");
  102.  
  103.     // Set the drawing color
  104.     glColor3f(1.0, 1.0, 1.0);
  105.  
  106.     // Specify which primitive type is to be drawn
  107.     glBegin(GL_POLYGON);
  108.         // Specify verticies in quad
  109.         glVertex2f(-0.5, -0.5);
  110.         glVertex2f(-0.5, 0.5);
  111.         glVertex2f(0.5, 0.5);
  112.         glVertex2f(0.5, -0.5);
  113.     glEnd();
  114.  
  115.     // Flush the buffer to force drawing of all objects thus far
  116.     glFlush();
  117. }
  118.  
  119. void main(int argc, char **argv) {
  120.  
  121.     // Open a window and name it
  122.     if (tkInitWindow("Events") == GL_FALSE) {
  123.         tkQuit();
  124.     }
  125.  
  126.     // Set the clear color to black
  127.     glClearColor(0.0, 0.0, 0.0, 0.0);
  128.  
  129.     // Assign expose() to be the function called whenever
  130.     // an expose event occurs
  131.     tkExposeFunc(expose);
  132.  
  133.     // Assign reshape() to be the function called whenever 
  134.     // a reshape event occurs
  135.     tkReshapeFunc(reshape);
  136.  
  137.     // Assign key_down() to be the function called whenever
  138.     // a key is pressed
  139.     tkKeyDownFunc(key_down);
  140.  
  141.     // Assign mouse_down() to be the function called whenever
  142.     // a mouse button is pressed
  143.     tkMouseDownFunc(mouse_down);
  144.  
  145.     // Assign mouse_up() to be the function called whenever
  146.     // a mouse button is released
  147.     tkMouseUpFunc(mouse_up);
  148.  
  149.     // Assign mouse_move() to be the function called whenever
  150.     // the mouse is moved
  151.     tkMouseMoveFunc(mouse_move);
  152.  
  153.     // Assign draw() to be the function called whenever a display
  154.     // event occurs, generally after a resize or expose event
  155.     tkDisplayFunc(draw);
  156.  
  157.     // Assign idle() to be the function called whenever there 
  158.     // are no events, NOT USED IN THIS EXAMPLE
  159. //    tkIdleFunc(idle);
  160.  
  161.     // Pass program control to tk's event handling code
  162.     // In other words, loop forever
  163.     tkExec();
  164. }
  165.