 Hello everybody and welcome back to some C++ SDL programming tutorials. I'm your host, Root of the Null, and in the last video we were looking at some of the cool and nifty window functions that we could use with SDL. In fact, I ended off the code, at least I ended off the video anyway, with some bad code that I ended up freezing my gnome session and that sort of thing. So, yeah, in this video we're going to move on from that. I'm going to save a new copy of this and you'll notice I have a lot of code things already kind of prepared. So, I'm going to save this as 04 and you'll notice that that's already existing, changing the color, changing the color actual, is what I want to call this, 04A changing the color, how about that, changing the color, CPP. I should kind of get a terminal ready that I don't actually have ready for us. Okay, I have a new monitor setup so you guys might have to put up with me actually using the Windows key and changing my windows gnome shell rather than just using the Alt tab. But, regardless, let's move on. In this video, like I said, we're going to be changing the color of our screen. I'm going to go ahead and delete some of the stuff we were playing with in the last tutorial. We don't need all these integer values. We don't need these functions that will do stupid things for us, especially the things that will break our gnome shell. And then let's go ahead and continue. Above our SDL event declaration was where I'm going to start to type my code. And I'm going to show you guys the new function that we're going to end up using. And, first of all, we need to actually grab the screen or the surface that is being displayed in the window that we're using. We have this function SDL create window and that's wonderful. It will create a window for us. But the window isn't really what we're drawing things onto when we modify our program and we play with SDL. It's actually a surface and a surface is just that. It's kind of a surface that you can draw or blit in this case. Blit is to actually make things output and display on the screen. And that's what we're going to be using to interact with the display that we have for our program. So that is an SDL surface. And typically all SDL surfaces or anything SDL really kind of stores is a pointer. So I'm going to call mine screen. That's the variable that I'm going to use. And we're going to set this equal to a new function. And in fact, we're going to check out the documentation for this. Head on over to wiki.libsdl.org. And what I'm actually going to use is SDL get window surface. Hopefully this will find it for us. And yeah, okay, here we go. SDL get window surface. Use this function to get the SDL surface associated with the window that we're using. And you'll note this is very, very simple. What it returns is the SDL surface pointer, like we're going to be setting that up with. And the only thing that happens, or the only thing we really need to pass into this function, is the window variable and the pointer to it. And we know that SDL window is already something that we've created up here. And in fact, note up at the top here, we have this line SDL window, and that's a pointer to the window variable. This right here, this declaration of a pointer is kind of bad practice. It being a pointer, you should always set it to something. And I'm just going to go ahead and set it equal to null to begin with. And of course we're going to redefine it down here. But it's good practice to kind of set all your pointers to null whenever you're working with C++. So just take note of that and we'll hopefully have a bit more code where we set our pointers to null if they don't have a value yet. And looking back at our documentation over here, the function parameters are the window to carry, window to query, sorry, and it returns the surface associated with the window or null on failure. And SDL get error will do just fine for us. Very, very simple function. Check out the remarks. A new surface will be created with the optimal format from the window if necessary. The surface will be freed when the window is destroyed. So we don't really have to free the surface ourselves. Whenever we create a surface in SDL, there is a corresponding function called SDL free surface that we would normally pass in a surface variable name. And then it would kind of remove that variable in memory. It does kind of the same thing as destroy window or SDL quit. It's just a good kind of cleanup function. So we'll do that with a lot of other services that we actually end up creating. But for now, all we need to worry about is the screen. So I've been kind of beating around the bush for this topic for a while now. Let's just go ahead and set this to equal. SDL get window surface. And we'll pass in the window variable. And we already know that it's a pointer, so we don't have to do that at all. We just use window right here. Okay, cool. So now we can actually work with this screen. We can work with the display that we have here. I'll fire up the terminal and let's see what we can do. We're going to see work SDL G plus plus. It was zero for a change in the color and we want package config. Libs and C flags. SDL two. They should compile without a problem. Please, please, please. It does great. Now we can run it out. I'll bring this window down and who we have our black box. Subscribe and very, very simple. Now that we have created this screen variable, this pointer to an SDL surface, we can manipulate it. We can do things to it. And the way that we're going to change the color is by storing a value for the color, a variable that we can use over and over again. I'm just going to call mine whites. That's all it's going to be. And of course, this variable needs a type. Now, in SDL, you're going to be using a different type than you're kind of used to for some SDL created stuff. Okay. This type is an unsigned integer that has 32 bits. So all of this stuff is kind of combined into you int 32. That's the type that we're going to be using. Okay. Now that I've gotten that out of the way, keep that in your mind. Keep that really in your back pocket. Anyway, that's the type that we're going to be using a lot of the time. And this is the type that the color is going to be. So you went 32 whites. What that is equal to is another function that we can check out in the documentation. This one is SDL map RGB. Now, if I search for this, this one is SDL map RGB. There's another one map RGB A. Right now we're just going to look at this one here. This function is used to map an RGB triple to an opaque pixel value for a given pixel format. So like you said, or at least like I said and like you saw, this takes you went 32 unsigned integer 32 with a constant SDL pixel format format. And you might be wondering what the heck is that? We haven't seen that before. Well, we can actually get some pretty easy stuff for that. And 8-bit unsigned integers for red, green and blue. Those are typically just in the range 0 to 255, 0 to 255. Kind of like you typically see in an RGB triplet. And you may very well have seen it in like Pygame or any of the game development library. So format is an SDL pixel format structure, sorry, describing the format of the pixel. I'm disappointed they don't show a code example here because the way that we can actually access that is by using the screen pointer that we just created. Let's go ahead and type our function SDL map RGB. And remember the first argument is the pixel format. So we're going to use screen and keep in mind this is a pointer. So we actually have to use the pointers attribute locator rather than the dot selector. We're going to use the pointer selector here. And that's the hyphen and the arrow, the greater than symbol of the walker. So it looks like an arrow. And then we're just going to use the special attribute that kind of comes with an SDL surface format. I'll show you this in the documentation, SDL surface. You'll notice it does have an SDL pixel format variable. It is a data field and it's just a format. The format of the pixel stored in the surface and you can look on an SDL pixel format if you want more information about that. I would recommend checking it out. If you're curious, know a bit more of. And then it was asking for the variables and the values that we need for red, green and blue. So 255, 255, 255 for white and that's very simple all that we need. And now that we've created the color, let's actually go ahead and do something with it. Let's fill the screen or fill the surface with this color. Now that's a whole new function that I'm going to introduce you guys. And it's called SDL fill rect. Okay, cool. Fill rect, this first kind of return thing. Use this function to perform a fast fill of the rectangle with a specific color. All this takes is an integer, at least it'll return an integer. It is the function, SDL fill rectangle. And the first argument is the surface pointer, the destination. The second one is a rectangle or a rect object for the rectangle that we're actually using. And then the Uint or unsigned in 32 bits, the color. This kind of gives a bit more information about this function parameters. Everything I just said, rect, everything I just said, color, everything I just said. It'll return zero on a success or a negative error code on failure. SDL get error will give you everything you need to know. This code example is kind of doing exactly what we had before. Our surface, which is, like I said, a good idea to put to null if you're using a pointer. You can create an RGB surface with this value. That's a good function to use. And fill rect, the function that we're looking at, if we're used to surface, we can pass in null to actually declare how much of it that we want to cover. Now notice here, over in the function parameters, the SDL rectangle or SDL rect structure represents the rectangle to fill. We can kind of crop in on the surface of the image if we wanted to or null to fill the entire surface. And that's actually what we're going to do. I didn't get into this too much, but notice the SDL surface actually has a data field or a variable for a rect, SDL rect. It has its own rect. And you'll see this a lot in Pi Game if you work with that before. I honestly kind of recommend checking out the Pi Game series before you watch this one. This clip rect is the SDL rectangle that's the structure in that class that's used to clip bits the surface that can be set by SDL set clip rect. This variable is read only, but it's the rectangle which really kind of has X and Y coordinates and width and height coordinates, these values for the surface and for the rectangle that's taking up. So all the way back to the SDL fill rect function, I just want to read through the remarks. Colors should be a pixel of the format used by the surface and can be generated by the SDL map RGB or map RGBA functions we were looking at earlier. If the color alpha contains an alpha component, then the destination is simply filled with that alpha information. No blending will really take place. Clip rectangle you can set, that sort of thing. Okay, it looks like that's all we need to know about this function. Now let's go back to our code and actually run that command. Fill rect, remember we're going to fill the screen and we're going to fill all of it. So the second argument for the rectangle is going to be null and the color that we want is white because we just set a variable for that mapped RGB triplet and the values over here. Awesome. Now the next thing we want to do is actually update the screen with this new information because I can run this code for you. I can show you this compiled and everything, but you'll notice that it's still black. What the heck? So we actually have to update the screen with the change that we just made and this next function update is really what you're going to be seeing a lot of the time in our main event loop because the game has to update all the time, right? Or at least the program or the code, whatever we're actually building here. It has to update. So this next function is crucial. Right now we're going to be using it outside of the while loop because we're actually only making one change to the program. There's no need to keep updating if those changes are going to happen. So outside of our loop, we're going to use a new function and this one is called SDL underscore, of course, because that's a typical naming convention, update window surface. If you're coming from SDL 1.2, you may be kind of familiar and acquainted with SDL underscore flip and update window surface is kind of the new version of that in SDL 2. This function will copy the window surface to the screen and update. All it takes is a window variable. It will return whether or not it was successful, is your own success or negative error if it's failure. And this function is equivalent to the SDL 1.2 version, SDL flip, just like I was telling you. This is how you reflect any changes to the surface on the screen. So let's run this, SDL update window surface and the pointer is, of course, window. Awesome. Now, if we run this code, we should be all done. I'll compile this and I'll run it and do, do, do, do. Sweet, we got a white window. That's all it took. That's really all that we were looking at, just creating a color with a new type, with new functions, filling a rectangle and a surface, new types for SDL that you should know and understand and a new function to actually update the screen. We're going to be doing a whole lot more with this in the next couple of videos, especially with the surface and rect. These are really crucial to understanding how you can get things to be displayed on your screen or on your window and that's really the fundamentals for the rest of SDL. You need to know what a surface is and you need to know what a rect or a rectangle is. Check those out in the documentation if you really don't understand what I'm getting at here, but I think I did a pretty thorough job of reading through every bit of the documentation. You probably hate it, you're probably getting really bored. But regardless, this is it. Thank you for watching, guys. We've successfully changed the color and we can obviously change these values for R, G and B to create anything we really want here. I'll just go ahead and create blue and show you how this works because honestly, all I'm doing is just driving this home, trying to show you, yeah, this is the cool thing we can do. We've got colors now and lots of good, good stuff. Thank you for watching, guys. If you like the series, please like the video. Please comment, please subscribe, get some interaction, show me what it is that you really want to see, what you like, what you don't like. I can only get that information from you guys. Again, thank you for everything and I will see you in the next tutorial. Bye. Thank you.