# Game programming/Abstract Data Types

Notice this page was automatically translated from Spanish. This will be fixed when the author wants to do it.
"A master in the art of life makes no much distinction between his work and play, his work and leisure, his mind and body, his education and recreation, his love and his religion. He just distinguishes which is which. Simply collecting his vision of excellence in every thing he does, leaving others to decide if he is playing or working. In his own eyes, he is always doing both." Anonymous

## Requirements

* C knowledge
* To know what an abstract data type (ADT) is, and why using that approach is preferable to using several unlinked variables.
* To know how to implement an ADT in C programming language.
* To learn how to modify data in an ADT through indirection/pointers.

## Programming example

With the introduction over, let's go inside the field of C language and data abstract types implementation.
An important feature of C related to programming games is the creation of abstract data types (ADTs). ADTs have a higher level of abstraction from the basic data types in the language (int, float, double and char). With ADTs it is possible to create new ways of organizing game data: sprites (all moving objects), states of sprites, achievements, menus, buttons, animations, linked lists, players, levels, games, Events, etc..
To clarify this matter see this example:
As a reminder, typedef is the keyword for defining a new name for a type, allowing us to use Sprite instead of struct _sprite. This snippet creates a sprite type, which contains data for position, speed, angle, animation and energy variables. To use this type, you might do (in a program that already has a main function):
As can be seen, you can set the variables in this way. However, only upon the initialization of the ADT, you can use a different notation:
This snippet sets a sprite's data. A real game might initialize this data from a file or use different values for energy and framerates depending on the sprite, which would each be initialized with different values. Another thing to note is the difference from the basic data types, which are declared and then assigned values. Here, the Sprite is a collection of variables and thus to access a sprite's data, we must use the period operator (.).
Why do we use ADTs? Why not to use theunstructured variables?
This could be as follows:
This is somewhat equivalent to the first fragment (Code 1.2). However, it possesses several flaws. The first one is that when we need several sprites (and we will need many sprites), we should have parallel arrays of the variables or have variables such as:
Which is by any standard unworkable if we have several types of sprites. The why is to do with alignment (though to get around this one can use an array of ints - the flaw being that we must access speed as a uint. Plus, it gets worse if we need larger data types, also due to alignment). The second thing is that the data is encapsulated in the Code 1.2, but in 1.3 it is loose. The advantage to having the data encapsulated is that if we need to do something with the data we have to spend only a Sprite pointer, as follows:
This function is very simple. Using a pointer to a sprite, it reduces their energy. To modify the data we use the arrow operator (->). This operator can be thought to access the value pointed to by sprite, then access whatever variable in the struct has the name following (note that if you look at the assembly it works a bit differently). It is, in fact, equivalent to:
This is called pass by reference in C. You can also simple as int rates:
The reason the values swap is because we pass by reference. If passed by value, due to the copying mechanism, the original x and y values remain unchanged. We use the reference operator * to access and modify the original values. That is what makes (*sprite).energy or, in abbreviated form for a ADT: sprite->energy.
Remember:
We use an ADT to avoid declaring separate variables and to encapsulate the information in a single type. Knowing this, we use the Sprite struct.
All of the above ideas are joined in the following code. If you did not understand some code above, it is advised that you return to it and go over it to better understand this block.
This code closes this chapter. My style is to enter the field from the start so that the reader curious about what can be achieved as soon as possible. If they found very difficult to advise you to review the key concepts of C-pointers, structures, step by reference, etc.. If you have not understood do not worry because the other chapters will be clarified the concepts outlined here.

## Exercises

Note from person checking English translation: I can't really figure out much of this part, and the challenges seem a bit obfuscated, so I wouldn't worry about being unable to do them.
1. Show the code to declare a TAD animation. An animation must have a displacement x, y (a BMP file in which all are), a pointer to bmp (type SDL_Surface), width, height, number of tables animation.
2. Show the code for declaring a player named Player of TAD. Must have lives, score, color, name and sprite (sprites) partner. Hint:
3. Declare and initialize a list of under 4 players. Lives, score and color should all be 0. The name of each should get out of a settlement (char *names[4]). The sprite must emerge from an array of sprites (Sprite sprites *[4]);
4. Declare a variable pointer of a player to create a dynamic array of players. The function will receive the number of players, dynamically allocate the memory and initialize all fields to 0 (Hint: use the functions of memory management found in mem.h)