The game level class gives us a lot of flexibility since any amount of rows and columns are supported and a user could easily create his/her own levels by modifying the level files.
OPENGL 4.3 C++ DELETE OBJECT ON KEY PRESS CODE
You can find the source code of the GameLevel class below:
![opengl 4.3 c++ delete object on key press opengl 4.3 c++ delete object on key press](https://www.3dgep.com/wp-content/uploads/2011/02/OpenGL-RenderScene1.png)
The GameLevel class also houses a few other functions, like rendering all non-destroyed bricks, or validating if all non-solid bricks are destroyed. This way, customizing the appearance of their colors doesn't look too weird or unbalanced.
![opengl 4.3 c++ delete object on key press opengl 4.3 c++ delete object on key press](https://docs.nvidia.com/gameworks/content/technologies/mobile/images/convert_to_cpp.png)
The effect is that we can neatly manipulate their colors within the game-code by multiplying their grayscale colors with a defined color vector exactly as we did within the SpriteRenderer. Here we load the game objects with two new textures, a block texture and a solid block texture.Ī nice little trick here is that these textures are completely in gray-scale. The size of each brick is automatically calculated ( unit_width and unit_height) based on the total number of bricks so that each brick perfectly fits within the screen bounds. The init function iterates through each of the loaded numbers and adds a GameObject to the level's Bricks vector based on the processed number. GameObject(pos, size, ResourceManager::GetTexture("block"), color) Glm::vec2 pos(unit_width * x, unit_height * y) Glm::vec3 color = glm::vec3(1.0f) // original: white initialize level tiles based on tileDataįor (unsigned int y = 0 y Bricks.push_back(obj) Unsigned int lvlWidth, unsigned int lvlHeight)įloat unit_width = lvlWidth / static_cast(width) Void GameLevel::init(std::vector> tileData, The loaded tileData is then passed to the game level's init function: This->init(tileData, levelWidth, levelHeight) While (sstream > tileCode) // read each word separated by spaces While (std::getline(fstream, line)) // read each line from level file Void GameLevel::Load(const char *file, unsigned int levelWidth, unsigned int levelHeight) It first loads all the numbers in a two-dimensional vector within its Load function that then processes these numbers (to create all game objects) in its init function. The GameLevel class uses two functions to generate a level from file. The example level listed above would, after being processed by GameLevel, look like this:
![opengl 4.3 c++ delete object on key press opengl 4.3 c++ delete object on key press](https://s3.manualzz.com/store/data/025988528_1-d9980db356996fa512b85dc075525116-360x466.png)
It holds a color, a rotation component, whether it is solid and/or destroyed, and it also stores a Texture2D variable as its sprite.Įach object in the game is represented as a GameObject or a derivative of this class. Such a game object holds state data like its position, size, and velocity. We create a component called a game object that acts as the base representation of an object inside the game. We first have to define what an actual brick is though. In this chapter we'll briefly walk through the code of a game level object that is used to manage a large amount of bricks. We want these levels to be configurable such that they can support any number of rows and/or columns, we want the levels to have solid bricks (that cannot be destroyed), we want the levels to support multiple brick colors, and we want them to be stored externally in (text) files.
![opengl 4.3 c++ delete object on key press opengl 4.3 c++ delete object on key press](https://root.cern.ch/root/htmldoc/guides/users-guide/pictures/03000093.png)
Breakout is unfortunately not just about a single happy green face, but contains complete levels with a lot of playfully colored bricks.