Fifth week – Arkanoid and SDL

During last weeks lectures our lecturer started live coding for Arkanoid (or Breakout). We went through some of the basic stuff you need for almost any game, the engine and a couple of managers. This week we finished it by adding stuff specific to Arkanoid (blocks, paddle, ball and movement; in short, the logic of the game). We also implemented SDL_Image and SDL_TTF as an addition.

In SDL you can only handle .bmp images, and to be able to utilize for example transparency in images we must use a different kind of image (.PNG for example). This could be done with the SDL_Image library. It could take almost any kind of image, but there is one problem. When loading in a image to a surface we don’t know what kind of image it is. Thus we have to convert it to a format that we desire by using SDL_ConvertSurfaceFormat(parameters) and then making it into a texture which we could use in our game.

In a similar way we could insert text into the game, though as images, with the SDL_TTF library. With TTF_OpenFont(parameters) we load in a desired font and then create a new instance of an inherited class SpriteText from our Sprite class. This SpriteText class takes the font and the text and creates a surface with TTF_RenderText_Solid(parameters), much like we did with images. We also convert this surface into a desired format with SDL_ConvertSurfaceFormat(parameters) and then create a texture which can be used in our game.

From now on we are going to mainly focus on our own projects and I have started looking up different kinds of Bomberman games for inspiration.

Best Regards,

Sven

Music of the week: Hungarian Dance no. 5 – Brahms

Week three and four – Classes, inheritance and polymorphism

During last week (week 3 in the course) we started using classes. Classes are something defined in C++ and are similar to structs, which are mainly used in C. As structs, a class is something that can hold a number of values and it can be instantiated for use in your program. Though, one thing with classes is that it can also hold member functions (methods) which can be called with the instantiated object. To access the method we use the . (dot) operator after the instantiated class, followed by the name of the method. For example, if we have a class called Ball:

Ball myBall;
myBall.SetColor(a color here);
myBall.SetSize(20.0f);

We call the two methods SetColor() and SetSize() which both are defined in the class Ball. If we want to allocate the object ourselves we have to use a pointer and the -> (arrow) operator instead:

Ball* myBall = new Ball;
myBall->SetColor(a color here);
myBall->SetSize(20.0f);
delete myBall;
myBall = nullptr;

IMPORTANT! When allocating something you also have to remove it from the memory!

When writing a class it will always have three standard kind-of functions no matter if you write them or not. They are:

  • Default constructor
  • Destructor
  • Copy constructor

The default constructor are called when instantiating a class, if the instance does not carry any parameters. The destructor is called when the instance is deleted. The copy  constructor is a little special, it will try to copy the instance and make a new instance of the same class. These can all be written to do whatever you want, but if you do not write them they will still be there.

Something really useful with classes is inheritance. You can make a class that inherits everything in another class and use it in the new class. It also inherits the private variables and methods, but can not access them. If you want your ‘child’ class to access these variables you have to write them as protected instead of private in the ‘parent’ class. The : (colon) is used as the operator for inheritance, for example:

class Humanoid {
public:
void SetAge(int age);
protected:
int age;
}

class Elf : public Humanoid {
public:
void useNightvision();
...
}

int main(int argc, char* argv[]) {
Elf legolas;
legolas.SetAge(5000);
return 0;
}

In the above code I instantiate an Elf, which are a Humanoid, therefore it can call the method SetAge() to set the age of my Elf. (The age of Legolas from LOTR seems to be unknown, but he must at least be over 5000 years old, which is kinda cool!)

Another thing with classes and inheritance are polymorphism, which means that a class can take many forms. For example, if we a class named State and a couple of other classes named PlayState and MenuState. PlayState and MenuState inherits from the class State.

class State {
virtual bool Update(float delta) = 0;
virtual void Draw() = 0;
}

The syntax virtual makes it possible for ‘child’ classes of State to re-write the methods they inherit from State, and when also writing = 0 after the method (meaning they are pure virtual) you MUST write them in the ‘child’ class.

We are beginning to start our projects where we have to create a copy of a retro game. I have chosen Bomberman, and I am really looking forward to it. I hope I will be able to add my own twist to the game without it messing up or taking to long.

Best regards,

Sven

Music of the week: Uptown funk – Mark Ronson ft. Bruno Mars