View Full Version: Getting Started

C++ Learning Community > Direct X > Getting Started


Title: Getting Started
Description: Uhh.... i need some help


MonkeyMan - November 30, 2003 11:04 PM (GMT)
Ok. i want to start trying some directx.... i downloaded the SDK 9 and installed it which took forever <_<. What do i need to do to use the borland command line compiler with the batmaker? I tried a few things with the lib but it didnt work. Also I looked at the other posts in this section.

MonkeyMan - December 1, 2003 12:59 AM (GMT)
well... maybe ill just try and see if what i need can be done in OpenGL :D

TheHawgMaster - December 1, 2003 01:14 AM (GMT)
The libs don't seem to be compatible with the the Borland Command-Line compiler. Voodoo might have gotten it to work though, maybe you could ask him.

MonkeyMan - December 1, 2003 01:26 AM (GMT)
Well if it doesnt work then ill just see what OpenGL can do besides draw a triangle :P... DirectX might be too much of a challenge.

Dragon - December 1, 2003 04:06 AM (GMT)
I tried to learn OpenGL before, but I got tired of it. I'm not learning it anymore. :P

dr voodoo - December 1, 2003 05:26 PM (GMT)
QUOTE
The libs don't seem to be compatible with the the Borland Command-Line compiler. Voodoo might have gotten it to work though, maybe you could ask him.

The libs aren't compatible with the sdk aren't but you can get compatible ones from http://clootie.narod.ru/cbuilder/index.html. Download the files:
  • CBuilder_DX90_libs.zip
  • CBuilder_D3DX9_Summer2003.zip
  • D3DX9Sab.zip
To install directx 9 simply copy all headers that come with the sdk into C:\Borland\BCC55\Include Replace the ones that already exist.
Copy the import libs (*.lib) from CBuilder_DX90_libs.zip into C:\Borland\BCC55\Lib
Copy the import libs from CBuilder_D3DX9_Summer2003.zip into C:\Borland\BCC55\Lib
Open D3DX9Sab.zip and copy D3DX9Sab.dll to somewhere where windows can find it (this means same dir as exe or the system dir). Best is to put it with exe since it's (as far as I know) no offical MS DX dll.
Now create a new header (you can put in the include dir because you'll need it regulary) and name is something like VC6.h Put this in it:
CODE
#include <math.h>
inline float sqrtf(float f)
{return(sqrt((double)f));}

DX9 was written for VC6 and VC6 has some non standard functions like for example float versions of the math functions. Simply put the needed functions in that header (there might be some functions missing in the code above, if so simply add them).
Here's an example prog so you can test if it works:
CODE
#include <VC6.h>//always include the VC6 header first!
#include <windows.h>
#include <string.h>//for memcpy
#include <d3d9.h>
//Window callback function and window handle
LRESULT WINAPI MsgProc(HWND, UINT, WPARAM, LPARAM);
HWND hwnd;

//application internaly step function
void step(void);

//DX handle
IDirect3D9*dx9;

//Device (sort of DX child window)
IDirect3DDevice9*dx_device;

//Verticals
struct vertex
{
   float x, y, z, rhw;
   int color;
};
vertex vertices[] =
{
   { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000 }, // x, y, z, rhw, color
   { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00 },
   {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff },
};

//The vertex buffer
IDirect3DVertexBuffer9*vertex_buffer;

int WINAPI WinMain( HINSTANCE inst, HINSTANCE, LPSTR, INT style)
{
   // Register the window class.
   WNDCLASSEX wc = { sizeof(WNDCLASSEX),
                     CS_CLASSDC,
                     MsgProc,
                     0L,
                     0L,
                     inst,
                     NULL,
                     NULL,
                     NULL,
                     NULL,
                     "App",
                     NULL };

   if(!RegisterClassEx( &wc ))
        return 0;

   // Create the application's window.
   hwnd = CreateWindow( "App",
                        "Hello",
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, CW_USEDEFAULT, 300, 300,
                        GetDesktopWindow(),
                        NULL,
                        inst,
                        NULL );

   //Show the window
   ShowWindow(hwnd,style);

   //Set timer etc
   SetTimer(hwnd,1,50,0);
   //Setup DX
   dx9=Direct3DCreate9(D3D_SDK_VERSION);
   if(!dx9)
        return 0;

   D3DPRESENT_PARAMETERS parm;
   ZeroMemory( &parm, sizeof(parm) );
   parm.Windowed = TRUE;
   parm.SwapEffect = D3DSWAPEFFECT_DISCARD;
   parm.BackBufferFormat = D3DFMT_UNKNOWN;
   

   if(D3D_OK!=dx9->CreateDevice( D3DADAPTER_DEFAULT,//default value
                                 D3DDEVTYPE_HAL,
                                 hwnd,
                                 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                 &parm,
                                 &dx_device))
        return 0;
   //Setup vertex buffer
   if(D3D_OK!=dx_device->CreateVertexBuffer( 3*sizeof(vertex),
                                             0 ,
                                             D3DFVF_XYZRHW | D3DFVF_DIFFUSE,
                                             D3DPOOL_DEFAULT,
                                             &vertex_buffer,
                                             0))
        return 0;

   //now we write vertices into the buffer

   //First we lock the mem of the buffer (so that DX won't move it while we're writting)
   void* b_mem;
   if(D3D_OK!=vertex_buffer->Lock( 0, sizeof(vertices), &b_mem, 0 ))
        return 0;
   memcpy( b_mem, vertices, sizeof(vertices) );
   vertex_buffer->Unlock();


   MSG msg;
   while( GetMessage( &msg, NULL, 0, 0 ) )
   {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
   }
   
   if(!dx_device)
      dx_device->Release();
   if(!dx9)
      dx9->Release();

   return 0;
}

void step(void)
{
   // Clear the back buffer to a blue color
   dx_device->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

   // Begin the scene.
   dx_device->BeginScene();

   dx_device->SetStreamSource( 0, vertex_buffer, 0, sizeof(vertex) );
   
   dx_device->SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE );

   dx_device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );


   // End the scene.
   dx_device->EndScene();
   
   dx_device->Present( NULL, NULL, NULL, NULL );
}
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
   switch( msg )
   {
           
       case WM_DESTROY:
            PostQuitMessage( 0 );
            return 0;

       case WM_TIMER:
            if(wParam==1)
                  step();
            return 0;
   }

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

It should open a window with a triangle in it. You need to link d3d9.lib

The import library names are the same as the ones in the sdk docs. Some Dx headers may give you a warning about a \ after //. You can simply ignore that warning. Perhaps I'll a switch to batmaker to deactivate that warning.

There's a rather good beginner tut in the DX docs under
DirectX Graphics/Programming Guide/Tutorials, Samples, Tools, and Tips




* Hosted for free by InvisionFree