This new version of the winbgi graphics files allows you to get input from the mouse. It also allows you to use 64 different colors in the palette. In order to use this version, you need these files:Here's what's new in this version:
- winbgi2.cpp - Contains graphics functions (Don't change)
- graphics2.h - Contains header info (Don't change)
- blank_graphics2.cpp - An example program to use.
Every click on the window generates two Windows events: the WM_LBUTTONDOWN then the WM_LBUTTONUP, corresponding to when the user clicks down, then when they let go of the mouse button. It's typical to interpret the Mouse Up event as having "clicked" on something. In other words, if you want to check for something being clicked, just use the mouseup() function.
- The initgraph() function now allows you to specify the beginning size of the graphics window.
- The mouseup() and mousedown() functions each return TRUE once if the mouse was clicked in the window.
- The mouseclickx() and mouseclicky() functions return the X and Y values of where the mouse was clicked.
- The mousecurrentx() and mousecurrenty() functions return the X and Y position of the mouse.
- The whichmousebutton() returns either LEFT_MOUSE or RIGHT_MOUSE (Only after a mouseup or mousedown event)
The best way to see how to use these new functions is by example. See the listing below.
Note: This demo also contains an example of how to print values to the screen. Here are two more examples:
sprintf(tempstring, "%d", iSize); //Prints the value of integer iSize
outtextxy(20, 20, tempstring);sprintf(tempstring, "%f", fSpeed); //Prints the value of float fSpeed
outtextxy(20, 20, tempstring);
Special thanks to Erik Habbestad, who helped figure out how to increase the palette size, and how to intercept the Windows mouse events.//Finally, here's the listing of the mouse_demo.cpp program
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "graphics2.h"
#define ESC 0x1b
void main()
{
srand(time(NULL)); // Seed the random number
generator
int
GraphDriver=0,GraphMode=0;
initgraph( &GraphDriver, &GraphMode, "", 640, 480 );
// Start Window
char
tempstring[80];
int mouseX, mouseY;
settextstyle(GOTHIC_FONT, HORIZ_DIR,
1);
outtextxy(0, 0, "Click the
mouse... Press any key to move on.");
// Simple example, looking for a mouse click
while (!kbhit()) {
if (mouseup()) {
circle( mouseclickx(), mouseclicky(),
10);
}
delay(100);
}
getch();
// Tracking the movement of the mouse
moveto(mousecurrentx(),
mousecurrenty());
while (!kbhit())
{
lineto(mousecurrentx(),
mousecurrenty());
delay(20);
}
getch();
//More complicated example, also demonstrates printing numbers on screen
clearmouse(); //Flush all the mouse input
while (!kbhit()){ // Keep going until a key is hit
if (mousedown()) {
mouseX = mouseclickx();
mouseY = mouseclicky();
sprintf(tempstring, "Down at: (%d,
%d) ", mouseX, mouseY);
outtextxy(20, 20, tempstring);
if (whichmousebutton() ==
LEFT_BUTTON) {
outtextxy(400, 20, "left button ");
} else {
outtextxy(400, 20, "right button");
}
}
if
(mouseup()) {
mouseX =
mouseclickx();
mouseY =
mouseclicky();
sprintf(tempstring, "Up at: (%d,
%d) ", mouseX, mouseY);
outtextxy(20, 60, tempstring);
if (whichmousebutton() ==
LEFT_BUTTON) {
outtextxy(400, 60, "left button ");
} else {
outtextxy(400, 60, "right button");
}
}
delay(100);
}
getch(); //Wait for a key. (When main function
ends, the window will close)
}