/* November 2000 * Brian J.Ross * Brock University, Dept of Computer Science * COSC 3P98 Midterm solution */ #if !defined(Linux) #include //Not Linux must be windows #endif #include #include #include //#include //#include //#include #include #define MINDIM 0 #define MAXDIM 500 #define MYRANDOM ((float) rand()/RAND_MAX) struct glob { int x, y, mode; }; struct glob global = {250, 250, 0}; void my_clear(void) { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void keybd(unsigned char key, int x, int y) { switch (key) { case 'q': case 'Q': exit(0); break; } } void scribble(void) { int x2, y2; x2 = global.x; y2 = global.y; switch (global.mode) { case 0: // blue right x2 += (int) (MYRANDOM * (MAXDIM-x2)); glColor3f(0.0, 0.0, 1.0); break; case 1: // red up y2 += (int) (MYRANDOM * (MAXDIM-y2)); glColor3f(1.0, 0.0, 0.0); break; case 2: // green left x2 -= (int) (MYRANDOM * (x2 - MINDIM)); glColor3f(0.0, 1.0, 0.0); break; case 3: // black down y2 = (int) (MYRANDOM * (y2-MINDIM)); glColor3f(0.0, 0.0, 0.0); break; } glBegin(GL_LINES); glVertex2i(global.x, global.y); glVertex2i(x2, y2); glEnd(); if (++global.mode > 3) global.mode=0; global.x = x2; // save for next time around global.y = y2; } int main(argc, argv) int argc; char **argv; { glutInit(&argc, argv); glutInitWindowSize(MAXDIM, MAXDIM); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutCreateWindow("window"); glutDisplayFunc(my_clear); glutIdleFunc(scribble); glutKeyboardFunc(keybd); glMatrixMode(GL_PROJECTION); gluOrtho2D(MINDIM, MAXDIM, MINDIM, MAXDIM); glClearColor (1.0, 1.0, 1.0, 1.0); glutMainLoop(); return 0; }