Compiling SDL_gfx (2.0.17) on Visual Studio 2008 (VS9)


IMPORTANT: Turns out I was wrong about why SDL_gfx doesn’t compile - thanks to sweetlilmre for pointing it out.

I strongly suggest using his suggestions for steps 2 and 8, whereas step 4 merely is a matter of preference. You should probably skip step 10 as well, since static_cast() is not C, but C++.

Sadly there are no officially provided binaries for SDL_gfx (unlike most other SDL_* libraries), and so I had to compile it myself. SDL_gfx, however has just about no documentation telling you how to do this - and the methods provided doesn’t even work.

Since it took me a while to get this working, so I thought I’d write it down for future reference.

1. Download and unpack the source package.
2. Rename *.c to *.cpp. This is to solve what seems to be this bug, that supposedly was fixed in VS6.
3. Extract sdlgfx.vcproj from ‘Other Builds\VisualC7.zip’ into the source directory and open it, and go through the Conversion Wizard.
4. Remove the source files from the project, and then add the *.cpp files and the *.h files to the project. This is needed because the paths in the original project file are incorrect.
5. Change target to Release.
6. Add SDL.lib to Linker -> Input -> Additional Dependencies in the project properties.
7. Add BUILD_DLL to C/C++ -> Preprocessor -> Preprocessor Definitions.
8. In SDL_gfxPrimitives, replace

#ifdef WIN32

__inline long int
lrint (double ftl)
{
int intgr;
_asm
{
fld flt
fistp intgr
};
return intgr;
}

#endif

with

#define lrint(x) (floor(x+(x>0) ? 0.5 : -0.5))

(As per this site)
9. Compile, and save the solution somewhere when asked to.
10. There should be 4 compilation errors, all of which can be solved by using static_cast<>():
SDL_rotozoom.cpp (Line 501) becomes:
pc = static_cast<tColorRGBA*>(dst->pixels);
SDL_rotozoom.cpp (Line 657) becomes:
pc = static_cast<tColorY*>(dst->pixels);
SDL_gfxPrimitives.cpp (Line 3105) becomes:
sab = sqrt(static_cast<double>(a2 + b2));
SDL_gfxPrimitives.cpp (Line 4289) becomes:
currentFontdata = static_cast<const unsigned char*>(fontdata);
11. Compile. The project should now compile correctly, and you will find sdlgfx.dll and sdlgfx.lib in the Release sub-folder of your source directory.

Now, this is all good, but I would really prefer either official binaries, *or* fixes to the code problems, and instructions that are actually up to date.


Since there was interest I’ve now uploaded my binaries as well. It’s SDL_gfx-2.0.17 compiled against SDL-1.2.13, and can be downloaded here.

14 Responses to “Compiling SDL_gfx (2.0.17) on Visual Studio 2008 (VS9)


  • Andreas
    July 13th, 2008 01:25
    1

    Thanks for putting this together. I (the maintainer of SDL_gfx) try to incorporate these changes into the next release.

  • Els
    August 14th, 2008 17:15
    2

    Thank you. This article has helped me a lot!

  • Daniel
    August 16th, 2008 06:30
    3

    It’s people like you that make the internet worthwhile. Thanks.

  • n00b Developer · The Fun That Is SDL_gfx (or, how i stopped worrying and learned how to build DLLs)
    August 29th, 2008 11:46
    4

    […] How to Build My SDL_gfx […]

  • FyberOptic
    August 31st, 2008 03:42
    5

    Wow, thanks a lot for this. I wasted so much time today trying to get clanlib to work using SDL + SDL_gfx. That was partially because I started with msys + mingw to build clanlib, but after many hours of frustration only for my test program to still not build properly in the end, I decided to just go to Visual Studio. Everything built just fine (half an hour compared to almost six with mingw), up until SDL_gfx. I was ready to give up completely till I found this page. After your fixes, it compiled like a charm. So thanks again!

  • renkin
    September 9th, 2008 18:49
    6

    Thanks a great deal! I was about to get gloomy about sdlgfx.

  • Chris
    October 4th, 2008 03:41
    7

    I can’t begin to tell you how thankful I am for this post! I had totally given up hope. Keep up the great work!

  • Ande
    October 5th, 2008 19:43
    8

    Is there any place where I could download this newest DLL? It makes no sense everyone has to compile it by themselves.

  • Swiz
    October 11th, 2008 13:20
    9

    If anyone cares, I tweaked the VC6 makefile to get it working using VC9 Express. It’s built, but I’ve not tested it yet. You’ll need to change the include and lib defines, and still follow all the other steps (rename to cpp etc).

    # This makefile will build an SDL_gfx.dll with Visual C
    # It was made for VC6 on Windows 98
    #
    # nmake should build the lot.
    # .dll file goes in your system directory
    # .lib and the .h files go into the searchpath of you compiler
    #
    # Edit this:
    LIBSDL=C:\dev\SDL-1.2.13\lib\sdl.lib
    # Leave the rest

    CFLAGS=-DBUILD_DLL -DWIN32 -IC:\dev\SDL-1.2.13\include

    .cpp.obj:
    $(CC) $(CFLAGS) -I. $(CPPFLAGS) -c $(<:/=\)

    OBJS=SDL_framerate.obj SDL_gfxPrimitives.obj SDL_imageFilter.obj SDL_rotozoom.obj

    SDL_gfx.dll: $(OBJS)
    cl $(OBJS) /link /dll /out:$@ $(LIBSDL)

    clean:
    del *.obj
    del *.lib
    del *.exp
    del *.dll

  • PRIZRAK
    October 18th, 2008 18:07
    10

    Thank you very much!

  • sweetlilmre
    October 31st, 2008 15:37
    11

    No offence, but your assumptions as to why this does not compile are completely incorrect.

    point 2: the compile errors are NOT a result of the mentioned bug. C89 only allows variable declaration at the top of the function. If you hoist the vars it complains about to the top, the errors disappear.

    e.g. in:

    Uint8 drawoct = 0; // 0×00000000

    It is the definition of the var drawoct at this point that causes the issue, move the definition to the top of the function and change above to:

    drawoct = 0; // 0×00000000

    Most of these errors are in arcColor() and easily fixable.

    point 4: the project file can be fixed very simply by opening it in a text editor and deleting the references to “..\..\..\Documents and Settings\James\Desktop\SDL_gfx-2.0.3\” and making sure the vcproj is in the same dir as the source.

    point 8: this is simply a typo, if you look carefully, the paramer is named ‘ftl’ and the operand is named ‘flt’. Either change the param to flt, or the op to ftl and all is well.

  • Chris
    December 2nd, 2008 17:36
    12

    Amazing work, thanks.

  • Paul
    December 4th, 2008 23:56
    13

    Thank you so much - you are a lifesaver.

    You made a correct decision to upload the binaries, because i still get four compiler errors. (cannot open include file: ‘SDL.h’)

    I think that’s because it doesn’t see SDL.lib

  • tobbez
    December 5th, 2008 04:09
    14

    @Paul: You still have to setup the include and library paths for SDL properly, i.e. adding them in Tools -> Options -> Projects and Solutions -> VC++ Directories.

Leave a Reply