Assignment 5 - Due Thr. Mar. 3 (by 5:00 pm)

No hardcopy needed - Please e-mail your file as an attachment

A simple drawing program

At the botom of this document, I have listed a primitive drawing program which lets the user draw curves, but loses the display upon refresh (as when we visit another program and then return to this window).

Modify the program to include these features:

Email your source code to me as an attachment by 5:00 pm Monday 3/3. Please make sure your name is on the program.

Here's a screen shot of typical menus:

You'll need some appropriate data structures. I'd recommend an array of colors and a global variable for the current color index (set by the color menu) rather than setting the color directly through the menu. You'll have to associate a color with each curve drawn, and an index is more economical than a color vector. Also, you'll need a large array of points and/or curves; you can assume some upper bound on the number of points/curves, but you should check against overflow.

Since your image will be a collection of curves, each with a fixed color and width, it would be wasteful to associate a color and width with every single point -- try to think of some way to store the instances where colors and widths change. Also, check that if you draw two curves and the screen is redrawn, the curves are not connected to each other by mistake.

Let's agree that the user will not connect more than 1000 points -- this allows you to use static arrays. Just have the program quit drawing if the limit is reached.

And here's the current program:

/* A simple curve drawing program. This program 
Illustrates mouse, mouse motion callback functions

#include <GL/glut.h>

int squarecolor=1;
int anchorx,anchory;
int WindowWidth=500;
int WindowHeight=500;

void myinit(void)

        /* attributes */

        glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */

        /* set up viewing */
        /* 500 x 500 window with origin lower left */

        gluOrtho2D(0.0, 500.0, 0.0, 500.0);

void display( void )
        glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */
        glFlush(); /* clear buffers */

void mouse(int button,int state,int x,int y)
        y=500-y; // convert system orientation

void motion(int x, int y)
        y=500-y; // convert system orientation
        glColor3f(1,0,0); /* draw in red */
        anchorx=x; // update anchor point


void main(int argc, char** argv)

        glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
        glutInitWindowPosition(0,0); /* place window top left on display */
        glutCreateWindow("Draw"); /* window title */
        glutDisplayFunc(display); /* display callback invoked when window opened */
        myinit(); /* set attributes */

        glutMainLoop(); /* enter event loop */
You'll be modifying this code to implement color selection and a display function which resurrects everything you've drawn, including curves, colors, and widths.