This section covers the following topics:
The second program is called flex>, for flexible square. It uses the square program as a base, and adds some simple user interaction. The size of the red square can now be interactively controlled by using the mouse pointer.
----------------------------------------------------------------- /* * COMPONENT_NAME: graPHIGS API Samples * * ORIGINS: 27 * * (C) COPYRIGHT International Business Machines Corp. 1990 * All Rights Reserved * * Licensed Materials - Property of IBM * * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ /*--------------------------------------------------------------*/ /* graPHIGS Start-Up : adding simple input */ #include [afmnc.h] /* graPHIGS include file */ main() { int wsid = 1, viewid = 0, strid = 1; int n4[1] = {4}; int lcview,major,class,minor; float matrix[9],lcpos[3]; static float oldpos[2] = {1.0,1.0}; static float pts[8] = {1.0,1.0, 1.0,-1.0, -1.0,-1.0, -1.0,1.0}; GPOPPH (" ",0); /* open graPHIGS */ GPOPWS (wsid,"*","X "); /* open a workstation */ GPOPST(strid); /* open structure */ GPINLB(1); /* insert label 1 */ GPINLB(2); /* insert label 2 */ GPEF (2); /* set edge on */ GPIS (2); /* set interior style solid */ GPICI (2); /* set interior color RED */ GPECI (1); /* set edge color WHITE */ GPPG2 (1,n4,2,pts); /* add 2D polygon */ GPCLST(); /* close structure */ GPARV (wsid,viewid,strid,1.0); /* link root to view */ GPUPWS(wsid,2); /* update workstation */ GPCHMO(wsid,2,3,2); /* choice: event mode */ GPLCMO(wsid,1,2,2); /* locator: sample mode */ do { /*loop until mouse hit */ GPSMLC (wsid, 1, &lcview,lcpos); /* get mouse position */ if (lcpos[0] != oldpos [0] || lcpos[1] != oldpos [1] ) { oldpos[0] = lcpos [0] ; oldpos[1] = lcpos [1] ; GPSC2 (lcpos, matrix); /* calculate scale matrix */ GPOPST (strid); /* open structure for edit */ GPDELB (1,2); /* delete between labels */ GPMLX2 (matrix, 3); /* insert new model matrix */ GPCLST (); /* close structure */ GPUPWS (wsid,2): /* update workstation */ } GPAWEV (0.0,&major,&class,&minor); /* check for any events */ } while (class != 4); GPCLPH(); }
cc -o flex flex.c -lgP
./flex
A window pops onto the screen. Inside the window is a red rectangle with a white border. The size of the rectangle can be controlled by moving the mouse pointer while it is inside the graPHIGS API window. To exit the program, position the mouse pointer in the graPHIGS API window and press any button.
This section looks at the program in more detail:
Creating the geometry structure is essentially the same as for the square program. The only additions are two label elements, added with the Insert Label (GPINLB) subroutine. These labels will allow you to later insert and delete elements between the two labels.
---------------------------------------------------------------- GPOPST(strid); /* open structure */ GPINLB(1); /* insert label 1 */ GPINLB(2); /* insert label 2 */ GPEF (2); /* set edge on */ GPIS (2); /* set interior style solid */ GPICI (2); /* set interior color RED */ GPECI (1); /* set edge color WHITE */ GPPG2 (1,n4,2,pts); /* add 2D polygon */ GPCLST(); /* close structure */ ----------------------------------------------------------------
This program also adds a loop to handle input. Inside the loop, the Sample Locator (GPSMLC) subroutine samples the position of the mouse pointer (or locator device). The locator position is returned as x,y,z coordinates in the variable lcpos. If the new and old locator positions are different, the shape of the square is modified. This is done by editing the geometry structure containing the square.
The Scale 2D (GPSC2) subroutine is used to create a new 2D scale matrix which is then inserted into the geometry structure using the Set Modeling Transformation 2D (GPMLX2) subroutine.
Also inside the input loop, the graPHIGS API event queue is checked using the Await Event (GPAWEV) subroutine. The loop is exited if the value for the returned class is 4 (e.g., the mouse button is pressed).
---------------------------------------------------------------- do { /* loop until mouse hit */ GPSMLC(wsid,1,&lcview,lcpos); /* get the mouse position */ if (lcpos[0] != oldpos[0] || lcpos[1] != oldpos[1]) { oldpos[0] = lcpos[0]; oldpos[1] = lcpos[1]; GPSC2 (lcpos,matrix); /* calculate scale matrix */ GPOPST(strid); /* open structure for edit */ GPDELB(1,2); /* delete between labels */ GPMLX2 (matrix,3); /* insert new model matrix */ GPCLST(); /* close structure */ GPUPWS(wsid,2); /* update workstation */ } GPAWEV(0.0,&major,&class,&minor); /* check for any events */ } while (class != 4); ----------------------------------------------------------------