Sign in to follow this  
NZT

DrawText not working?

Recommended Posts

Okay, so I can't seem to get the [url="http://msdn.microsoft.com/en-us/library/ms901121.aspx"]DrawText[/url] function to work (I'm a beginner in Directx). I'm following [url="http://www.toymaker.info/Games/html/text.html"]ToyMaker's DX9 Tutorial[/url]. I have sufficient error checking, none of them are indicating any failures anywhere. I'm also not receiving any compile-time nor run-time errors/warnings. I have properly configured my project to include the necessary Directx9 headers & libraries, as well. This is a default Win32 Windows application.

I'm using Microsoft Visual C++ Express edition on a 64 bit version of Windows 7.

Source Code: ([url="http://codeviewer.org/view/code:248d"]Highlighted Version[/url])
[CODE]
// DirectxText.cpp: Application entry point
#include "stdafx.h"
#include "DirectxText.h"
#include <d3d9.h>
#include <d3dx9.h>
#include <Windows.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define MAX_LOADSTRING 100
// Window size (width & height)
#define WINDOW_WIDTH 600
#define WINDOW_HEIGHT 400
// Window positions (x & y)
#define WINDOW_POSX 500
#define WINDOW_POSY 400
// Use code segments only available for debugging purposes
#ifndef DEBUG
#define DEBUG
#endif
// ---------------------------------------------------------------------------
// Global Variables:
HWND g_hWnd; // Handle (identifier) for main window
HINSTANCE g_hInst; // Current instance
TCHAR g_szTitle[MAX_LOADSTRING]; // Title bar text
TCHAR g_szWindowClass[MAX_LOADSTRING]; // Main window's class name
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Forward declarations for Win32 functions
USHORT MyRegisterClass(HINSTANCE hInstance); // USHORT is explicitly defined in WinDef.h
int InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Directx
HRESULT D3D_Init(HWND hWnd); // Initialize Directx globals
HRESULT D3D_Display(); // Render a frame
bool D3D_Cleanup(); // Gracefully release COM objects
IDirect3D9 *g_pD3D = NULL; // D3D interface
IDirect3DDevice9 *g_pD3Ddev = NULL; // D3D device interface
ID3DXFont *g_pD3DFont = NULL;
// ---------------------------------------------------------------------------

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, g_szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DIRECTXTEXT, g_szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
return false;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DIRECTXTEXT));
// Main message loop:
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
#ifdef DEBUG
// Only perform if DEBUG mode is specified:
if(FAILED(D3D_Display()))
MessageBox(g_hWnd, "D3D_Display failed!", "Main Message Loop", MB_ICONEXCLAMATION | MB_OK);
#else
D3D_Display();
#endif
}
}
#ifdef DEBUG
// Only perform if DEBUG mode is specified:
if(!D3D_Cleanup)
MessageBox(g_hWnd, "D3D_Cleanup failed, live COM objects may still reside in memory!", "Error", MB_ICONEXCLAMATION | MB_OK);
#else
D3D_Cleanup();
#endif
return (int) msg.wParam;
}
USHORT MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DIRECTXTEXT));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_DIRECTXTEXT);
wcex.lpszClassName = g_szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
int InitInstance(HINSTANCE hInstance, int nCmdShow)
{
g_hInst = hInstance; // Store instance handle in our global variable
g_hWnd = CreateWindow(g_szWindowClass, g_szTitle, WS_OVERLAPPEDWINDOW,
WINDOW_POSX, WINDOW_POSY, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
if (!g_hWnd)
return false;
if(SUCCEEDED(D3D_Init(g_hWnd)))
{
ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);
return true;
}
else
return false;
}
LRESULT CALLBACK WndProc(HWND g_hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(g_hWnd);
break;
default:
return DefWindowProc(g_hWnd, message, wParam, lParam);
}
break;
case WM_KEYDOWN: // Escape key was pressed, time to bounce!
if(wParam == VK_ESCAPE)
PostQuitMessage(0);
break;
case WM_PAINT:
hdc = BeginPaint(g_hWnd, &ps);
#ifdef DEBUG
if(FAILED(D3D_Display()))
MessageBox(g_hWnd, "D3D_Display failed!", "Main Message Loop", MB_ICONEXCLAMATION | MB_OK);
#else
D3D_Display();
#endif
EndPaint(g_hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(g_hWnd, message, wParam, lParam);
}
return 0;
}
HRESULT D3D_Init(HWND hWnd)
{
if(FAILED(g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return false;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = g_hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = WINDOW_WIDTH;
d3dpp.BackBufferHeight = WINDOW_HEIGHT;
HRESULT hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
NULL,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&g_pD3Ddev);
if(FAILED(hr))
return hr;
hr = 0;
// Create font object:
hr = D3DXCreateFont(g_pD3Ddev, 20, 5, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &g_pD3DFont);

if(FAILED(hr))
return hr;
// Purple background:
hr = g_pD3Ddev->Clear(0,
NULL,
D3DCLEAR_TARGET,
D3DCOLOR_XRGB(100, 0, 255),
1.0f,
0);
if(FAILED(hr))
return hr;
return D3D_OK;
}
HRESULT D3D_Display()
{
char Text[] = "Hello World!"; // Text to output
HRESULT hr = 0; // Function return code(s)
D3DCOLOR fontColor = D3DCOLOR_ARGB(255,0,255,0); // Red text (A, R, G, B)
// Create a rectangle to indicate where on the screen the text shall be drawn:
RECT rct;
ZeroMemory(&rct, sizeof(RECT));
rct.left = 2;
rct.right = 50;
rct.top = 10;
rct.bottom = rct.top + 20;
if(g_pD3DFont->DrawText(NULL, Text, sizeof(Text), &rct, 0, fontColor) == 0)
#ifdef DEBUG
// Only perform if DEBUG mode is specified:
MessageBox(g_hWnd, "DrawText function failed!", "D3D_Display", MB_ICONEXCLAMATION | MB_OK);
#else
return false;
#endif
hr = g_pD3Ddev->Present(NULL, NULL, NULL, NULL);
if(FAILED(hr))
return hr;
}
bool D3D_Cleanup()
{
if(g_pD3DFont)
g_pD3DFont->Release();
if(g_pD3Ddev)
g_pD3Ddev->Release();
if(g_pD3D)
g_pD3D->Release();
if(!g_pD3DFont && !g_pD3Ddev && !g_pD3D)
return true;
else
return false;
}
[/CODE]

Share this post


Link to post
Share on other sites
ok, you need to fill the first parameter to DrawText method which is a pointer to an existing ID3DXSprite interface. Use D3DXCreateSprite to do so. And after that:

[code] // Prepare the device for drawing sprites
if(SUCCEEDED(m_pD3DXSprite->Begin(D3DXSPRITE_SORT_TEXTURE | D3DXSPRITE_ALPHABLEND)))
{
// Render data on the screen
m_pD3DXFont->DrawText(m_pD3DXSprite, m_szFPSData, -1, &oClientRect[0], 0, 0xFFFFFFFF); // FPS data
m_pD3DXFont->DrawText(m_pD3DXSprite, m_oString.data(), -1, &oClientRect[1], 0, 0XFFFF0000); // Specified string

// End target sprite drawing
m_pD3DXSprite->End();
}[/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