should I learn DirectX?

Miscellaneous Forums/General Discussion/should I learn DirectX?

I'm wondering if I should bother learning DirectX. I already know basic C++, and, for gaming programming, should I learn DirectX?? At the moment, I am simply failing to learn DirectX. The commands are all just so needlessly complex and hard to remember, just look at this:
/*
   Demo Name:  World Matrix
      Author:  Allen Sherrod
     Chapter:  Ch 1
*/


#include<d3d9.h>
#include<d3dx9.h>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

#define WINDOW_CLASS    "UGPDX"
#define WINDOW_NAME     "World Matrix"
#define WINDOW_WIDTH    640
#define WINDOW_HEIGHT   480

// Function Prototypes...
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();


// Direct3D object and device.
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

D3DXMATRIX g_projection;
D3DXMATRIX g_worldMatrix;
D3DXMATRIX g_translation;
D3DXMATRIX g_rotation;

float g_angle = 0.0f;

// Vertex buffer to hold the geometry.
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

// A structure for our custom vertex type
struct stD3DVertex
{
    float x, y, z;
    unsigned long color;
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)


LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
      {
         case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
            break;

         case WM_KEYUP:
            if(wParam == VK_ESCAPE) PostQuitMessage(0);
            break;
      }

   return DefWindowProc(hWnd, msg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
   // Register the window class
   WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                     GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                     WINDOW_CLASS, NULL };
   RegisterClassEx(&wc);

   // Create the application's window
   HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
                            100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, GetDesktopWindow(),
                            NULL, wc.hInstance, NULL);

   // Initialize Direct3D
   if(InitializeD3D(hWnd, false))
      {
         // Show the window
         ShowWindow(hWnd, SW_SHOWDEFAULT);
         UpdateWindow(hWnd);

         // Enter the message loop
         MSG msg;
         ZeroMemory(&msg, sizeof(msg));

         while(msg.message != WM_QUIT)
            {
               if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
                  {
                     TranslateMessage(&msg);
                     DispatchMessage(&msg);
                  }
               else
                  RenderScene();
            }
      }

   // Release any and all resources.
   Shutdown();

   // Unregister our window.
   UnregisterClass(WINDOW_CLASS, wc.hInstance);
   return 0;
}


bool InitializeD3D(HWND hWnd, bool fullscreen)
{
   D3DDISPLAYMODE displayMode;

   // Create the D3D object.
   g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
   if(g_D3D == NULL) return false;

   // Get the desktop display mode.
   if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
      return false;

   // Set up the structure used to create the D3DDevice
   D3DPRESENT_PARAMETERS d3dpp;
   ZeroMemory(&d3dpp, sizeof(d3dpp));

   if(fullscreen)
      {
         d3dpp.Windowed = FALSE;
         d3dpp.BackBufferWidth = WINDOW_WIDTH;
         d3dpp.BackBufferHeight = WINDOW_HEIGHT;
      }
   else
      d3dpp.Windowed = TRUE;
   d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
   d3dpp.BackBufferFormat = displayMode.Format;

   // Create the D3DDevice
   if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice)))
      {
         return false;
      }

   // Initialize any objects we will be displaying.
   if(!InitializeObjects()) return false;

   return true;
}


bool InitializeObjects()
{
   // Set the projection matrix.
   D3DXMatrixPerspectiveFovLH(&g_projection, D3DX_PI / 4,
      WINDOW_WIDTH/WINDOW_HEIGHT, 0.1f, 1000.0f);

   g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_projection);

   // Set default rendering states.
   g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	g_D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

   // Fill in our structure to draw an object.
   // x, y, z, color.
   stD3DVertex objData[] =
      {
         {-0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255,255,0)},
         {0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255,0,0)},
	      {0.0f, 0.3f, 1.0f, D3DCOLOR_XRGB(0,0,255)}
      };

   // Create the vertex buffer.
   if(FAILED(g_D3DDevice->CreateVertexBuffer(3 * sizeof(stD3DVertex), 0,
             D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL))) return false;

   // Fill the vertex buffer.
   void *ptr;
   if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData), (void**)&ptr, 0))) return false;
   memcpy(ptr, objData, sizeof(objData));
   g_VertexBuffer->Unlock();

   return true;
}


void RenderScene()
{
   // Clear the backbuffer.
   g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

   // Begin the scene.  Start rendering.
   g_D3DDevice->BeginScene();

      // Build translation and rotation matrices.
      D3DXMatrixTranslation(&g_translation, 0.0f, 0.0f, 3.0f);
      D3DXMatrixRotationY(&g_rotation, g_angle);

      // Build the world matrix and update angle.
      g_worldMatrix = g_rotation * g_translation;
      g_angle += 0.01f;
      if(g_angle >= 360) g_angle = 0.0f;
   
      // Set the world matrix.
      g_D3DDevice->SetTransform(D3DTS_WORLD, &g_worldMatrix);

      // Draw the object.
      g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));
      g_D3DDevice->SetFVF(D3DFVF_VERTEX);
      g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

   // End the scene.  Stop rendering.
   g_D3DDevice->EndScene();

   // Display the scene.
   g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}


void Shutdown()
{
   if(g_D3DDevice != NULL) g_D3DDevice->Release();
   if(g_D3D != NULL) g_D3D->Release();
   if(g_VertexBuffer != NULL) g_VertexBuffer->Release();

   g_D3DDevice = NULL;
   g_D3D = NULL;
   g_VertexBuffer = NULL;
}


Anyways, I was wondering if there is any real need to ever learn DirectX, in both a profession and hobby environment? Also, other than DirectX, are there any good, easy to learn, well-documented, free, and DirectX-version-recent engines out there? I'd like to code in C++, as it's a vital language, but using functions from a easily usable engine (quick-commands, like in blitz).

(the main reason I want to use something other than blitz is the fact that it's dx7 and the shaders are terrible and slow)

thanks

As for whether you should learn DirectX for your profession and/or hobby I guess that depends what you want to do as a profession and a hobby. If you want to write 3d engines and/or low level games engines, then you're going to need to learn DirectX if you plan on supporting Windows. If you're going to write games, on the other hand, then it's entirely up to you. Some people do, some people don't. Ultimately you need to decide what level of programming you're most comfortable with. I'm a big fan of high level programming. I only ever go to low level code when it's essential. But I don't discourage people from being low level guys because someone has to write the low level stuff I use in my games.

The shaders in DX7 are non-existant. They didn't exist until DX8. I know one or two people confuse the issue by calling certain effects shaders, but they're just needlessly confusing the issue.

If you're writing something like this:
The commands are all just so needlessly complex and hard to remember

then a high level engine is probably the way to go. The higher the better as far as I'm concerned. If I ever go back to 3d it will be via something like Unity3d. I'm very much over low-level 3d programming - there's just so much work required to keep up with ongoing developments.

Having said that, Gabriel is spot on with this:
But I don't discourage people from being low level guys because someone has to write the low level stuff I use in my games.


..very good and very very clean code 3D engine, opensource and free, with pretty much all next gen features should be Horde3D..give it a try..it will be nice to see this engine nailed on to B3D or Bmax..

http://www.nextgen-engine.net/

I'm checking that one out, but I can't seem to install it...

Are there any high-level engines that are nice and straightforward, like blitz, but with higher graphical capabilities?? I'd really like that...

try JME..it rocks..and its multiplatform....very easy to code

aww...java??

Why beat around the bush learn c++/c#

Im liking c++ lately myself as it is cross platform and its not as 'scary' as it looks.

@chwaga..yup...java...nice thing...but if you have learned concept of C++ as you did mentioned, then Java will be very easy to get in to..

I too am returning to the C++ fold :), there is still loads to learn and I would like to get round to fully learning and making a game in OpenGL :D or maybe use OGRE to do it.

@Edzup, ditto. If you are interested, i have started writing out a load of functions for pseudo-b3d-like commands using irrlicht for use in c++.
Im doing this because i find the command structure for irrlicht quite longwinded for simple stuff and would be nice to have a solid set of commands that i know very well. (I can still easily use the irrlicht ones too if i need something beyond the normal scope of my own commands)
I have many similar movement, creation and a few appearance commands already but the more minds on the job the quicker it can be created and the quicker we learn. Email me if you are interested.

From the perspective of a small game developer and if you don't need it for your work or do it out of fun, i see it as a waste of time.

If you want to mess around with low level programming etc. then go for it.
If you actually want to get things done then use Blitzmax and one of the various engines...

Im liking c++ lately myself as it is cross platform and its not as 'scary' as it looks.
No. It's much, much worse.

From the perspective of a small game developer and if you don't need it for your work or do it out of fun, i see it as a waste of time.
I like to second that. More importantly there's no such thing as "learning DirectX". There is learning DirectX <version number> and a large part of the knowledge you gain will be obsolete, or flat out wrong, buy the time DirectX <version number+1> comes around. The real question is can you learn DirectX, and realize your vision, faster than Microsoft can change it, and you have to start all over again?

Since you seem to be having trouble with it, I'm guessing not.

aww...java??
Yes. C++ is not as "vital" as you think, unless you happen to be one of the 100 or so people on this planet who are smarter than Bjarne Stroustrup, and actually understand all the intricacies of the language.

I understand every intricacy of C++. I just don't know them yet. I'm tired.

> Im liking c++ lately myself as it is cross platform and its not as 'scary' as it looks.
No. It's much, much worse.

I know you have a dislike for C++ FlameDuck, but come on - this is ridiculous. He simply stated that he liked C++, which is a fact. Sorry, but someone's opinion is the one thing you can't argue.

The real question is can you learn DirectX, and realize your vision, faster than Microsoft can change it, and you have to start all over again?

No, the real question is which version of DirectX to use. I'd recommend DirectX 9. DirectX 8 and below is outdated, DirectX 9 is still a very good API, and DirectX 10, while it may be "better" than DirectX 9 in some ways, is too new to be of much practical use (only new video cards support it).

Yes. C++ is not as "vital" as you think, unless you happen to be one of the 100 or so people on this planet who are smarter than Bjarne Stroustrup, and actually understand all the intricacies of the language.

C++ is "vital" in that, for use in game engine programming, the major C++ compilers still produce the fastest code. In fact, almost all benchmarks comparing the speed of programming languages compare against C++.

C++ isn't best for everything. Personally, I use C# for applications (where speed is not critical), and C++ for high-speed 3D simulations. For many people C++ is overkill and isn't worth the extra effort to get the additional speed, in which case Java or C# is vastly superior. But remember that there are applications that do require every bit of power out of your GPU and CPU, in which case C++ (and sometimes inline assembly) is still the best solution.

chwaga: If you have the time, then it certainly won't hurt to learn DirectX. I don't know a lot of DirectX, but I've made a few simple 3D tests with it, and personally I think it's not nearly as complex as people make it out to be. Just make sure you have a solid understanding of C++ pointers & arrays. Otherwise you're sure to get totally confused (as I did years ago).

in that case I'm screwed, because I have no idea what pointers are (none of my books explained it well) and I keep forgetting array syntax.

lol?
seriously, though, is there anything that's as easy to code as blitz, but with better graphic support?

Java man...Java will do..it is different than Blitz, but its easy to pick up and deal with...and after that you can use JME, very good stuff with really nice features and multiplatform...or javaScript, very very easy scripting language you can use with Unity3D..etc..

..or take a look at GL Basic..you may like it :), it has support for shaders, and stuff.. :)

www.glbasic.com

but java would involve me buying more books...I already have too many...

Is the java logic any different from c++/blitz logic?? I just want to learn new syntax, not new logic. The transition from c++/python to blitz was very easy and fun. Would blitz/C++ to java/jME be the same?

but java would involve me buying more books...I already have too many...

That statement is just so wrong.

Java/Jme is beauty..and with included Eclipse IDE, whats rocks, its very very nice development environment...

dang, glbasic seemed nice...until it cost 120$ :(

There's nothing wrong with learning.


That statement is just so wrong.


what's so wrong about it? That java requires books, or that I have too many books?

well, unless someone can convince me otherwise, I'm gonna try out jME...

Why does everyone worship Java? It's horrendously slow, and it's not as cross-platform as everyone thinks (you have to have the Virtual Machine mega-download installed on every gamer's computer).

I stopped once I realized that I couldn't use booleans in statements (i.e. "variable = (otherVariable mod 2)*3;"), for no reason other than the fact that Java hates me.

>>(you have to have the Virtual Machine mega-download installed on every gamer's computer).<<
maybe you should include JVM in to your installer package..hows that? And regarding use java with JME, im not sure that your complaining about speed can stand ground..

Learning new langauges is good but if you just keep switching everytime you get into difficulties, you won't get anywhere. There is no magical language that gets you super results really fast and with extra easy coding.

Pick a language, pick some books, and stick at it for a month, no excuses. Then if you don't like it you can change.

what's so wrong about it? That java requires books, or that I have too many books?

The notion that it's possible to have too many books.

ah

crap, I'm having trouble on step 4 here

in that case I'm screwed, because I have no idea what pointers are
A pointer is just a stored memory address or location of data within a datastructure.
Or more specifically a variable that 'points' to an area of memory either directly (an address) or can refer to something stored in ram through a controlled environment - ie: a position in an array or the 'handles' in b3d (an indirect pointer to the data's real location in ram)

I would like to get into DirectX, but need to know which C++ compiler to use.

I have Borlands C++ builder X, and Microsofts Visual C++ express edition.

Are these good compilers?
And whats the difference between C++ express edition,
and the normal edition?

What is the fascination with "directX" anyway?
Unless you are writing an actual rendering/sound engine (a windows/ms only one i might add) then theres absolutely no point.
Learn an established engine instead (if you want to make games/apps).

if you're going to do directx, I suggest visual c++, it's a bit tricky to install, but intellisense saves a ton of time.

Ok, Thanks.
Ill Try Visual C++.

He simply stated that he liked C++, which is a fact.
Well actually he said "it's not as scary as it looks". He then goes on to say that "he doesn't understand pointers" which I think proves my point rather well, namely that he has not understood just how scary C++ is.

That statement is just so wrong.
Yes. Both as a statement of fact, and as a metaphor. The best Java (or programming in general) book ever written (Bruce Eckel's Thinking in Java, for those playing at home) is absolutely free. You can download it off his site.

Why does everyone worship Java?
Because along with C# and Visual Basic, it's the holy trinity of imperative programming languages.

It's horrendously slow
Compared to what? Java hasn't been "horrendously" slow in 5 or 6 years (since the release of 1.5).

and it's not as cross-platform as everyone thinks (you have to have the Virtual Machine mega-download installed on every gamer's computer).
Yes. However the JVM is available for a ridiculous amount of platforms, which is really all it takes for something to be cross platform. Also considering that most Operating Systems weigh in at more than 1 GB these days, the 50-60 MB that is the JVM is dwarfed by comparison, for any platform that isn't QNX.

I stopped once I realized that I couldn't use booleans in statements (i.e. "variable = (otherVariable mod 2)*3;"), for no reason other than the fact that Java hates me.
First of all, that's an assignment operation, not a boolean expression. The reason it doesn't work is because "mod" is not a keyword in Java (which uses % for modulus). Other than syntactical errors, your example works just fine here.

Well actually he said "it's not as scary as it looks". He then goes on to say that "he doesn't understand pointers" which I think proves my point rather well, namely that he has not understood just how scary C++ is.

No i didnt.

I only said its not as scary as it looks (and I understand pointers just fine, that was someone else ;)

Thinking in Java is 25$...

Thinking in Java is 25$...

Following FlameDuck's guide on where to find it..

http://www.mindviewinc.com/Books/

either way, I'm mentally incapable of reading a book on a computer. Do you think a quick read like java for the absolute beginner would teach me enough of the syntax to use jME, as a large amount of the commands would be replaced by jME stuff?

A 3D engine will not replace core language commands. If you don't plan on taking the time to learn the language, I entirely fail to see the point of doing it. To be honest, I never really grasped what was driving you to learn DirectX or whatever in the first place. What exactly are you trying to achieve? What's the ultimate goal?

..chwaga...you have to know some basics in java, whats clearly stated on JME site, but still..if you take a look few examples and their source code, its really easy and simple to use...dont expect same approach as in blitz since its OO stuff, but its very easy to digest, just spend some reasonable time on it and get some good Java book and it will do...I guarantee..

Gabriel, interesting question, as I now realize, I'm really not sure... I guess I just trying to learn stuff so I can make games in the future, hoping to start my own game company. I guess it's time to head over to barns & noble...

after a lot of thinking, I've decided (for the time being) to code BlitzMax (2D - where things can never be outdated) and just create random 3d "art" (I'm trying to order a student liscence of 3ds max 9). Thanks for the help!

either way, I'm mentally incapable of reading a book on a computer.
Start practicing. Most of the really interesting articles you'll come across (and need to read) are electronic.

Do you think a quick read like java for the absolute beginner would teach me enough of the syntax to use jME, as a large amount of the commands would be replaced by jME stuff?
In my experience "for dummies" books and the like are almost entirely useless. If you think you can get by only knowing the syntax, get a reference manual like Java in a Nutshell. But you'll be mistaken.

I hate the for dummies series anyway, never learn anything from them...

This reminds me of some "getting your computer to work 49034% faster for free" guides my friend showed me recently. Every step, such as clearing temp Internet files, is enshrined as the l33test computing secret ever ever and aren't you glad you bought this guide!