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

https://sourceforge.net/projects/sdl2gfx/

Older versions

SDL2_gfx-1.0.1.tar.gz (.zip)

SDL2_gfx-1.0.0.tar.gz

Documentation

Doxygen generated API documentation

(C) A. Schiffler, aschiffler [at] ferzkopp.net 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] ferzkopp.net 1999-2017, licensed under the zlib license

Screenshots

SDL_gfxPrimitives

SDL_rotozoom
Documentation

Overview and doxygen generated API documentation

Downloads

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

https://sourceforge.net/projects/sdlgfx/

Older Releases

SDL_gfx-2.0.25.tar.gz

SDL_gfx-2.0.24.tar.gz

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: http://www.freepascal-meets-sdl.net
  • Perl: http://sdl.perl.org
  • Python: http://www.pygame.org
  • C#: http://cs-sdl.sourceforge.net
  • Lua: http://www.egsl.retrogamecoding.org
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)
Interface
Please check README in archive and .h files for up-to-date interface information.
SDL_gfx / SDL2_gfx
Tagged on:             

29 thoughts on “SDL_gfx / SDL2_gfx

  • November 25, 2017 at 8:35 am
    Permalink

    Can anyone please tell me how to build this library on android using c4droid!I would be really thankful if you could.Sorry if this comment sounds dumb

    Reply
    • November 26, 2017 at 1:20 am
      Permalink

      No idea – sorry.

      Reply
  • August 29, 2017 at 8:15 pm
    Permalink

    when I use texturedPolygon, my FPS plummets and other sprites in my game begin to flash!
    any idea what could cause this?
    The polygon has a low number of points (<16), and the texture, a PNG 320×320, isn't anything fancy either.

    Reply
    • August 29, 2017 at 9:12 pm
      Permalink

      SDL_gfx/SDL2_gfx is a software renderer to create various geometric objects with the API SDL1 or SDL2 provides. The texturedPolygon routine was added during the SDL1 release when this still made some sense for certain usage scenarios, and worked reasonably well with the SDL_Surface in-memory blitting interface. In SDL2, the core drawing interface changed to a small set of accelerated point and line drawing commands. In particular this routine does not work/perform well under SDL2 as it pretty much copies the texture pixel-by-pixel using SDL_RenderCopy(renderer, texture, &source_rect, &dst_rect) calls. So in SDL2 the API is provided as backwards compatibility layer, but it really shouldn’t be used for anything but small areas or simple stuff. It is also not multi-thread capable due to its use of a shared line-buffer object. If you want to draw textured polygons efficiently, you really need to use OpenGL or some other hardware accelerated API. SDL should help you initialize the API, but otherwise does not provide any other interfaces.

      Reply
      • August 29, 2017 at 10:20 pm
        Permalink

        that makes things clear, thanks a lot.
        then I’ll go ahead and setup OpenGL, though will I be able to continue to use the SDL_Renderer simultaneously then? I guess not when it uses DirectX… could it work when I force it to use OpenGL?

        Reply
        • August 29, 2017 at 11:59 pm
          Permalink

          You would probably need to render into a texture (SDL_RENDERER_TARGETTEXTURE = the renderer supports rendering to texture) and then draw the texture using a quad.

          Reply
  • July 15, 2017 at 5:30 am
    Permalink

    Can not build on VC2015.
    Error C1083 Cannot open include file: ‘SDL.h’: No such file or directory

    Reply
    • July 15, 2017 at 5:52 am
      Permalink

      When linked , gives another errors :
      Severity Code Description Project File Line Suppression State
      Warning LNK4217 locally defined symbol _printf imported in function _SDLTest_CommonInit TestGfx d:\Nowy folder\SDL2_gfx-1.0.1\test\SDL2test.lib(SDL_test_common.obj) 1
      Warning LNK4075 ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification SDL2_gfx d:\Nowy folder\SDL2_gfx-1.0.1\SDL2_framerate.obj 1
      Warning C4267 ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data TestGfx d:\nowy folder\sdl2_gfx-1.0.1\test\testgfx.c 186
      Warning C4267 ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data TestGfx d:\nowy folder\sdl2_gfx-1.0.1\test\testgfx.c 188
      Warning C4267 ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data TestGfx d:\nowy folder\sdl2_gfx-1.0.1\test\testgfx.c 190
      Warning C4267 ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data TestGfx d:\nowy folder\sdl2_gfx-1.0.1\test\testgfx.c 192
      Warning C4267 ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data TestGfx d:\nowy folder\sdl2_gfx-1.0.1\test\testgfx.c 194
      Warning C4267 ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data TestGfx d:\nowy folder\sdl2_gfx-1.0.1\test\testgfx.c 217
      Warning C4267 ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data TestGfx d:\nowy folder\sdl2_gfx-1.0.1\test\testgfx.c 234
      Warning LNK4075 ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification TestGfx d:\Nowy folder\SDL2_gfx-1.0.1\test\TestGfx.obj 1
      Warning LNK4075 ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification TestRotozoom d:\Nowy folder\SDL2_gfx-1.0.1\test\TestRotozoom.obj 1
      Warning LNK4217 locally defined symbol _fprintf imported in function _SDLTest_CommonInit TestGfx d:\Nowy folder\SDL2_gfx-1.0.1\test\SDL2test.lib(SDL_test_common.obj) 1
      Error LNK2019 unresolved external symbol __imp____iob_func referenced in function _SDLTest_CommonInit TestGfx d:\Nowy folder\SDL2_gfx-1.0.1\test\SDL2test.lib(SDL_test_common.obj) 1
      Error LNK1120 1 unresolved externals TestGfx d:\Nowy folder\SDL2_gfx-1.0.1\test\TestGfx\Win32\Debug\TestGfx.exe 1
      Error LNK2019 unresolved external symbol __imp__printf referenced in function _SDLTest_CommonInit TestRotozoom d:\Nowy folder\SDL2_gfx-1.0.1\test\SDL2test.lib(SDL_test_common.obj) 1
      Error LNK2019 unresolved external symbol __imp__fprintf referenced in function _SDLTest_CommonInit TestRotozoom d:\Nowy folder\SDL2_gfx-1.0.1\test\SDL2test.lib(SDL_test_common.obj) 1
      Error LNK2019 unresolved external symbol __imp____iob_func referenced in function _SDLTest_CommonInit TestRotozoom d:\Nowy folder\SDL2_gfx-1.0.1\test\SDL2test.lib(SDL_test_common.obj) 1
      Error LNK1120 3 unresolved externals TestRotozoom d:\Nowy folder\SDL2_gfx-1.0.1\test\TestRotozoom\Win32\Debug\TestRotozoom.exe 1
      Warning LNK4075 ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification TestImageFilter d:\Nowy folder\SDL2_gfx-1.0.1\test\testimagefilter.obj 1

      Reply
      • July 15, 2017 at 2:04 pm
        Permalink

        Not sure what’s happening. My VisualStudio build shows the same warning, but otherwise works:

        1>—— Build started: Project: SDL2_gfx, Configuration: Debug Win32 ——
        1>SDL2_imageFilter.c
        1>SDL2_rotozoom.c
        1>SDL2_gfxPrimitives.c
        1>SDL2_framerate.c
        1>Generating Code…
        1>SDL2_framerate.obj : warning LNK4075: ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification
        1> Creating library Win32\Debug\SDL2_gfx.lib and object Win32\Debug\SDL2_gfx.exp
        1>SDL2_gfx.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\Win32\Debug\SDL2_gfx.dll
        1>SDL2_gfx.vcxproj -> Win32\Debug\SDL2_gfx.pdb (Full PDB)
        1>Done building project “SDL2_gfx.vcxproj”.
        2>—— Build started: Project: TestGfx, Configuration: Debug Win32 ——
        3>—— Build started: Project: TestImageFilter, Configuration: Debug Win32 ——
        4>—— Build started: Project: TestRotozoom, Configuration: Debug Win32 ——
        5>—— Build started: Project: TestFramerate, Configuration: Debug Win32 ——
        2>TestGfx.c
        2>d:\projects\sdl2_gfx-1.0.3\test\testgfx.c(186): warning C4267: ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data
        2>d:\projects\sdl2_gfx-1.0.3\test\testgfx.c(188): warning C4267: ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data
        2>d:\projects\sdl2_gfx-1.0.3\test\testgfx.c(190): warning C4267: ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data
        2>d:\projects\sdl2_gfx-1.0.3\test\testgfx.c(192): warning C4267: ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data
        2>d:\projects\sdl2_gfx-1.0.3\test\testgfx.c(194): warning C4267: ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data
        2>d:\projects\sdl2_gfx-1.0.3\test\testgfx.c(217): warning C4267: ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data
        2>d:\projects\sdl2_gfx-1.0.3\test\testgfx.c(234): warning C4267: ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data
        3>testimagefilter.c
        2>TestGfx.obj : warning LNK4075: ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification
        4>TestRotozoom.c
        5>TestFramerate.c
        5>d:\projects\sdl2_gfx-1.0.3\test\testframerate.c(104): warning C4267: ‘function’: conversion from ‘size_t’ to ‘Sint16’, possible loss of data
        2>TestGfx.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\test\TestGfx\Win32\Debug\TestGfx.exe
        2>TestGfx.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\test\TestGfx\Win32\Debug\TestGfx.pdb (Full PDB)
        4>TestRotozoom.obj : warning LNK4075: ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification
        2> 1 file(s) copied.
        2> 1 file(s) copied.
        2>sample16x16.bmp
        5>TestFramerate.obj : warning LNK4075: ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification
        2>sample24-box.bmp
        2>sample24.bmp
        2>sample2x2.bmp
        2>sample3x3.bmp
        2>sample8-box.bmp
        2>sample8.bmp
        2> 7 file(s) copied.
        2>Done building project “TestGfx.vcxproj”.
        4>TestRotozoom.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\test\TestRotozoom\Win32\Debug\TestRotozoom.exe
        3>testimagefilter.obj : warning LNK4075: ignoring ‘/EDITANDCONTINUE’ due to ‘/SAFESEH’ specification
        5>TestFramerate.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\test\TestFramerate\Win32\Debug\TestFramerate.exe
        4>TestRotozoom.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\test\TestRotozoom\Win32\Debug\TestRotozoom.pdb (Full PDB)
        4> 1 file(s) copied.
        4> 1 file(s) copied.
        4>.\sample16x16.bmp
        4>.\sample24-box.bmp
        4>.\sample24.bmp
        4>.\sample2x2.bmp
        4>.\sample3x3.bmp
        4>.\sample8-box.bmp
        4>.\sample8.bmp
        4> 7 file(s) copied.
        4>Done building project “TestRotozoom.vcxproj”.
        5>TestFramerate.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\test\TestFramerate\Win32\Debug\TestFramerate.pdb (Full PDB)
        5> 1 file(s) copied.
        5> 1 file(s) copied.
        5>Done building project “TestFramerate.vcxproj”.
        3>TestImageFilter.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\test\TestImageFilter\Win32\Debug\TestImageFilter.exe
        3>TestImageFilter.vcxproj -> D:\Projects\SDL2_gfx-1.0.3\test\TestImageFilter\Win32\Debug\TestImageFilter.pdb (Full PDB)
        3> 1 file(s) copied.
        3> 1 file(s) copied.
        3>Done building project “TestImageFilter.vcxproj”.
        ========== Build: 5 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

        Reply
    • July 15, 2017 at 1:58 pm
      Permalink

      Run notepad SDL2_gfx.vcxproj and edit the AdditionalIncludeDirectories element from the default ..\SDL2-2.0.5\include;%(AdditionalIncludeDirectories) to match the local install location of SDL2.

      Reply
    • July 15, 2017 at 2:02 pm
      Permalink

      Should be fixed now. Thanks for pointing this out.

      Reply
    • July 15, 2017 at 1:53 pm
      Permalink

      I don’t think SSL is enabled on the site; wget http://www.ferzkopp.net/Software/SDL2_gfx/SDL2_gfx-1.0.1.tar.gz works.

      Reply
  • July 7, 2017 at 3:59 pm
    Permalink

    For anyone struggling with the (seemingly) lack of an SDL_gfx.framework for xcode:

    – Download the latest version of SDL_gfx and unzip it.
    – Open the folder that was made by unzipping the download and unzip xocde.zip that’s in there .
    – Open the xcode folder that was made when you unzipped the second time and click the xcode project file to open it with xcode.
    – Build the project.
    – Go to wherever your output path is and copy the resulting SDL2_gfx.framework ‘file’ to wherever you keep your extra frameworks which for me is /Library/frameworks.

    Don’t forget to add this as a linked library under the ‘build phases’ tab of your project!

    Big thanks to the author by the way 🙂

    Reply
  • July 4, 2017 at 9:50 am
    Permalink

    In case anyone was having trouble compiling this in MSYS with MinGW, or a similar unix on Windows environment, and getting an error:

    *** Warning: linker path does not have real file for library -lmingw32.
    *** Warning: linker path does not have real file for library -lSDL2main.
    *** Warning: linker path does not have real file for library -luuid.

    Exporting the following variable did the trick:
    export lt_cv_deplibs_check_method=’pass_all’

    Reply
  • June 4, 2017 at 2:09 pm
    Permalink

    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;
    }
    }
    }
    break;
    }

    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.

    Reply
    • June 4, 2017 at 9:52 pm
      Permalink

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

      Reply
  • April 21, 2017 at 5:26 pm
    Permalink

    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

    Reply
    • May 2, 2017 at 8:07 am
      Permalink

      Thanks for reporting.

      Reply
  • March 16, 2017 at 5:39 am
    Permalink

    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. 🙂

    Reply
    • March 27, 2017 at 8:31 am
      Permalink

      Thanks – link is fixed now.

      Reply
  • February 4, 2017 at 6:12 am
    Permalink

    Hi!

    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.

    Reply
    • February 20, 2017 at 9:35 pm
      Permalink

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

      Reply
  • December 25, 2016 at 2:23 pm
    Permalink

    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

    Thanks!

    Reply
    • December 25, 2016 at 6:49 pm
      Permalink

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

      Reply
  • October 15, 2016 at 4:03 pm
    Permalink

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

    Reply
    • December 25, 2016 at 2:11 pm
      Permalink

      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.

      Reply

Leave a Reply to Austin Gibbons Cancel reply

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