Well there's been talk about making this section more active, and I suppose one way I could help is by participating in sharing my own troubles coding as well. Now the reason I haven't posted code in the longest is because I've been working on a rather large project, but about 1 month ago it grinded to a halt because I had a mysterious error. I didn't want to get help because I would have to release all my code. Even though Im all for open source I got kinda full of myself. I should have asked for help ages ago. So from now on I have decided I will make this project open source, and in the future may even make it a group project if anyone is interested.
On to the problem. The technique Im using is pretty unique. Instead of creating and destroying a font object when I need it Im creating roughly 1250 font objects. A class manages all of the fonts. When finished accessing fonts it will optimize and destroy all unneccesary pointers. It also will create new font objects when you need them and deallocate them when you don't. Its sorta like a Java-style implementation for DirectX, in that the memory allocation is handled by the class.
The problem I run into is on initializing the fonts. Now despite hours of tinkering I just don't know how to find the error. There's no way for me to debug it, because I must inject it into the D3D test environment.....
Hmm... an idea just hit me that I could in fact compile the code into an executable instead of a DLL and create my own D3D environment. That would allow me to debug it. Ofcourse Im not good at debugging. So anyone who knows any good tutorials for debuugging please let me know.
Also since the god of open source seems to have blessed me with this idea. Im going to go ahead and fulfill my promise and release the source and hope for the best:
Header file for DPrint class
Code:
/*********************************************
DPrint is a class that streamlines the simple
2D DirectX functions often used in D3D Menus
such as drawing Text, simple shapes, etc.
Created By: why06 (why06mail@gmail.com)
*********************************************/
#include"HEADERS.h"
#ifndef DPRINT_H
#define DPRINT_H
const unsigned int NUMTYPEFACES = 16;
const unsigned int NUMFONTWEIGHTS = 10;
const unsigned int NUMFONTSIZES = 26;
const unsigned int LARGESTFONTSIZE = 50;
const unsigned int LARGESTFONTWEIGHT = 1000;
const unsigned int ITALICS = 2;
const unsigned int MAXFONTS = (NUMTYPEFACES * NUMFONTSIZES * NUMFONTWEIGHTS * ITALICS);
struct TYPEFACE
{
static const UINT ARIAL = 0;
static const UINT ARIALBLACK = 1;
static const UINT BOOKANTIQUA = 2;
static const UINT CALIBRI = 3;
static const UINT COMICSANSMS = 4;
static const UINT COURIERNEW = 5;
static const UINT GEORGIA = 6;
static const UINT IMPACT = 7;
static const UINT LUCIDACONSOLE = 8;
static const UINT LUCIDASANSUNICODE = 9;
static const UINT MICROSOFTSANSSERIF = 10;
static const UINT SYMBOL = 11;
static const UINT TAHOMA = 12;
static const UINT TIMESNEWROMAN = 13;
static const UINT TREBUCHETMS = 14;
static const UINT VERDANA = 15;
};
typedef struct FFORMAT
{
UINT typeface;
UINT fontweight;
UINT fontsize;
BOOL italic;
UINT ordinal;
BOOL In_use;
}*LPFFORMAT;
class DFont
{
LPDIRECT3DDEVICE9 pDevice;
LPD3DXFONT* pfont;
string* fontnames;
void GenerateFont(unsigned int fontindex);
public:
DFont(LPDIRECT3DDEVICE9 pDevice);
LPD3DXFONT GetFont(LPFFORMAT pFormat);
LPD3DXFONT GetFont(unsigned int index);
bool GetFontFormatFromIndex(LPFFORMAT pformat, UINT index);
int DeleteFont(unsigned int index);
int DeleteFontsExcept(int* intarray);
void PreReset();
void PostReset();
};
//***********************************************************************************
//class: Dprint
//purpose: A wrapper class for D3DFONT to simplify the construction of this menu base
//***********************************************************************************
class DPrint
{
LPDIRECT3DDEVICE9 pDevice;
int origx, origy;
int x, y;
int vspacing;//vertical spacing
int width;
DFont* pDFont;
LPFFORMAT* UsedFonts;
unsigned int ufindex;
bool checkusage;
void AddtoUsedFonts(LPFFORMAT pFormat);
public:
//********************************************************************************
//funtion: DPrint()
//purpose: Contructs new Dprint object
//********************************************************************************
DPrint(int startx, int starty, int vertical_spacing, LPDIRECT3DDEVICE9 pdevice);
//********************************************************************
//function: TestPrint
//purpose: Tests boundaries of a line of text, preparing to be written.
//returns: An array of RECTs holding the calculated size of each character
// in the text and the whole length of the text.
//********************************************************************
void TestPrintln(RECT* Rectarr, LPFFORMAT pFormat, char* str);
//*******************************************************************************************
//function: CalcConcat
//purpose: will return the least amount of letters that must be erased for a string in order
// for that string to be concatenated with a new string and still fit in the pixel
// limitations supplied.
//*******************************************************************************************
int CalcConcat(RECT* RectArr, char* bufout, char* bufin, int maxpixwidth, LPFFORMAT pFormat);
//*********************************************************************
//funtion: Println
//purpose: Will print a new line of text to screen
//returns: A reactangle holding the size of the text drawn
//*********************************************************************
void Println(RECT* Rectptr, D3DCOLOR fontColor, LPFFORMAT pFormat, char* str);
//*********************************************************************
//funtion: Print
//purpose: Will print text to the size of the RECT provided
//*********************************************************************
void Print(RECT* rbox, D3DCOLOR fontColor, LPFFORMAT pFormat, char* str);
//*******************************************************
// function: DrawBox
// purpose: Will draw a box based on dimensions specified
// NOTE: Not mine. Leeched code.
//*******************************************************
void DrawBox(int x, int y, int w, int h, D3DCOLOR col);
//********************************************************
// function: TextBox
// purpose: Will try to draw text in the given RECT
// *******************************************************
void TextBox(RECT* Rectptr, RECT* FormatRect, string text, LPFFORMAT pFormat, D3DCOLOR inside, D3DCOLOR border, D3DCOLOR textcol);
//Font Functions
void DeleteUnusedFonts();
void DeleteFontsExcept(LPFFORMAT pFormat_arr[]);
void DeleteFont(LPFFORMAT pFormat);
void StopCheckingFonts();
void StartCheckingFonts();
//Set Functions
void SetCursor(int newx, int newy); //Set X and Y coords for text printing
void ResetCursor();
void Setvspc(int vertical_spacing);
//Get Functions
int getx();
int gety();
int getvspc();
//Resets Font Pointers
void PreReset(); //OnLostDevice
void PostReset(); //OnResetDevice
//RECT HELPERS
static RECT* rectcpy(RECT* out, RECT* in);
static int rectw(RECT* in); //width
static int recth(RECT* in); //height
//TEXT HELPERS
// a bit misleading. Does not tick all the way through only to end of text. Does not loop.
static int TickerText(char* strin, char* strout, unsigned int constraint, int wait, int time);
};
#endif
Steal it. Leech it. Help me Improve it. I don't care. The benefit of sharing this code is greater then keeping it hidden.
