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.

This entry was posted in Computers. Bookmark the permalink.

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

  1. Andreas says:

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

  2. Els says:

    Thank you. This article has helped me a lot!

  3. Daniel says:

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

  4. Pingback: n00b Developer · The Fun That Is SDL_gfx (or, how i stopped worrying and learned how to build DLLs)

  5. FyberOptic says:

    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!

  6. renkin says:

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

  7. Chris says:

    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!

  8. Ande says:

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

  9. Swiz says:

    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

  10. PRIZRAK says:

    Thank you very much!

  11. sweetlilmre says:

    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.

  12. Chris says:

    Amazing work, thanks.

  13. Paul says:

    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

  14. tobbez says:

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

  15. Pingback: Devnetik » SDL_gfx Libs und Header

  16. GoldenCage says:

    I had to add, “WIN32″ to the preprocessor definition list to get it to export the symbols to a lib. Thanks for this article it has been a great help and saved me a lot of time.

  17. Skabus says:

    Thank you for this!

    Help me a lot!

    fR Skabus

  18. Ketan says:

    For some reason my output doesn’t general a .lib file.
    What am I doing wrong? Everything else is working fine.

  19. younes says:

    I’m really thankful
    i’m helping a friend in his PFE (End of study project), and i was blocked was all this complication.
    Now all goes like a charm.
    thanks a lot
    and keep going.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>