 Hey, how's it going everybody? Welcome back to some C++ SDL video tutorial series stuff. I'm Rudolf Dennell, I hope you're enjoying the series so far. In this video, well, anyway to review the last video, we finally had our code create a simple window. And it didn't do much of anything other than just display for a few seconds and then immediately go away. But you'll notice we had a 01 basic win.cpp file, and when we ran the code, we'd have a window show up. The thing is we were only delaying its presence or for how long it would actually exist and have the code be visible. And that is nice, but how are we going to have the window stay up for more time than that? I'm going to go ahead and recompile what I had here beforehand anyway, just so we have that line package config. Config, come on, come on honey, honey, what are you doing? Package config, libs, and C flags, SDL 2, and then I'll actually end that in bash with edit out. So every time, yeah, we can just keep that command. So we have our window show up, but after five seconds, it kind of goes away. Well, how do we keep it there? And this topic actually kind of gets to the idea of a game or programs kind of main event loop. And the system or the loop that continues over and over again so the window actually stays alive. And we'll get into that, of course, by looking at the documentation. So in the last video, we were looking at display and window management. In this time, we're going to go back to the API by category quicklink, kind of skim through here, see what we've got. And you'll notice under input events, we've got event handling. And event handling is what we want to be getting into. It allows your application to receive input from the user, no matter anything that you do, anything that you do. If you move your mouse, if you press keys on your keyboard, if you, I don't know, I can't think of any better examples on that. That's just what you do. If you do this, your program will receive events. And even closing a window or minimizing the window or dragging the window around, that's an event. So closing the window is crucial. If we wanted to be able to close the program by closing the window, we need to be able to do that. So we have to process this in our code. It happens by storing all the events in a specific event queue. Using functions like SDL poll event, which is what we're going to be using. SDLP events and SDL wait events, we can get into more of those as we continue or if you want to look at them. You can observe and handle waiting input events. Now, that itself, the event queue is composed of a series of SDL event structures. SDL event types. SDL event structures are read from the queue with the SDL poll event function. And then it's up to the application to process the information stored with them or see what is really going to happen. So with that description, let's check out what we're really doing here. We need SDL event and we need the SDL poll event, those functions and those variables. SDL poll event will take actually an SDL event pointer. And that's going to be the event that we use. It will return one if there's a pending event or zero if there are none available. And the way that that is set up is by, okay, while one or all the time, it's an infinite loop because one is always going to equal one. One is always true. That's going to keep repeating over and over and over again. We're going to have our event and while this SDL poll event is actually being processed from that function, you can handle the events and do what you want. You can do some other stuff there and that sort of thing. Let's try this in our code. Let's actually keep the window alive. I'm going to go ahead and try to clean up this by just code folding some of the things that we've got here and kind of tightening our display in our code anyway. And looking at this example, I'm not going to declare the SDL event inside the initial while loop or the main loop. I'm actually going to just clear that out of there. SDL event event. I'm just going to call the variable event just to keep things easy. And we also need another variable, which is going to be a boolean, and I'm going to use a variable running, which is going to be equal true, which kind of is going to be a placeholder for this while one that they use in the documentation. While running, in this case, as in while the program is running, what we're going to do is we're going to poll the event. You can see in the example over there it used while SDL poll event. And remember, this function takes a pointer over here. So we actually need to pass in the reference value for event. And the way we do that is the ampersand, the shift version of the function of sorry of the seven key on your keyboard. We pass an event and that it processes that. Okay, cool. What we'll do is we'll say if the event, once it has been once it has been kind of pulled and processed, we can test if the event dot type is equal to something else. We actually get to determine what the kind of event it is. SDL event has lots of fascinating things. It's a union that contains structures for the different event types. And there are a lot of different types. There are window events, there are keyboard events, there are mouse motion events, and plenty of things here. Quit event is what we're going to be looking at. And let's actually take a look at that over here. Structure that contains the quit requested event. Now you can see the type is this flag at this constant here, SDL quit. It doesn't give any example or anything, but let's keep hunting. If we go back, window manager event, drop event, pull event, test event. While we're pulling that event, just keep scrolling. We can even use user events. Those are cool to know. We'll have to look at that later, or at least you can do that on your own. Okay, there we go. SDL quit. There's that flag for SDL quit event. That's the type that it is. And that's just simply quit. It doesn't give any example, but I'm sure we can kind of figure out how to do it. We're just testing if the event dot type. I'm disappointing it doesn't show that variable. Okay, no, it does type right here, variable type. This table below. Okay, right here. And again, this links us right back over here. All right, I'm moving around in circles and avoiding the real thing here. We just want to use SDL, anyway, in the SDL library, event dot type. If the SDL dot event dot type is equal to SDL underscore quit, what we will do is, in our case, we'll set running to equal false. I keep thinking I'm in Python, sorry. I keep capitalizing my true and false values, and we'll just break out of this loop so we don't have to keep doing that again. Cool. So nothing is really going to happen in this while loop, but right here, this is where all the event testing actually happens. Let's go ahead and see how this compiles, or even if it does. And over to the terminal, right now, damn it, this is still basicwindow.cpp. I should save this as a different thing, 02 underscore mainloop.cpp. I really need to get in the habit of that, hop back over to our terminal here, and I'm going to rename this to 02 mainloop.cpp. We can run this code and my window appears. It compiles and it's ran just fine, and the window will never disappear. Until, of course, I hit this close button right up here. If I close this, sweet, the program ends. That works just fine for us. That works exactly how we wanted it to. We have the main loop in our code, and the way that's happening is through the SDL event type, SDL pole event function, and testing the event type in the loop that we have here in the main loop. Super duper cool. That's how it all works. Thanks for watching, guys. Hope you enjoyed this, and be sure to check out the documentation and know what other things you can do with this. It's awesome and crucial, and it's very exciting to see the world at your fingertips. Thank you again, guys, and I'll see you in the next tutorial.