Using Graphics in a C++ Code.

  1. How can I use graphics in my C++ code? I'm making a project of basic level (cricket score board) and I need to incorporate graphics in it. Are there any online tutorials etc. for graphics?

    P.S. I use Visual C++ 6.0 compiler.
  2. jcsd
  3. I assume you mean to have a GUI with graphics on it.

    Well, you can use either the Windows API - it has methods for making windows and drawing on them, including lines, points, text, and basic shapes - or you can look into something called Direct Draw (maybe overkill for your application).

    I think the Windows API will probably do it for you.
  4. What's windows API and how can I use it?
  5. daniel_i_l

    daniel_i_l 866
    Gold Member

    A good place to start is at nehe's opengl tutorials:

    Instead of using win32 directly I'd recommend glut or SDL. The nehe website contains basecode for both of these options.
  6. OpenGL is great and worth learning. But I would not recommend it for a beginning project. And I personally wouldn't use it just to make a scoreboard. I'd stick with the windows API or tk.

    If this is a personal project, you might consider making a text-based version first. And if you are new to GUIs and programming in general, Python is very good to start GUI programming with. tk of wx would be more than fine.

    And when learning graphics, google will bring A LOT of tutorials.

    Best of luck!
  7. And only non-computing people say "a code" or "codes". Just a little pet peeve. Try "code" without an article, in the singular, to denote the actual text representation of the source code of a program. When the code is compiled, it becomes an executable form of the program. When you run it, you get a process running the program.
  8. Do NOT use Win32 API.

    You need to find an acceptable graphics API.

    The most powerful 3D graphics API that is used for all modern games is Direct3D (part of DirectX). This API is only available for Windows and XBox.

    The most cross platform (but horribly out of date) 3D graphics API is OpenGL. If you use this you'll also need a window mangement program like Glut or SDL so that you DON'T have to use Win32 API which is a nightmare that nobody ever touches.

    Even if your game is 2D, the most powerful API is still to use a 3D API like Direct3D (DirectDraw is deprecated).

    If you are only doing 2D and you're not making a professional game, my recommendation is to use CImg which is a hell of a lot simpler than the above options. It's also cross platform. This is by far the best and simplest library for doing simple graphics in C++.
  9. I recommend Also, SFML looks nice, but not quite as well documented (though it is supposed to be faster than SDL). Another option is the allegro game programming library. And I second staying away from the Win32 API. Blah! Unless 3d, don't worry about OpenGL or DirectX yet. Though great and fast for 2d, these two libraries will take longer to learn than the others I mentioned.
  10. "Do NOT use Win32 API."

    Why not? Did he say he was making a game? Did he say he needed fast, smooth graphics? Why make this that complicated?

    Unless I'm remembering incorrectly, the Win32 API has some very simple functions for doing graphics... bitblt for drawing bitmaps, setpixel for drawing dots, also things for lines, ellipses, etc. You end up doing about the same, or more, work using fancy packages.

    IMHO, DirectX and OpenGL sound like overkill.
  11. What about Qt?

    Free, cross-platform, very polished.

    Note there's two different things being described here. There are libraries for graphics (OpenGL, DirectX), and for graphical user interfaces (Qt, Tk, wxWidgets, the Windows APIs). The latter group gives you useful primitives for interaction (e.g. windows, widgets, layout management) and is probably what you want.

    The Qt tutorials are here
  12. The win32 api is highly overcomplicated and not very powerful. That's why I suggested three alternatives, the two most powerful alternatives (Direct3D and OpenGL) and one less powerful but simple alternative CImg. Regardless of his skill level or need, one of these will be superior to using the win32 api directly.
  13. I respectfully disagree with this:"The win32 api is highly overcomplicated and not very powerful."

    How is it overcomplicated? It was my understanding that to even *use* DirectX you had to create a window and manage resources... using the Win32 API. Can you use DirectX without making a window, having a message handler, and all the baggage that goes along with it?

    And once you have a window, how is it hard to start drawing on it? I don't see how this is any harder than using DirectX.

    As far as power goes... well yes, DirectX is more powerful, but that doesn't mean you should use it by default. I would argue that the least powerful mechanism which can get the job done should always be preferred... and I know for a *fact* that a simple scoreboard for a cricket game can be programmed with the Win32 API. To me, it sounds like a few bitmaps, a few lines, and some text drawing.

    I don't have a lot of experience with OpenGL, but I suspect that you have to jump through the same Win32 API hoops no matter what...
  14. As I said in my post, Direct3D is the most complicated API but also the most powerful, making it the easiest API to use for the most complex effects, and the most difficult API to use for the least complex effects. The reason I did not suggest Direct3D was because he is a beginner and probably doesn't require all that complexity. The alternative API which I did suggest is significantly easier to use and more efficient than the Win32 API.

    You don't. That is the purpose of Glut and SDL.
  15. D3d and OGL are comparable in power and OGL is hardly 'horribly out of date'. The real problem with OGL is that Windows has no updated versions of it available meaning you have to rely on extension mechanisms in order to gain access to the up-to-date features. OGL 2.0 and 2.1 have all the same shader features as part of the standard as D3D and geometry shaders available as extensions on any graphics hardware that supports them (as opposed to D3D which restricts this to DX10 and only runs them on windows). With the new OGL 3.0 and 3.1 standards as well, it's pretty much a new game out there. Incidentally, Doom3 and Quake4 were done using OpenGL rather than D3D.

    On the topic of the original topic though... firstly, upgrade your C++ compiler. Visual C++ 6 IS horribly out of date, and barely counts as a C++ compiler due to it's lack of standards compliance in so many areas. Visual C++ 2008 is available as a free download for the express version and is much improved.
    Secondly, if what you want is just your basic UI widgets, then look into a UI library. QT, GTK, wxWidgets or something... those all have C++ bindings and are of varying complexity. None are as complex as trying to do the UI widgets yourself though.
    If you want something more complex, then SDL or SFML are pretty decent cross-platform 2d libraries for your own drawing. SFML is designed around a more OO approach than SDL (which is a C API) and is IMO easier to use. It also uses OpenGL under the hood (SDL uses GDI calls) and takes advantage of hardware acceleration making it by default a faster 2d library and easier to integrate with 3d graphics.
  16. As suggested qt is another good one to learn. So, I would probably start with qt, tk, or wxWidgets. In fact, give them all a shot.

    I still suggest learning openGL as someone else suggested... but do it later. It is overkill for a beginning scoreboard project. An F1 racer would just become burdensome on the highway during rush hour. And when you tack on learning to drive... it just isn't the best starting point.

    Learn a simple GUI that is made to just create buttons and fields. Get used to mouse events, then move onto sdl and opengl if you need them or if you are just curious. But starting trying to do that stuff early probably chases away a lot of new programmers. Walk before you run.
  17. Sorry for the double post, but I thought I would be kind and give a link to a tutorial

    Enjoy! And most of all make it fun! That's what programming should be. And if you become very ambitious and want to know comp sci in depth. The Wizard book is free online.

    It is written by abelson and sussman, two of the greats. But that link is just something to save for later when you already know you want to program!
Know someone interested in this topic? Share a link to this question via email, Google+, Twitter, or Facebook