Graphics drawing primitives and other support functions wrapped up in an add-on, C-based library for the Simple Direct Media (SDL) cross-platform API layer. SDL_gfx supports the original SDL1 library using software surfaces, and SDL2_gfx supports SDL2 using rendering functions.

SDL2_gfx Library

SDL2 graphics drawing primitives and other support functions

Latest Version (Ver 1.0.3 – Mon Feb 20 2017)

SDL2_gfx-1.0.3.tar.gz (.zip) (or on SourceForge)

Current Development Version via SVN

Older versions

SDL2_gfx-1.0.1.tar.gz (.zip)



Doxygen generated API documentation

(C) A. Schiffler, aschiffler [at] 2012-2017, licensed under the zlib license

SDL_gfx Library

SDL graphics drawing primitives and other support functions

The SDL_gfx library evolved out of the SDL_gfxPrimitives code which provided basic drawing routines such as lines, circles or polygons and SDL_rotozoom which implemented a interpolating rotozoomer for SDL surfaces.

The current components of the SDL_gfx library are:

  • Graphic Primitives (SDL_gfxPrimitves.h)
  • Rotozoomer (SDL_rotozoom.h)
  • Framerate control (SDL_framerate.h)
  • MMX image filters (SDL_imageFilter.h)
  • Custom Blit functions (SDL_gfxBlitFunc.h)

The library is backwards compatible to the above mentioned code. Its is written in plain C and can be used in C++ code.

(C) A. Schiffler, aschiffler [at] 1999-2017, licensed under the zlib license




Overview and doxygen generated API documentation


Latest Version (Ver 2.0.26 – Mon Feb 20 2017)

SDL_gfx-2.0.26.tar.gz (or on SourceForge)

Current Development Version via SVN

Older Releases



Supported Platforms

The library compiles and is tested for a Linux target (gcc compiler) and a Win32 target (VisualC, xmingw32 cross-compiler) as well as BeOS and MacOS X PowerBuilder See README for VC and PowerBuilder compile information. When using the cross-compiler (available on the author’s homepage), the build process generates .DLLs. You can use the command line ‘LIB.EXE’ tool to generate VC6 compatible .LIB files for linking purposes.

Language Interfaces

SDL_gfx has been integrated with the following language interfaces:

  • Pascal:
  • Perl:
  • Python:
  • C#:
  • Lua:
Notes on Graphics Primitives

Care has been taken so that all routines are fully alpha-aware and can blend any primitive onto the target surface if ALPHA<255. Surface depths supported are 1,2,3 and 4 bytes per pixel. Surface locking is implemented in each routine and the library should work well with hardware accelerated surfaces. Currently, The following Anti-Aliased drawing primitives are available:

  • AA-line
  • AA-circle
  • AA-ellipse
  • AA-polygon (not filled)
Please check README in archive and .h files for up-to-date interface information.
SDL_gfx / SDL2_gfx
Tagged on:             

13 thoughts on “SDL_gfx / SDL2_gfx

  • June 4, 2017 at 2:09 pm

    You have an off-by-one error in your rotateSurface90Degrees method. It is in the 270 degree rotation part of the switch statement.

    case 3: /* rotated 270 degrees clockwise */
    for (row = 0; row h; ++row) {
    srcBuf = (Uint8*)(src->pixels) + (row * src->pitch);
    dstBuf = (Uint8*)(dst->pixels) + (row * bpp) + (dst->h * dst->pitch);
    for (col = 0; col w; ++col) {
    memcpy (dstBuf, srcBuf, bpp);
    srcBuf += bpp;
    dstBuf -= dst->pitch;

    the initial dstBuf value goes too far in memory by a pitch line.
    dstBuf = (Uint8*)(dst->pixels) + (row * bpp) + (dst->h * dst->pitch);
    should be
    dstBuf = (Uint8*)(dst->pixels) + (row * bpp) + ((dst->h – 1) * dst->pitch);

    imagine if the image were just a line of 10 vertical pixels being rotated 270 degrees causing them to become horizontal.
    dstBuf’s pitch would be 40 if the bytes per pixel were 4.
    say dstBuf’s pixel memory location were 0.
    dst’s pixel data would be from byte 0 to 39.
    dstBuf would first evaluate to 0 + (0 * 4) + (1 * 40) = 40 which is beyond the allocated memory for dst surface.

    hopefully you see the problem.
    I figure you could have figured it out yourself just by looking at the difference in code I presented, but the explanation for the change is to save you some thinking.

    • June 4, 2017 at 9:52 pm

      Thanks for reporting. I’ll the fix into the next release.

  • April 21, 2017 at 5:26 pm

    There’s a bug with drawing horizontal AA lines. If xx0 > xx1, the line gets drawn in the wrong direction: (line 952 in SDL2_gfxPrimitives.c)

    return (hlineRGBA(renderer, xx0, xx0+dx, y1, r, g, b, a));

    Changing the above to:

    return (hlineRGBA(renderer, xx0, xx0+(xdir*dx), y1, r, g, b, a));

    fixed the problem as xdir was set to -1 when xx0 > xx1

    • May 2, 2017 at 8:07 am

      Thanks for reporting.

  • March 16, 2017 at 5:39 am

    SDL_gfx-2.0.26 link is pointing to the SDL_gfx.2.0.25.tar.gz? The repository seems to be on 2,0.26, but there is no tarball. 🙂

    • March 27, 2017 at 8:31 am

      Thanks – link is fixed now.

  • February 4, 2017 at 6:12 am


    Have you considered enriching the SDL_BlitScaled()
    with the smooth zooming like your zoomSurface() has?
    The problem I have with zoomSurface() is that it creates
    the new surface from which I have to blit. Extra copy
    operation. Having the smoothing functionality in SDL_BlitScaled()
    and SDL_SoftStretch() would save me from extra copy.

    • February 20, 2017 at 9:35 pm

      I don’t have the time to work on integrating this into SDL at this time – sorry.

  • December 25, 2016 at 2:23 pm

    Thanks for your work on SDL2_gfx ferzkopp!

    Its helping me a lot on a current project.

    I’ve noticed something in the source code of SDL2_gfxPrimitives.c:
    int roundedBoxRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2,
    Sint16 y2, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    * Special case – no rounding
    if (rad <= 1) {
    return rectangleRGBA(renderer, x1, y1, x2, y2, r, g, b, a);

    maybe it shoud be:
    * Special case – no rounding
    if (rad <= 1) {
    return boxColor(renderer, x1, y1, x2, y2, r, g, b, a);
    If a roundedBoxRGBA is being asked with no radius, a boxColor probably should be delivered instead of rectangleColor.

    I've patched and rebuilt that way but figured you could use this feedback


    • December 25, 2016 at 6:49 pm

      Yes, looks like a copy-n-paste bug. Thanks for letting me know.

  • October 15, 2016 at 4:03 pm

    Can you provide steps on to how build the SDL2_gfx dll for visual studio 2015?

    • December 25, 2016 at 2:11 pm

      In you’re still looking for an answer: the SDL2_gfx downloads are available in .tar.gz and .zip: the .zip comes with vs2015 solution, (almost) ready to build. If you have the SDL2-devel-2.0.5-VC (or other version) development Libraries already the only thing you’ll need to do is set its include and library paths on the vs project and build it.


Leave a Reply

Your email address will not be published. Required fields are marked *