Tcl/Tk Cookbook - Tcl/Tk and C


Step 1: Create PHIGS related C routines.

Script

This routines below are not described as it is not directly relevant to this Tcl/Tk Cookbook. The PHIGS box with the text "It's a square world" in the routine "MakeCube" is adopted from A Practical Introduction to PHIGS and PHIGS Plus.


#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <phigs/phigs.h>
#include "tcl.h"
#include "tk.h"

#define CUBE (Pint) 1
#define WS_ID (Pint) 1
#define PRIORITY (Pfloat) 1.0
#define PI (Pfloat) 3.141592654
#define CMAX (Pfloat)0.7
#define CMIN (Pfloat)0.3

static Ppoint3 ORIGIN ={0.5,0.5,0.5};

#define set_colour(colrv,typ,r,g,b) colrv.type=typ; \
	colrv.val.general.x=r;colrv.val.general.y= g;colrv.val.general.z=b;

#define fill_struc(A,B,C,D) A.x = B; A.y = C; A.z = D;

extern Pconnid_x_drawable conn_id;
static char text1[] = "It's a ";
static char text2[] = "square world!";

int CleanupAndQuitProc (ClientData clientdata, Tcl_Interp *interp,
	 int argc, char *argv[])
{
pclose_ws(WS_ID);
pclose_phigs();
exit(0);
}

int MakeCubeProc (ClientData clientdata, Tcl_Interp *interp,
	 int argc, char *argv[])

{
Pfloat angle;
Pint err;
Pmatrix3 transform;
Pfloat mid;
Ppoint3 front_face[5], back_face[5],link1[2], link2[2],  link3[2], link4[2]; 
Ppoint_list3 list1,list2,list3,list4,list5,list6;
Pgcolr yellow,white;


static Pvec3 shift = {0.0,0.0,0.0};
static Pvec3 scale = {1,1,1};
static Ppoint3 textloc1 = {0.5,0.6,0.7};
static Ppoint3 textloc2 = {0.5,0.5,0.7};
static Pvec textdir[] = {1.0,0.0,0.0,0.0,1.0,0.0};
static Ptext_align txalign = {PHOR_CTR, PVERT_HALF};

set_colour(yellow,PMODEL_RGB,1.0,1.0,0.0);
set_colour(white,PMODEL_RGB,1.0,1.0,1.0);


fill_struc(front_face[0], CMIN,CMIN,CMAX);
fill_struc(front_face[1], CMIN,CMAX,CMAX);
fill_struc(front_face[2], CMAX,CMAX,CMAX);
fill_struc(front_face[3], CMAX,CMIN,CMAX);
ppolyline3(list6);
pset_text_align(&txalign);
pset_char_ht((Pfloat)0.015);
pset_text_colr(&yellow);
ptext3(&textloc1,textdir,text1);
ptext3(&textloc2,textdir,text2);
pclose_struct();
ppost_struct(WS_ID, CUBE, PRIORITY);
return 0;
}

int rotate_boxProc ( ClientData clientdata, Tcl_Interp *interp, int argc, char *argv[])

{ 
Pint val;
Pfloat angle;
Pint err, i;
Pmatrix3 transform;
Ppoint3 origin;
Pint cube;
static Pvec3 shift = {0.0,0.0,0.0};
static Pvec3 scale = {1,1,1};  
val = (Pint ) argv[1];

val = (Pint ) atoi(argv[1]);
angle =val*PI/180;

popen_struct(CUBE);               /* Open the structure. */
pset_edit_mode(PEDIT_REPLACE);     /* Select REPLACE mode. */
pset_elem_ptr(2);
pbuild_tran_matrix3(&ORIGIN, &shift, angle,angle,angle, 
                   &scale, &err, transform);
pset_local_tran3(transform, PTYPE_REPLACE); 
pclose_struct();
pupd_ws(WS_ID, PFLAG_PERFORM);
return 0;
}