>> SPEAKER 1: Let's now write a program that somehow interacts with my mouse.
In particular, let's instantiate, or create a window.
And then let's listen, so to speak, within that window for mouse clicks.
And as soon as we detect a mouse click, let's print with printf the
coordinates x comma y of that mouse click, relative to that window.
>> Here we go.
Let's first include gevents.h, which is another header file in the Stanford
portable library that relates to graphical events.
Let's also include gwindow.h, which contains functions related to Windows.
Let's now declare main in the usual way.
>> And let's now instantiate that window.
Gwindow--
we'll call it window--
equals newGwindow.
And I'll specify somewhat arbitrarily a width of 320 pixels and a height of
240 pixels.
>> Now we need to proceed to listen for mouse events, so to
speak, within that window.
Now events can include clicks or drags or movements.
But for now we'll focus only on clicks.
I'm going to intentionally induce an infinite loop with a while construct,
simply so that this program runs forever, or at least until I click the
X in the top right-hand corner.
>> To do so, let's do while true.
And then inside of that loop, lets first check for the
mouse event as follows.
We'll declare a GEvent, so to speak.
>> We'll call the variable event.
And we're going to store in that the return value of getNextEvent, passing
in a special constant, which by convention is written in all caps,
called MOUSE_EVENT.
In other words, somewhere in this Stanford portable library, there's a
constant called MOUSE_CLICED.
And there is a function defined that's called getNextEvent, whose purpose in
life is to do exactly that.
Listen for, and then when it hears one, returns the next event that's
been triggered by the user's mouse.
>> Now let's check whether that event is null.
Because it's not null, and that is we actually heard something from the
user's mouse, we're going to proceed to check now what type
of event that was.
If getEventType, passing an event as an argument, equals equals
MOUSE_CLICKED, which happens to be a specific type of events, which is just
another constant declared in the Stanford portable library.
Now as promised, let's now print out the coordinates of that mouse click.
Printf " %.0f.
>> In other words, I'm going to print out a floating point value.
But I don't want to see anything after the decimal point.
So I'll specify .0.
Comma, %.0f for the y-coordinate as well.
Backslash n close quote.
>> And now let's get the x-coordinate of that event.
That is the x-coordinate of the mouse click.
And then let's get y of the event, which is the y-coordinate
of the mouse click.
>> Let's now save, compile, and run this program.
And when I do, it should be the case that no matter where I click within
the confines of this 320 by 240 pixel window, I should then see on my
console window the x comma y-coordinates of where I clicked.
Make click.
./click.
And there's that window we predicted.
>> Now let me click roughly in the top left-hand corner of the window.
In my console screen, I see that I happen to click on x-coordinate seven
and y-coordinate seven.
Let's now click roughly in the bottom right-hand corner of the window.
And I seem to have clicked on pixel coordinate 314, 229.
>> Let's now do the bottom left corner.
And I see 6, 233.
And top right corner, 305, 4.
>> In other words, it seems to be the case that the top left-hand corner of
our window is 0, 0.
And the bottom right-hand corner of my window is 320, 240.
In other words, I would have seen precisely those values, had I just
perfectly aligned my mouse cursor in each of those corners.
This is true in general in graphical programming, whether using the
Stanford portable library or most any other, whereby we represent the top
corner as 0, 0, and the bottom right-hand corner as the width comma
height, using positive numbers, even though it's down and to the right.