Skip to content

Instantly share code, notes, and snippets.

@james-jhang
Last active December 8, 2022 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james-jhang/e16605018e4bee141e61f40a2c0a5789 to your computer and use it in GitHub Desktop.
Save james-jhang/e16605018e4bee141e61f40a2c0a5789 to your computer and use it in GitHub Desktop.
Install SDL2

Install SDL2 on WSL

Install SDL2 on MacOS

SDL2 tutorial

Install SDL2 on WSL

Install VcXsrv on Windows

  1. Download the installer from https://sourceforge.net/projects/vcxsrv/
  2. Install the VcXsrv by the installer.
  3. Open XLaunch and keep clicking "Next" until you see the option: "Disable access control". Enable this option.
  4. At the last step, save the config file: "config.xlaunch" at a path you like.

Install SDL2 on WSL

sudo apt-get update
sudo apt-get install libsdl2-dev libsdl2-gfx-dev libsdl2-image-dev

Setup VcXsrv on WSL

  1. vim ~/.bashrc

Paste the 5 lines below.

alias xlaunch='/c/Program\ Files/VcXsrv/xlaunch.exe -run ~/Projects/config.xlaunch'
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
export LIBGL_ALWAYS_INDIRECT=1
export XDG_RUNTIME_DIR=/home/james/Projects
export RUNLEVEL=3

The path at the first line: "~/Projects/config.xlaunch" can be a path you like, same for the "/home/james/Projects" at the fourth line. Make sure these two pathes are the same folder. If you are using WSL1, please change the export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0 to export DISPLAY=:0. You can type wsl -l -v on PowerShell to check which version you are using.

  1. source ~/.bashrc

Make sure you reopen all terminals that are opened before setting the .bashrc.

Build and run the main program

g++ main.cpp -lSDL2 -lSDL2_image -o mySDL; ./mySDL

Install SDL2 on MacOS

brew install SDL2
brew install SDL2_image

Make sure you reopen all terminals that are opened before installing the SDL2 by brew.

Build and run the main program

g++ main.cpp -lSDL2 -lSDL2_image -o mySDL; ./mySDL

SDL2 tutorial:

https://lazyfoo.net/tutorials/SDL/

If an example uses #include <SDL.h>, just replace it with #include <SDL2/SDL.h>, same for other SDL libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment