Thread: C tutorial Help

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    23

    Question C tutorial Help

    I only Program in C, not c++, and I don't use the Visual C++ IDE, I can't find any tutorials on DirectX programming in C, they are all for C++ does anyone know any DirectX tutorials that i could use, I've googled it with no prevail. I don't want to move onto C++ just yet I've only just got the hang of the WIN API, and I find C programs are a lot faster and smaller than C++ programs.

    cal.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    I think DirectX is directed towards C++. I'm sure you don't have to use C++ but I've head it's easier to do so.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    I use DirectX with C. I don't code in C++ at the mo because I don't think I'd make the best use of its features (you know how it is, you become set in your non class-oriented ways ), but it's quite possible.

    Most C people see a COM object, scream and peg it back to Linux or somewhere similarly coded. Fortunately COM is C compatible, just looks a bit uglier when you have to go through the lpVtbl pointer and specify the address of the object you want to perform the function with, e.g.:-
    Code:
    LPDIRECTDRAW2 lpdd2 = NULL;
    
    // To initialize DirectX 2 in C (After first getting it with CoCreateInstance, etc.)
    lpdd2->lpVtbl->Initialize(lpdd2, NULL);
    
    // The same in C++
    lpdd2->Initialize(NULL);
    That said, I'm not sure whether the same can be done with the most recent version of DX, I know the above works up to DX 8.

    And this "C is faster than C++" argument is a bit tired. On modern machines (i.e. anything above the original Pentium) it isn't noticable, and if you configure the C++ compiler properly the difference is practically nil.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    ...I don't use the Visual C++ IDE...
    It would actually be more useful if you stated which compiler you do use. And the version of directx you're programming with might be handy, too.
    Quote Originally Posted by SMurf
    I'm not sure whether the same can be done with the most recent version of DX, I know the above works up to DX
    Yes, it's good to go. The 'c' syntax is generally:
    Code:
    dx_object->lpVtbl->fn_name(params);
    where params is the function parameters with an additional first parameter of dx_object.
    Quote Originally Posted by SMurf
    Most C people see a COM object, scream...
    There's a bunch of macros defined in d3d9.h (and possibly elsewhere) that simplify the syntax. They also have the added bonus of mapping to c or c++ syntax depending on whether __cplusplus ( or CINTERFACE ) is defined. For example:
    Code:
    #if !defined(__cplusplus) || defined(CINTERFACE)
      #define IDirect3D9_Release(p) (p)->lpVtbl->Release(p)
    #else
      #define IDirect3D9_Release(p) (p)->Release()
    #endif
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    // To initialize DirectX 2 in C
    are my eyes playing tricks on me?

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You guys are all barking up the wrong tree. First of all COM can be used with either C or C++ because COM has nothing to do with either. COM is not C++ and interfaces are not C++ classes. COM is a binary standard and while it acts very much like a C++ class, it still cannot be considered a pure C++ class.

    COM and COM +, ActiveX, and others are readily used in VB 6 apps and other languages as well.

    DirectX can be utilized from VB code and from C/C++ code. Do not confuse COM as having anything to do with C++.

    If you are serious about using DirectX or any API for that matter you will soon find out that there are thousands of functions at your disposal, yet they have not been grouped together in a nice unit. For instance to create 3D models you could use the D3DX mesh library of functions. But there are a lot of them and to just use them willy nilly inside of your init functions, etc., would be a huge mess. So you encapsulate them in a nice class that provides a nice neat interface to important mesh functions or important functions for your particular use of the mesh library. Don't derive ten thousand times and stay away from virtual functions as much as possible because they have with them an associated performance hit....that may not matter in normal C++ apps but trust me it will bring your Direct3D framerates and games to their knees in a flash.

    If you only code in C you can use the DirectX API but its going to be some very messy code. The reason that Quake and all those engine are C-based and not so much C++ based is that they are, in essence, writing an API for their game engine that in turn uses the DirectX API. API programming just lends itself better towards C. But if you are creating objects and such inside of your game then you will want to encapsulate their behaviors and such in a class.

    Also you must be extremely careful with how you manage resources. At first glance most people would create a class to encapsulate animation, animation frames, etc. They would store the IDirect3DTexture9 pointer inside of the animation class, etc, etc. Big problem here already. This works fine for say a couple of instances of the class. But remember that each object of that type will have a texture pointer in it. Let's say we have 100 explosions happening but they all use the same animation sequence. Are you going to store 10 frames of the same animation in 100 separate sequences when they all use the same images, just at different times? Absolutely not. So what you need to develop are resource manager classes that manage all the resources for the engine. Ideally in the explosion example you would want an animation sequence manager class. In this way you could create several animation sequences during the load time and then just 'link' your objects to the desired sequence. The animation sequence would keep a list or a vector of the current frame number for each class and then return the appropriate texture to be rendered. In this way and in our example you would have 10 frames of animation and any number of objects could use or 'link' to this animation. Storing 10 frames is a lot better than storing 10*100. You will run into this a lot in game programming. Proper resource management is one of the major keys to framerates, and framerates are vital to interactive programming.
    Last edited by VirtualAce; 12-11-2004 at 05:17 PM.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    >>If you only code in C you can use the DirectX API but its going to be some very messy code.

    Exactly what I had thought.

  8. #8
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Quote Originally Posted by sand_man
    >>If you only code in C you can use the DirectX API but its going to be some very messy code.

    Exactly what I had thought.
    But don't you wanna be like Mr. Carmack?
    DON'T YOU WANNA BE A GOD?!?!?!11!

    I've always wondered what the kid'd be like if Bubba and Mr. Carmack could somehow make a child together.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  2. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 02:49 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM