#include #include // prototypes void update_pen_color(int &c); void draw_pixel(int c); // constants // (Note: This is an alternative way of defining constants instead of // using "const int".) #define XDIM 500 #define YDIM 400 #define PEN_UP -1 int main() { int pen_color = PEN_UP; // PEN_UP means no drawing will be performed initwindow(XDIM, YDIM); // open graphics window do { update_pen_color(pen_color); draw_pixel(pen_color); } while (!kbhit()); // loop until user hits a key } // This routine sets the color to red as long as the left mouse button // is held down, and the color to blue as long as the right mouse button // is held down. If both buttons are held down, the color becomes magenta // (= red + blue). // // The "if" statements check to see if a mouse button press or release // has occurred since this routine was last called. After the color is // adjusted appropriately, the flag is cleared to indicate that the mouse // button press/release has been processed. void update_pen_color(int &c) { if (ismouseclick(WM_LBUTTONDOWN)) { c = (c == PEN_UP ? RED : MAGENTA); clearmouseclick(WM_LBUTTONDOWN); } if (ismouseclick(WM_LBUTTONUP)) { c = (c == RED ? PEN_UP : BLUE); clearmouseclick(WM_LBUTTONUP); } if (ismouseclick(WM_RBUTTONDOWN)) { c = (c == PEN_UP ? BLUE : MAGENTA); clearmouseclick(WM_RBUTTONDOWN); } if (ismouseclick(WM_RBUTTONUP)) { c = (c == BLUE ? PEN_UP : RED); clearmouseclick(WM_RBUTTONUP); } } // This routine colors a pixel at the current mouse location using // the current pen color. void draw_pixel(int c) { int mx, my; if (c != PEN_UP) // don't draw if the pen is lifted up (no mouse { // buttons are pressed) // get mouse location mx = mousex(); my = mousey(); // color pixel at mouse location with that color putpixel(mx, my, c); } }