This tutorial is simply going to show you how to implement the SPriG library using Tim’s SDL_Image tutorial that can be downloaded from www.sdltutorials.com in a two part series. This tutorial doesn’t resort as a step-by-step technical report on the ins and outs of SPriG, but a quick installation, and useful manual on how to implement SPriG using Tim’s SDL_Image tutorial in 2 parts using small examples, so you can study and use SPriG on your own free time.
I’m going to be using Code Blocks and MinGW compiler, since the SPriG tutorial is already in that format, and to make a Visual Studio compiler edition you would have to download the source file, and compile it using the Visual Studio compiler, or simply in Visual Studio switch your compiler to MinGW.
Installing SPriG
Like installing many of SDL third party libraries simply copy and paste the header files from the libSPriG folder in the include\SDL folder where all SDL header files resides on your hard drive. Afterwards, copy and paste the libSPriG.a file to the .lib MinGW, so CodeBlocks know where to look on your hard drive when using SPriG. Last, copy and paste the SPriG.dll into the WINDOWS\System32 folder, and you’ve successfully installed the SPriG library. In the folder there’s going to be a SPriG function list .txt file, for ease of programming I would stick that on my desktop, and resort to it when needed.
Implementing SPriG in CodeBlocks using SDL_Image Tutorial
Now that we have successfully told the compiler where the SPriG library is we can now tell the project where SPriG is, so we can use it. Open up Tim’s SDL_Image project, and go to Project > Build Options. Find the tab labeled “Linker Settings”, and click the add button. Simply add “libSPriG” and then click ok.
I’ll be adding or what is called implementing the SPriG library into Tim’s project using his CSurface.h file, so go there, and add the following.
#include <SDL\SPriG.h>
#include <SDL\SPriG_Inline.h>
These are the header files that loads all functions that SPriG has predefined for us to use. Without these header files if we tried to call a specific SPriG function we would get errors.
Example 1: Working with Primitives!
Let’s create a small example. In the CSurface.h file let’s add some static boolean functions much like his OnDraw functions that he calls in later source files.
Add the following after the static call of OnDraw in the CSurface.h file.
//Drawing primitives, transforming surfaces, and drawing polygons using SPriG library
//Basic Primitives (You can draw more if needed)</strong>
static bool HorizontalLine(SDL_Surface *surface, Sint16 x1, Sint16 y, Sint16 x2, Uint32 Color);
static bool VerticalLine(SDL_Surface *surface, Sint16 x, Sint16 y1, Sint16 y2, Uint32 Color);
static bool Rect(SDL_Surface *surface, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
static bool Circle(SDL_Surface *surface, Sint16 x, Sint16 y, float r, Uint32 color);
static bool CircleFilled(SDL_Surface *surface, Sint16 x, Sint16 y, float r, Uint32 color);
These are going to be the simple boolean calls we call in the CSurface source file that will draw the basic primitives we’ve selected that SPriG can draw on the fly.
Next open up the CSurface.cpp file, and the following lines of code.
/*===============================================================================
*Seperation from Tim's SDL_Image tutorial CPP file to Carl' SPriG source file</strong>
=================================================================================*/
bool CSurface::HorizontalLine(SDL_Surface *surface, Sint16 x1, Sint16 y, Sint16 x2, Uint32 Color)
{
if(surface == NULL) return false;
SPG_LineH(surface, x1, y, x2, Color);
return true;
}
bool CSurface::Rect(SDL_Surface *surface, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
{
if(surface == NULL) return false;
SPG_Rect(surface, x1, y1, x2, y2, color);
return true;
}
bool CSurface::Circle(SDL_Surface *surface, Sint16 x, Sint16 y, float r, Uint32 color)
{
if(surface == NULL) return false;
SPG_Circle(surface, x, y, r, color);
return true;
}
The following adds the source of the Rect, Circle, and HorizontalLine function that we hadded in the header file. As I mentioned before I’m not going to write a full tutorial on SPriG, and teach you step-by-step, but simply by short examples teach you how to explore SPriG on your own by showing you how to implement this in Tim’s tutorials.
OK, so now let’s make a quick Test run. If you compile and run this you’ll simply get Tim’s old SDL_Image .exe file, for we changed the code, but haven’t modified the screen a bit. This is all building up on modifying the screen, and drawing our primitives on a fly, so when we need to do so we won’t have to work as much.
Drawing the Primitives
Now all we must do now is in the Capp_OnRender is call our functions we defined in the CSurface class, so we can have some nice primitives on the screen. I have made a significant comment after the surface displays on Tim’s project, and where he flips the screen for the double buffer, for organization purposes. So in between Tim drawing the surfaces onto the screen, and flipping the screen add the following code.
//================================================
//draw 2 circles one filled one non filled 50 pixels away from each other
CSurface::Circle(Surf_Display, 250, 250, 5, 250);
CSurface::CircleFilled(Surf_Display, 300, 250, 5, 250);
//draw a rectangle
CSurface::Rect(Surf_Display, 100, 100, 300, 300, 250);
//draw a trigon "triangle"
CSurface::Trigon(Surf_Display, 100, 100, 100, 200, 200, 200, 250);
//===================================================
That’s going to be it for Part I of deploying SPriG using Tim’s SDL_Image tutorial. In part 2 I will transform surfaces by rotating and scaling surfaces while they’re being rendered, so they have the look of rotating in real-time. Until then…