Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This file builds on the 'skeleton' for creating CS110 graphic programs.
- -- It draws a jointed object on-screen (a simple robot) to demonstrate that
- ResetDrawingAxes(), MoveDrawingAxes(),
- SaveDrawingAxes(), RetrieveDrawingAxes()
- implement a 'push-down stack' (an abstract data type) for drawing axes.
- -- It defines mouse callback functions that print text onscreen,
- -- and keyboard functions:
- -Arrow keys move robot left,right,up,down;
- -pageUp, pageDn keyss make entire robot grow/shrink,
- -'1', '2' keys move robot arms up/down,
- -'3', '4' keys tilt entire robot left/right (clockwise/counterclockwise)
- -exit the program on the ESC, q, or space keys
- 2009.02.18 Created -J. Tumblin
- 2011.02.16 Updated for CodeBlocks and revised introGlutLib
- */
- #include "introGlutLib.h" //include the basic drawing library
- #include <time.h> // for MS Windows time-finding functions
- #include <stdlib.h> // for malloc and free
- #include <string.h> // for strcpy and other string fcns.
- //================ROBOT PROPORTIONS=================
- // (puts these important literals in one place; permits easy robot design changes
- #define ROB_TALL 70.0 // half-height of robot body (in pixels)
- #define ROB_WIDE 30.0 // half-width of robot body
- #define ROB_ARM_LONG 100.0 // entire length of robot arm
- #define ROB_ARM_WIDE 5.0 // half-width of robot arm
- // Global Variables (a bad idea; sometimes required by callbacks)
- //===============================
- double rob_xpos, rob_ypos; // robot position (in pixels)
- double rob_tilt, rob_size; // robot tilt (in degrees)
- double rob_armL, rob_armR; // robot left, right arm angle (in degrees)
- // ========================== Callback Functions ===============================
- // The following functions must be defined for this file to compile.
- // They are called by the GLUT main loop started by InitGraphics().
- //
- // myDisplay(): called repeatedly to update the display.
- // myMouseClik(): called when any mouse button clicked.
- // myMouseMove(); called when mouse dragged (with a button held down)
- // myKeyboard(): called when a key is pressed.
- // myKeySpecialKey(): special key pressed (e.g. arrow keys, fcn keys...)
- //
- // Do NOT call these functions yourself.
- // =================================Main:=======================================
- int main()
- {
- // Initial values for robot drawing
- rob_xpos = NU_SCREENWIDTH / 2.0; // Initial position: screen center
- rob_ypos = NU_SCREENHEIGHT / 2.0;
- rob_tilt = 0.0; // Initial tilt: zero degrees
- rob_size = 1.0; // Initial scale factor: 1.0
- rob_armL = rob_armR = 10.0; // Initial arm angles: 10 deg from body
- InitGraphics(); // start GLUT/OpenGL
- return 0;
- }
- /***************************************************************
- myDisplay()
- Draw a green box to verify everything is working.
- ***************************************************************/
- void myDisplay(void)
- {
- SetBackgndColor(0.0, 0.0, 0.0); // Black Background,
- ClearWindow(); // wipe out any previous drawing
- ResetDrawingAxes(); // Put axes at lower left corner, then
- //...............DRAW A ROBOT..................
- SaveDrawingAxes(); // 'Push': save the robot-location axes on stack
- // Now make robot-body-drawing axes:
- MoveDrawingAxes(rob_xpos, rob_ypos, // move them to robot location, but
- 0.0, 1.0); // use NO rotation or scale.
- MoveDrawingAxes(0.0, 0.0, rob_tilt, rob_size); // rotate,scale the axes,
- //DRAW ROBOT BODY:
- SetPenColor(0.7, 0.7, 0.7); // draw a gray body;
- DrawFillBox(-ROB_WIDE,-ROB_TALL,
- ROB_WIDE, ROB_TALL); //box centered at origin
- //..........DRAW LEFT ARM...................
- SaveDrawingAxes(); // 'Push' body-drawing axes onto stack;
- // Now make left-arm-drawing axes:
- // Move axes to left shoulder, and rotate by
- // arm-rotation-angle. (don't change scale)
- MoveDrawingAxes(-ROB_WIDE,ROB_TALL,rob_armL,1.0);
- SetPenColor(1.0, 0.0, 0.0); // Red left arm:
- // a box extending leftwards from origin
- DrawFillBox(-ROB_ARM_LONG, -ROB_ARM_WIDE,0.0, ROB_ARM_WIDE);
- RetrieveDrawingAxes(); // 'Pop': go back to body-drawing axes;
- //..........DONE with LEFT ARM..............
- //..........DRAW RIGHT ARM...................
- SaveDrawingAxes(); // 'Push': save body-drawing axes;
- // Now make right-arm-drawing axes:
- // Move axes to right shoulder, and rotate by
- // arm-rotation-angle (don't change scale).
- MoveDrawingAxes(ROB_WIDE, ROB_TALL, rob_armR,1.0);
- SetPenColor(0.0,1.0,0.0); // Green right arm:
- // a box extending rightwards from origin:
- DrawFillBox(0.0, -ROB_ARM_WIDE, ROB_ARM_LONG, ROB_ARM_WIDE);
- RetrieveDrawingAxes(); // 'Pop': go back to body-drawing axes;
- //...........DONE with RIGHT arm.................
- RetrieveDrawingAxes(); // 'Pop': go back to 'world' drawing axes.
- //................DONE with ROBOT DRAWING.
- SetPenColor(1.0, 1.0, 0.0); //Cyan text:
- // and use no green or blue.
- DrawText2D(rom24,50,90, "Mouse Click to position robot on-screen");
- DrawText2D(rom24,50,70, "Arrow Keys, PgUp, PgDn will move & resize robot,");
- DrawText2D(rom24,50,50, "Press 1,2 or 3,4 to tilt robot or wave arms,");
- SetPenColor(0.7, 0.7, 0.7); // Gray text:
- DrawText2D(helv18, 50,30, "To quit: Q,q,ESC, or SPACE bar.");
- DrawRobot1(rob_xpos, rob_ypos, ROB_WIDE, ROB_TALL,
- rob_armL, rob_armR, ROB_ARM_WIDE, ROB_ARM_LONG,
- rob_tilt, rob_size, );
- }
- //void DrawRobot1(int robot_xpos, )
- /**********************************************************************
- myKeyboard(key, x, y)
- Exit the program if user presses 'esc' Q,q, or spacebar
- ***********************************************************************/
- void myKeyboard(unsigned char key, int x, int y)
- {
- int i;
- i=x; i=y; // to prevent warnings from compiler.
- switch(key)
- {
- case '1':
- rob_tilt += 3.5; // tilt entire robot left (counter-clockwise)
- break;
- case '2':
- rob_tilt -= 3.5; // tilt entire robot right (clockwise)
- break;
- case '3':
- rob_armL -= 3.5; // adjust both robot arm angles downwards
- rob_armR -= 3.5;
- break;
- case '4':
- rob_armL += 3.5; // adjust both robot arm angles upwards.
- rob_armR += 3.5;
- break;
- case ' ': // User pressed the spacebar.
- case 27: // User pressed the 'Esc' key...
- case 'Q': // User pressed the 'Q' key...
- case 'q':
- exit(0); // Done! quit the program.
- break;
- default: // anything else;
- SetPenColor(0.0, 1.0, 1.0); //Cyan text:
- // and use no green or blue.
- DrawText2D(rom24,NU_SCREENWIDTH/2, NU_SCREENHEIGHT/2, "WHAT?");
- break;
- }
- }
- /**********************************************************************
- myKeySpecial(key, x, y)
- Exit the program if a special key is pressed
- ***********************************************************************/
- #define JT_ARROW_STEP 2.5 // stepsize for arrow keys
- #define JT_PAGE_STEP 1.07 // factor used for page-up, page-down keys
- void myKeySpecial(int key, int x, int y)
- {
- int junk;
- junk = x; junk = y; // to stop the 'unused parameter' warning
- switch(key)
- {
- case GLUT_KEY_LEFT: // left arrow key; both fish swim left.
- printf("left-arrow key.\n");
- rob_xpos -= JT_ARROW_STEP ;
- break;
- case GLUT_KEY_RIGHT: // right arrow key; both fish swim right
- printf("right-arrow key.\n");
- rob_xpos += JT_ARROW_STEP ;
- break;
- case GLUT_KEY_DOWN: // dn arrow key; both fish swim upwards.
- printf("dn-arrow key.\n");
- rob_ypos -= JT_ARROW_STEP ;
- break;
- case GLUT_KEY_UP: // up arrow key; both fish swim downwards
- printf("up-arrow key.\n");
- rob_ypos += JT_ARROW_STEP ;
- break;
- case GLUT_KEY_PAGE_UP:
- printf("page-up key.\n");
- rob_size *= JT_PAGE_STEP; // GROW by 7%
- break;
- case GLUT_KEY_PAGE_DOWN:
- printf("page-down key.\n");
- rob_size /= JT_PAGE_STEP; // SHRINK by 7%
- break;
- // SEARCH glut.h for more arrow key #define statements.
- default:
- break;
- }
- // We might have changed something. Force a re-display
- glutPostRedisplay();
- }
- /**********************************************************************
- myMouseClik(buttonID, upDown, x, y)
- Exit the program if user presses 'esc' Q,q, or spacebar
- ***********************************************************************/
- // GLUT 'mouseClik' Callback. User caused a click/unclick event with the mouse:
- // buttonID== 0 for left mouse button,
- // (== 1 for middle mouse button?)
- // == 2 for right mouse button;
- // upDown == 0 if mouse button was pressed down,
- // == 1 if mouse button released.
- // xpos,ypos == position of mouse cursor, in pixel units within the window.
- // *CAREFUL!* Microsoft puts origin at UPPER LEFT corner of the window.
- void myMouseClik(int buttonID, int upDown, int x, int y)
- {
- SetPenColor(0.0, 1.0, 0.0); //GREEN text:
- DrawText2D(rom24, NU_SCREENWIDTH/3, NU_SCREENHEIGHT/3, "!MOUSE CLICK!");
- if(buttonID == 0 && upDown == 0) // left mouse clicked at x,y
- {
- rob_xpos = x;
- rob_ypos = y;
- }
- }
- /**********************************************************************
- myMouseMove(int x, int y)
- print "MOVED!" on-screen if user moves mouse.
- ***********************************************************************/
- // GLUT 'mouseMove' Callback, called when user moves mouse to new place (x,y).
- void myMouseMove(int x, int y)
- {
- SetPenColor(1.0, 0.0, 0.0); //RED text:
- DrawText2D(rom24,NU_SCREENWIDTH/3, NU_SCREENHEIGHT/4, "!MOUSE MOVE!");
- rob_xpos = x;
- rob_ypos = y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement