Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualXuchilbara

Posted 26 November 2012 - 11:03 AM

Hey there,

I've been trying to create a font engine using Rastertek's guide over at http://rastertek.com/dx11tut12.html

I've come across a problem I can't seem to fix, I've created a struct which holds some data which is used to find the location and size of the font I am using from a image file (a .dds file). It initializes fine, but when it comes to rendering the struct's values are all 0. I'm going to show my font class and say whats going on, you can reference it all back from the tutorial linked above.

If anyone who is experienced in DirectX11 could see a solution to this problem I'd be grateful Also the 'fontdata.txt" and "font.dds" are available on that link also

Font.h
//--------------------------------------------------\\
// Font.h under Text class	   \\
// handles texture for the font	 \\
//--------------------------------------------------\\
#ifndef _FONT_H
#define _FONT_H
//includes
#include <D3D11.h>
#include <D3DX10math.h>
#include <fstream>
using namespace std;
#include "Texture.h"
//class
class Font
{
private:
  struct FontType
  {
   float left, right;
   int size;
  };
  struct VertexType
  {
   D3DXVECTOR3 position;
   D3DXVECTOR2 texture;
  };
public:
  Font();
  Font(const Font&amp;);
  ~Font();
  bool Init(ID3D11Device*, char*, CHAR*);
  void Shutdown();
  ID3D11ShaderResourceView* GetTexture();
  void BuildVertexArray(void*, char*, float, float);
private:
  bool LoadFontData(char*);
  void ReleaseFontData();
  bool LoadTexture(ID3D11Device*, CHAR*);
  void ReleaseTexture();
private:
  FontType* fonttype;
  Texture* texture;
};
#endif


Font.cpp
//--------------------------------------------------\\
// Font.cpp under Text class	  \\
// handles texture for the font	 \\
//--------------------------------------------------\\
#include "Font.h"
Font::Font()
{
fonttype = 0;
texture = 0;
}
Font::Font(const Font&amp; other){}
Font::~Font(){}
bool Font::Init(ID3D11Device* device, char* fontFilename, CHAR* textureFilename)
{
bool result;

//Load in the text file containing the font data.
result = LoadFontData(fontFilename);
if(!result)
{
  return false;
}
// Load the texture that has the font characters on it.
result = LoadTexture(device, textureFilename);
if(!result)
{
  return false;
}
return true;
}
void Font::Shutdown()
{
//Release the font texture.
ReleaseTexture();
//Release the font data.
ReleaseFontData();
return;
}
bool Font::LoadFontData(char* filename)
{
ifstream fin;
int i;
char temp;
//Create the font spacing buffer
fonttype = new FontType[95];
if(!fonttype)
{
  return false;
}
//Read in the font size and spacing between chars
fin.open(filename);
if(fin.fail())
{
  return false;
}
//Read in the 95 used ascii characters for text.
for(i=0; i<95; i++)
{
  fin.get(temp);
  while(temp != ' ')
  {
   fin.get(temp);
  }
  fin.get(temp);
  while(temp != ' ')
  {
   fin.get(temp);
  }
  fin >> fonttype[i].left;
  fin >> fonttype[i].right;
  fin >> fonttype[i].size;
}
// Close the file.
fin.close();
return true;
}
void Font::ReleaseFontData()
{
// Release the font data array.
if(fonttype)
{
  delete [] fonttype;
  fonttype = 0;
}
return;
}
bool Font::LoadTexture(ID3D11Device* device, CHAR* filename)
{
bool result;
//Create the texture object.
texture = new Texture;
if(!texture)
{
  return false;
}
//Initialize the texture object.
result = texture->Init(device, filename);
if(!result)
{
  return false;
}
return true;
}
void Font::ReleaseTexture()
{
//Release the texture object.
if(texture)
{
  texture->Shutdown();
  delete texture;
  texture = 0;
}
return;
}
ID3D11ShaderResourceView* Font::GetTexture()
{
return texture->GetTexture();
}
void Font::BuildVertexArray(void* vertices, char* sentence, float drawX, float drawY)
{
VertexType* vertexPtr;
int numLetters, index, i, letter;

// Coerce the input vertices into a VertexType structure.
vertexPtr = (VertexType*)vertices;
// Get the number of letters in the sentence.
numLetters = (int)strlen(sentence);
// Initialize the index to the vertex array.
index = 0;
// Draw each letter onto a quad.
for(i=0; i<numLetters; i++)
{
  letter = ((int)sentence[i]) - 32;
  // If the letter is a space then just move over three pixels.
  if(letter == 0)
  {
   drawX = drawX + 3.0f;
  }
  else
  {
   // First triangle in quad.
   vertexPtr[index].position = D3DXVECTOR3(drawX, drawY, 0.0f);  // Top left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3((drawX + fonttype[letter].size), (drawY - 16), 0.0f);  // Bottom right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 1.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3(drawX, (drawY - 16), 0.0f);  // Bottom left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 1.0f);
   index++;
   // Second triangle in quad.
   vertexPtr[index].position = D3DXVECTOR3(drawX, drawY, 0.0f);  // Top left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3(drawX + fonttype[letter].size, drawY, 0.0f);  // Top right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3((drawX + fonttype[letter].size), (drawY - 16), 0.0f);  // Bottom right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 1.0f);
   index++;
   // Update the x location for drawing by the size of the letter and one pixel.
   drawX = drawX + fonttype[letter].size + 1.0f;
  }
}
return;
}

During the while loop that fills up the array, it starts to store the data. But when it gets to 'drawX = drawX + fonttype[letter].size + 1.0f;' the fonttype array is empty, and stores all 0s for left, right and size.

Any ideas?

EDIT:

to get a better understanding of what I mean, you can see here the fonttype is holding values:
http://img651.imageshack.us/img651/9278/fonttypefilled.png

But at the end of the method it is empty:
http://img715.imageshack.us/img715/3660/fonttypeempty.png

I'm stumped

#2Xuchilbara

Posted 26 November 2012 - 11:00 AM

Hey there,

I've been trying to create a font engine using Rastertek's guide over at http://rastertek.com/dx11tut12.html

I've come across a problem I can't seem to fix, I've created a struct which holds some data which is used to find the location and size of the font I am using from a image file (a .dds file). It initializes fine, but when it comes to rendering the struct's values are all 0. I'm going to show my font class and say whats going on, you can reference it all back from the tutorial linked above.

If anyone who is experienced in DirectX11 could see a solution to this problem I'd be grateful Also the 'fontdata.txt" and "font.dds" are available on that link also

Font.h
//--------------------------------------------------\\
// Font.h under Text class	   \\
// handles texture for the font	 \\
//--------------------------------------------------\\
#ifndef _FONT_H
#define _FONT_H
//includes
#include <D3D11.h>
#include <D3DX10math.h>
#include <fstream>
using namespace std;
#include "Texture.h"
//class
class Font
{
private:
  struct FontType
  {
   float left, right;
   int size;
  };
  struct VertexType
  {
   D3DXVECTOR3 position;
   D3DXVECTOR2 texture;
  };
public:
  Font();
  Font(const Font&amp;);
  ~Font();
  bool Init(ID3D11Device*, char*, CHAR*);
  void Shutdown();
  ID3D11ShaderResourceView* GetTexture();
  void BuildVertexArray(void*, char*, float, float);
private:
  bool LoadFontData(char*);
  void ReleaseFontData();
  bool LoadTexture(ID3D11Device*, CHAR*);
  void ReleaseTexture();
private:
  FontType* fonttype;
  Texture* texture;
};
#endif


Font.cpp
//--------------------------------------------------\\
// Font.cpp under Text class	  \\
// handles texture for the font	 \\
//--------------------------------------------------\\
#include "Font.h"
Font::Font()
{
fonttype = 0;
texture = 0;
}
Font::Font(const Font&amp; other){}
Font::~Font(){}
bool Font::Init(ID3D11Device* device, char* fontFilename, CHAR* textureFilename)
{
bool result;

//Load in the text file containing the font data.
result = LoadFontData(fontFilename);
if(!result)
{
  return false;
}
// Load the texture that has the font characters on it.
result = LoadTexture(device, textureFilename);
if(!result)
{
  return false;
}
return true;
}
void Font::Shutdown()
{
//Release the font texture.
ReleaseTexture();
//Release the font data.
ReleaseFontData();
return;
}
bool Font::LoadFontData(char* filename)
{
ifstream fin;
int i;
char temp;
//Create the font spacing buffer
fonttype = new FontType[95];
if(!fonttype)
{
  return false;
}
//Read in the font size and spacing between chars
fin.open(filename);
if(fin.fail())
{
  return false;
}
//Read in the 95 used ascii characters for text.
for(i=0; i<95; i++)
{
  fin.get(temp);
  while(temp != ' ')
  {
   fin.get(temp);
  }
  fin.get(temp);
  while(temp != ' ')
  {
   fin.get(temp);
  }
  fin >> fonttype[i].left;
  fin >> fonttype[i].right;
  fin >> fonttype[i].size;
}
// Close the file.
fin.close();
return true;
}
void Font::ReleaseFontData()
{
// Release the font data array.
if(fonttype)
{
  delete [] fonttype;
  fonttype = 0;
}
return;
}
bool Font::LoadTexture(ID3D11Device* device, CHAR* filename)
{
bool result;
//Create the texture object.
texture = new Texture;
if(!texture)
{
  return false;
}
//Initialize the texture object.
result = texture->Init(device, filename);
if(!result)
{
  return false;
}
return true;
}
void Font::ReleaseTexture()
{
//Release the texture object.
if(texture)
{
  texture->Shutdown();
  delete texture;
  texture = 0;
}
return;
}
ID3D11ShaderResourceView* Font::GetTexture()
{
return texture->GetTexture();
}
void Font::BuildVertexArray(void* vertices, char* sentence, float drawX, float drawY)
{
VertexType* vertexPtr;
int numLetters, index, i, letter;

// Coerce the input vertices into a VertexType structure.
vertexPtr = (VertexType*)vertices;
// Get the number of letters in the sentence.
numLetters = (int)strlen(sentence);
// Initialize the index to the vertex array.
index = 0;
// Draw each letter onto a quad.
for(i=0; i<numLetters; i++)
{
  letter = ((int)sentence[i]) - 32;
  // If the letter is a space then just move over three pixels.
  if(letter == 0)
  {
   drawX = drawX + 3.0f;
  }
  else
  {
   // First triangle in quad.
   vertexPtr[index].position = D3DXVECTOR3(drawX, drawY, 0.0f);  // Top left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3((drawX + fonttype[letter].size), (drawY - 16), 0.0f);  // Bottom right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 1.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3(drawX, (drawY - 16), 0.0f);  // Bottom left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 1.0f);
   index++;
   // Second triangle in quad.
   vertexPtr[index].position = D3DXVECTOR3(drawX, drawY, 0.0f);  // Top left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3(drawX + fonttype[letter].size, drawY, 0.0f);  // Top right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3((drawX + fonttype[letter].size), (drawY - 16), 0.0f);  // Bottom right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 1.0f);
   index++;
   // Update the x location for drawing by the size of the letter and one pixel.
   drawX = drawX + fonttype[letter].size + 1.0f;
  }
}
return;
}

During the while loop that fills up the array, it starts to store the data. But when it gets to 'drawX = drawX + fonttype[letter].size + 1.0f;' the fonttype array is empty, and stores all 0s for left, right and size.

Any ideas?

EDIT:

to get a better understanding of what I mean, you can see here the fonttype is holding values:
http://img651.imageshack.us/img651/9278/fonttypefilled.png

But at the end of the method it is empty:
http://img715.imageshack.us/img715/3660/fonttypeempty.png

I'm stumped

#1Xuchilbara

Posted 26 November 2012 - 10:51 AM

Hey there,

I've been trying to create a font engine using Rastertek's guide over at http://rastertek.com/dx11tut12.html

I've come across a problem I can't seem to fix, I've created a struct which holds some data which is used to find the location and size of the font I am using from a image file (a .dds file). It initializes fine, but when it comes to rendering the struct's values are all 0. I'm going to show my font class and say whats going on, you can reference it all back from the tutorial linked above.

If anyone who is experienced in DirectX11 could see a solution to this problem I'd be grateful Also the 'fontdata.txt" and "font.dds" are available on that link also

Font.h
//--------------------------------------------------\\
// Font.h under Text class	   \\
// handles texture for the font	 \\
//--------------------------------------------------\\
#ifndef _FONT_H
#define _FONT_H
//includes
#include <D3D11.h>
#include <D3DX10math.h>
#include <fstream>
using namespace std;
#include "Texture.h"
//class
class Font
{
private:
  struct FontType
  {
   float left, right;
   int size;
  };
  struct VertexType
  {
   D3DXVECTOR3 position;
   D3DXVECTOR2 texture;
  };
public:
  Font();
  Font(const Font&);
  ~Font();
  bool Init(ID3D11Device*, char*, CHAR*);
  void Shutdown();
  ID3D11ShaderResourceView* GetTexture();
  void BuildVertexArray(void*, char*, float, float);
private:
  bool LoadFontData(char*);
  void ReleaseFontData();
  bool LoadTexture(ID3D11Device*, CHAR*);
  void ReleaseTexture();
private:
  FontType* fonttype;
  Texture* texture;
};
#endif


Font.cpp
//--------------------------------------------------\\
// Font.cpp under Text class	  \\
// handles texture for the font	 \\
//--------------------------------------------------\\
#include "Font.h"
Font::Font()
{
fonttype = 0;
texture = 0;
}
Font::Font(const Font& other){}
Font::~Font(){}
bool Font::Init(ID3D11Device* device, char* fontFilename, CHAR* textureFilename)
{
bool result;

//Load in the text file containing the font data.
result = LoadFontData(fontFilename);
if(!result)
{
  return false;
}
// Load the texture that has the font characters on it.
result = LoadTexture(device, textureFilename);
if(!result)
{
  return false;
}
return true;
}
void Font::Shutdown()
{
//Release the font texture.
ReleaseTexture();
//Release the font data.
ReleaseFontData();
return;
}
bool Font::LoadFontData(char* filename)
{
ifstream fin;
int i;
char temp;
//Create the font spacing buffer
fonttype = new FontType[95];
if(!fonttype)
{
  return false;
}
//Read in the font size and spacing between chars
fin.open(filename);
if(fin.fail())
{
  return false;
}
//Read in the 95 used ascii characters for text.
for(i=0; i<95; i++)
{
  fin.get(temp);
  while(temp != ' ')
  {
   fin.get(temp);
  }
  fin.get(temp);
  while(temp != ' ')
  {
   fin.get(temp);
  }
  fin >> fonttype[i].left;
  fin >> fonttype[i].right;
  fin >> fonttype[i].size;
}
// Close the file.
fin.close();
return true;
}
void Font::ReleaseFontData()
{
// Release the font data array.
if(fonttype)
{
  delete [] fonttype;
  fonttype = 0;
}
return;
}
bool Font::LoadTexture(ID3D11Device* device, CHAR* filename)
{
bool result;
//Create the texture object.
texture = new Texture;
if(!texture)
{
  return false;
}
//Initialize the texture object.
result = texture->Init(device, filename);
if(!result)
{
  return false;
}
return true;
}
void Font::ReleaseTexture()
{
//Release the texture object.
if(texture)
{
  texture->Shutdown();
  delete texture;
  texture = 0;
}
return;
}
ID3D11ShaderResourceView* Font::GetTexture()
{
return texture->GetTexture();
}
void Font::BuildVertexArray(void* vertices, char* sentence, float drawX, float drawY)
{
VertexType* vertexPtr;
int numLetters, index, i, letter;

// Coerce the input vertices into a VertexType structure.
vertexPtr = (VertexType*)vertices;
// Get the number of letters in the sentence.
numLetters = (int)strlen(sentence);
// Initialize the index to the vertex array.
index = 0;
// Draw each letter onto a quad.
for(i=0; i<numLetters; i++)
{
  letter = ((int)sentence[i]) - 32;
  // If the letter is a space then just move over three pixels.
  if(letter == 0)
  {
   drawX = drawX + 3.0f;
  }
  else
  {
   // First triangle in quad.
   vertexPtr[index].position = D3DXVECTOR3(drawX, drawY, 0.0f);  // Top left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3((drawX + fonttype[letter].size), (drawY - 16), 0.0f);  // Bottom right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 1.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3(drawX, (drawY - 16), 0.0f);  // Bottom left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 1.0f);
   index++;
   // Second triangle in quad.
   vertexPtr[index].position = D3DXVECTOR3(drawX, drawY, 0.0f);  // Top left.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].left, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3(drawX + fonttype[letter].size, drawY, 0.0f);  // Top right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 0.0f);
   index++;
   vertexPtr[index].position = D3DXVECTOR3((drawX + fonttype[letter].size), (drawY - 16), 0.0f);  // Bottom right.
   vertexPtr[index].texture = D3DXVECTOR2(fonttype[letter].right, 1.0f);
   index++;
   // Update the x location for drawing by the size of the letter and one pixel.
   drawX = drawX + fonttype[letter].size + 1.0f;
  }
}
return;
}

During the while loop that fills up the array, it starts to store the data. But when it gets to 'drawX = drawX + fonttype[letter].size + 1.0f;' the fonttype array is empty, and stores all 0s for left, right and size.

Any ideas?

PARTNERS