1 00:00:00,000 --> 00:00:00,260 2 00:00:00,260 --> 00:00:02,980 >> SPEAKER 1: Let's now write a program that somehow interacts with my mouse. 3 00:00:02,980 --> 00:00:06,150 In particular, let's instantiate, or create a window. 4 00:00:06,150 --> 00:00:09,700 And then let's listen, so to speak, within that window for mouse clicks. 5 00:00:09,700 --> 00:00:12,940 And as soon as we detect a mouse click, let's print with printf the 6 00:00:12,940 --> 00:00:17,830 coordinates x comma y of that mouse click, relative to that window. 7 00:00:17,830 --> 00:00:18,880 >> Here we go. 8 00:00:18,880 --> 00:00:24,670 Let's first include gevents.h, which is another header file in the Stanford 9 00:00:24,670 --> 00:00:27,670 portable library that relates to graphical events. 10 00:00:27,670 --> 00:00:33,480 Let's also include gwindow.h, which contains functions related to Windows. 11 00:00:33,480 --> 00:00:37,390 Let's now declare main in the usual way. 12 00:00:37,390 --> 00:00:39,680 >> And let's now instantiate that window. 13 00:00:39,680 --> 00:00:40,830 Gwindow-- 14 00:00:40,830 --> 00:00:42,180 we'll call it window-- 15 00:00:42,180 --> 00:00:44,710 equals newGwindow. 16 00:00:44,710 --> 00:00:49,150 And I'll specify somewhat arbitrarily a width of 320 pixels and a height of 17 00:00:49,150 --> 00:00:51,460 240 pixels. 18 00:00:51,460 --> 00:00:54,380 >> Now we need to proceed to listen for mouse events, so to 19 00:00:54,380 --> 00:00:56,020 speak, within that window. 20 00:00:56,020 --> 00:00:59,450 Now events can include clicks or drags or movements. 21 00:00:59,450 --> 00:01:01,400 But for now we'll focus only on clicks. 22 00:01:01,400 --> 00:01:05,740 I'm going to intentionally induce an infinite loop with a while construct, 23 00:01:05,740 --> 00:01:08,770 simply so that this program runs forever, or at least until I click the 24 00:01:08,770 --> 00:01:10,450 X in the top right-hand corner. 25 00:01:10,450 --> 00:01:13,670 >> To do so, let's do while true. 26 00:01:13,670 --> 00:01:16,900 And then inside of that loop, lets first check for the 27 00:01:16,900 --> 00:01:18,430 mouse event as follows. 28 00:01:18,430 --> 00:01:21,200 We'll declare a GEvent, so to speak. 29 00:01:21,200 --> 00:01:22,920 >> We'll call the variable event. 30 00:01:22,920 --> 00:01:27,740 And we're going to store in that the return value of getNextEvent, passing 31 00:01:27,740 --> 00:01:31,970 in a special constant, which by convention is written in all caps, 32 00:01:31,970 --> 00:01:34,060 called MOUSE_EVENT. 33 00:01:34,060 --> 00:01:36,800 In other words, somewhere in this Stanford portable library, there's a 34 00:01:36,800 --> 00:01:39,120 constant called MOUSE_CLICED. 35 00:01:39,120 --> 00:01:43,040 And there is a function defined that's called getNextEvent, whose purpose in 36 00:01:43,040 --> 00:01:44,360 life is to do exactly that. 37 00:01:44,360 --> 00:01:48,330 Listen for, and then when it hears one, returns the next event that's 38 00:01:48,330 --> 00:01:50,500 been triggered by the user's mouse. 39 00:01:50,500 --> 00:01:53,710 >> Now let's check whether that event is null. 40 00:01:53,710 --> 00:01:57,400 Because it's not null, and that is we actually heard something from the 41 00:01:57,400 --> 00:02:00,450 user's mouse, we're going to proceed to check now what type 42 00:02:00,450 --> 00:02:03,001 of event that was. 43 00:02:03,001 --> 00:02:09,050 If getEventType, passing an event as an argument, equals equals 44 00:02:09,050 --> 00:02:14,070 MOUSE_CLICKED, which happens to be a specific type of events, which is just 45 00:02:14,070 --> 00:02:17,110 another constant declared in the Stanford portable library. 46 00:02:17,110 --> 00:02:21,400 Now as promised, let's now print out the coordinates of that mouse click. 47 00:02:21,400 --> 00:02:26,000 Printf " %.0f. 48 00:02:26,000 --> 00:02:28,340 >> In other words, I'm going to print out a floating point value. 49 00:02:28,340 --> 00:02:30,240 But I don't want to see anything after the decimal point. 50 00:02:30,240 --> 00:02:32,260 So I'll specify .0. 51 00:02:32,260 --> 00:02:36,380 Comma, %.0f for the y-coordinate as well. 52 00:02:36,380 --> 00:02:38,000 Backslash n close quote. 53 00:02:38,000 --> 00:02:41,260 >> And now let's get the x-coordinate of that event. 54 00:02:41,260 --> 00:02:43,530 That is the x-coordinate of the mouse click. 55 00:02:43,530 --> 00:02:46,990 And then let's get y of the event, which is the y-coordinate 56 00:02:46,990 --> 00:02:48,170 of the mouse click. 57 00:02:48,170 --> 00:02:50,920 >> Let's now save, compile, and run this program. 58 00:02:50,920 --> 00:02:54,160 And when I do, it should be the case that no matter where I click within 59 00:02:54,160 --> 00:02:58,850 the confines of this 320 by 240 pixel window, I should then see on my 60 00:02:58,850 --> 00:03:03,570 console window the x comma y-coordinates of where I clicked. 61 00:03:03,570 --> 00:03:05,430 Make click. 62 00:03:05,430 --> 00:03:07,890 ./click. 63 00:03:07,890 --> 00:03:09,630 And there's that window we predicted. 64 00:03:09,630 --> 00:03:14,300 >> Now let me click roughly in the top left-hand corner of the window. 65 00:03:14,300 --> 00:03:18,150 In my console screen, I see that I happen to click on x-coordinate seven 66 00:03:18,150 --> 00:03:19,700 and y-coordinate seven. 67 00:03:19,700 --> 00:03:22,840 Let's now click roughly in the bottom right-hand corner of the window. 68 00:03:22,840 --> 00:03:27,890 And I seem to have clicked on pixel coordinate 314, 229. 69 00:03:27,890 --> 00:03:30,330 >> Let's now do the bottom left corner. 70 00:03:30,330 --> 00:03:32,430 And I see 6, 233. 71 00:03:32,430 --> 00:03:35,680 And top right corner, 305, 4. 72 00:03:35,680 --> 00:03:38,810 >> In other words, it seems to be the case that the top left-hand corner of 73 00:03:38,810 --> 00:03:40,655 our window is 0, 0. 74 00:03:40,655 --> 00:03:46,080 And the bottom right-hand corner of my window is 320, 240. 75 00:03:46,080 --> 00:03:48,990 In other words, I would have seen precisely those values, had I just 76 00:03:48,990 --> 00:03:52,180 perfectly aligned my mouse cursor in each of those corners. 77 00:03:52,180 --> 00:03:54,960 This is true in general in graphical programming, whether using the 78 00:03:54,960 --> 00:03:58,860 Stanford portable library or most any other, whereby we represent the top 79 00:03:58,860 --> 00:04:03,050 corner as 0, 0, and the bottom right-hand corner as the width comma 80 00:04:03,050 --> 00:04:06,810 height, using positive numbers, even though it's down and to the right. 81 00:04:06,810 --> 00:04:08,615