Skip to content
Saarrthi Rana

Game / September to December 2024

The Legend

A side-scrolling action game in C++ and SDL2, made for Humber's GAME211 course.

Role
Team Lead and Programmer
Team
Four developers
When
September to December 2024
C++SDL2SDL_mixerVisual StudioGit
Castle wall and wooden bridge level art from The Legend
Video
Our technical presentation of The Legend for the course.
Overview

There was no engine, so we wrote the camera, physics and sound systems ourselves in C++.

You fight through waves of enemies across castle levels, then a boss and a final boss. It runs on our course's C++ framework, with SDL2 handling the window, input, drawing and sound.

What I did
  1. Led the team of four and ran our GitHub repo. I set it up, organised the project and kept everyone's work merged.
  2. Wrote the camera. It follows the player and stays inside the level, and every sprite is drawn relative to it.
  3. Wrote the first enemy. It chases you when you're within 500 units and wanders around when you're far away.
  4. Added collision, player and enemy movement, and the level design for the early scenes.
  5. Built the stamina, health and XP systems and the on-screen display for them.
  6. Wrote the sound manager with SDL_mixer.
  7. Improved the enemy wave code a teammate started.
How it works

Movement

Every character moves by its velocity and acceleration once a frame, scaled by the frame time, so the game runs at the same speed on fast and slow computers.

Sprites and animations

Characters switch between idle, run, attack and death animations from sprite sheets, and flip to face the way they're moving.

Code

Code I wrote

The repository is private, so these are short excerpts. I can walk you through the rest on a call.

The camera

Camera.h, C++ and SDL2

This is the camera I wrote for The Legend. Every frame it centres on the player, then stops at the edges of the level so you never see past the map. Everything else in the game is drawn relative to this rectangle.
class Camera {
private:
    SDL_Rect cameraRect;  // The rectangle defining the camera's dimensions and position.
    int screenWidth, screenHeight;
    int levelWidth, levelHeight;

public:
    Camera(int screenW, int screenH, int levelW, int levelH)
        : screenWidth(screenW), screenHeight(screenH), levelWidth(levelW), levelHeight(levelH) {
        cameraRect = { 0, 0, screenWidth, screenHeight };
    }

    // Updates camera position based on player's position.
    void Update(const Vec3& playerPos) {
        // Center the camera over the player
        cameraRect.x = static_cast<int>(playerPos.x - (screenWidth / 2));
        cameraRect.y = static_cast<int>(playerPos.y + (screenHeight / 2));

        // Keep the camera within the level's boundaries
        if (cameraRect.x < 0) cameraRect.x = 0;
        if (cameraRect.y < 0) cameraRect.y = 0;
        if (cameraRect.x > levelWidth - cameraRect.w) cameraRect.x = levelWidth - cameraRect.w;
        if (cameraRect.y > levelHeight - cameraRect.h) cameraRect.y = levelHeight - cameraRect.h;
    }

    SDL_Rect GetViewPort() const { return cameraRect; }
};

The sound manager

SoundManager.h and SoundManager.cpp, C++ and SDL_mixer

The game's sound system. Sounds and music are loaded once and stored by name, played by name from anywhere in the game, and all freed in one place when the game closes, so nothing leaks.
class SoundManager {
private:
    std::map<std::string, Mix_Chunk*> soundEffects;  // For sound effects
    std::map<std::string, Mix_Music*> musicTracks;   // For background music
    // ...
};

bool SoundManager::LoadSoundEffect(const std::string& id, const std::string& filepath) {
    Mix_Chunk* sound = Mix_LoadWAV(filepath.c_str());
    if (!sound) {
        std::cerr << "Failed to load sound effect: " << filepath << " - " << Mix_GetError() << std::endl;
        return false;
    }
    soundEffects[id] = sound;
    return true;
}

void SoundManager::PlaySoundEffect(const std::string& id, int loops) {
    if (soundEffects.count(id) == 0) {
        std::cerr << "Sound effect not found: " << id << std::endl;
        return;
    }
    Mix_PlayChannel(-1, soundEffects[id], loops);
}

void SoundManager::CleanUp() {
    for (auto& sound : soundEffects) {
        Mix_FreeChunk(sound.second);
    }
    soundEffects.clear();

    for (auto& music : musicTracks) {
        Mix_FreeMusic(music.second);
    }
    musicTracks.clear();
}

A team of four, led by Saarrthi Rana. Built on the course's C++ framework.

Next project

Knight Demo

A 2D action demo in Unreal Engine 5, made with Paper2D and Blueprints as a group school project.