DirectX9 - Wyswietlanie tekstu

0

Witam, mam 2 problemy.
Wyglada to tak:
http://prntscr.com/4d62ze

  1. Wyciek pamieci (log)
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00BB72E8: 76 bytes ----------
  Leak Hash: 0xBE400EB3, Count: 1, Total 76 bytes
  Call Stack (TID 8960):
    0x00DBC260 (File and line number not available): MSVCR120D.dll!operator new
    c:\users\theaifam5\documents\visual studio 2013\projects\cs-go hack\cs-go hack\main.cpp (47): CS-GO Hack.exe!WinMain + 0x7 bytes
    f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (618): CS-GO Hack.exe!__tmainCRTStartup + 0x15 bytes
    f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): CS-GO Hack.exe!WinMainCRTStartup
    0x7721919F (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0xE bytes
    0x77C0A22B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x84 bytes
    0x77C0A201 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x5A bytes
  Data:
    20 03 00 00    58 02 00 00    16 00 00 00    01 00 00 00     ....X... ........
    00 00 00 00    00 00 00 00    01 00 00 00    6C 09 0C 00     ........ ....l...
    01 00 00 00    01 00 00 00    50 00 00 00    00 00 00 00     ........ P.......
    00 00 00 00    00 00 00 00    48 F5 BB 00    E0 0F BF 00     ........ H.......
    E0 C2 20 01    6C 09 0C 00    00 00 00 00                    ....l... ........


Visual Leak Detector detected 1 memory leak (112 bytes).
Largest number used: 112 bytes.
Total allocations: 112 bytes.
Visual Leak Detector is now exiting.
  1. String sie wyświetla ale jako chińskie znaczki i nie wiem za bardzo o co chodzi, w czym problem.

w WinAPI:


	test = new DirectX9(hWindowMain);

	ShowWindow(hWindowMain, nShowCmd);

	test->Initialize();

	while (GetMessage(&hMessages, NULL, NULL, NULL))
	{
		test->Render(Render);

		TranslateMessage(&hMessages);
		DispatchMessage(&hMessages);
	}
delete test;

funkcja Ren:

void Render(LPDIRECT3D9 d3d, LPDIRECT3DDEVICE9 d3ddev, LPD3DXFONT d3dfont)
{
	// clear the window alpha
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);

	d3ddev->BeginScene();    // begins the 3D scene


	test->DrawString(10, 50, D3DCOLOR_ARGB(255, 255, 0, 0), d3dfont, "Test rendering string :D");



	d3ddev->EndScene();    // ends the 3D scene

	d3ddev->Present(NULL, NULL, NULL, NULL);   // displays the created frame on the screen
}

DirectX9.h

#pragma once
typedef void (*RenderCallback)(LPDIRECT3D9, LPDIRECT3DDEVICE9, LPD3DXFONT);

class DirectX9
{
protected:
	D3DPRESENT_PARAMETERS m_d3dpp;
private:
	LPDIRECT3D9 m_d3d;
	LPDIRECT3DDEVICE9 m_d3ddev;
	LPD3DXFONT m_d3dfont;
	HWND m_hWnd;
	RenderCallback* m_render;
public:
	DirectX9(HWND hWnd);
	void Initialize();
	void DrawString(int x, int y, DWORD color, LPD3DXFONT g_pFont, const char *fmt);
	void Render(RenderCallback callback);
	~DirectX9();
};

DirectX9.cpp

#include "stdafx.h"
#include "DirectX9.h"


DirectX9::DirectX9(HWND hWnd)
{
	m_hWnd = hWnd;

	m_d3d = Direct3DCreate9(D3D_SDK_VERSION);

	ZeroMemory(&m_d3dpp, sizeof(m_d3dpp));

	m_d3dpp.Windowed = TRUE;
	m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	m_d3dpp.hDeviceWindow = m_hWnd;
	m_d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	m_d3dpp.BackBufferHeight = 600;
	m_d3dpp.BackBufferWidth = 800;

	m_d3dpp.EnableAutoDepthStencil = TRUE;
	m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
}

void DirectX9::DrawString(int x, int y, DWORD color, LPD3DXFONT g_pFont, const char *fmt)
{
	RECT FontPos = { x, y, x + 120, y + 16 };
	char buf[1024] = { '\0' };
	va_list va_alist;

	va_start(va_alist, fmt);
	vsprintf_s(buf, fmt, va_alist);
	va_end(va_alist);
	g_pFont->DrawText(NULL, (LPCWSTR)buf, -1, &FontPos, DT_NOCLIP, color);
}

void DirectX9::Initialize()
{
	m_d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		m_hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&m_d3dpp,
		&m_d3ddev
		);

	D3DXCreateFont(
		m_d3ddev,
		50,
		0,
		FW_BOLD,
		1,
		0,
		DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_DONTCARE,
		L"Comic Sans MS",
		&m_d3dfont
		);
}

void DirectX9::Render(RenderCallback callback)
{
	callback(m_d3d, m_d3ddev, m_d3dfont);
}

DirectX9::~DirectX9()
{
	delete m_d3dfont;
	delete m_d3dfont;
	delete m_d3ddev;
	delete &m_d3dpp;
	delete m_d3d;
}
2

Tu jest prawdopodobnie twój problem:

aifam96 napisał(a):
	g_pFont->DrawText(NULL, (LPCWSTR)buf, -1, &FontPos, DT_NOCLIP, color);

Rzutujesz zwykłego stringa, na widestringa.

0

Ha! tak własnie myślałem.. ;) Zmieniłem na Multi-Byte character Set i jest ok.. dziekuje ;) Teraz skoro wiem gdzie problem z tekstem, zostało tylko z wyciekiem pamięci. ;/

1

Obiekty directxa powinno się chyba zwalniać poprzez wywołanie metody Release, a nie poprzez delete.

0
Call Stack (TID 9644):
    0x0F9BC260 (File and line number not available): MSVCR120D.dll!operator new
    c:\users\theaifam5\documents\visual studio 2013\projects\cs-go hack\cs-go hack\main.cpp (47): CS-GO Hack.exe!WinMain + 0x7 bytes

a w kodzie to "test = new DirectX9(hWindowMain);"
Myśle że w konstruktorze coś może byc nie tak.

Mimo ze jest Release();

0

Ok poradziłem sobie.
Sprawcą wycieku był: D3DPRESENT_PARAMETERS
:) d

1

LPDIRECT3D9 m_d3d;

Używaj smartpointerów do COM. Dzięki temu unikniesz wycieków.
Najpierw dodajesz do kodu takie makro:

#include <comdef.h>

_COM_SMARTPTR_TYPEDEF(IDirect3D9, __uuidof(IDirect3D9));

To ci tworzy typedefa o nazwie IDirect3D9Ptr który jest smartpointerem opakowującym IDirect3D9* (czyli LPDIRECT3D9).
Destruktor smartpointera automatycznie wywoła Release (a nie delete!)

http://msdn.microsoft.com/en-us/library/417w8b3b.aspx

0

Zrobiłem według twoich zaleceń. lecz nie wiem czy też dobrze zrobiłem robiąc to z Resource typu Font.

_COM_SMARTPTR_TYPEDEF(ID3DXFont, IID_ID3DXFont);

Po tym wywala mi przy zmianie rozdzielczości błąd.

D3DXCreateFont(
		g_d3ddev,
		100,
		0,
		FW_BOLD,
		1,
		0,
		DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_DONTCARE,
		L"Comic Sans MS",
		&g_d3dfont
		); // << Tutaj pokazuje bład, zatrzymuje program

Czyżby problem z "g_d3dfont" ? Ponieważ przy zmianie rozdzielczości wywołuje g_d3dfont->Release() by zresetowac urządzenie

void DirectX9::Resize() // Odniesienie w WinMain w WM_SIZE
{
	RECT rc = Utils::GetWindowRect(g_hWnd); // hWnd - jego rozdzielczośc jest ustawiana w WHILE w WinMain z innego hWnd.
	if (rc.bottom >= 0 && rc.left >= 0 && rc.right >= 0 && rc.top >= 0)
	{
		if (g_d3ddev != NULL)
		{
			if (g_d3dfont != NULL)
				g_d3dfont->Release();
			
			g_d3dpp.BackBufferHeight = rc.bottom - rc.top;
			g_d3dpp.BackBufferWidth = rc.right - rc.left;

			g_d3ddev->Reset(&g_d3dpp);
	
			InitializeResources(); // Ta funckcja zawiera tylko i wyłącznie powyższą funkcję inicjalizacji FONT
		}
	}
}
1

jakiego typu jest g_d3dfont?
jeśli to smartpointer to powinno być

ID3DXFont g_d3dfont;

i wtedy nie używasz Release, ale możesz ewentualnie przypisać NULL. To wywoła Release.

//            if (g_d3dfont != NULL)
//                g_d3dfont->Release();

            g_d3dfont = NULL;
            D3DXCreateFont(..., &g_d3dfont);

1 użytkowników online, w tym zalogowanych: 0, gości: 1