Kategoriarkiv: 5SD022

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

Second week and Pointers!

This week we have gone through some new things that I have never actually done before. The main thing being pointers and memory allocation. Arrays and functions were also brought up, but I already knew the basics around these. Another thing that we learnt was type casting. I have always used the C way of casting, for example (float)variable, but the right way to do it in C++ is to type static_cast<float>(variable); to cast the variable to a float value.

Memory allocation is something very important when programming. There are two memory locations you can access, stack and heap. Stack is memory that handles variables automatically when they are declared, where as heap is something we programmers must allocate memory for new variables we want to use. The benefit of using the heap is that it is so much bigger than the stack. To allocate memory we use the new and delete operators. For example:

 double* doubleVariable = new double;

This creates a pointer that points to a new double in the heap memory. But when allocating memory we must also delete it when we no longer need it. If we don’t do this it will create a memory leak and the space it is taking can’t be used for something else, it is just sitting there being useless. To delete we do as following:

delete doubleVariable;
doubleVariable = nullptr;

The second row sets the pointer to nullptr, making it invalid. We do this to make sure it does not point to some garbage that could mess up your code.

During this week we have learnt a couple of new operators and some of them are a bit hard to distinguish from each other, especially the ones often used with pointers. I will be listing some of the syntax below.

  • & – used to create references.
    int intVariable = 10;
    int&amp; referenceIntVar = intVariable;
    referenceIntVar = 5; // intVariable is also set to 5

    When used in declaration after the data type it will reference to the same memory address as the given variable. You could say that a value has two variables.
    When used in front of a variable you are returning the memory address of that variable.

    int variable = 1;
    std::cout &lt;&lt; &quot;Address: &quot; &lt;&lt; &amp;variable &lt;&lt; std::endl; 

    This will NOT show the value 1, but the memory address to the variable.

  • * – used to create pointers and dereferencing
    When creating a pointer you put the pointer after the data type when declaring it.

    int value = 10;
    int* intPointer = &amp;value;
    std::cout &lt;&lt; &quot;addr: &quot; &lt;&lt; intPointer &lt;&lt; std::endl; // Displays the memory address
    std::cout &lt;&lt; &quot;value: &quot; &lt;&lt; *intPointer &lt;&lt; std::endl; // Displays the value of the variable the pointer is pointing at

    When used in front of the pointers identifier (row 4) you dereference the pointer and is will show the value the pointer is pointing at.

The last two lectures we had this week we created the game Pong. Our lecturer had a live workshop and went through the code of the game. For such a small and simple game there were quite a bit of logic that we had to work out, but I feel that I understood the most part of it.

Best regards,

Sven

Music of the week: Low Hangin’ Fruit – Tenacious D

Start of Game Programming I at Campus Gotland

I have studied a few programming courses through the years. I started out learning the basics of C++ during my upper secondary school years, around 2009-2010. Later on I studied an introductory course on C++ and also in Java when I first began studying at a university. I have also studied a couple of distance courses which included learning C++, C#, Java and some other scripting language.

The first week of the course Game Programming I has come to an end. This week we have learnt the very fundamental basics of programming and a lot of the the things that was brought up already I knew from before. Though there was some things I did not know of or some things that I had not used before. For example:

  • Usage of Continue in loops
  • for-loop with empty parameters

The use of Continue in loops is similar to Break. Break stops the loop at the specific line, where if Continue is called the loop starts over until maybe Continue is not called anymore.

I have always written something inside the parentheses of the for-loop and did not know that it could be empty (still with commas with spaces between). Instead of declaring and initializing the variable used in the loop inside the parentheses, it could be declared outside the loop. And inside the curly brackets of the loop you could put the increment or decrement of this variable. To make it stop you could just use an if-statement to check something and then Break the loop.

The new things (even though they are few) I have learnt this week makes me eager to start coding games and learn even more about programming. I am really looking forward to this course!

Best regards,

Sven

(P.S. The pic is what I think the life of a programmer sometimes can be 😉 D.S.)

Music of the week: Get To The Choppa – Austrian Death Machine