Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.81 KB | None | 0 0
  1. /* This file builds on the 'skeleton' for creating CS110 graphic programs.
  2.  
  3.    -- It draws a jointed object on-screen (a simple robot) to demonstrate that
  4.             ResetDrawingAxes(), MoveDrawingAxes(),
  5.             SaveDrawingAxes(), RetrieveDrawingAxes()
  6.       implement a 'push-down stack' (an abstract data type) for drawing axes.
  7.    -- It defines mouse callback functions that print text onscreen,
  8.    -- and keyboard functions:
  9.         -Arrow keys move robot left,right,up,down;
  10.         -pageUp, pageDn keyss make entire robot grow/shrink,
  11.         -'1', '2' keys move robot arms up/down,
  12.         -'3', '4' keys tilt entire robot left/right (clockwise/counterclockwise)
  13.         -exit the program on the ESC, q, or space keys
  14.  
  15.         2009.02.18 Created -J. Tumblin
  16.         2011.02.16 Updated for CodeBlocks and revised introGlutLib
  17. */
  18.  
  19.  
  20. #include "introGlutLib.h"       //include the basic drawing library
  21. #include <time.h>               // for MS Windows time-finding functions
  22. #include <stdlib.h>             // for malloc and free
  23. #include <string.h>             // for strcpy and other string fcns.
  24.  
  25. //================ROBOT PROPORTIONS=================
  26. // (puts these important literals in one place; permits easy robot design changes
  27. #define ROB_TALL        70.0    // half-height of robot body (in pixels)
  28. #define ROB_WIDE        30.0    // half-width of robot body
  29. #define ROB_ARM_LONG    100.0   // entire length of robot arm
  30. #define ROB_ARM_WIDE    5.0     // half-width of robot arm
  31.  
  32. // Global Variables (a bad idea; sometimes required by callbacks)
  33. //===============================
  34. double rob_xpos, rob_ypos;      // robot position (in pixels)
  35. double rob_tilt, rob_size;      // robot tilt (in degrees)
  36. double rob_armL, rob_armR;      // robot left, right arm angle (in degrees)
  37.  
  38. // ========================== Callback Functions ===============================
  39. // The following functions must be defined for this file to compile.
  40. // They are called by the GLUT main loop started by InitGraphics().
  41. //
  42. //      myDisplay(): called repeatedly to update the display.
  43. //      myMouseClik(): called when any mouse button clicked.
  44. //      myMouseMove(); called when mouse dragged (with a button held down)
  45. //      myKeyboard(): called when a key is pressed.
  46. //      myKeySpecialKey(): special key pressed (e.g. arrow keys, fcn keys...)
  47. //
  48. // Do NOT call these functions yourself.
  49.  
  50. // =================================Main:=======================================
  51. int main()
  52. {
  53.     // Initial values for robot drawing
  54.     rob_xpos = NU_SCREENWIDTH  / 2.0;       // Initial position: screen center
  55.     rob_ypos = NU_SCREENHEIGHT / 2.0;
  56.     rob_tilt = 0.0;                         // Initial tilt: zero degrees
  57.     rob_size = 1.0;                         // Initial scale factor: 1.0
  58.     rob_armL = rob_armR = 10.0;             // Initial arm angles: 10 deg from body
  59.  
  60.     InitGraphics();         // start GLUT/OpenGL
  61.     return 0;
  62. }
  63.  
  64. /***************************************************************
  65.  myDisplay()
  66.  
  67.  Draw a green box to verify everything is working.
  68. ***************************************************************/
  69.  
  70. void myDisplay(void)
  71. {
  72.  
  73.     SetBackgndColor(0.0, 0.0, 0.0);     // Black Background,
  74.     ClearWindow();                      // wipe out any previous drawing
  75.  
  76.     ResetDrawingAxes();                 // Put axes at lower left corner, then
  77.     //...............DRAW A ROBOT..................
  78.     SaveDrawingAxes();          // 'Push': save the robot-location axes on stack
  79.                                 // Now make robot-body-drawing axes:
  80.         MoveDrawingAxes(rob_xpos, rob_ypos,     // move them to robot location, but
  81.                                  0.0, 1.0); // use NO rotation or scale.
  82.         MoveDrawingAxes(0.0, 0.0, rob_tilt, rob_size); // rotate,scale the axes,
  83.         //DRAW ROBOT BODY:
  84.         SetPenColor(0.7, 0.7, 0.7);             // draw a gray body;
  85.         DrawFillBox(-ROB_WIDE,-ROB_TALL,
  86.                      ROB_WIDE, ROB_TALL);   //box centered at origin
  87.         //..........DRAW LEFT ARM...................
  88.         SaveDrawingAxes();      // 'Push' body-drawing axes onto stack;
  89.                                     // Now make left-arm-drawing axes:
  90.                                     // Move axes to left shoulder, and rotate by
  91.                                     // arm-rotation-angle. (don't change scale)
  92.             MoveDrawingAxes(-ROB_WIDE,ROB_TALL,rob_armL,1.0);
  93.  
  94.             SetPenColor(1.0, 0.0, 0.0); // Red left arm:
  95.                                         // a box extending leftwards from origin
  96.             DrawFillBox(-ROB_ARM_LONG, -ROB_ARM_WIDE,0.0, ROB_ARM_WIDE);
  97.         RetrieveDrawingAxes();  // 'Pop': go back to body-drawing axes;
  98.         //..........DONE with LEFT ARM..............
  99.         //..........DRAW RIGHT ARM...................
  100.         SaveDrawingAxes();      // 'Push': save body-drawing axes;
  101.                                     // Now make right-arm-drawing axes:
  102.                                     // Move axes to right shoulder, and rotate by
  103.                                     // arm-rotation-angle (don't change scale).
  104.             MoveDrawingAxes(ROB_WIDE, ROB_TALL, rob_armR,1.0);
  105.  
  106.             SetPenColor(0.0,1.0,0.0);   // Green right arm:
  107.                                     // a box extending rightwards from origin:
  108.             DrawFillBox(0.0, -ROB_ARM_WIDE, ROB_ARM_LONG, ROB_ARM_WIDE);
  109.         RetrieveDrawingAxes();      // 'Pop': go back to body-drawing axes;
  110.         //...........DONE with RIGHT arm.................
  111.     RetrieveDrawingAxes();          // 'Pop': go back to 'world' drawing axes.
  112.     //................DONE with ROBOT DRAWING.
  113.  
  114.     SetPenColor(1.0, 1.0, 0.0); //Cyan text:
  115.                                 // and use no green or blue.
  116.     DrawText2D(rom24,50,90, "Mouse Click to position robot on-screen");
  117.     DrawText2D(rom24,50,70, "Arrow Keys, PgUp, PgDn will move & resize robot,");
  118.     DrawText2D(rom24,50,50, "Press 1,2 or 3,4 to tilt robot or wave arms,");
  119.     SetPenColor(0.7, 0.7, 0.7); // Gray text:
  120.     DrawText2D(helv18, 50,30, "To quit: Q,q,ESC, or SPACE bar.");
  121.  
  122.     DrawRobot1(rob_xpos, rob_ypos, ROB_WIDE, ROB_TALL,
  123.               rob_armL, rob_armR, ROB_ARM_WIDE, ROB_ARM_LONG,
  124.               rob_tilt, rob_size,   );
  125. }
  126.  
  127. //void DrawRobot1(int robot_xpos, )
  128. /**********************************************************************
  129.  myKeyboard(key, x, y)
  130.  
  131. Exit the program if user presses 'esc' Q,q, or spacebar
  132. ***********************************************************************/
  133.  
  134. void myKeyboard(unsigned char key, int x, int y)
  135. {
  136. int i;
  137.     i=x; i=y;                   // to prevent warnings from compiler.
  138.     switch(key)
  139.     {
  140.         case '1':
  141.             rob_tilt += 3.5;    // tilt entire robot left (counter-clockwise)
  142.             break;
  143.         case '2':
  144.             rob_tilt -= 3.5;    // tilt entire robot right (clockwise)
  145.             break;
  146.         case '3':
  147.             rob_armL -= 3.5;    // adjust both robot arm angles downwards
  148.             rob_armR -= 3.5;
  149.             break;
  150.         case '4':
  151.             rob_armL += 3.5;    // adjust both robot arm angles upwards.
  152.             rob_armR += 3.5;
  153.             break;
  154.         case ' ':       // User pressed the spacebar.
  155.         case 27:        // User pressed the 'Esc'  key...
  156.         case 'Q':       // User pressed the 'Q' key...
  157.         case 'q':
  158.             exit(0); // Done! quit the program.
  159.             break;
  160.         default:          // anything else;
  161.             SetPenColor(0.0, 1.0, 1.0); //Cyan text:
  162.                                 // and use no green or blue.
  163.             DrawText2D(rom24,NU_SCREENWIDTH/2, NU_SCREENHEIGHT/2, "WHAT?");
  164.             break;
  165.     }
  166. }
  167.  
  168.  
  169. /**********************************************************************
  170.  myKeySpecial(key, x, y)
  171.  
  172.  Exit the program if a special key is pressed
  173. ***********************************************************************/
  174.  
  175. #define JT_ARROW_STEP 2.5           // stepsize for arrow keys
  176. #define JT_PAGE_STEP  1.07          // factor used for page-up, page-down keys
  177. void myKeySpecial(int key, int x, int y)
  178. {
  179. int junk;
  180.  
  181.     junk = x; junk = y;         // to stop the 'unused parameter' warning
  182.     switch(key)
  183.     {
  184.         case GLUT_KEY_LEFT:     // left arrow key; both fish swim left.
  185.             printf("left-arrow key.\n");
  186.             rob_xpos -= JT_ARROW_STEP ;
  187.             break;
  188.         case GLUT_KEY_RIGHT:    // right arrow key; both fish swim right
  189.             printf("right-arrow key.\n");
  190.             rob_xpos += JT_ARROW_STEP ;
  191.             break;
  192.         case GLUT_KEY_DOWN:     // dn arrow key; both fish swim upwards.
  193.             printf("dn-arrow key.\n");
  194.             rob_ypos -= JT_ARROW_STEP ;
  195.             break;
  196.         case GLUT_KEY_UP:       // up arrow key; both fish swim downwards
  197.             printf("up-arrow key.\n");
  198.             rob_ypos += JT_ARROW_STEP ;
  199.             break;
  200.         case GLUT_KEY_PAGE_UP:
  201.             printf("page-up key.\n");
  202.             rob_size *= JT_PAGE_STEP;       // GROW by 7%
  203.             break;
  204.         case GLUT_KEY_PAGE_DOWN:
  205.             printf("page-down key.\n");
  206.             rob_size /= JT_PAGE_STEP;       // SHRINK by 7%
  207.             break;
  208.         // SEARCH glut.h for more arrow key #define statements.
  209.         default:
  210.             break;
  211.     }
  212.     // We might have changed something. Force a re-display
  213.     glutPostRedisplay();
  214. }
  215.  
  216.  
  217. /**********************************************************************
  218.  myMouseClik(buttonID, upDown, x, y)
  219.  
  220. Exit the program if user presses 'esc' Q,q, or spacebar
  221. ***********************************************************************/
  222. // GLUT 'mouseClik' Callback.  User caused a click/unclick event with the mouse:
  223. //     buttonID== 0 for left mouse button,
  224. //            (== 1 for middle mouse button?)
  225. //             == 2 for right mouse button;
  226. //      upDown == 0 if mouse button was pressed down,
  227. //             == 1 if mouse button released.
  228. //      xpos,ypos == position of mouse cursor, in pixel units within the window.
  229. // *CAREFUL!* Microsoft puts origin at UPPER LEFT corner of the window.
  230. void myMouseClik(int buttonID, int upDown, int x, int y)
  231. {
  232.     SetPenColor(0.0, 1.0, 0.0); //GREEN text:
  233.     DrawText2D(rom24, NU_SCREENWIDTH/3, NU_SCREENHEIGHT/3, "!MOUSE CLICK!");
  234.     if(buttonID == 0 && upDown == 0)        // left mouse clicked at x,y
  235.     {
  236.         rob_xpos = x;
  237.         rob_ypos = y;
  238.     }
  239. }
  240. /**********************************************************************
  241.  myMouseMove(int x, int y)
  242.  
  243.  print "MOVED!" on-screen if user moves mouse.
  244. ***********************************************************************/
  245. // GLUT 'mouseMove' Callback, called when user moves mouse to new place (x,y).
  246.  
  247. void myMouseMove(int x, int y)
  248. {
  249.     SetPenColor(1.0, 0.0, 0.0); //RED text:
  250.     DrawText2D(rom24,NU_SCREENWIDTH/3, NU_SCREENHEIGHT/4, "!MOUSE MOVE!");
  251.     rob_xpos = x;
  252.     rob_ypos = y;
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement