SDL_prims — simple 2D graphics primitives for SDL
SDL_prims is (yet another) implementation of simple 2D
graphics primitives for SDL. (The others that I know of either try to
do way too much or else fail horribly to compile on some
architecture/OS or other.) SDL_prims is presented as a
single C source file (with accompanying header file) that you can just
compile and link into the rest of your SDL-based project.
SDL_prims provides:
- pixel and line drawing;
- wide line drawing; and
- drawing or filling of rectangles, polygons and circles.
All of the above work in 8-, 16-, 24- and 32-bit depths and will obey the
clipping rectangle set for the destination surface.
SDL_prims was an evening hack to add some badly-needed
functionality to an SDL project; as such it currently does not provide
antialiasing or alpha blending, nor does it have any documentation
beyond the notes below. Sorry!
Download the source code: SDL_prims-0.2.tar.gz
Browse the source code: SDL_prims-0.2
A trivial program is included with the library that demonstrates some
of the shapes that can be drawn. (Just type make and hope for
the best.)
SDL_prims is distributed under the MIT license. It will not
infect your project with a contagious disease if you decide to use
and/or modify it.
Using the library
Drop the SDL_prims.c file next to your other source files and
add it to your Makefile. Include the SDL_prims.h in
any source file that needs to call any of the following functions:
- int SDL_DrawPixel(SDL_Surface *sfc, int x, int y, Uint32 colour)
draws a single pixel of the given colour at x,y.
- int SDL_DrawHLine(SDL_Surface *sfc, int x1, int y1, int x2, Uint32 colour)
Draws a 1 pixel-wide horizontal line in the given colour from x1,y1 to x2,y1 inclusive.
- int SDL_DrawVLine(SDL_Surface *sfc, int x1, int y1, int y2, Uint32 colour)
Draws a 1 pixel-wide vertical line in the given colour from x1,y1 to x1,y2 inclusive.
- int SDL_DrawLine(SDL_Surface *sfc, int x1, int y1, int x2, int y2, Uint32 colour)
Draws a 1 pixel-wide line in the given colour from x1,y1 to x2,y2 inclusive.
- int SDL_FillLine(SDL_Surface *sfc, int x1, int y1, int x2, int y2, unsigned int width, Uint32 colour)
Draws a line of the given width in the given colour from x1,y1 to x2,y2 inclusive.
The 'line cap style' is 'butt'; i.e., the line includes, but does not extend beyond, the endpoints.
- int SDL_DrawRect(SDL_Surface *sfc, SDL_Rect *rect, Uint32 colour)
Draws a 1 pixel-wide rectanglular outline in the given colour according to rect.
- int SDL_DrawCircle(SDL_Surface *sfc, int x, int y, int radius, Uint32 colour)
Draws a 1 pixel-wide circle of the given radius in the given colour centred on x,y.
- int SDL_FillCircle(SDL_Surface *sfc, int x, int y, int radius, Uint32 colour)
Fills a circle of the given radius in the given colour centred on x,y.
- int SDL_DrawPolygon(SDL_Surface *sfc, SDL_Point *vertices, int nVertices, Uint32 colour)
Draws a 1 pixel-wide closed polygon in the given colour between the given vertices.
Each element of the vertices is a SDL_Point (see below). The polygon is closed implicitly
by drawing a line from the last vertex back to the first.
- int SDL_FillPolygon(SDL_Surface *sfc, SDL_Point *vertices, int nVertices, Uint32 colour)
Just like DrawPolygon except that the polygon is filled with colour. Self-intersecting polygons
are handled according to a simple 'parity' rule: each successive pair of intersections between a given scan line
and the outline of the polygon delimits a region of pixels lying inside the polygon, with all other pixels lying outside it.
All of the above functions answer 0 if they succeed, or -1 if they fail.
The SDL_Point structures that represent vertices in the polygon functions are simply the first half of the familiar
SDL_Rect structure:
typedef struct SDL_Point {
Sint16 x;
Sint16 y;
} SDL_Point;
If you fix any bugs, please send your fixes to Ian Piumarta by email
at 'first-name at last-name dot com'. Thanks!