Ok
i will show you
class with all functions.
class FontField - reading keybord
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////FONT FIELD////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class FontField
{
private:
char TextField [64];
int PositionX, PositionY, CharNumber, MaxCharNumber, MaxCharNumberDefault;
bool empty, RunField;
public:
void SetTextField (char* _pString)
{
int i = 0;
if (dbLen(_pString) == 0)
{
for (i = 0; i < MaxCharNumber; i++) TextField [i] = *"";
dbClearEntryBuffer();
};//end if
for (i = 0; i < MaxCharNumberDefault; i++) if (i < dbLen(_pString)) TextField [i] = *(_pString + i); else TextField [i] = *"";
CharNumber = dbLen (&TextField [0]);
};//SetTextField
void SetPositionX (int x) {PositionX = x;};
void SetPositionY (int y) {PositionY = y;};
void SetMaxCharNumber(int c) {if (c<=64) MaxCharNumber = c; else MaxCharNumber = 64;};
void SetMaxCharNumberDefault (void) {MaxCharNumber = 64;};
void SetRunField (bool bR){RunField = bR;};
int GetMaxCharNumber(void) {return MaxCharNumber;};
int GetPositionX (void) {return PositionX;};
int GetPositionY (void) {return PositionY;};
int GetCharNumber (void) {return CharNumber;};
char* pGetTextField (void) {return &TextField[0];};
bool GetEmpty (void) {return empty;};
bool GetRunField (void) {return RunField;};
void Reset (void)
{
MaxCharNumber = 64;
MaxCharNumberDefault = 64;
for (int i = 0; i<MaxCharNumber; i++) TextField[i] = *"";
PositionX = 0;
PositionY = 0;
CharNumber = 0;
empty = true;
};
void AddChar (void)
{
char* _char;
_char = dbEntry ();
dbClearEntryBuffer();
if ((dbAsc (_char) == 13)) return;
if ((dbAsc (_char) == 9)) return;
if ((dbAsc (_char) == 27)) return;
if ((dbLen(_char)>0) && (dbAsc(_char) == 8))// if backspace pressed delete one char
{
CharNumber = dbLen(&TextField[0]) - 1;
TextField[CharNumber] = *"";
};//end if
if ((dbLen(_char)>0) && (CharNumber<MaxCharNumber - 1) && (dbAsc(_char) != 8)) // if not backspace pressed add the char
{
CharNumber = dbLen(&TextField[0]);
TextField[CharNumber] = *_char;
};//end if
if (dbLen(&TextField[0]) == 0) empty = true; else empty = false;
};//AddChar
};//FontField
class BitmapFont - converts char arry from "class FontField" into the sprite
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////BITMAP FONT///////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class BitmapFont
{
private:
char TextField [64];
int CurrentTextDist [64];
int PositionX, PositionY, CharNumber, MaxCharNumber, FontID, RenderTextID, CurrentLength, MaxLength, MemBlockImageID, RenderMemblockID, Priority;
int ASCii;
bool visible, fool, empty;
//text fit flags
bool FitRight;
bool FitLeft;
bool FitMiddle;
void AddCharToSprite (void)
{
int S_pixel_loc;
int S_blue;
int S_green;
int S_red;
int S_Alpha;
int B_pixel_loc;
int x, y, i;
int B_width;
int S_width;
int S_height;
int FontChar_X,FontChar_Y, CharDist = 0;
int AlphaColumn;
B_width = dbMemblockDWORD(MemBlockImageID, 0);
S_width = dbMemblockDWORD(RenderMemblockID, 0);
S_height = dbMemblockDWORD(RenderMemblockID, 4);
i = 0;
for (y = 1; y<17; y++)
{
for (x =1; x<17; x++)
{ i++;
if (i == ASCii-31)
{ FontChar_X = x;
FontChar_Y = y;
break;
};//end if
};//end for
};//end for
if (ASCii != 32)// if not space button pressed
{
for( x = 0; x < S_height; x++) // x position
{
for(y = 0; y < S_height; y++) // y position
{
if (y == 0)
{
AlphaColumn = S_height;
for (i=0; i<S_height; i++)
{ S_pixel_loc = (x + (FontChar_X-1)*S_height + (i + (FontChar_Y-1)*S_height) * B_width) * 4 + 12;
if (0 == dbMemblockByte(MemBlockImageID, S_pixel_loc + 3)) AlphaColumn--;
};//end for
};//end if
if (AlphaColumn > 0)
{
S_pixel_loc = (x + (FontChar_X-1)*S_height + (y + (FontChar_Y-1)*S_height) * B_width) * 4 + 12;
S_blue = dbMemblockByte(MemBlockImageID, S_pixel_loc); //reading blue value of current pixel
S_green = dbMemblockByte(MemBlockImageID, S_pixel_loc + 1); //reading green value of current pixel
S_red = dbMemblockByte(MemBlockImageID, S_pixel_loc + 2); //reading red value of current pixel
S_Alpha = dbMemblockByte(MemBlockImageID, S_pixel_loc + 3); //reading alpha value
//B_pixel_loc = (x + (CharNumber-1)* S_height + y * S_width) * 4 + 12; //reading pixel location in final image
B_pixel_loc = (x + CurrentLength + y * S_width) * 4 + 12; //reading pixel location in final image
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc, S_blue);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+1, S_green);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+2, S_red);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+3, S_Alpha);
};//end if
};//end for y
if (AlphaColumn > 0) CharDist +=1;
};//end for x
};//end if
if (ASCii == 32) // if "space" button presesd just add 5 pixels
{
for( x = 0; x < 6 ; x++) // x position
{
for(y = 0; y < S_height; y++) // y position
{
B_pixel_loc = (x + CurrentLength + y * S_width) * 4 + 12; //reading pixel location in final image
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+1, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+2, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+3, 0);
}; // end for
};//end for
CharDist = x;
}// end if
CurrentTextDist [CharNumber-1] = CharDist;
CurrentLength += CharDist;
if (1 == dbSpriteExist(RenderTextID)) dbDeleteSprite (RenderTextID);
if (0 == dbSpriteExist(RenderTextID)) dbMakeImageFromMemblock(RenderTextID, RenderMemblockID);
};//CreateSpriteFromFont
void DeleteCharFromSprite (void)
{
int x, y, S_width, S_height, B_pixel_loc;
CurrentLength -= CurrentTextDist [CharNumber];
S_width = dbMemblockDWORD(RenderMemblockID, 0);
S_height = dbMemblockDWORD(RenderMemblockID, 4);
for( x = 0; x < CurrentTextDist [CharNumber]+2 ; x++)
{
for(y = 0; y < S_height; y++)
{
B_pixel_loc = (x + CurrentLength + y * S_width) * 4 + 12;
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+1, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+2, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+3, 0);
}; // end for
};//end for
CurrentTextDist [CharNumber] = 0;
if (1 == dbSpriteExist(RenderTextID)) dbDeleteSprite (RenderTextID);
if (0 == dbSpriteExist(RenderTextID)) dbMakeImageFromMemblock(RenderTextID, RenderMemblockID);
if (CharNumber = 0) empty = true; else empty = false;
};//DeleteCharFromSprite
void ClearFontSprite (void)
{
int x, y, S_width, S_height, B_pixel_loc;
S_width = dbMemblockDWORD(RenderMemblockID, 0);
S_height = dbMemblockDWORD(RenderMemblockID, 4);
for( x = 0; x < S_width ; x++)
{
for(y = 0; y < S_height; y++)
{
B_pixel_loc = (x + y * S_width) * 4 + 12;
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+1, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+2, 0);
dbWriteMemblockByte(RenderMemblockID, B_pixel_loc+3, 0);
}; // end for
};//end for
if (1 == dbSpriteExist(RenderTextID)) dbDeleteSprite (RenderTextID);
if (0 == dbSpriteExist(RenderTextID)) dbMakeImageFromMemblock(RenderTextID, RenderMemblockID);
empty = true;
};//ClearFontSprite
void RenderFont (void)
{
if (visible)
{
dbSetSpritePriority (RenderTextID, Priority);
dbShowSprite (RenderTextID);
if (FitRight)
{
dbSprite(RenderTextID, PositionX, PositionY, RenderTextID);
};
if (FitLeft)
{
dbSprite(RenderTextID, PositionX + MaxLength - CurrentLength, PositionY, RenderTextID);
};
if (FitMiddle)
{
dbSprite(RenderTextID, PositionX + MaxLength/2 - CurrentLength/2, PositionY, RenderTextID);
};
};//end if
};//RenderFont
public:
bool IsFool (void) {return fool;}
void SetFontID (int c ) { FontID = c;};
void SetPositionX (int x) {PositionX = x;};
void SetPositionY (int y) {PositionY = y;};
void SetMaxLength (int c) {if (c<=270) MaxLength = c; else MaxLength = 270;};
void SetMaxCharNumber (int c) {if (c<=64) MaxCharNumber = c; else MaxCharNumber = 64;};
void SetMemBlockImageID (int c) {MemBlockImageID = c;};
void SetRenderMemblockID(int c) {RenderMemblockID = c;};
void SetVisibility (bool _v) {visible = _v;};
void SetRightFit (void) {FitRight = true; FitLeft = false; FitMiddle = false;};
void SetLeftFit (void) {FitRight = false; FitLeft = true; FitMiddle = false;};
void SetMiddleFit (void) {FitRight = false; FitLeft = false; FitMiddle = true;};
void SetRenderPriority (int a) {Priority = a;};
int GetMaxCharNumber (void) {return MaxCharNumber;};
int GetPositionX (void) {return PositionX;};
int GetPositionY (void) {return PositionY;};
int GetFontID (void) {return FontID;};
int GetCharNumber (void) {return CharNumber;};
int GetMaxLength (void) {return MaxLength;};
int GetCurrentLength (void) {return CurrentLength;};
int GetMemBlockImageID (void) {return MemBlockImageID;};
int GetRenderMemblockID(void) {return RenderMemblockID;};
bool GetVisibility (void) {return visible;};
char* pGetTextField (void) {return &TextField[0];};
bool GetRightFit (void) {return FitRight;};
bool GetLeftFit (void) {return FitLeft;};
bool GetMiddleFit (void) {return FitMiddle;};
int GetRenderPriority (void) {return Priority;};
bool GetEmpty (void) {return empty;};
void Reset (void)
{
MaxCharNumber = 64;
for (int i = 0; i<MaxCharNumber; i++)
{
TextField[i] = *"";
CurrentTextDist [i] = 0;
};//end for
empty = true;
PositionX = 0;
PositionY = 0;
CharNumber = 0;
CurrentLength = 0;
};//Reset
void LoadBitMapFontFromFile (char* _cFilePath, int _FontID)
{
FontID = _FontID;
RenderTextID = _FontID + 1;
MemBlockImageID = FontID;
RenderMemblockID = RenderTextID;
dbLoadImage (_cFilePath, FontID);
dbMakeMemblockFromImage (MemBlockImageID, FontID);
dbMakeMemblock (RenderMemblockID, (32*270)*4+12);
dbWriteMemblockDword (RenderMemblockID, 0, 270);
dbWriteMemblockDword (RenderMemblockID, 4, 32);
dbWriteMemblockDword (RenderMemblockID, 8, 32);
MaxLength = dbMemblockDWORD(RenderMemblockID, 0);
dbDeleteImage (FontID);
};//LoadBitMapFontFromFile
void SetTextToRender(char* _pString)
{
int L1, L2;
L1 = dbLen(_pString);
L2 = dbLen (&TextField [0]);
if ((L1 > L2) && (CurrentLength < MaxLength-32))
{
for (int i = 0; i < MaxCharNumber; i++) if (i < dbLen(_pString)) TextField [i] = *(_pString + i); else TextField [i] = *"";
CharNumber = dbLen (&TextField [0]);
ASCii = dbAsc (&TextField[CharNumber-1]);
AddCharToSprite ();
};// end if
if ((L1 < L2))
{
for (int i = 0; i < MaxCharNumber; i++) if (i < dbLen(_pString)) TextField [i] = *(_pString + i); else TextField [i] = *"";
CharNumber = dbLen (&TextField [0]);
DeleteCharFromSprite ();
};//end if
if (CurrentLength >= MaxLength-32) fool = true;
if (CurrentLength < MaxLength-32) fool = false;
if (CharNumber = 0) empty = true; else empty = false;
RenderFont ();
};//SetTextToRender
void CreateSpriteFromText (char* _pString)
{
int i = 0;
if (dbLen (_pString) >0 )
{
CurrentLength = 0;
ClearFontSprite ();
for (i = 0; i <dbLen(_pString)+1; i++)
{
if (CurrentLength < MaxLength-32)
{
TextField [i] = *(_pString + i);
ASCii = dbAsc (&TextField[i-1]);
AddCharToSprite ();
};// end if
};//end for
CharNumber = i;
};//end if
if (dbLen (_pString) == 0 )
{
for (i = 0; i < MaxCharNumber; i++)
{
TextField [i] = *"";
CurrentTextDist [i] = 0;
};//end for
ClearFontSprite ();
CurrentLength = 0;
CharNumber = 0;
};//end if
if (CharNumber = 0) empty = true; else empty = false;
RenderFont ();
};//CreateSpriteFromText
};//BitmapFont
The main function And
use 512*512 png file for the font
#include "DarkGDK.h"
#include "BitMapFont.h"
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 0 );
dbSetTextFont ("Times New Roman");
dbSetTextSize (20);
BitmapFont TestFont;
TestFont.LoadBitMapFontFromFile ("FONT_DEFAULT.png", 200);
TestFont.Reset();
TestFont.SetPositionX(200);
TestFont.SetPositionY(250);
FontField TestField;
TestField.Reset();
TestField.SetPositionX(200);
TestField.SetPositionY(300);
TestFont.SetVisibility(true);
TestFont.SetMiddleFit ();
TestField.SetTextField (TestFont.pGetTextField());
while ( LoopGDK ( ) )
{
dbCLS ();
if (TestFont.IsFool() == true) TestField.SetMaxCharNumber(TestFont.GetCharNumber());
if (TestFont.IsFool() == false) TestField.SetMaxCharNumberDefault ();
TestField.AddChar();
TestFont.SetTextToRender(TestField.pGetTextField());
dbText ( TestField.GetPositionX(), TestField.GetPositionY()+20, TestField.pGetTextField());
dbText ( TestFont.GetPositionX(), TestFont.GetPositionY()+ 40, TestFont.pGetTextField());
if ( dbEscapeKey())break;
dbSync ( );
};
return;
}
You are wellcome
Dark GDK IS THE BEST!!!!