Thread: DirectX9 Font Problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109

    Question DirectX9 Font Problem

    It is the *start* of my game and of my first real program with DirectX. I'm still working off tutorials and trying to learn it as fast as possible so that I can make a game immediately. I have come upon one issue when it came to writing text to the screen.

    All the tutorials I have read have told me to load/write fonts in a certain way. However, when I tried it in my compiler, it came up with an error, saying there were too many arguments and stuff. I am now doing it the way the compiler told me to.

    The prototype:
    Code:
    HRESULT WINAPI
        D3DXCreateFont(
            LPDIRECT3DDEVICE9   pDevice,
            HFONT               hFont,
            LPD3DXFONT*         ppFont);
    The original prototype:
    Code:
    HRESULT WINAPI D3DXCreateFont(LPDIRECT3DDEVICE9 pDevice,
        INT Height,
        UINT Width,
        UINT Weight,
        UINT MipLevels,
        BOOL Italic,
        DWORD CharSet,
        DWORD OutputPrecision,
        DWORD Quality,
        DWORD PitchAndFamily,
        LPCTSTR pFacename,
        LPD3DXFONT *ppFont);
    Even the MSDN tutorial said to use the second way, but it doesn't work. Also, the way of writing text in the scene didn't work.

    Old way:
    Code:
    Font->DrawText(NULL, "Text Here", -1, &FontPosition, DT_CENTER, 0xffffffff);
    The way the compiler said (cant find the prototype, so this is how I did it):
    Code:
    Font->DrawText("Demo", 0, &FontPosition, DT_CENTER, 0xffffffff);
    Anyway, I fixed both those things in my code and so I got no more errors. However, the program quits immediately after starting, and I want to know why.

    The code for my program (I am just starting out and have a random triangle in there for no reason):
    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include<d3dx9.h>
    
    #define D3DFVF_D3DVertex (D3DFVF_XYZ | D3DFVF_DIFFUSE)
    
    bool InitializeDirect3D(HWND hwnd, bool fullscreen);
    bool InitializeObject();
    void RenderScence();
    void ShutdownDirect3D();
    LPDIRECT3D9 Direct3D_Object = NULL;
    LPDIRECT3DDEVICE9 D3D_Device = NULL;
    LPD3DXFONT Font = NULL;
    LPDIRECT3DVERTEXBUFFER9 Vertex_Buffer = NULL;
    RECT FontPosition = {0, 0, 0, 0};
    HFONT hfont = CreateFont(0, 1, 0, 0, FW_DONTCARE, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_MODERN, "Courier New");
    struct Vertex
    {
        FLOAT x, y, z;
        DWORD color;
    };
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch(message) {
    		   case WM_KEYUP:
    		       switch(wParam) {
    		           case VK_ESCAPE:
    		               PostQuitMessage(0);
    		               break;
                   }        
                break;
             case WM_DESTROY:
             case WM_CLOSE:
                PostQuitMessage(0);
                break;
             default:
                   break;
         }
         return DefWindowProc( hwnd, message, wParam, lParam );
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
       MSG msg;
       HWND hwnd;
       WNDCLASSEX windowClass;
       bool done = false;
       windowClass.cbSize = sizeof(WNDCLASSEX);
       windowClass.style = CS_HREDRAW | CS_VREDRAW;
       windowClass.lpfnWndProc = WndProc;
       windowClass.cbClsExtra = 0;
       windowClass.cbWndExtra = 0;
       windowClass.hInstance = hInstance;
       windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
       windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
       windowClass.hbrBackground = NULL;
       windowClass.lpszMenuName = NULL;
       windowClass.lpszClassName = "XClass";
       windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
       if(!RegisterClassEx(&windowClass)) return 0;
       hwnd = CreateWindow("XClass", "Empty DirectX", WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU |WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 100, 100, 640, 480, NULL, NULL, hInstance, NULL);
       if(!hwnd) return 0;
    
       ShowWindow(hwnd, SW_SHOW);
       UpdateWindow(hwnd);
       done = false;
       if(!InitializeDirect3D(hwnd, true)) done = true;
       while(!done)
          {
             if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
                {
                   if(msg.message == WM_QUIT)
                      {
                         done = true;
                      }
                   TranslateMessage(&msg);
                   DispatchMessage(&msg);
                }
             else
                {
                   RenderScence();
                }
          }
    	ShutdownDirect3D();
       UnregisterClass("XClass", windowClass.hInstance);
    
       return (int)msg.wParam;
    }
    
    
    bool InitializeDirect3D(HWND hwnd, bool fullscreen) {
       D3DDISPLAYMODE DisplayMode;
       D3DPRESENT_PARAMETERS Present_Parameters;
       D3DCAPS9 D3DCaps;
       ZeroMemory(&Present_Parameters, sizeof(Present_Parameters));
       Direct3D_Object = Direct3DCreate9(D3D_SDK_VERSION);
       if(Direct3D_Object == NULL)
          {
             MessageBox(NULL, "Error, couldn't initialize DirectX", "Error!", MB_ICONSTOP | MB_OK);
             return false;
          }
       if(FAILED(Direct3D_Object->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode))) {
           MessageBox(NULL, "Error setting the display mode.", "Error!", MB_ICONSTOP | MB_OK);
           return false;
       }
       if(FAILED(Direct3D_Object->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3DCaps))) {
           return false;
       }
       DWORD VertexProcessing = 0;
       if(D3DCaps.VertexProcessingCaps != 0) {
           VertexProcessing |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
       }
       else {
           VertexProcessing |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
       }
       if(fullscreen) {
           Present_Parameters.Windowed = FALSE;
           Present_Parameters.BackBufferWidth = 800;
           Present_Parameters.BackBufferHeight = 600;
       }
       else {
           Present_Parameters.Windowed = TRUE;
       }
       Present_Parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
       Present_Parameters.BackBufferFormat = DisplayMode.Format;
       Present_Parameters.BackBufferCount = 1;
       Present_Parameters.EnableAutoDepthStencil = TRUE;
       Present_Parameters.AutoDepthStencilFormat = D3DFMT_D16;
       if(FAILED(Direct3D_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, VertexProcessing, &Present_Parameters, &D3D_Device))) {
           MessageBox(NULL, "CreateDevice() failed!  Make sure you have DirectX 9.", "Error!", MB_ICONSTOP | MB_OK);
           return false;
       }
       D3D_Device->SetRenderState(D3DRS_LIGHTING, false);
       D3D_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
       if(D3D_Device == NULL) {
           MessageBox(NULL, "D3D_Device setup error", "Error!", MB_ICONSTOP | MB_OK);
           return false;
       }
       if(!InitializeObject()) {
           return false;
       }
       if(FAILED(D3DXCreateFont(D3D_Device, hfont, &Font))) {
           return false;
       }
       FontPosition.top = 8;
       FontPosition.left = 8;
       FontPosition.right = 48;
       FontPosition.bottom = 16;
       return true;
    }
    
    
    bool InitializeObject()
    {
        Vertex Triangle_Data[6] =
       {
    	   {-0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)},
           {0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)},
    	   {0.0f, 0.3f, 0.0f, D3DCOLOR_XRGB(0,0,255)},
           {-2.5f, -2.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)},
           {3.0f, 3.0f, 2.23f, D3DCOLOR_XRGB(100,100,100)},
    	   {2.5f, -2.5f, 0.0f, D3DCOLOR_XRGB(0,0,255)}
       };
       if(FAILED(D3D_Device->CreateVertexBuffer(3 * sizeof(Vertex), 0, D3DFVF_D3DVertex, D3DPOOL_DEFAULT, &Vertex_Buffer, NULL))) {
           return false;
       }
       Vertex* Vertices;
       if(FAILED(Vertex_Buffer->Lock(0, sizeof(Triangle_Data), (void**)&Vertices, 0))) {
           return false;
       }    
       memcpy(Vertices, Triangle_Data, sizeof(Triangle_Data));
       Vertex_Buffer->Unlock();
       return true;
    }
    
    
    void RenderScence() {
       D3D_Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
       D3D_Device->BeginScene();
          Font->DrawText("Demo", 0, &FontPosition, DT_CENTER, 0xffffffff);
          D3D_Device->SetStreamSource(0, Vertex_Buffer, 0, sizeof(Vertex));
          D3D_Device->SetFVF(D3DFVF_D3DVertex);
          D3D_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
       D3D_Device->EndScene();
       D3D_Device->Present(NULL, NULL, NULL, NULL);
    }
    
    
    void ShutdownDirect3D() {
       if(D3D_Device != NULL) {
           D3D_Device->Release();
           D3D_Device = NULL;
       }    
       if(Direct3D_Object != NULL) {
           Direct3D_Object->Release();
           Direct3D_Object = NULL;
       }
       if(Vertex_Buffer != NULL) {
           Vertex_Buffer->Release();
           Vertex_Buffer = NULL;
       }
       if(Font) {
           Font->Release();
           Font = NULL;
       }
    }
    I am using Dev-C++ 4.9.9.2.

    If anyone can help me, it would be much appreciated.
    Last edited by Morgul; 04-15-2005 at 04:34 PM.
    Sic vis pacum para bellum. If you want peace, prepare for war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Problem with DrawText (DirectX9)
    By Diod in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2006, 07:21 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. problem with my font manager
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 03-07-2006, 08:03 AM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM