Sign in to follow this  
kamal7

Need Help With Directx 9 Font

Recommended Posts

kamal7    140
Hello I am having trouble displaying font with directx 9 with an external command (different class). However if I keep all the code in one class it works, but I would like to seperate the code to display the font from a different class other than "main" (shown below) for certain reasons. Perhaps I am going in the wrong direction, if so please advise me another path. Thank-you.

In my code I use the line:
DxDraw.DisplayText("Beta Client", 10, wy-20, -1, -1, 255, 0, 200, 0);
to initiate the font "Beta Client" which would work if it wasn't an external command (having DxDraw class implemented into WinMain.cpp).
This however doesn't work as an external command, and if the command was removed, the .exe would open perfectly. I need help solving the external command issue.

I was thinking as I reasearshed a solution to this issue, perhaps I should call the font device in the WinMain class first? (i.e. using GetDC(), etc. )

Here is my "main" code: DxDraw.DisplayText(...); function called where I will initiate the font display commands.
[code]
#include "WinMain.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{ char szAppClass[32];
MSG uMsg;
RECT rScreen;

sprintf(szAppClass, "Client-%d", hInstance);

WNDCLASSEX wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = (CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS);
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppClass;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

RegisterClassEx(&wc);

DWORD dwStyle;

g_hWnd = CreateWindow(szAppClass, "Client Title", dwStyle, 0, 0, wx, wy, NULL, NULL, hInstance, NULL);

if (!g_hWnd) {
MessageBox(g_hWnd, "Create window pointer is corrupt.", "ERROR!", MB_OK);
return 0;
}


if (DxDraw.DX3DInit(wx, wy) != S_OK)
{
MessageBox(g_hWnd, "Initialization error.", "ERROR!", MB_OK);
return 0;
}

ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);

DxDraw.bInit(); // Create sprite classes.

while(GetMessage(&uMsg, NULL, 0, 0))
{
DxDraw.BeginRender();
DxDraw.DisplayText("Beta Client", 10, 20, -1, -1, 255, 0, 200, 0);
DxDraw.EndRender();

TranslateMessage(&uMsg);
DispatchMessage(&uMsg);
}

#ifndef _DEBUG
ReleaseMutex(hMutex);
CloseHandle(hMutex);
#endif

return 0;
}

LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}
[/code]

Here is the class for displaying the font, etc.:
[code]
#include "DxDraw.h"

extern bool bFullScreen;
extern HWND g_hWnd;

CDxDraw::CDxDraw()
{
D3DXCreateFont(g_pD3DDevice, 13, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Tahoma"), &m_pFont);
g_pDirect3D = NULL;
g_pD3DDevice = NULL;
m_pFont = NULL;
}

CDxDraw::~CDxDraw()
{ int i;

for (i = 0; i < DEF_MAXSPRITES; i++) m_pSprite[i] = NULL;
m_pFont->Release();
g_pD3DDevice->Release();
g_pDirect3D->Release();
}

HRESULT CDxDraw::DX3DInit(DWORD iWidth, DWORD iHeight)
{
if ((g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL){
MessageBox(g_hWnd, "Direct3D create interface pointer is undefined.", "ERROR!", MB_OK);
return 0;
}

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));

d3dpp.BackBufferWidth = iWidth;
d3dpp.BackBufferHeight = iHeight;
d3dpp.hDeviceWindow = g_hWnd; // set the window to be used by Direct3D
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; // set the back buffer format: D3DFMT_A16B16G16R16 = 64-bit (unavailable), D3DFMT_A8R8G8B8 = 32-bit with alpha channel

if (bFullScreen) { // Fullscreen Mode
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP; // discard old frames
d3dpp.BackBufferCount = 1;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.Windowed = FALSE; // program fullscreen
}else { // Window Mode
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.Windowed = TRUE; // program windowed
}

HRESULT hRes;
if(FAILED(hRes = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice))) return hRes;

float Aspect = (float)d3dpp.BackBufferWidth / (float)d3dpp.BackBufferHeight;
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection, D3DX_PI/4.0f, Aspect, 0.001f, 1000.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProjection);
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

return S_OK;
}

void CDxDraw::BeginRender()
{ int i;
if (g_pD3DDevice == NULL) return;

// Clear the backbuffer to a black color
g_pD3DDevice->Clear( 0, //number of rectangles to clear, 0 for all
NULL, //rects to be cleared, NULL for entire buffer
D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, //the buffers to clear
D3DCOLOR_XRGB(255,255,255), //the Color to fill the buffer with
1.0f, //depth for the z-buffer to clear
0 //stencil buffer clear
);

g_pD3DDevice->BeginScene();

for (i = 0; i < DEF_MAXSPRITES; i++)
if (m_pSprite[i] != NULL) m_pSprite[i]->render(g_pD3DDevice, 100);

}

void CDxDraw::EndRender()
{
g_pD3DDevice->EndScene();
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}

void CDxDraw::bInit()
{
m_pSprite[0] = new class CSprite(g_pD3DDevice,"1_b.jpg", 0, 0, 1366, 768);
m_pSprite[1] = new class CSprite(g_pD3DDevice,"logo.png", 500, 0, 290, 240);
}

void CDxDraw::DisplayText(char *pString, short sX, short sY, short sMaxW_X, short sMaxL_Y, short sA, short sR, short sG, short sB)
{
// Create a rectangle to indicate where on the screen it should be drawn
RECT rct;
rct.left = sX;
rct.top = sY;

//Set default rectangle coordinates (hidden)
m_pFont->DrawText(NULL, pString, -1, &rct, DT_CALCRECT | DT_WORDBREAK | DT_SINGLELINE, NULL);

//Determine if align is required and set max rectangle rct.right(x)/rct.bottom(y) coordinates.
if (sMaxW_X != -1) rct.right = rct.left+sMaxW_X;
if (sMaxL_Y != -1) rct.bottom = rct.top+sMaxL_Y;

//Draw text in set rectangle coordinates
m_pFont->DrawText(NULL, pString, -1, &rct, DT_WORDBREAK | DT_SINGLELINE | DT_CENTER | DT_VCENTER, D3DCOLOR_ARGB(sA,sR,sG,sB)); //D3DCOLOR_ARGB(alpha,red,green,blue)
}
[/code]


If you guys need more details please respond.

[b][size="4"]Thanks a lot for your help in advance![/size][/b]

Sincerely, Kamal.

Share this post


Link to post
Share on other sites
kamal7    140
Can anyone help me? I want to be able to display fonts from an external command but my code seems to not work and causes the .exe to crash when opened.

Share this post


Link to post
Share on other sites
ankhd    2304
did you call DX3DInit before [color="#660066"]CDxDraw[/color][color="#666600"]::[/color][color="#660066"]CDxDraw[/color][color="#666600"]();

I say that would be it there going by your lack off error reports[/color]

Share this post


Link to post
Share on other sites
kamal7    140
[quote name='ankhd' timestamp='1310887646' post='4836277']
did you call DX3DInit before [color="#660066"]CDxDraw[/color][color="#666600"]::[/color][color="#660066"]CDxDraw[/color][color="#666600"]();

I say that would be it there going by your lack off error reports[/color]
[/quote]

Yes I called DX3DInitbefore [color="#660066"]CDxDraw[/color][color="#666600"]::[/color][color="#660066"]CDxDraw[/color][color="#666600"](); I will call it after and report back.[/color]

Share this post


Link to post
Share on other sites
kamal7    140
I edited my post to better organize and explain the issue present. Please, if anyone could tell me what is wrong in my code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this