How to program it - nicobabot/Multi-cameraculling GitHub Wiki
To obtain more than one camera and only printing the textures that are inside all the cameras of the game you have to start doing a class Camera with one iPoint that will be the position of the camera and a SDL_Rect to define the space where will be printed the camera. We use the camera as a iPoint because the width and the height will be the same as the viewport but if you want other dimensions the iPoint of the camera will be a SDL_Rect.
We add a constructor to receive the position of the camera (iPoint) and the viewport where you want to print the camera (SDL_Rect). The next thing you should do is create a vector of cameras. In this vector you will store all the cameras in the game.
You can also create a function to create cameras. This is a good idea because with this function you can create cameras in other parts of the code.
In this function you will create a camera and you will store this camera on the array. Is a good idea to return a pointer of the camera because if you want to do any changes of the camera in the file (.cpp) you created it is easier having this pointer that having to iterate the vector.
The next step is creating the cameras you want in the game. In our case we created two.
One camera will be above the other and both will fill half the height of the window. We have printed two sprites and made the camera follow the sprites to show that our optimization works.
As follows we have to adapt the blit function (the function you use to print all the textures of the game) to allow printing in all the cameras of the game. This is the function we use to print all the textures (Blit):
First you have to have to iterate all the cameras and print the all the textures in all the cameras. As you can see on the image you should use a for to iterate all the cameras. Then the first thing before starting to print will be setting the viewport of the camera we are iterating. Then you must put that all the positions of the textures have to be printed regarding to the camera. At the end of the blit function remember to set the viewport to NULL. If you don’t do this all the textures will be printed in the same viewport.
The viewport the only thing that does is showing what is inside of his rectangle but in the function of printing the operations to print a texture that is outside of the rectangle are done and then we are wasting resources. To solve this we have to make two conditions (if) inside the iteration of the cameras (for) to establish that the textures outside the camera will not be printed.
Remember as we said before how is the axis of our game and maybe you will need other conditions. With this now we should have more than one camera printing the same textures and only printing what is inside of the camera. But if you can see the optimization is not already done because we can optimize more. We have a map of 150x150 and what we can do is in the function where the map is printed you can solo establish other conditions to reject the part of the map that is outside of the camera. This step will optimize a lot the code. This is the function we use to print all the map:
First of all we add an iteration of all the cameras and then between the iteration of the y and the x we need to add two conditions to know if this tile of the map that you are going to print is inside the camera you are iterating or not. With this condition we optimize a lot because we don’t do useless operations.
But the problem now is that in the function where we print all the textures we have a for that iterates all the cameras and print all the textures in all the cameras in the game. Now if we have two cameras and we iterate all the cameras where we print the map as you can see in the image we will be printing the map two times for camera. This is because when prints the map for the camera one in the blit we have a for to print all the textures in both cameras, then we are printing the map for the camera one and two at the same time. To solve this we adapt the blit function to accept another argument. This argument will be a pointer of camera (Camera*).
This argument (Camera *cam) will be initialized with nullptr. Now the blit camera will have another argument and if you want to print a texture in all the cameras on the game you only need to call the function blit without saying in which camera do you want to print, then the function will print the texture in all the cameras. But if you want to print a texture only in a camera you have to say in what camera you want to print this texture. With this we can solve the problem i had explained before because then in the draw of the map al tiles in the map will send alse the camera that is iterating and it will only print this tile of the map in this camera.
As you can see at the end on the function blit we send the camera we are iterating and this tile of the map will be printed only in the camera we are sending.
And in the function blit you should add a condition to see if the camera is nullptr or not. If it's nullptr you will print the texture in all cameras as we modify before the blit. If the camera is not nullptr you will only print the texture in the camera that the function have received.