Breakout Clone

Keywords

C++
SDL2
SDL2 TTF
Memory Leak

Background Context

One day, I got curious about how you would be able to add video games to game consoles like the PSP or Nintendo DS. I remember that I had a PS Vita with CFW installed on it which allowed me to create my own homebrew games for it. After doing some research, I was able to find a website called vitasdk which is an open source toolchain for PS Vita development. The goal was to see if I can port code I wrote from Windows PC to the PS Vita.

Tech Stack I used

Approximate Completion Time

  • 2 days

What challenges did I run into?

Controller Inputs that SDL provides through its API doesn’t work on PS Vita

I couldn’t use the SDL_KEYDOWN or SDL_KeyCode enums that SDL2 provided:

// SDL_KEYDOWN and SDL_KeyCode enums not supported on PS Vita
if (e.type == SDL_KEYDOWN)
{
  if (e.key.keysym.sym == SDL_KeyCode::SDLK_LEFT)
  {
    //std::cout << "left pressed" << std::endl;
    leftPressed = true;
  }
  else if (e.key.keysym.sym == SDL_KeyCode::SDLK_RIGHT)
  {
    //std::cout << "right pressed" << std::endl;
    rightPressed = true;
  }
}

Instead I had to use the sceCtrlPeekBufferPositive() function and SceCtrlData struct from the vitasdk toolchain to determine if the dpad or any of the buttons on the PS Vita were pressed:

SceCtrlData ctrl;

// this obtains what button is pressed on the PS Vita by storing it in the SceCtrlData struct
sceCtrlPeekBufferPositive(0, &ctrl, 1);

bool isLeftDPadPressed = ctrl.buttons == SCE_CTRL_LEFT;

Cryptic Linker Errors

Before LLMs were a thing, it took me a few days to figure out why I was getting linker errors as I was compiling the Breakout clone game to work on the PS Vita.

It turned out that one of the dependencies I was using to display text and the number of lives left on the screen was causing the issue. I used SDL2_TTF to render fonts on the screen and discovered that the SDL2_TTF dependency also needed to link the freetype, png, and z libs as those were direct dependencies of SDL2_TTF. Once I linked those dependencies up, I was able to display text on the screen!

The game would freeze after a few minutes of play

My first instinct was to search on Google if someone else ran into the same or similar issue I did but couldn’t find any answers. I ended up reading through the code and discovered that my code was leaking memory through the score and player lives text.

On every frame, I would create a new texture that would be rendered on the screen representing the player score and player lives text.

To fix the issue, at the end of every frame, I would use SDL_DestroyTexture() function on both the player score and lives text which ended up fixing the memory leak.

If you want to see how the full code is implemented, you can view it here

What did I learn?

  • I learned that relying on Google search to find the answer may not always help save time if little to no documentation is available for such issues. Investigating and narrowing down what the issue could be by challenging assumptions I think are true by actively trying to disprove them is one way to find things I can confidently say as true.
  • For the next time I write or work in C++, I need to make sure I get tools like Valgrind to check for memory leaks.

Game Features

  • can view the Github README here