Day 40/100 100 Days of Code

Day 40/100 100 Days of Code

Rebuild Back Better

In early 2022, I joined my City's Global Game Jam for fun. The result was a cookie clicker game named Build Back Better where the player had to collect nutrients to help a tree become bigger.

I wanted to add more features and create a complete game but never got around it because I was unsure if I wanted to keep using Unity and decided to focus on CS50 instead.

After CS50, I experimented with web development which I didn't find as interesting as computer programming and I found older languages intriguing and much more fun to work with.

At first, I tried to develop the game with the Allegro library but couldn't install it with Clion and I decided to use SDL instead since multiple games used it.

I decided to use C++ instead of C because Object Oriented Programming works quite well in Game Development and I prefer to work with it to keep the codebase tidy.

Today, I set up the top-level CMakeLists.txt and added SDL3 as a submodule. For now, CMake only creates a MacOS because I am using a Macintosh Laptop.

CMakeLists.txt

cmake_minimum_required(VERSION 3.28.3)
project("RebuildBackBetter" LANGUAGES CXX VERSION 0.1 DESCRIPTION "Originally made during GGJ22") 
set(CMAKE_CXX_STANDARD 20)

# Handle Submodules: https://cliutils.gitlab.io/modern-cmake/chapters/projects/submodule.html 
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
    option(GIT_SUBMODULE "Check submodules during build" ON)
    if(GIT_SUBMODULE)
        message(STATUS "Submodule update")
        execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
                        WORKING_DIRECTORY "./modules/"
                        RESULT_VARIABLE GIT_SUBMOD_RESULT)
        if(NOT GIT_SUBMOD_RESULT EQUAL "0")
            message(FATAL_ERROR "git submodule update --init --recursive failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
        endif()
    endif()
endif()

# Add the following for each submodule to make sure that it exists
# before downloading the project
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/modules/SDL/CMakeLists.txt")
    message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

# Handle Libraries
add_subdirectory("${PROJECT_SOURCE_DIR}/modules/SDL")
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
add_executable(${PROJECT_NAME} MACOSX_BUNDLE ./src/main.cpp ./src/game.h ./src/game.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3)

The text sets the C++ standard to version 20, installs the submodules if they do not exist, and checks if they have been installed properly. Then, it handles library linking and creates a .app bundle container that holds all the files needed to run the software.

Source files

Currently, the application only creates a window. Here's the code:

// main.cpp
#include "game.h"

int main()
{
    Game game;
    return 0;
}
//game.h
#include <SDL.h>
#include <iostream>

class Game
{
public:
    Game();

// Game Initialisation
private:
    static const int height;
    static const int width;

    static SDL_Window *window;
    static bool Initialise();

//};
// game.cpp
#include "game.h"

const int Game::height = 720;
const int Game::width = 1280;
SDL_Window *Game::window = nullptr;

bool Game::Initialise()
{
    // Make sure that video and audio have been initialised
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        std::cout << "Failed to initialise SDL" << std::endl;
        return false;
    }

    // Create Window
    window = SDL_CreateWindow("Rebuild Back Better",
                              width,
                              height,
                              0);

    // Set window position
    int windowPOSOutput = SDL_SetWindowPosition(window,
                                                SDL_WINDOWPOS_CENTERED,
                                                SDL_WINDOWPOS_CENTERED);

    if (window == nullptr)
    {
        std::cout << "Failed to create window" << std::endl;
        return false;    
    }

    return true;
}

Game::Game()
{
    if (!Initialise())
    {
        exit(-1);
    }

    std::cout << "Start Game" << std::endl;

    SDL_Event events;
    bool quit = false;

    while(!quit)
    {
        while(SDL_PollEvent(&events))
        {
            if (events.type == SDL_EVENT_QUIT)
            {
                quit = true;
            }
        }    
    }


    SDL_DestroyWindow(window);
    window = nullptr;
    SDL_Quit();
}