Simple DirectMedia Layer (SDL) is a free, open source library that simplifies cross-platform development. In this course, we will use SDL 2.0.
Most tutorials and articles on the Web deal with SDL 1.2. There are significant diffences between SDL 1.2 and 2.0, so keep this in mind.
Create folder ~/441/sdl to store the files you create to complete this assignment.
Create file ~/441/sdl/main.cpp with the following contents.
#include <iostream> #include "SDL.h" int main(int argc, char * argv[]) { if (SDL_Init(SDL_INIT_EVERYTHING) == -1){ std::cout << SDL_GetError() << std::endl; return 1; } SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_INFORMATION, "Hello Program", "Hello SDL", NULL); return 0; }
This file comprises our entire program at this point. Your objective is to setup a project that builds this program correctly. This depends on the development environment you are working in.
If you are working under windows, skip this section.
Download the source distribution of SDL2. Expand and run the following in the expanded folder.
./configure make sudo make install
Create ~/441/sdl/build-osx.sh with the following contents.
clang++ `sdl2-config --libs --cflags` main.cpp
Make the build script runnable.
chmod +x build-osx.sh
Run the script and run the resulting executable.
./build.sh ./a.out
The build command above builds an executable that dynamically links to SDL. If we distribute a program with dynamic linkage, then we need to add the SDL dynamic library to the application bundle. I hope to cover this in a future assignment.
When targeting iOS, I believe we will need to build our program with static linkage to SDL. The following command shows how to statically link when targeting OS X. The command to build for iOS should be similar.
clang++ `sdl2-config --libs --cflags --static-libs` -Bstatic main.cpp
In Xcode, select File ... New ... Project. Create a Cocoa Application under the OS X Application category. Specify osx as the application name. Click Next and navigate to ~/441/sdl. This will create a folder ~441/sdl/osx with the project files in it.
Delete and move to trash the following files.
Add main.cpp to the project.
Add /usr/local/lib/libSDL2.dylib to your project frameworks. To "go to" hidden folder /usr/local in the finder window, press SHIFT OPTION G and enter the path /usr/local.
Select Buid Settings. Go to the Search Paths section. Double click the rightmost part of the line titled User Header Search Paths. Add the following path.
/usr/local/include/SDL2
Build and run.
Download the development libraries for Windows. The file name is SDL2-devel-2.0.3-VC.zip.
Expand the zip file and move the extracted SDL2-2.0.3 folder to your home dir. In my case, this is the following.
C:\Users\turner\SDL2-2.0.3
Create an empty C++ project, call the project “win” and place under ~/441/sdl.
Add ~/441/sdl/main.cpp to the project.
Open the project properties window and make the following changes.
Path=$(PATH);$(HOMEPATH)/sdl2-2.0.3/lib/x86
$(HOMEPATH)\sdl2-2.0.3\include
$(HOMEPATH)/sdl2-2.0.3/lib/x86
SDL2.lib SDL2main.lib
Build and run.
In the project you have set up, do the SDL 2 Tutorial. Keep platform-independent code in an src folder that is separate from any platform-dependent project files.
As you progess through the tutorial, you will need to download the libraries SDL2_image and SDL2_ttf and configure your project to use them.
Commit and push your work into your remote repository. Make sure that you omit derived, temporary and user-specific files.