< In the previous tutorial i didn't made use of part of the features that come with C++ : classes, so it's time to use them to define and store data about our hero and the map, into the player class i want to define the player creation process, finally i want to create a Main Menu for the game...>
I think I'm very ambitious with that post, but the objectives are:
- Create at least two classes: cplayer and cmap
- Define a Player creation process
- Create a Main Menu..
First let's start creating two classes: cplayer and cmap, to do that code::blocks has an assistant to design classes, so resuming from the code in the previous post we should select "File" > "New" > "Class":
In the class name , write "cplayer", and then mark "Header and implementation shall be in the same folder", then press "Create":
Select "Yes" to add the files to the project and...
Press ok, so we will use files both to debug and compile the release project, we need to create a second class "cmap", so just repeat the steps,and save the project.
Now let's start modifying the cmap class, we have two files cmap.h (header) and cmap.cpp (implementation), in the header we are going to define the variables and functions , so we are going to move the Uint8 Map[x][y][layer] to the class, we need too a pair of functions to get and set the values in the map array, so our cmap header will finally be:
Now we have to implement the functions in cmap.cpp:
That's it! Now we have a simple class that will store the map data, notice that i've inserted into the creator the code to define a map.. it's not the better way to do that , but i wanted to show how we can separate code into classes , now we need to change a few things in the main program to use the new class, first we have to insert into rpg.cpp:
So we actually use the class, now let's create a variable to use the map:
Then i will erase the old Uint8 Map definition and the old map initialization, now everything is inside mymap, finally we have to update the map blitting functions to draw the map:
Notice before i used Map[x][y][layer], now mymap.GetTile(x,y,layer), time to "Build and Run" the project to see what happens, if you have followed all the steps, you should get the same results than in the previous post, it's ok because we have just changed a variable with a class.
We have to create the player class too, so let's move the PcX,PcY and PcSpeed to the cplayer class:
I've added too the player's name variable, and the functions to get/set the values:
Now we return again to rpg.cpp, we have to add:
So we actually use the class, now let's create a variable to use the player:
And initialize the values:
Keyboard input changes, to use the player's class:
And finally the blitting functions need to be adapted too so they use the player class:
Well, our player has a name and we want to show it, so that's what i'm going to make here:
We display player's name at his right side, it can be easily modified so the name moves to another side...
Post is getting way too big, I'm going to split it so it's going to be easier to follow..