Skip to content Skip to sidebar Skip to footer

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.

Many companies in the industry actually rely on SDL, among them well-known game developer Valve, which are using SDL for their Source Engine or Crytek for their CryEngine.

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.

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

Head 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!

Now let’s have a look at a basic running example in SDL 2.

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

That’s it! Run your executable with ./main and observe a basic Window created with SDL.

Leave a comment