Previous Next Up Title Contents Index Top Library

7.4. EXAMPLE

Let us examine a detailed example. We take a program that uses PHIGS to draw a line and performs a continuous rotation of this line. We carry out the same actions, but add toolkit calls for Motif support.

This program demonstrates animation with GPHIGS. It does not have a user interface. (See the equivalent version of this program demonstrating the addition of a user interface using the Motif widget

toolkit.)

/*
 * Standard include
 */

#include "phigs.h"
#include <math.h>

#define WSID1 1

main (argc, argv) 
	int argc ; 
	char **argv;
{
	static Ppoint pt[2] = { {0.3,0.3},{0.6,0.7} };
	static Ppoint_list pt_list={2,pt};
	static double angle=0; 
	/* Open PHIGS */
	popen_phigs(PDEF_ERROR_FILE,PDEF_MEM_SIZE);

	/* Open workstation No 1, type X11 */
	popen_ws(WSID1,"/dev/tty",8887);

	/* Open structure No 1 */  
	popen_struct(1);
	pset_line_colr_ind(3);
	ppolyline(&pt_list);
	pclose_struct();

	ppost_struct(WSID1,1,1.);
	predraw_all_structs(WSID1,PFLAG_ALWAYS);

	/* Animation loop */
	for ( ; ; ) {
		pt[0].x = (float)cos(angle);
		pt[0].y = (float)sin(angle);
		pt[1].x = -pt[0].x;
		pt[1].y = -pt[0].y;
		pt[0].x = pt[0].x/3. + .5;
		pt[0].y = pt[0].y/3. + .5;
		pt[1].x = pt[1].x/3. + .5;
		pt[1].y = pt[1].y/3. + .5;
    
		angle += 3.14/180.;
		if (angle >= 6.28) angle = 0.;
    
		popen_struct(1);
		pset_elem_ptr(2);
		pdel_elem();
		ppolyline(&pt_list);
		pclose_struct();

		predraw_all_structs(WSID1,PFLAG_ALWAYS);
	}

	/* Close workstation */
	pclose_ws(WSID1);

	/* Close PHIGS */
	pclose_phigs();
}

This program is the equivalent of the demo animation program. A user interface has been added by creating a GPHIGS widget using the Motif widget toolkit.

/* Standard include */

#include <stdio.h>
#include <math.h>
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/PushB.h>
/* phigs.h is in GPhigsW.h */
#include "GPhigsW.h"

#define WSID1 1
/* Callback for the redraw  Creation of the CSS */

void redraw_CB(w, client_data, call_data)
	Widget w;
	caddr_t client_data, call_data;
{
	Pint wsid = (int)client_data;
	static Ppoint pt[2] = {{0.3,0.3},{0.6,0.7}};
	static Ppoint_list pt_list={2,pt};

	/* Draw a polyline */
	popen_struct(1);
	pset_line_colr_ind(3);
	ppolyline(&pt_list);
	pclose_struct();

	ppost_struct(wsid,1,1.);
	predraw_all_structs(WSID1,PFLAG_CONDI);
}

/* User animation function */

animation()
{
	static Ppoint pt[2];
	static double angle=0; 
	static Ppoint_list pt_list={2,pt};

	pt[0].x = (float)cos(angle);
	pt[0].y = (float)sin(angle);
	pt[1].x = -pt[0].x;
	pt[1].y = -pt[0].y;
	pt[0].x = pt[0].x/3. + .5;
	pt[0].y = pt[0].y/3. + .5;
	pt[1].x = pt[1].x/3. + .5;
	pt[1].y = pt[1].y/3. + .5;

	angle += 3.14/180.;
	if (angle >= 6.28) angle = 0.;

	popen_struct(1);
	pset_elem_ptr(2);
	pdel_elem();
	ppolyline(&pt_list);
	pclose_struct();

	predraw_all_structs(WSID1,PFLAG_ALWAYS);

}
/* MAIN */
main (argc, argv) 
	int argc ; 
	char **argv;
{
	Widget         toplevel, r1, r3, form;
	Arg            args[50];
	int            n,i     ;

	/* Toolkit initialization*/
	toplevel = XtInitialize (argv [0], "Test", NULL, 0, &argc, argv);
	/* Open GPHIGS */
	popen_phigs("/dev/tty",-1);

	/* Creation of widget background for the application */
	/* (not required) */
	n = 0;
	XtSetArg(args[n], XmNwidth    , 600 ); n++;
	XtSetArg(args[n], XmNheight   , 600 ); n++; 
	XtSetArg(args[n], XmNsensitive,True ); n++;
	form = XtCreateWidget("form", xmFormWidgetClass, toplevel, args,
						 n);
	XtManageChild(form);
  
	/* Creation of GPHIGS widget No 1 */
	n = 0;

	/* Position of GPHIGS widget in the application form */
	XtSetArg(args[n], XmNx, 10); n++;
	XtSetArg(args[n], XmNy, 10); n++;	

	/* Size of GPHIGS widget in the form */
	XtSetArg(args[n], XmNwidth, 200 ); n++; 
	XtSetArg(args[n], XmNheight,200 ); n++;
 
	/* Size of the workstation = graphic window */
	/* ( default 50x50 ) */
	XtSetArg(args[n], XmNwidthWs, 400 ); n++; 
	XtSetArg(args[n], XmNheightWs,400 ); n++; 

	r1 = XtCreateManagedWidget ("GPHIGS", xmGPhigsWidgetClass, form, args, n);

	/* Redraw as soon as the window is mapped */
	XtAddCallback(r1, XmNcreateDACallback, redraw_CB, WSID1 );

	/* Animation button */
	n = 0;
	XtSetArg(args[n], XmNx, 350); n++;
	XtSetArg(args[n], XmNy, 500); n++;
	r3 = XtCreateManagedWidget("ANIM",
				xmPushButtonWidgetClass, form, args, n);

	/* Callback for animation */
	XtAddCallback(r3,XmNactivateCallback,GPHIGS_ANIM_CB,
				animation);  

	XtRealizeWidget  (toplevel);
	XtMainLoop();
}


Previous Next Up Title Contents Index Top Library