How to use SDL2 library in a Vagrant environment
So I was willing to use a library in C that will allow me to make some graphics, this library is called SDL2, and I installed it on a vitual env in vagrant, but when I tried to use it, it was giving me an error, I searched some info online and then I figure out that SDL2 needed a desktop enviroment in order to run, hahaha, pretty obvious right!!. Well so this blog is about that, installing a lightweight desktop environment to use it on a Vagrant environment in order to run SLD2 library, let’s get started.
Keep in mind that this tutorial was made using Vagrant 2.2.14 running on Windows 10.
Let’s begin:
- Create adirectory that will hold your Vagrant file and cd into it:
c:\user\vagrant\> mkdir new_env
c:\user\vagrant\> cd new_env
c:\user\vagrant\new_env>
2. Create a new vagrant env, in my case ubuntu/trusyt64:
c:\user\vagrant\new_env> vagrat init ubuntu/trusty64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.c:\user\vagrant\new_env>
3. On the explorer, open your newly created Vagrantfile and uncomment these lines:
4. Also add these lines in order to allow your VM acces to internet:
5. Save the file and go back to the command prompt to initialize your VM, and ssh into it:
c:\user\vagrant\new_env> vagrant up
...
c:\user\vagrant\new_env> vagrant ssh
...
$
6. Now that you are inside your VM, first update and then install xfce4, by typing the following:
$ sudo apt update
$ sudo apt install xfce4
...
7. Start the graphical environment:
$ sudo startxfce4&
Great!!, after doing this, you should be able to see the GUI appearing, you should see something like this:
Pretty awesome!!, right?. Well now that we have installed a desktop environment, now let’s install SDL2 and see if it works as spected:
Installing SDL2:
- Open a terminal on the desktop env, right click on the screen and select ‘open terminal here’
2. In the terminal let’s create a folder called SDT2 and a file ‘install_sdl2.sh’.
$ mkdir SDT2
$ cd SDT2
SDL2$ touch install_sdl2.sh
3. Open the file install_sdl2.sh and copy the following.
#!/usr/bin/env bash
GREEN='\033[0;32m'
BLUE='\033[0;36m'
NC='\033[0m'
if [ "$(id -u)" != "0" ]; then
echo "Sorry, you are not root."
exit 1
fi
echo -e "Checking PATH..."
if [[ ! "$PATH" =~ (^|:)"/usr/local/bin"(:|$) ]]; then
export PATH="/usr/local/bin:$PATH"
fi
if [ "$(uname)" == "Darwin" ]; then
# Mac OS X
echo -e "\n${BLUE}Installing SDL2 !${NC}"
rm -fr /tmp/SDL2_install
mkdir -p /tmp/SDL2_install
cd /tmp/SDL2_install
echo -e "${BLUE}Downloading SDL sources...${NC}"
if ! curl https://libsdl.org/release/SDL2-2.0.4.tar.gz > SDL2-2.0.4.tar.gz; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Unziping sources ...${NC}"
if ! tar -zxf SDL2-2.0.4.tar.gz; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
cd SDL2-2.0.4
echo -e "${BLUE}Downloading script...${NC}"
if ! curl https://hg.libsdl.org/SDL/raw-file/afd286e26823/build-scripts/gcc-fat.sh > gcc-fat.sh; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
if ! chmod 755 gcc-fat.sh; then exit 1; fi
mkdir build
cd build
echo -e "${BLUE}Configure...${NC}"
if ! CC=/tmp/SDL2_install/SDL2-2.0.4/gcc-fat.sh /tmp/SDL2_install/SDL2-2.0.4/configure; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Building...${NC}"
if ! make; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Installing...${NC}"
if ! make install; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
rm -fr /tmp/SDL2_install
echo -e "\n${BLUE}Installing freetype2 !${NC}\n\n"
#Install freetype2
rm -fr /tmp/freetype2_install
mkdir -p /tmp/freetype2_install
cd /tmp/freetype2_install
echo -e "${BLUE}Downloading freetype2 sources...${NC}"
if ! curl https://s3.amazonaws.com/intranet-projects-files/holbertonschool-low_level_programming/graphics_programming/freetype-2.6.5.tar.gz > freetype-2.6.5.tar.gz; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Unziping sources ...${NC}"
if ! tar -zxf freetype-2.6.5.tar.gz; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
cd freetype-2.6.5
echo -e "${BLUE}Configure...${NC}"
if ! ./configure; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Building...${NC}"
if ! make; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Installing...${NC}"
if ! make install; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
rm -fr /tmp/freetype2_install
echo -e "\n${BLUE}Installing SDL2_ttf !${NC}\n\n"
#Install SDL2_ttf
rm -fr /tmp/SDL2_ttf_install
mkdir -p /tmp/SDL2_ttf_install
cd /tmp/SDL2_ttf_install
echo -e "${BLUE}Downloading SDL2_ttf sources...${NC}"
if ! curl https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.14.tar.gz > SDL2_ttf-2.0.14.tar.gz; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Unziping sources ...${NC}"
if ! tar -zxf SDL2_ttf-2.0.14.tar.gz; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
cd SDL2_ttf-2.0.14
echo -e "${BLUE}Configure...${NC}"
if ! ./configure; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Building...${NC}"
if ! make; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
echo -e "${BLUE}Installing...${NC}"
if ! make install; then exit 1; fi
echo -e "${GREEN}Done!${NC}"
rm -fr /tmp/SDL2_ttf_install
echo -e "\n${GREEN}All set!${NC}"
echo "\n\n"
echo "Don't forget to compile with: \`sdl2-config --cflags\`"
echo "Don't forget to link with: \`sdl2-config --libs\` -lSDL2_ttf"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
# GNU/Linux
if ! apt-get install libsdl2-dev; then exit 1; fi
if ! apt-get install libsdl2-ttf-dev; then exit 1; fi
echo -e "\n${GREEN}All set!${NC}"
echo "\n\n"
echo "Don't forget to compile with: \`sdl2-config --cflags\`"
echo "Don't forget to link with: \`sdl2-config --libs\` -lSDL2_ttf"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
# Windows NT
echo "Can't be installed on Windows..."
echo "Please visit SDL2 website for more informations"
exit 1
else
echo "Can't be installed on this operating system..."
echo "Please visit SDL2 website for more informations"
exit 1
fi
4. Assing this permissions to the file:
SDL2$ chmod 755 install_sdl2.sh
5. Execute the installation file:
SDL2$ ./install_sdl2.sh
...
All set!
\n\n
Don't forget to compile with: `sdl2-config --cflags`
Don't forget to link with: `sdl2-config --libs` -lSDL2_ttf
SDL2$
Great!!, the library have been installed succesfully.
Now le’ts create this library, to draw something, to do this we’ll create 2 files: main.c and my_sdl2.h
my_sdl2.h:
#ifndef MY_SDL2_H#define MY_SDL2_H#include <SDL2/SDL.h>typedef struct SDL_Instance{SDL_Window *window;SDL_Renderer *renderer;} SDL_Instance;int init_instance(SDL_Instance *);
void draw_stuff(SDL_Instance instance);#endif /* MY_SDL2_H */
main.c:
#include "my_sdl2.h"int main(){SDL_Instance instance;/* Only returns 0 when it succeeds */if (init_instance(&instance) != 0)return (1);while (1){SDL_SetRenderDrawColor(instance.renderer, 0, 0, 0, 0);SDL_RenderClear(instance.renderer);draw_stuff(instance);SDL_RenderPresent(instance.renderer);}return (0);}int init_instance(SDL_Instance *instance){if (SDL_Init(SDL_INIT_VIDEO) != 0){fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());return (1);}instance->window = SDL_CreateWindow("SDL2 \\o/", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 300, 200, 0);if (instance->window == NULL){fprintf(stderr, "SDL_CraeteWindow Error: %s\n", SDL_GetError());SDL_Quit();return (1);}instance->renderer = SDL_CreateRenderer(instance->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);if (instance->renderer == NULL){SDL_DestroyWindow(instance->window);fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError());SDL_Quit();return (1);}return (0);}void draw_stuff(SDL_Instance instance){SDL_SetRenderDrawColor(instance.renderer, 0xFF, 0xFF, 0xFF, 0xFF);SDL_RenderDrawLine(instance.renderer, 10, 10, 100, 100);}
All that this code code does is to draw a line, let’s compile and see if it works.
$ gcc -c main.c `sdl2-config --cflags`
$ gcc -o draw main.o `sdl2-config --libs`
Awesome!!, until this point, a ‘draw’ executable file has been created, now we need to execute it and see a new window with a white line in it:
$ ./draw&
And you should see something like this:
Well this is just an alternative, if you think this can help you, then you can try this solution, also I want to mention that sometimes there are problems with the terminal in GUI, but you can overcome this by using the terminal from windows and using the Desktop environment on vagrant just to run the executables.
Hope this helped you in some way, take care, and, see you!!.