Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 14 Sep 2011
Offline Last Active Sep 20 2014 07:31 PM

Topics I've Started

Font Rendering Woes (FreeType)

31 August 2014 - 04:20 AM

Not sure if it's better to post in here or the DirectX forums, but I digress.
Just starting out with using FreeType for my font system under DirectX9, and am having horrible results - the output characters look worse than the regular D3DXCreateFont / DrawText methods, for example:
Lower-case 'k':
Lower-case 'w':

Currently, all that's happening is the font is being loaded through TrueType, and I'm taking the bitmap it generates and putting it into an IDirect3DTexture9 struct, and then writing it to a file as a jpg.

Here is the code currently in use to do this (non-interesting bits removed, apologies for the generally atrocious code):

CFont::CFont(char* face, int height, int width, bool italic)
	FT_New_Face(library, face, NULL, &fFace);
	FT_Set_Char_Size(fFace, width, height, 300, 300);
	FT_Set_Pixel_Sizes(fFace, 0, 16); //ToDo: Fix this

void SetPixel(D3DLOCKED_RECT* rect, int x, int y, D3DCOLOR col)
	unsigned char *bits = (unsigned char *)rect->pBits;
	memcpy(&bits[rect->Pitch * y + 4 * x], &col, 4);

HRESULT CFont::Draw(char* text, int count, D3DCOLOR colour, DWORD format)
	FT_GlyphSlot slot = fFace->glyph;

	for(int n = 0; n < count; n++)
		FT_Set_Char_Size(fFace, 24 << 6, 24 << 6, 300, 300);

		FT_UInt glyph_index = FT_Get_Char_Index(fFace, text[n]);
		if(FT_Load_Glyph(fFace, glyph_index, FT_LOAD_DEFAULT))

		if(FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_NORMAL))

		FT_Bitmap *ftBitmap = &fFace->glyph->bitmap;

		const int height = ftBitmap->rows;
		const int width = ftBitmap->width;

		D3DXCreateTexture(Renderer().GetD3DDev(), width, height, D3DX_DEFAULT, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tmp);

		tmp->LockRect(0, &tmp2, NULL, D3DLOCK_DISCARD);
		FillMemory(tmp2.pBits, width*height*tmp2.Pitch, 255);
		//Code from: http://www.gamedev.net/topic/545202-freetype/
		for(int y = 0; y < height; ++y)
			for(int x = 0; x < width; ++x)
				byte intensity = ftBitmap->buffer[y * ftBitmap->pitch + x];
				byte r = (byte)(0 * intensity);
				byte g = (byte)(255 * intensity);
				byte b = (byte)(0 * intensity);
				SetPixel(&tmp2, x, y, D3DCOLOR_ARGB(255/*intensity*/, r, g, b));


		char finalName[6];
		sprintf(finalName, "%c.jpg", text[n]);
		D3DXSaveTextureToFile(finalName, D3DXIFF_JPG, tmp, NULL);

	return S_OK;

My question is, how can I improve the output from this code?

I've also attached the whole alphabet's images.
Many thanks in advance,

Tracing a ray to a certain X value

01 March 2012 - 01:34 AM

Hi all,

I'm trying to work on an editor for my game, and I'm having issues tracing a ray to a certain point.
On click, the Editor needs to trace the mouse ray to the Z point of 3, and add a 'plane' (rectangle) at that point.
It seems to be getting in the right sort of area on click, but its always off.
Posted Image
The red dot illustrates where I've clicked.
I've uploaded the files for you to play with (easier than me trying to explain)

And heres the code that handles the tracing of the ray and adding plane:
void Editor::addPlane(D3DXVECTOR3 oldMPos, D3DXVECTOR3 oldMDir, string texName, float* texScale, int whichForm)
D3DXMATRIX matView, matWorld;  

d3ddev->SetTransform(D3DTS_WORLD, &matStart);
matWorld = matStart;

if(whichForm == 0)
cameraPos[whichForm] = fpsCamera->getPosition();
cameraEyePos[whichForm] = fpsCamera->getEyePos();
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));  
d3ddev->SetTransform(D3DTS_VIEW, &matView);

D3DXMATRIX matProjection;	
(FLOAT)screenSize[whichForm].x / (FLOAT)screenSize[whichForm].y,
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);

POINT mousePos;

D3DXVECTOR3 mouseOut, mouseOut2;
D3DVIEWPORT9 viewPort;


ScreenToClient(hWnd[whichForm], &mousePos);

RECT temp;
GetClientRect(hWnd[whichForm], &temp);

D3DXVec3Unproject(&mouseOut, &D3DXVECTOR3(mousePos.x, mousePos.y, viewPort.MinZ), &viewPort, &matProjection, &matView, &matWorld);
D3DXVec3Unproject(&mouseOut2, &D3DXVECTOR3(mousePos.x, mousePos.y, viewPort.MaxZ), &viewPort, &matProjection, &matView, &matWorld);

float wz = 3.0f;
float wx = ((wz - mouseOut.z) * (mouseOut2.x - mouseOut.x)) / (mouseOut2.z - mouseOut.z) + mouseOut.x;
float wy = ((wz - mouseOut.z) * (mouseOut2.y - mouseOut.y)) / (mouseOut2.z - mouseOut.z) + mouseOut.y;
D3DXVECTOR3 mouseFinal = D3DXVECTOR3(wx, wy, wz);

addPlane(D3DXVECTOR2(mouseFinal.x, mouseFinal.y), D3DXVECTOR2(1.0f, 1.0f), 0.0f, PhysDefinition(NoPhysics, 0.0f, 0.0f, 0.0f), ForeGroundPlane, TextureDefinition(texName, D3DXVECTOR2(texScale[0], texScale[1]), 0.0f));

Any help appreciated


Rendering onto DepthStencilSurface causing black screen

15 February 2012 - 02:51 AM

Hi all,

Working on an editor for my game, with multiple swap chains (3 excluding implicit one to be exact) and I've run across a slight problem.
Since you cant have D3D9 create and manage the depth stencil with multiple swap chains, I've come across the need to make my own.
The swap chains initialize right, and are set to render to per frame. All the HRESULTs return S_OK and theres no errors in the log from DirectX (debug mode is on) - theres only warnings about redundant render states.
Any ideas? Please post if you want me to add some code from the editor.


[D3D9] 'Drawing' into window

12 February 2012 - 09:41 PM

Hi all,

Working on a level editor for my engine (still), and I'm at a loss as to how I should go about this next bit.
I want to have the user click, drag and release to make a box (which will be turned into a plane - 2D engine).
I can't seem to get the mouse coordinates to relate to the position in the 3d view.
Heres an image just so you guys can see what I've currently got:
Posted Image
Note, it has 3D views because I want this editor to be re-usable in the future.

Any help appreciated,

James Warner

Creating a grid (DX9)

24 January 2012 - 08:42 PM

Hi all,

Looking into creating a grid like in the Valve Hammer editor for use in my map editor.
I'll attach what I've got so far and a snippet of the valve hammer editors grid for reference.
What I have at the moment:
Posted Image
What I want it to look like:
Posted Image
The code Im using is:
void EditorDXHandler::renderGrid()
static int GridSize = 10;
static D3DXMATRIX matIdentity;


d3ddev->CreateVertexBuffer(sizeof(GRIDVERTEX) * GridSize * 9,

GRIDVERTEX* pGridData = 0;
g_buffer->Lock(0, 0, (void**)&pGridData, 0);

int index = 0;
for(; index <= GridSize * 4 + 1; index++)
float x = (index % 2) ? GridSize : -GridSize;
float y = index / 2 - GridSize;
float z = 0.0f;

pGridData[index].position = D3DXVECTOR3(x, y, z);
pGridData[index].color = D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f);

for(; index <= GridSize * 8 + 4; index++)
static int half = index;
float x = (index - half) / 2 - GridSize;
float y = (index % 2) ? -GridSize : GridSize;
float z = 0.0f;

pGridData[index].position = D3DXVECTOR3(x, y, z);
pGridData[index].color = D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f);


gridInit = true;

d3ddev->SetTransform(D3DTS_WORLD, &matIdentity);
d3ddev->SetStreamSource(0, g_buffer, 0, sizeof(GRIDVERTEX));
d3ddev->SetTexture(0, NULL);
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, GridSize * 4 + 2);

James Warner