Kevin De Baerdemaeker

SDL

Modified 2023-03-31

Installation

Setting up the Makefile

Use pkg-config to properly link to the corresponding library binaries.

build:
	g++ `pkg-config --cflags sdl2` -Wall -std=c++17 -I"./libs" src/* `pkg-config --libs sdl2` -llua -o gameengine

run:
	./gameengine

clean:
	rm gameengine

NixOS

I'm using NixOS, so it's pretty easy to setup the development environment using direnv and a shell.nix.

The shellHook sets up the compile_commands.json file when running it through make.

{ pkgs ? import <nixpkgs> {} }:

with pkgs;

mkShell {
  name = "Simple 2D Engine - Shell";
  packages = [
    # used as a language server
    clang
    clang-tools
    # used to generate compile_commands.json
    bear
    # used to link the SDL2 sub dependencies
    pkgconfig
    # project dependencies to trigger autocompletion
    lua5_3_compat
    SDL2
    SDL2_ttf
    SDL2_mixer
    SDL2_image
  ];

  shellHook = ''
    bear -- make
  '';
}

MacOS

Every MacOS already comes with gcc in the form of a clang alias, so we only need to brew install sdl2.

Windows

  1. Install Visual Studio Community Edition
    • Desktop development with C++
  2. Create an empty project
    • Add a main.c to Source Files
  3. Install SDL development library
    • Extract the SDL zip
    • Add the include folder to Include Directories
    • Add the lib folder to Library Directories
  4. Setup the Linker configuration
    • Add the lib folder to Additional Library Directories
    • Add SDL2.lib and SDL2main.lib to the Input dependencies
  5. Add the SDL2.dll in the project folder
  6. Add a main and don't forget the proper arguments required by Windows
#include <SDL2/SDL.h>
#include <stdio.h>

int main(int argc, char* args[]) {
  SDL_Init(SDL_INIT_EVERYTHING);
  printf("Hello World!");
  return 0;
}

References


Backlinks