Dennis Schneider

Software Engineer
Microsoft

Blog

How to install SDL2 on Mac OS X

Tuesday, 12th February 2019

Introduction

SDL stands for Simple DirectMedia Layer and provides an abstraction for underlying hardware, such as video, audio and input devices.
It runs on all major platforms like Android, iOS, Windows, Linux and MacOS and several ports for consoles like Playstation 4 and Nintendo Switch exist.

Setup

I am on the latest version of Mac OS X, 10.14.2, Mojave.
I personally prefer the manual installation from source a lot over either installing a .dmg file or using homebrew for installation.

We are going to install both SDL2 (v. 2.0.9) and SDL Image (v. 2.0.4).
SDL Image makes it easy to load images as SDL surfaces and textures with file formats such as JPG, BMP and PNG.

Head over to the SDL2 download page and download the latest SDL2 version.

Unzip the directory and cd into it. Run the following commands:

                        ./configure
make
make install
Header over to the SDL Image page and download the latest SDL Image version.
Unzip the directory and cd into it. Run the following commands:
                        ./configure
make
make install
This should do the trick already.

Compilation and Execution of an SDL2 program

A small example for an SDL2 program:

                        #include "stdlib.h"
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"

int SCREEN_HEIGHT = 800;
int SCREEN_WIDTH = 600;

int main() {
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow("SDL Game", 0, 0, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_WINDOW_HIDDEN);
    SDL_ShowWindow(window);

    SDL_Event event;
    int running = 1;

    while(running) {
        while(SDL_PollEvent(&event)) {
            if(event.type == SDL_QUIT) {
                running = 0;
            }
        }

        SDL_Delay( 32 );
    }   

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}
                    
Now when you compile your code with GCC, make sure to link both SDL2 and SDL2 Image.
                        gcc -o main main.c -lSDL2 -lSDL2_image