Here's a little bit of (heavily) commented code that simply sets up the screen and draws a line. This should be more than enough to get you started (it's almost how I started).

        /* necessary headers */
        #include <u.h>
        #include <libc.h>
        #include <draw.h>
        #include <cursor.h>

        /* main routine */
        void main(int argc, char *argv[])
        {
                int c;
                void draw_line(void);

                /* int initdraw(err_func, default_font, program_name)
                        If err_func is 0, use default
                        If default_font is 0, use default */
                if (initdraw(0, 0, "program name") < 0)
                        sysfatal("could not initialize graphics");
                draw_line();
                for (;;) /* not the best way to wait for a key hit
                                I don't want to go into events here */
                        if (read(0, &c, 1) == 1)
                                break;
                exits(0); /* graphics is automatically terminated */
        }

So here's how you draw a line:

        void draw_line(void)
        {
/* this is how to create a color - see allocimage(2) for a complete list */
                Image *black_color = allocimage(display, Rect(0, 0, 1, 1),
                        RGB24, 1, DBlack);
                Point a = Pt(3, 2), b = Pt(400, 900);

                /* line(destination, from, to, from_end_type, to_end_type, 
thickness,
                        source_color, point_to_use);
                                To get the thickness of the line, use the 
equation
                                        1 + (2 * thickness)
                                In this case, the thickness is 1.
                                Other choices for end types are Enddisc and 
Endarrow.
                                Plan 9 is more powerful than this, however. You 
can take any
                                color from any image or create more elaborate 
arrows with
                                ARROW() as the type parameter. You can even mix 
and match
                                types. But this is more advanced. Refer to 
draw(2).
                */
                line(screen, a, Endsquare, Endsquare, 0,
                        black_color, Pt(0, 0));
        }

Just stick that in the code section.

On Nov 2, 2007, at 3:12 PM, Anant Narayanan wrote:

Pietro Gagliardi wrote:
cover in a clear way, so I think a tutorial should be put in. I already
started writing one, and I think it would benefit from being in
    - graphics and controls

While I am mostly able understand the other concepts you mention from
existing documentation, I find graphics (libdraw) to be somewhat
cryptic. I've always felt the need for a tutorial-style introduction on
how to do graphics in Plan 9.

The current solution seems to be mostly be: "Read the code". Which isn't
really as good.

Reply via email to