[comp.windows.x] Naive C bindings for X graphics?

km@mathcs.emory.edu (Ken Mandelberg) (09/18/90)

We use workstations running X as the environment for teaching our CS
curriculum, including first level programming courses. In an introductory
C course, the X programming model, even with the popular toolkits are
out of reach for this group.

Still being able to do some simple graphics from C programs would be
very motivational. Has anyone done an X graphics library with a very
simple minded interface? I have in mind something like the graphics support
that goes with popular microcomputer Basic and Pascal interpreters.

-- 
Ken Mandelberg      | km@mathcs.emory.edu          PREFERRED
Emory University    | {rutgers,gatech}!emory!km    UUCP 
Dept of Math and CS | km@emory.bitnet              NON-DOMAIN BITNET  
Atlanta, GA 30322   | Phone: (404) 727-7963

erc@pai.UUCP (Eric Johnson) (09/19/90)

In article <1990Sep18.104958@mathcs.emory.edu>, km@mathcs.emory.edu (Ken Mandelberg) writes:
> We use workstations running X as the environment for teaching our CS
> curriculum, including first level programming courses. In an introductory
> C course, the X programming model, even with the popular toolkits are
> out of reach for this group.

X programming is difficult. Period. But, X does do a lot of nice things,
so you win some, you lose some.


> Still being able to do some simple graphics from C programs would be
> very motivational. Has anyone done an X graphics library with a very
> simple minded interface? I have in mind something like the graphics support
> that goes with popular microcomputer Basic and Pascal interpreters.
> 
> -- 
> Ken Mandelberg      | km@mathcs.emory.edu          PREFERRED
> Emory University    | {rutgers,gatech}!emory!km    UUCP 
> Dept of Math and CS | km@emory.bitnet              NON-DOMAIN BITNET  
> Atlanta, GA 30322   | Phone: (404) 727-7963

I've done some work in this area, but its for my employer.  Anyway,
you won't find it too hard to create a simple Xlib interface.
That is, a set of C functions that provide a layer between your
students' code and Xlib.

If you can make some assumptions about the environment, your job becomes
a lot easier. For a simple graphics library, I'd assume (or mandate):
   * Each application will connect to only one server.
   * Each application will have only one X window.
   * Each application will have only one graphics context,
     but users should be able to change colours and pen width.
   * The number of fonts available will be limited. (One should even
     do fine).
   * The number of colours available will be limited (so you
     don't have to mess too much with visuals and whatnot).

I think these are fairly reasonable assumptions for students learning
Computer Science. After all, they're learning CS, not X.

What you can do is:

1) Create an initialization function. This function should:
    a) Set up a connection to X server.
    b) Load any fonts.
    c) Create an application window. (In a very simple library,
    you could force all your students' programs to run with
    only one window.)
    d) Map the application window.
    e) Allocate any colours you want to use.
    To make sure everything is on the screen, you may want to call
    XSync() here.

2) Create a set of drawing functions. You'll probably find a turtle
graphics model is easiest to learn. That is, create a function to
move the pen and then another function to draw from there.
Some examples (with functions familiar to Mac people):

	int	global_x, global_y;

	MoveTo( x, y )
	int	x, y;
	{
		global_x = x;
		global_y = y;
	}

	LineTo( x, y )
	int	x, y;
	{
		XDrawLine( global_display,
			global_window,
			global_gc,
			global_x,
			global_y,
			x, y );

		XFlush( global_display );
		
		global_x = x;
		global_y = y;
	}

3) Create more functions to set line width and change colours.
You probably want to create a set of colours and give each colour
a name or number. Then, let your library translate that into
X-based GC calls.

4) You can handle Expose events by drawing everything to a pixmap
and to the window. Then, on an Expose event, copy parts of the
pixmap to the window.

Anyway, you won't find it too hard to create a library
that insulates your students from X. They won't be able to
do everything that X allows, but they probably don't want to
(at least not yet).

I suspect you could do fine with these limited output functions
(input is left as an exercise for the reader):
	InitGraphics()
	MoveTo( x, y )
	LineTo( x, y )
	PenSize( new_pen_width )
	PenColor( foreground_colour )
	PenBackColor( background_colour )
	RectTo( x, y )
	OvalTo( x, y )	/* using a bounding rectangle */
	Text( string )
        EndGraphics()

Hope this helps,
-Eric

-- 
Eric F. Johnson               phone: +1 612 894 0313    BTI: Industrial
Boulware Technologies, Inc.   fax:   +1 612 894 0316    automation systems
415 W. Travelers Trail        email: erc@pai.mn.org     and services
Burnsville, MN 55337 USA