Gameboy emulator Part 1: setting up SDL

This has been a project that I have started and stopped so many times!

This time around lets at least finish the CPU and get a display up and running!

Chosen technologies

For this project, I want to use the following:

  • C/C++
  • SDL 2/3
  • CMake

So why these technologies? Well a lot of them I do want to learn to a competent degree (alongside Assembly) and I’d feel so much better playing (legally…obtained) GameBoy ROMs on an emulator I’d written myself right?

C/C++

I’ll be sticking with this language as it is pretty low level without dropping to Assembly and tying yourself to a physical architecture.

I do like the idea of getting things done from scratch, and being able to cross compile for different systems such Windows, Linux, WASM and RISC OS and being able to develop on different platforms than those to ship on.

SDL 2/3?

I was on the fence about using the newer version, but this video from Gamefromscratch convinced me that it would be worth moving to 3 and dealing with the different API/lack of tutorials etc out there (compared to SDL2)

I’ve used SDL2 before and I’m keen to continue using it as the framework of choice for developing this project (as opposed to the likes of SFML or a full blown game engine)

Hmm seems that installing SDL3 might be a bit tricker that just running the SDL2 commands:

sudo apt-get install libsdl2-dev
sudo apt-get install libsdl2-image-dev
sudo apt-get install libsdl2-ttf-dev
sudo apt-get install libsdl2-mixer-dev

SDL3 needs to be installed from source at this stage, at least until package managers start shipping it.

So I followed the instructions on the git repo and it didn’t want to work for me, all sorts of errors 🙁 but the instructions here work (for each of the libraries too)

git clone https://github.com/libsdl-org/SDL
cd SDL
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release --parallel
sudo cmake --install . --config Release

SDL_ttf complained about finding FreeType, try:

Go on and click on the sourceforge link.

Download the tar.xz file.

Extract with tar -xf freetype-<version>.tar.xz, replacing <version> by the version you have, then move inside the extracted folder.

You can find documentation for the installation depending your system, on the doc/ folder.

Then build the package :

sh autogen.sh
./configure # this will install in /usr/local. See --help for moving target
make
sudo make install

https://gist.github.com/NoxFly/1067c9fc24024d26b51a6825de5cff74#freetype

On my laptop I needed to run this afterwards so that CLion could find SDL3 libraries:

sudo ldconfig

After this, a small test program:

/ Example program:
// Using SDL3 to create an application window

#include <SDL3/SDL.h>

int main(int argc, char* argv[]) {

    SDL_Window *window;                    // Declare a pointer

    SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL3

    // Create an application window with the following settings:
    window = SDL_CreateWindow(
        "An SDL3 window",                  // window title
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );

    // Check that the window was successfully created
    if (window == NULL) {
        // In the case that the window could not be made...
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    // The window is open: could enter program loop here (see SDL_PollEvent())

    SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

    // Close and destroy the window
    SDL_DestroyWindow(window);

    // Clean up
    SDL_Quit();
    return 0;
}

Then compile with the following command:

gcc -o main main.cpp -L/usr/local/include/SDL3 -lSDL3

Looks like I might be learning something here as that did not take me long to figure out

I do think that this means all the other libraries will need to be made by hand, but I think I might just be able to figure that out (and document it here)

Leave a Reply

Your email address will not be published. Required fields are marked *

Hey!

Hello! I’m doglitbug, a 40 yo software engineer from New Zealand. This blog is to chronicle my exploits outside of work, hobbies etc!

Categories