Thread: Basic Graphics Libraries (C)

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    Basic Graphics Libraries (C)

    Hey guys, I've been slowly teaching myself C over this past year and I've been doing simple things like making a hangman game to understand character arrays and pointers, and incorporating stuff like structs into it, just mainly to get used to using them in a simple setting. About the only topic I haven't covered yet are linked lists and stuff like binary trees. But alas I'm really only telling you guys this so you kind of understand where I'm at in programming. I've reached this point where I'd like to start delving into simple 2d graphics, but I'm not entirely sure where to start. I looked at some openGL tutorials, but not only did it seem to be way over my head, but that's mainly 3d right? I was thinking of maybe looking into SDL or something of that nature, but I wasn't sure if that was really a good path to go down at this point.

    So I was just wondering if maybe you guys knew of some good (possibly simple) libraries and/or tutorials I could start off with for doing 2d graphics in C. I have done a few google searches on my own(which led to SDL coming to mind), but I figured it wouldn't hurt to ask some of those who are more experienced.

    Thanks in advance ^_^

    Michael

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Are you wanting cross platform or a specific platform?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Cross platform would probably be preferable, but I personally use Windows XP, so so long as it works with that it would be fine...

    Michael

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    SDL actually does not provide any graphics library at all (or barely any); it must use another library (either DirectX or openGL).

    It is popular because it provides a cross-platform interface to sound and windowing (then the graphics API of your choice runs in the window).

    Conversely, openGL (and probably DirectX, I dunno), does not provide windowing, meaning you need to provide a window for the graphics to appear inside, either through SDL, or some other means.

    SDL probably is a good idea, esp on Windows -- just keep this in mind. For example, if you decide to use DirectX, there may be a simpler way to learn it than via SDL.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    SDL probably is a good idea, esp on Windows -- just keep this in mind. For example, if you decide to use DirectX, there may be a simpler way to learn it than via SDL.
    I would go as far as to say that using the raw API is simpler than and much more documented than using SDL.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    So you're saying I should just learn DirectX or openGL and not bother w/ SDL?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    OpenGL has a 2D mode. Anyway, if graphics is what you want to get into, yeah, focus on which graphics library you want to use.

    Which realistically, is either openGL or DirectX since these are the only two commonly available hardware acceleration interfaces. That is, all nvidia and ATI cards implement both openGL and DirectX.

    Later on, if you want to start using SDL with it, then you can -- it is still the same. With or without SDL, you will be making calls directly to the graphics library.

    The complication is simply you cannot use the openGL core functions "on their own". I don't know anything about DirectX, it may be similar in this sense, but since it is not portable (eg, it is only ever used on windows), it may not be, since the only window it will be using is a windows window.

    The graphical windowing system used with your OS (whatever it is) is a programming interface -- library -- unto itself. This is what is used to create and manipulate windows and their characteristics on the screen. "Fullscreen" is actually a window with no frame or titlebar that occupies the whole screen.

    OpenGL is cross platform and the means of using it differ from place to place. On windows I think it is called WGL.

    There are also a few cross platform "toolkits" (such as SDL) which provide a set of windowing commands which will also work the same every everywhere (because they deal with the different windowing systems instead of you), allowing you to write completely cross platform (Mac, MS, Linux) games.
    Last edited by MK27; 02-16-2010 at 06:49 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    I'll probably check out some of the openGL tutorials on this website and maybe do some google searches. I assume there's probably a thread somewhere on these forums that lists some good starting openGL tutorials, so it seems I have some more searching to do ^_^

    If you guys have any suggestions of sites that I could head to that'd be great, but otherwise thanks for pointing me in the right direction!

    Michael

    Edit: Actually I'm a bit confused, according to this site I've been poking around on it refers to SDL as an API and says that you can later integrate openGL into it??? I'm not saying you guys are all wrong, I'm just confused >.<

    Edit again...: would've helped if I put the link to the site xD: http://lazyfoo.net/SDL_tutorials/index.php
    Last edited by mkylman; 02-16-2010 at 07:32 PM.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    For Direct3D to work you simply pass in a window handle to the D3DPRESENT_PARAMETERS. It then will look at the windowed attribute of the D3DPRESENT_PARAMETERS struct and if it is set it will run in windowed mode if you have the back buffer format set to D3DFMT_UNKNOWN and the width and height set to 0. If you are running in full-screen then the driver will take over provided you have set the back buffer format to a supported format and the requested mode is supported. After that Direct3D is up and running and once you get your message loop to call your Update() and Render() functions that is all the interaction you will have with the Win32 API. The rest is going to look like C++ that uses Direct3D COM objects.

    There is very little Win32 setup required to get Direct3D up and running. All of the COM nastiness like QueryInterface() and so forth has been completely hidden from view. You can still do this if you really enjoy self-inflicted pain but you don't have to.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Yeah, see the win32 stuff was what turned me off of openGL when I first looked at the tutorials on here; it all seemed confusing and unnecessary, w/o much explanation. The only issue I have w/ using directx is that like I'd like to avoid something that's totally platform dependent, yknow what I mean? I mean it really doesn't matter, but I guess I'm just explaining my reasoning. I think Dev Cpp comes w/ a bunch of d3d headers though, so it might be a bit less painless than attempting openGL or SDL right now. Gah, decisions, decisions...

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just my view on things: the SDL is a multimedia library. It lets you do things like read keyboard input, set screen modes, load bitmaps, access CDs and joysticks, that sort of thing. You can also do 2D graphics with it. OpenGL is purely a graphics library; there is no way to load images, get keypresses, etc. If you want to use OpenGL, you pretty much need to use another "windowing" library along with it. The SDL works, and so do other libraries, such as GLUT (which is probably the simplest but rather annoying in my opinion) and Qt.

    As far as I know, DirectX is more all-encompassing, but of course it is Windows-specific. I have never used DirectX myself.

    The SDL is fun and it lets you do much of what you want to do when you're starting out with graphics. The LazyFoo tutorial you linked to is a very good one. I'd suggest learning it if you want to. Of course you can later integrate OpenGL with your SDL code. Alternatively, if you want to simply start right with OpenGL, you could learn to use GLUT (it's *really* simple), and then write OpenGL code from there. Others would have to tell you how to start with DirectX.

    As for getting the SDL and OpenGL working with Dev-C++, it's not that bad, really. For the SDL it's a matter of downloading the MSVC development libraries (they use the same format), creating a Dev-C++ project, adding the .lib files to the project, and placing the .dll where it can be found. (Either in the directory your application will be run from or in c:\windows\system32 [or whatever they call it these days -- the folder with all the DLLs] works.) I was never able to get ordinary main() working with Dev-C++, but if you use a WinMain() it works okay. (Then when you compile the code in Linux you just need to switch to an ordinary main().) You can see how I did it here if you like:
    http://dwks.theprogrammingsite.com/myprogs/checker.htm
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    I've actually already got SDL set up w/ Dev-C++ and done a few of the tutorials of LazyFoo's.

    The only thing that's kind of annoying is that every time I make a new project I have to add stuff in the linker parameters under Project Options to make it compile correctly (-lSDL -lSDL_image and a few other things I can't remember right now). Is there a way around that or am I just stuck doing that with every project?

  13. #13
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413

  14. #14
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Allegro - A game programming library
    They actually still make Allegro? Wow


    I've actually already got SDL set up w/ Dev-C++ and done a few of the tutorials of LazyFoo's.

    The only thing that's kind of annoying is that every time I make a new project I have to add stuff in the linker parameters under Project Options to make it compile correctly (-lSDL -lSDL_image and a few other things I can't remember right now). Is there a way around that or am I just stuck doing that with every project?
    First, why are you using Dev C++? I used to use it as well, but as far as I know development on it ceased about 3 years ago, and it's a dead project, hence you're better off using a newer IDE. I suggest Code::Blocks, which is very similar in style, but more up-to-date.

    Regardless, linking is just something you have to learn to deal with. You have to use linking of some form or another in any IDE. Some IDEs, of course, will set up many of the linking parameters for you, but in general, when you use an external library, you have to tell the IDE that you are using it so that it knows where to find it and how to link it.

    Code::Blocks has a special "SDL Template" which lets you set up the SDL linking parameters once, and then any time you want to start a new SDL project you can just use that template. I'm not sure if Dev-C++ has the same or not.

    Anyways, use SDL! It's a great API I love it.
    My Website

    "Circular logic is good because it is."

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    sweet, I'll snag Code::Blocks instead ^_^

    I downloaded Allegro but it seems like a .......... to compile >.<
    Haven't looked at the cairo link yet.
    Thanks though! I'll probably wind up learning SDL and then taking a look at other libraries to see what I like the most(Allegro says it's pretty simple, but SDL has been suggested to me a lot too)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  3. Dos Graphics Libraries.
    By emus21 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-02-2002, 12:20 PM
  4. basic graphics!!! I'm stressing here!!!
    By CumQuaT in forum C++ Programming
    Replies: 5
    Last Post: 09-17-2001, 11:26 AM