 What is going on everybody welcome back to another C++ SDL programming tutorial in the last video We were creating sprite groups. We created a few classes one for a sprite object itself I'm actually gonna code fold that and a another one sprite group that would keep track of multiple sprite objects And we'd be able to kind of keep track of them and draw them all together add remove and test whether or not We have them update multiple sprites at the same time and that sort of thing So that's the the benefit of what we were doing in the last tutorial in this one We're gonna move on to images. So I'm gonna go ahead and save this file as 0 8 a Images Okay, cool, and now what I want to do or at least what I want to go ahead and get started with is I want to Talk a little bit more about our sprite class and our sprite objects I'll open that right back up again And I want to talk about this class because I think it's important to note that we designed this class We built this framework and we set up this blueprint So we'd be able to use other objects and instances that are Sprites that are images or kind of movable characters in the game So the sprite the class the sprite is the base class It's it's kind of abstract because we want to be able to override and and grab more from it This is just the framework that most things will build off of So what I'm getting at is that the sprite class is going to be inherited by anything that we really actually create Now if we want to inherit anything from a class and C++ We know we have these kind of access member kind of qualifications or attributes We have the private members. We have the public members and that sort of thing we also have what are called protected members and Private members won't actually be inherited if we have another class or another object inherit from this base class So this needs to be changed no longer than being privates. We're going to change this to be protected Anything that's protected will be actually accessible to any children So if we derive from this class if we inherit from this the sprite class We'll actually be able to go ahead and use these attributes and variables and properties that we've already set So that's easy enough. That's that's pretty good for us The next thing that we need to do is actually create the new class that we're going to use that is based off of the sprite class Now I'm just going to call mine block You can call yours object or instance or anything that you really want to and the way that we denote inheritance in C++ is By using the colon and that's the shift formation of the semi colon on your keyboard it should be right to the L key and Then we need the attributes that we want to inherit and I'm going to say public Because that'll actually grab everything from the Parent class and next we actually need the parent class name So in this case it is sprite because we're inheriting from this class up here the sprite class then of course we need our Braces and brackets to denote that we have a code block and then we can go ahead and end it with our white space now We'll create our public definitions here We don't have anything private because anything that we really care about is already being inherited from this sprite class so we do need the constructor though and Funny enough the constructor is really just going to take all the same arguments as our original sprite class, so I'm actually just going to copy and paste this and We'll throw that right in there inside the parentheses and We want to say that okay once we have this block Constructor once we actually called a constructor for this object what we're going to do in turn is call the constructor for the parent object or for the sprite class itself and Again, we denote this with a colon so create a colon and then we just type in sprites I'm actually going to create an enter line here Sprite because that's the the parent object that we're using We're actually going to call its constructor and we'll pass in all of the variables that we've already been passed to the original constructor so again now we don't use the Variable types, but we still want to pass in the same arguments for color x y With the height so that's good and we can do anything more after this if we just supply a few more code blocks So after this after we've ran the original sprite constructor what I'm going to do is I'm going to call a new function that we're going to create called update properties and This is going to be a void function. We can go ahead and create that void update properties It's not going to do much But it isn't going to take any arguments even what I'm going to do is I'm going to reset the origin x and the origin y to be something specific to this object or to this block This is the benefit of actually creating it in classes and that sort of thing And I'm going to creating or I'm going to call a new function that we're going to create called a set position And that is going to take a few arguments it's going to take the x and y coordinates of this object itself and you know that we access that by our Rectangle our SDL wrecked and we have that set up in the variable wrecked So we can just use wrecked out x and wrecked out y now Let's go ahead and create the function that actually does this the way that this works is by another void function Set position and it's going to take some arguments It's going to need an integer x and obviously an integer y use the corns that we use to set the position on the screen and The way that we can set it up is by telling the wrecked x or the SDL wrecked that we're using to maintain this object's coordinates That is going to equal x that we've passed in minus origin x So that will we account for the origin and obviously the place we want to go to and we can do the same for y so awesome Now the next big function is what we're going to be doing that actually gets to the core of this tutorial I said we're going to be actually getting into images and More multimedia and things that you can include in your in your game and in your program And that's exactly what we're going to incorporate right now. So let's set up a new function This isn't going to return anything. It's going to be void and I'm going to call mine set image Set underscore image you can call yours whatever you want, but it might be a good idea to name things after mine now What I'm going to pass in for an argument here is Essentially a string and this string is going to again go through this function and go to a much more elaborate function that SDL provides that actually loads a file and loads the image Into our program into our code so the way that I set this up is by setting up a constant variable See on ST and it's going to be a typical string or a seasile string And the way that we build that is by an array of characters So I could use an asterisk or anything here, but I'm just going to use an array denoted by tube races And the variable can be called file name by default. I'm going to set this to be null and That way we can just test okay if we've actually even passed anything into this function or not When we get our code block ready We can test if file name is not equal to null then we know we actually want to be doing something and Then we'll actually get started with an SDL surface because obviously the image has to be stored somewhere and That that image that we load is going to be stored in an SDL surface Just like our sprites have been stored in and just like obviously the screen in our window So it isn't a it is a pointer. We're going to need the asterisk and I'll call it loaded image You can call it whatever you'd like as usual and by default this is going to be null But what we're actually going to do is reset it loaded image can equal and here's where the new handy-dandy function That we're going to be using comes in it's called SDL underscore load BMP for load bitmap Let's check this out in the documentation. I'll hop over to my internet browser over here and the the URL for SDL online is lib SDL.org if you want to get to the documentation. It's wiki.sdl wiki.libsdl.org or from the main page you can just click on the left-hand side Documentation wiki. I wanted to search for SDL load BMP, but from what I've seen It's given me some trouble here So what I'm going to do is I'm going to head over to API by category. Oh, sorry. Sorry API by name and Just kind of control F for SDL load bitmap and I see it right there Okay, use this function to load a surface from a bitmap file The syntax is very very simple just it'll return a surface pointer And it takes a array of characters from file name and that's exactly what we've set up in our function So we take that as a parameter and we'll just pass it right in awesome In the documentation it doesn't really say anything more other than that So we should be good to go We got to keep in mind that the new surface should be freed with SDL free surface and we're gonna get into that very very soon Let's keep going regardless Next what we'll do is we'll actually test if the loaded image was successfully loaded and the way that we'll do that We'll test if loaded image is not equal to null because if it is to be equal to null then there must have been an issue That's the benefit of having it be set to null to begin with now. We can go ahead and set up a Actual image that we're gonna use from this loaded image Actually, we don't even need to do that I have in my notes over off on the side here that I reset the image variable for some reason But I don't think there's any need for that. Oh No, it is it is good for that because that image variable is actually the self image For this object the sprite itself needs to know the image that we've already loaded So image is going to equal loaded image Okay Now the next thing that we kind of need to take account for at least Accommodate for is that when we reset the image when we change the image for this sprite and for this object It actually changes the rectangular properties You know the SDL wrecked and the way we've been keeping track of of our wrecked information with wrecked on X and Wrecked on width and height and that sort of thing well Those are kind of changed if we were to change the image and we don't want to do that We want to actually keep track of the current position and without it being reset So we're gonna keep track of some variables. I'm gonna call mine old X and I'll have another one old Y And I'm sure you can guess these are going to be the wrecked dot X and the wrecked dot Y Properties before we actually reset the new wrecked value because eventually wrecked is gonna have to equal This new image that we've loaded and it is a it is a point to keep in mind So we have to use the arrow selector It's it's clip wrecked and that way we can return the specific rectangle that we're using for this image that we've just loaded and Now we'll be able to access it within our block object and Eventually ultimately because if you go through the class hierarchy our sprite object, so that's kind of cool Now we'll reset our new wrecked dot X and a wrecked dot Y variables to be what we've just set beforehand So this is just kind of a small variable swap to keep track of the information that we had previously and Finally now that we have the wrecked X and wrecked Y set up We want to apply the origin so the way that we did that is by in our update properties function So we can very easily just go ahead and run that one more time Update properties nice Now I believe we should be good with our block class We haven't seen any kind of errors or anything just yet, but then again, we haven't really even compiled So let's go down to the main portion of our code and let's see what we can do here We have active sprites and we've got other objects. What I'm gonna do is I'm gonna kill these and We don't need any testing of the has and that sort of thing We do whoever want to draw them on the sprint on the screen, but let's go ahead and create a block We're gonna use the block class We just write that with it with its capital letter to note that this is the type of the object. We're creating it can be a block The by default will be the color red and we'll put it at the 0 0 place on the screen and Then we'll add it to the active groups group and that will be drawn on to the screen for us and I believe that shouldn't be good to go or at least a good testing Portion let's hop over to the terminal. I'm gonna clear the screen because I had a good mess happening over there and We'll compile it with g plus plus. Oh eight a whatever the file name you had and Package config with libs and C flags or SDL to if I run this. Oh, I do get an error Into main error object was not declared in the scope. Oh duh We want to add in actually the variable block since that's what we've just created over here. Nice now Let's run this and I need a Semi colon at the end of the class definition for our block object Runs again, okay now it compiles. Let's run eight on out if I move this over Okay, it looks like the center of our of our object is being put It looks like the origin is being put at the center. So we do have an issue. Let's check that out Okay, after some tinkering, I think I figured out why the origin might be giving us some problems I realized in the Code for the sprite class. I had already applied the origin and it might be a good idea to kind of leave that alone in at least until the Well, you know the the block or the inherited object is the one that actually deals with the origin It might be a good idea to declare the variable in the sprite In the sprite class and the protected definitions or at least the declarations But actually only apply it when we inherit the class. So that's what we have set up here now I just removed the origin from the sprite class one and hopefully okay Yeah, see now it puts it in the in the zero zero position just like we wanted. Okay, great So now we can go ahead and actually include an image. First of all, we need to go find one Remember I had said or at least the code that we wrote Says we're going to load a bitmap. It's SDL load a BMP Well by default SDL only supports bitmap files or BMP files So at least for now what we're gonna have to do is just find one online because I'm a little too lazy to go ahead and make one of my own so I'll look up BMP sprite and I guess we'll just kind of look all over the internet simple stupid Google results until we find something that we like The rocket looks good view that image and I'll go ahead and save this as Sprites I suppose sprite dot BMP and I'm gonna replace whatever is there Because I have been testing this beforehand. Alright, and now in our code once we create the block object What we can go ahead and do is actually do block Dot set image because that's the function that we wrote remember and the file name all it takes is a string for sprite dot BMP and When we run our code compile this No errors we check it out and whoa, we've got a rocket Taking off right into the subscribe text at the title of our window. That's awesome. All we did was load an image It's a simple bitmap that has the still still a white background, but it looks kind of nice And we've successfully loaded an image even onto a sprite even an object that we created ourselves So that's awesome. Hope you enjoyed this. I hope you enjoyed this pretty simple tutorial There's a lot more to it. I suppose and we're actually inheriting the sprite class changing the private variables to be protected Showing how the constructor is working creating all these other functions and getting the origin to work that we want the way That we wanted to but thank you guys This has been fun, and I hope you're having as much fun as I am. I'll see you in the next tutorial. Bye