This section covers the following topics:
Your first program is called square, since it puts a red square with a white border on the screen.
/* * 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 : our first program */ #include <afmnc.h> /* graPHIGS include file */ main() { int wsid = 1, viewid = 0, strid = 1; int n4[1] = {4}; int status,choice; static float pts[8] = {0.5,0.5, 0.5,-0.5, -0.5,-0.5, -0.5,0.5}; GPOPPH (" ",0); /* open graPHIGS */ GPOPWS (wsid,"*","X "); /* open a workstation */ GPOPST(strid); /* open structure */ 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 */ GPRQCH(wsid,2,&status,&choice); /* wait for mouse hit */ GPCLPH(); /* close graPHIGS */ } -------------------------------------------------------------------
cc -o square square.c -lgP
./square
A window pops onto the screen. Inside the window is a red square with a white border. To exit the program, place the mouse pointer, or locator input device, inside the graPHIGS API window and press any mouse button.
This section describes the purpose of various parts of the program:
Every graPHIGS API program written in the C programming language must include the file afmnc.h. This file contains the entry point definitions for all graPHIGS API functions.
#include <afmnc.h> /* graPHIGS include file */
The following two lines are what actually initialize the graPHIGS API. The Open graPHIGS (GPOPPH) subroutine puts the graPHIGS API into the open state, and Open Workstation subroutine opens one graphics workstation with the specified workstation identifier, wsid.
---------------------------------------------------------------- GPOPPH (" ",0); /* open graPHIGS */ GPOPWS (wsid,"*","X "); /* open a workstation */ ----------------------------------------------------------------
The next section of code creates a geometry structure with the specified structure identifier, strid. The actual geometry and attributes are placed inside this structure. In this case, polygon edge attributes and polygon interior attributes are inserted into the structure, followed by a 2D polygon.
---------------------------------------------------------------- GPOPST(strid); /* open structure */ 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 */ ----------------------------------------------------------------
The next two lines of code display the geometry on the screen. The geometry structure you just created must be associated with a workstation view before it can be displayed. The first line of code associates the structure (called a root structure) with the default graPHIGS API view 0, specified by the view identifier viewid. The second line of code tells the workstation to update itself using the Update Workstation (GPUPWS) subroutine.
---------------------------------------------------------------- GPARV (wsid,viewid,strid,1.0); /* link root to view */ GPUPWS(wsid,2); /* update workstation */ ----------------------------------------------------------------
The last two lines of code allow you to exit the program gracefully. The Request Choice (GPRQCH) subroutine tells the graPHIGS API to wait until a mouse button is pressed. Then, the Close graPHIGS (GPCLPH) subroutine closes the graPHIGS API and the program exits.
----------------------------------------------------------------- GPRQCH(wsid,2,&status,&choice); /* wait for mouse hit */ GPCLPH(); /* close graPHIGS */ -----------------------------------------------------------------