User Tools

Site Tools


notes:data:fall2023:projects:ttb0

TTB0

Setting Up TTB0

Migrating Sprites Onto lab46 and PI

After creating your sprites, you will need to move them onto lab46 and possibly onto your PI. One way of doing this is using the scp command. To use the scp command use to following format:

scp FILENAME DESTINATION:PATH

Here is an example:

scp sprites.png username@lab46.g7n.org:~/src/fall2023/discrete/ttb0

If necessary, this is how you would get the desired file onto your PI:

scp sprites.png username@raspberrypi:~/src/fall2023/discrete/ttb0

Adding Sprites To Game

Once you have your sprites .png file on lab46 and on your PI it is now time to load them into the cartridge.
Make sure your texture.png is in your ttb0 directory or you supply the correct path to the file.

To add them to the cartridge open your make.sh file.

Once in the make.sh file, add the following lines:

echo
echo Convert the PNG textures
echo --------------------------
png2vircon BreakoutTextures.png -o BreakoutTextures.vtex || abort_build

“BreakoutTextures.png” is an example that represents the name of your texture file. Yours could be TexturesBreakout.png for example

After including the texture file in make.sh you also need to add it to your .xml file. To do so, open your .xml file and add the following:

    <textures>
        <texture path="BreakoutTextures.vtex" />
    </textures>

“BreakoutTextures” represents the name of your breakout texture file.

Once you have added your texture file to BOTH the make.sh and the .xml file your textures are loaded into the cartridge.

STRUCT

Making Brick Struct

When making your structure for the brick, it is important to remember that every time the ball hits the brick it needs to disappear. This means that inside the brick structure, you need to have a flag, in the below example the flag would be a boolean named Active. The value for a particular index's Active member would change upon collision.

Here is a simple brick structure that you can add or build onto if necessary:

struct BrickOBJ
{
    bool Active;
    int X, Y;          // Brick X and Y location
    int Width, Height; // Brick size
};

POINTERS

A pointer points to and references a location in memory. We can obtain the value that is stored at that location by de-referencing the pointer. The main use of pointers in this project will be to reference/de-reference each brick you have allocated memory for (malloc).

MALLOC

Memory Allocation. This is a way to perform memory management manually for dynamic memory. The syntax for malloc is below.

  ptr = (cast-type*)malloc(byte-size);
  
  
  

MALLOC’ED ARRAY

The array exists as a linear allocation of memory, and any particular value in the array can be accessed by adding the value of that location to the pointer when calling it

Example:

  (*(b_arr + 0)).x = 20;

would make the first x value of the array 20 and

  (*(b_arr + 1)).x = 30;

would make the second x value of the array 30, and so on

ACCESSING WITH POINTER ARITHMETIC

Pointers to structs can be accessed the same way a class can, like so

  [POINTER NAME].[VARIABLE NAME] = [VARIABLE VALUE]

a more practical example would be

  (*b_arr).x = 64

or some such, it functions as classes would in C++, we just need to make them ourselves since this is just C

MALLOC’ED ARRAY OF STRUCTS

For this project we will be using a pointer to point to an array of structs that we have allocated memory for. So we can use a line that looks similar to this.

  ptr = (Brick *)malloc(sizeof(Brick)*number_of_bricks);
  Brick*ptr = NULL;
  

With Brick being a struct. You need to make sure you set the ptr to = NULL before you use it as that is creating it and it needs a value.

free( ptr );

Be sure to free any allocated memory by the end of the program.

notes/data/fall2023/projects/ttb0.txt · Last modified: 2023/09/14 02:58 by cfoster8