Simple DirectMedia Layer, or SDL for short, is a great library for building applications which run outside the terminal or command prompt in their own window. Although the program does not require a prompt to run, I believe it helps to have something simple to print values to, so the following posts will use a mixture of output from the prompt and graphics in the window.
Setup:
To prepare the SDL library (and most other libraries), we need header files and library files. Header files have a .h extension and are used to access the functions and structures found in the library. Library files have several extensions: .lib, .a, .la, .dll, and possible others; and contain the code necessary for the library functions to work. Library files must be linked into a program during its transition from C code to machine language.
To learn more about this transition, an informative video can be found at http://www.youtube.com/watch?v=gUKXew-0L1I and an in-depth page at http://www.tenouk.com/ModuleW.html.
I will attempt to cover setup for multiple systems, but please review the pages http://lazyfoo.net/SDL_tutorials/lesson01/index.php and http://content.gpwiki.org/index.php/SDL:Tutorials:Setup because they will be much more insightful. To obtain the header and library files for your system, visit the SDL 1.2 download page at http://www.libsdl.org/download-1.2.php. Unfortunately, I have not found a working online compiler with SDL support.
For Linux:
Downloading from the site is optional because you can use package managers. There are the packages "libsdl1.2-dev" or "SDL-devel-1.2" for SDL and "build-essentials" or "Development Tools" if one of them is not already installed. These should place the necessary header and library files in the "include" and "lib" folders. Otherwise, try running the RPM file from the site.
To link the libraries to the program with GCC, add -l(library name) after the usual compile command. For example, "gcc (file name).c -o (program name) -lSDLmain -lSDL" will link the basic SDL library to the created program.
For Mac:
Currently, there does not appear to be a link under "development libraries" as seen in the Lazy Foo Tutorial pages. Hopefully, the page http://macemulators.wordpress.com/2009/11/05/how-to-install-sdl/ will provide better assistance.
I believe the GPwiki link will be more thorough explaining how to setup a working SDL project in xcode. I apologize for not knowing much about SDL on Mac OSX :(.
For Windows:
If you use visual studio, download the VC package under "development libraries" and extract the contents. There should be an "include" and "lib" folder as well as some DLL's in "bin". To get the include and library files to visual studio, you can add the lib and include folder locations to Tools->options->VC++ Directories. Alternatively, you can take the contents of these folders and add them to the existing include and lib folders where visual studio is installed (usually C:/Program Files/Microsoft Visual Studio/VC). Also, in linker settings, specify the subsystem to be "console" preferably, or "windows" if that does not work.
If you use MinGW without a compiler, place the lib and include folder contents in MinGW's lib and include folders. This can usually be found in C:/MinGW/. To link libraries, see the above instructions for GCC under For Linux. If you downloaded the vc package, the .lib libraries can be linked with -l:(library file with extension). For example, "gcc (file name).c -o (program name) -lmingw32 -l:SDLmain.lib -l:SDL.lib" will link the basic SDL library to the program.
The DLL's are not needed during compilation, but must be present when your program is run. This means they can be placed right next to the program or in the system32 or syswow64 folder in C:/windows/. For portability, it is recommended you place the DLL's in the same folder as your program.
Linking libraries in IDE's:
Most IDE's, like Code::Blocks, Eclipse, Visual Studio, and Xcode, have features to help link libraries called linker options. This usually allows you to find the library files manually and add them to a project. For IDE's running the GCC/MinGW compiler (Xcode, Code::Blocks, or Eclipse) link the library files "libSDL.la" and "libSDLmain.a" or the .lib files "SDL.lib" and "SDLmain.lib". For Visual Studio, simply link the .lib files from the VC package.
Sample Program:
To test whether SDL is now working with your C compiler, here is a simple C program with SDL integrated:
This short program starts by including the header file "SDL.h", which is all that is required for the basic SDL library. The function main has two parameters used to read from a prompt when starting a program: the first parameter is the number of inputs and the second is an array of strings representing the input. For example, if you run the program with the command "program_name", where program_name is the name of your program, argc will be 1 and argv[0] will be the string "program_name". You can add command-line arguments by typing "program_name argument1 argument2" which makes argc 3 and argv[1] and argv[2] "argument1" and "argument2" respctively.
These posts will rarely use this feature, but it is required to work with c++ programs under visual studio. Next, SDL_Init is given the flag SDL_INIT_EVERYTHING (the value 65535 or 0x0000FFFF), which starts up all parts of the SDL library. SDL_SetVideoMode sets the properties of the window created: here we use a 640 pixels wide, 480 pixels tall, 32-bit color window with software (or processor) rendering, as indicated by the SDL_SWSURFACE flag (value of 0). Finally, a message is printed to the prompt, the program waits 3000 milliseconds, then shuts down the SDL library and terminates.
Text Output:
In order to test the value of variables, it is recommended that printf be able to print to a prompt. For most cases, this will not be a problem, but for instances where the prompt is not visible, check the folder containing the program for a file called "stdout" (with possible extension) after testing the program. This file should contain the text that normally prints to the prompt.
The command "fflush(stdout);" writes any text currently in memory into the file stdout. However, this does not mean changing the input from stdout to file_name_here will create a file called "file_name_here"; stdout is a built-in file for C which refers to the standard output. Without the command, sometimes text output can remain in memory and not be written to the stdout file.
That's all for this post, the next post will cover events and drawing in SDL. For more SDL tutorials, check out http://lazyfoo.net/SDL_tutorials/ and http://content.gpwiki.org/index.php/SDL:Tutorials. Thank you for reading.
Setup:
To prepare the SDL library (and most other libraries), we need header files and library files. Header files have a .h extension and are used to access the functions and structures found in the library. Library files have several extensions: .lib, .a, .la, .dll, and possible others; and contain the code necessary for the library functions to work. Library files must be linked into a program during its transition from C code to machine language.
To learn more about this transition, an informative video can be found at http://www.youtube.com/watch?v=gUKXew-0L1I and an in-depth page at http://www.tenouk.com/ModuleW.html.
I will attempt to cover setup for multiple systems, but please review the pages http://lazyfoo.net/SDL_tutorials/lesson01/index.php and http://content.gpwiki.org/index.php/SDL:Tutorials:Setup because they will be much more insightful. To obtain the header and library files for your system, visit the SDL 1.2 download page at http://www.libsdl.org/download-1.2.php. Unfortunately, I have not found a working online compiler with SDL support.
For Linux:
Downloading from the site is optional because you can use package managers. There are the packages "libsdl1.2-dev" or "SDL-devel-1.2" for SDL and "build-essentials" or "Development Tools" if one of them is not already installed. These should place the necessary header and library files in the "include" and "lib" folders. Otherwise, try running the RPM file from the site.
To link the libraries to the program with GCC, add -l(library name) after the usual compile command. For example, "gcc (file name).c -o (program name) -lSDLmain -lSDL" will link the basic SDL library to the created program.
For Mac:
Currently, there does not appear to be a link under "development libraries" as seen in the Lazy Foo Tutorial pages. Hopefully, the page http://macemulators.wordpress.com/2009/11/05/how-to-install-sdl/ will provide better assistance.
I believe the GPwiki link will be more thorough explaining how to setup a working SDL project in xcode. I apologize for not knowing much about SDL on Mac OSX :(.
For Windows:
If you use visual studio, download the VC package under "development libraries" and extract the contents. There should be an "include" and "lib" folder as well as some DLL's in "bin". To get the include and library files to visual studio, you can add the lib and include folder locations to Tools->options->VC++ Directories. Alternatively, you can take the contents of these folders and add them to the existing include and lib folders where visual studio is installed (usually C:/Program Files/Microsoft Visual Studio/VC). Also, in linker settings, specify the subsystem to be "console" preferably, or "windows" if that does not work.
If you use MinGW without a compiler, place the lib and include folder contents in MinGW's lib and include folders. This can usually be found in C:/MinGW/. To link libraries, see the above instructions for GCC under For Linux. If you downloaded the vc package, the .lib libraries can be linked with -l:(library file with extension). For example, "gcc (file name).c -o (program name) -lmingw32 -l:SDLmain.lib -l:SDL.lib" will link the basic SDL library to the program.
The DLL's are not needed during compilation, but must be present when your program is run. This means they can be placed right next to the program or in the system32 or syswow64 folder in C:/windows/. For portability, it is recommended you place the DLL's in the same folder as your program.
Linking libraries in IDE's:
Most IDE's, like Code::Blocks, Eclipse, Visual Studio, and Xcode, have features to help link libraries called linker options. This usually allows you to find the library files manually and add them to a project. For IDE's running the GCC/MinGW compiler (Xcode, Code::Blocks, or Eclipse) link the library files "libSDL.la" and "libSDLmain.a" or the .lib files "SDL.lib" and "SDLmain.lib". For Visual Studio, simply link the .lib files from the VC package.
Sample Program:
To test whether SDL is now working with your C compiler, here is a simple C program with SDL integrated:
This short program starts by including the header file "SDL.h", which is all that is required for the basic SDL library. The function main has two parameters used to read from a prompt when starting a program: the first parameter is the number of inputs and the second is an array of strings representing the input. For example, if you run the program with the command "program_name", where program_name is the name of your program, argc will be 1 and argv[0] will be the string "program_name". You can add command-line arguments by typing "program_name argument1 argument2" which makes argc 3 and argv[1] and argv[2] "argument1" and "argument2" respctively.
These posts will rarely use this feature, but it is required to work with c++ programs under visual studio. Next, SDL_Init is given the flag SDL_INIT_EVERYTHING (the value 65535 or 0x0000FFFF), which starts up all parts of the SDL library. SDL_SetVideoMode sets the properties of the window created: here we use a 640 pixels wide, 480 pixels tall, 32-bit color window with software (or processor) rendering, as indicated by the SDL_SWSURFACE flag (value of 0). Finally, a message is printed to the prompt, the program waits 3000 milliseconds, then shuts down the SDL library and terminates.
Text Output:
In order to test the value of variables, it is recommended that printf be able to print to a prompt. For most cases, this will not be a problem, but for instances where the prompt is not visible, check the folder containing the program for a file called "stdout" (with possible extension) after testing the program. This file should contain the text that normally prints to the prompt.
The command "fflush(stdout);" writes any text currently in memory into the file stdout. However, this does not mean changing the input from stdout to file_name_here will create a file called "file_name_here"; stdout is a built-in file for C which refers to the standard output. Without the command, sometimes text output can remain in memory and not be written to the stdout file.
That's all for this post, the next post will cover events and drawing in SDL. For more SDL tutorials, check out http://lazyfoo.net/SDL_tutorials/ and http://content.gpwiki.org/index.php/SDL:Tutorials. Thank you for reading.
No comments:
Post a Comment