An SDL codebase is given with this tutorial, called QuickCG. All the C++ code in
the articles uses QuickCG and its functions to draw graphics on the screen.
This page will explain how to use QuickCG to get the examples working.
Information is provided to get SDL and QuickCG working in the DevC++ IDE, as
well as how to do ith with g++.
Then a few examples of scripts for QuickCG follow.
And finally, tables are given with all the QuickCG functions, some C++
functions and operators, QuickCG and SDL datatypes, and all the SDL names for
keyboard keys.
SDL is an API that can handle graphics, audio, keyboard, mouse and joystick
input on many platforms, including Windows and Linux. SDL can also be used to
initialize and use OpenGL for 3D graphics. The codebase of this tutorial uses
SDL for it's graphics and input, but the rest of the focus of this tutorial is
on the graphics algorithms, and not SDL. In this tutorial SDL's functions are
rarely used directly (but through QuickCG instead), though it's always handy to
learn a thing or two about SDL if you'd like to make some games with it.
QuickCG was designed specifically for this tutorial, to be as easy to use and
as universal as possible and to feel a bit like a scripting language. QuickCG
includes functions to set up the screen, draw pixels and 2D primitives on it,
handle input, do color model conversions, print texts and numbers on the screen
using a built in font, load pictures, etc... QuickCG is less useful for making
actual games because it's not really optimized.
A full list of all functions it contains is further on this page.
DevC++
This section is for if you want to use DevC++ in Windows to run the examples.
DevC++ is a free IDE for Windows, that uses the compiler gcc 3.2 or later.
For help on using DevC++, I suggest you check out the help files, try to open
and compile some of the included examples, check out the bloodshed.net website,
or just read on here.
This chapter is only for Windows users who want to be able to compile QuickCG
and the examples with the free DevC++ IDE. It's also perfectly possible to use
SDL and QuickCG on other platforms, but for now that's not explained here. If
you don't know how, use the SDL site or google to find out how to install it
with your compiler, and then make a new project in your compiler and add the
.cpp and .h files of QuickCG to it.
If you don't have the newest version of DevC++ installed yet, follow these
steps to get it up and running with SDL. If you have it installed, but not yet
the SDL developer libraries, start at step 2).
It's not very easy to install SDL for DevC++, but it is possible to
get the newest version of SDL to work with DevC++, if you follow these
steps very carefully:
1) download and install the latest version of DevCpp from
http://www.bloodshed.net/dev/devcpp.html
2) download the latest version of SDL from
http://www.libsdl.org/: download the
Development Libraries for Mingw32 (under Windows) and the runtime libraries for
Windows. You may need WinRAR to be able to unpack the .tar.gz files.
3) The runtime library contains the file SDL.DLL, put it either in your
C:\Windows\System32 folder, or put one in the folder of your compiled program,
and don't forget that you always have to include SDL.DLL with your program if
you want other people who don't have the file to run it. Programs compiled for
the newest version of SDL won't work with older versions of the DLL.
4) Unpack the SDL developer files, but not yet inside the DevC++ folder,
put them in another folder.
5) Copy the SDL folder of the development files, which is inside the
folder include, into the include folder of DevC++ (so that all the SDL header
files are in DevC++ under include/SDL)
6) Copy the files in the lib folder of the development files, into the
lib folder of DevC++.
Now you have installed DevC++ with SDL! You still need to know how to make a
working project that uses SDL though. The other files in the development package
include the full SDL documentation, example and tests, and the binaries. Your
DevC++ folder structure should now look like this, where the SDL folder is new,
and the lib folder contains new files from the SDL development package.
If you use the SDL codebase (QuickCG) provided with the tutorial, unzip it to
your code folder (that folder where you store all your DevC++ projects and code
in, which is hopefully not in the unreliable My Documents folder of Windows),
and just open it's *.dev file with Dev-C++, and it should compile and run
correctly. To compile it, use "Execute -> Compile" from the menu. The exe file
will appear in the same folder as where your code is located. You can run it
with "Execute -> Run". You can also compile&run your projects by pressing F9. If
it doesn't work, look a bit further for a list of possible solutions. It's
highly recommended that you use QuickCG for this tutorial, because the rest of
the tutorial focuses on the commands of it and not on the actual SDL itself.
But if you want to make a new SDL project of your own in DevC++ (not needed
for this tutorial):
7) Start up DevC++ and make a new project. Add any .cpp and .h files
you need to it.
8) In the menu Project open Project Options and locate the setting for
Linker Commands (in DevC++ 5 Beta 8.10 it's under Parameters) and add:
-lmingw32
-mwindows
-lSDLmain
-lSDL
Note that the order IS important! If you put -lSDL first and then -lSDLmain it
won't work!
9) In your cpp files, #include <SDL/SDL.h>
10) Under Linker options, turn off the console.
11) Under Compiler in Project Options, and under the general Compiler
Options, turn on all settings for optimizations, because they really make a big
difference! You can also add -s to the linker parameters to reduce the size of
the *.exe files it generates, if you don't do this, the exe's will be full of
debugging information, even if the debugging setting is off (as of DevC++ 5 Beta
8.10)
If you're getting compiler errors when trying to compile your SDL project, here
are some things to check:
- Did you follow steps 1-6 or 1-11 correctly, in the correct order? Read
every detail?
- Did you #include the SDL.h header file in the code?
- Is the SDL.h file in the correct folder of DevC++? (if it's in the SDL
folder inside include, you have to include it with #include "SDL/SDL.h", if
it's directly in the include folder, use #include "SDL.h")
- Are all the other SDL header files in the correct folder of DevC++?
- Are the SDL library files (libSDL.a and libSDLmain.a) correctly in the
lib folder of DevC++?
- Did you put -lSDLmain and -lSDL in the correct order in the Linker
parameters?
- Are the options -lmingw32 -mwindows -lSDLmain -lSDL correctly in the
linker parameters, and not wrongly in the compiler parameters?
- Did you make a main function?
- Try if the FAQ on the
http://bloodshed.netorhttp://www.libsdl.org
site answer your problem. There's info about DevC++ in the faq from
libsdl.org. These sites also have search functions.
- Try searching for some keywords from the compiler or linker error with
google. You might find forum posts which describe your problem.
- If you have a different version of DevC++ than version 5 Beta 8.10,
things might be different (I encourage using the newest version, there may
be more recent information available than this tutorial). Try to find the
correct way to tell the linker to add the files libSDLmain.a and libSDL.a,
and give the linker the options -lmingw32 and -mwindows, and the correct
folders for include and library files.
- If you're trying to get your own project to work, try if QuickCG that
you can download with this tutorial works correctly (open it's *.dev file,
compile and run it). If it does, but your own project doesn't, try giving
your project the same settings as the project from QuickCG.
Once you got it to work, you can run example code of the tutorials by opening
the quickcg.dev file with DevC++ and going to main.cpp file where you can enter
the code. Here's a screenshot of the main.cpp file after opening it.
g++
This section is for if you want to use g++ to run the examples.
With g++ it's quite easy: place all .cpp and .h files of QuickCG in the same
directory, put the example code of tutorials in the main.cpp file, and then you
can compile it with the following command:
g++ *.cpp -lSDL -O3
The -O3 is optional if you want compiler optimization. The -lSDL lets it link
to SDL. This requires you to have SDL installed on your distro (look for sdl and
sdl-devel packages or similar).
Using QuickCG
QuickCG currently exists out of the following code files and a .dev file:
- QuickCG.cpp: this contains all the functions of QuickCG
- QuickCG.h: #include this header file in all *.cpp files where you want
to use QuickCG's functions
- Q3DMath.cpp: contains 3D functions (currently not used anymore in the
tutorial)
- Q3DMath.h: #include this if you want to use the 3D functions and classes
(currently not used anymore in the tutorial)
- main.cpp: in this file you can type the code of the examples of this
tutorial. Leave the #includes at the top of the file intact to be able to
run all examples.
- QuickCG.dev: this is the project file for DevC++ that contains all
necessary settings, if you don't use DevC++ you can remove this file and
make your own project that uses the .cpp and .h files.
The main.cpp file file is where you can put the code of the examples of this
tutorial. If no main function is mentioned in a code example, make one yourself
and put the example inside the main function.
There's normally a small example included in the main.cpp file:
int main(int argc, char *argv[])
{
screen(256, 256, 0, "Small Test Script");
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
pset(x, y, ColorRGB(x, y, 128));
}
redraw();
sleep();
}
|
Here's what it does:
- The screen function will create the graphical window, with a
resolution of 256*256 pixels, windowed, and with the caption "Small Test
Script". You always have to use the screen function at the start of any
program, or you'd be drawing pixels on a non existing window.
- Next comes a double for loop, which goes through every pixel of the
window: w and h are global variables that contain the
width and the height of the window.
- Inside the for loops is the function pset. This function will
plot a pixel at location x, y with RGB color x, y, 128. This means the red
color component will depend on the location x of the pixel, the green color
component will vary with the location y of the pixel, and blue component
will always be 128.
- After the loop comes the function redraw. You always have to
use this function before you'll be able to see any new pixels you've drawn.
The redraw function is relatively slow, so do NOT call it after every single
pixel, but only once after all pixels of the screen have been drawn.
- Finally, the sleep function will let the program pause until
you press any key. If the sleep function wouldn't be there, the program
would close immediately and you wouldn't be able to see the result.
If you compile and run this code, the output will be as follows:
Now try to change the program a bit, for example, copypaste this instead into
the main function (use CTRL+SHIFT+i to indent it in DevC++5):
int main(int argc, char *argv[])
{
screen(256, 256, 0, "Small Test Script");
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
pset(x, y, ColorRGB(0, 128 + 128 * sin(x / 8.0), 0));
}
print("Hello World!");
redraw();
sleep();
}
|
What has changed, is that you'll now see a green sine function pattern, and a
text "Hello World!", because putting text on the screen is what the
print
function does. The output should look as follows:
Now another, easier, example:
int main(int argc, char *argv[])
{
screen(256,256, 0, "A Face!");
drawDisk(128, 128, 100, colorRGB(255, 128, 200));
drawDisk(88, 100, 10, colorRGB(0, 0, 255));
drawDisk(168, 100, 10, colorRGB(0, 0, 255));
drawLine(88, 150, 168, 150, colorRGB(255, 0, 0));
redraw();
sleep();
}
|
The
drawDisk command will draw a filled circle at the given position,
with a certain radius and RGB color. The disk function is called 3 times: to
draw a face, and two eyes.
The
drawDine command will draw a line.
The
redraw() and
sleep() commands are again needed so that the
new pixels will show up, and so that the program won't close immediately.
The output should look like this:
A full list of all commands is below, and the next chapter is full of easy
examples of all functions to learn working with QuickCG.