Graphics: Added font rendering functions Graphics::DrawText and Graphics::StringImage.

FileHandler: Added support for wav
Audio: Added support to play back a sound with XAudio2. We can't delete sounds until we make our own system to keep track of available voices.
LoginScreen scene: Upped framerate from 15fps to tickless, and added new logic to clone the scrolling text at the bottom. All of it.
This commit is contained in:
Fatbag 2012-04-20 19:37:08 -05:00
parent 64a5c0a425
commit 06f13d50ac
14 changed files with 429 additions and 162 deletions

View file

@ -22,7 +22,7 @@ namespace Graphics {
FT_Library FreeTypeLibrary;
FT_Face FontFace;
static void FindStringSize(const wchar_t * String, unsigned * width, unsigned * height, int font){
static void FindStringSize(const wchar_t * String, unsigned * width, unsigned * height, int * xoffset, int * yoffset, int font){
int x = 0, y = 0;
int lowestx = 0, lowesty = 0, highestx = 0, highesty = 0;
@ -30,19 +30,22 @@ static void FindStringSize(const wchar_t * String, unsigned * width, unsigned *
int error = FT_Load_Char(FontFace, letter, FT_LOAD_RENDER);
if(error) continue;
if(x < lowestx) lowestx = x;
if(y < lowesty) lowesty = y;
int bottomx = x + FontFace->glyph->bitmap_left;
int bottomy = y + FontFace->glyph->bitmap_top - FontFace->glyph->bitmap.rows;
if(bottomx < lowestx) lowestx = bottomx;
if(bottomy < lowesty) lowesty = bottomy;
int newx = x + FontFace->glyph->bitmap.width + FontFace->glyph->bitmap_left;
int newy = y + FontFace->glyph->bitmap_top;
if(newx > highestx) highestx = newx;
if(newy > highesty) highesty = newy;
int topx = x + FontFace->glyph->bitmap_left + FontFace->glyph->bitmap.width;
int topy = y + FontFace->glyph->bitmap_top;
if(topx > highestx) highestx = topx;
if(topy > highesty) highesty = topy;
x += FontFace->glyph->advance.x >> 6;
y += FontFace->glyph->advance.y >> 6;
}
*width = highestx-lowestx, *height = highesty-lowesty;
*xoffset = -lowestx, *yoffset = -lowesty;
}
void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned width, unsigned height,
@ -52,13 +55,11 @@ void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned wi
//The destination image must be stored in bottom-up order.
if(x >= (signed)Image->Width || y >= (signed)Image->Height) return;
int Blue = GetBValue(Color), Green = GetGValue(Color), Red = GetRValue(Color);
//(stringx,stringy) will refer to the top-left of the string in bottom-up coordinates
int stringx, stringy;
unsigned StringWidth, StringHeight;
FindStringSize(String, &StringWidth, &StringHeight, font);
FindStringSize(String, &StringWidth, &StringHeight, &stringx, &stringy, font);
//Horizontal alignment
if(Alignment < 2) stringx = x; //Left
@ -98,11 +99,11 @@ void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned wi
int originalcolor;
originalcolor = *ptr;
*ptr++ = (uint8_t) (originalcolor + (int)((Blue-originalcolor)*2*value+255)/510);
*ptr++ = (uint8_t) (originalcolor + (int)((GetBValue(Color)-originalcolor)*2*value+255)/510);
originalcolor = *ptr;
*ptr++ = (uint8_t) (originalcolor + (int)((Green-originalcolor)*2*value+255)/510);
*ptr++ = (uint8_t) (originalcolor + (int)((GetGValue(Color)-originalcolor)*2*value+255)/510);
originalcolor = *ptr;
*ptr++ = (uint8_t) (originalcolor + (int)((Red-originalcolor)*2*value+255)/510);
*ptr++ = (uint8_t) (originalcolor + (int)((GetRValue(Color)-originalcolor)*2*value+255)/510);
}
}
stringx -= FontFace->glyph->bitmap_left;
@ -115,4 +116,61 @@ void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned wi
}
}
Image_t * StringImage(const wchar_t * String, int font, COLORREF Color){
Image_t * Image = (Image_t*) malloc(sizeof(Image_t));
if(Image == NULL) return NULL;
unsigned StringWidth, StringHeight;
int stringx, stringy;
FindStringSize(String, &StringWidth, &StringHeight, &stringx, &stringy, font);
Image->Data = (uint8_t*) malloc(4 * StringWidth * StringHeight);
if(Image->Data == NULL){
free(Image);
return NULL;
}
for(unsigned i=0; i<4*StringWidth*StringHeight;){
Image->Data[i++] = GetBValue(Color);
Image->Data[i++] = GetGValue(Color);
Image->Data[i++] = GetRValue(Color);
Image->Data[i++] = 0;
}
for(wchar_t letter=*String; letter!='\0'; letter=*(++String)){
int error = FT_Load_Char(FontFace, letter, FT_LOAD_RENDER);
if(error) continue;
int cWidth = FontFace->glyph->bitmap.width, cHeight = FontFace->glyph->bitmap.rows;
if(cWidth && cHeight){
uint8_t * cRender; /* Convert to Bottom-up */
uint8_t * OriginalRender = FontFace->glyph->bitmap.buffer;
if(FontFace->glyph->bitmap.pitch > 0){
cRender = (uint8_t *) malloc(cWidth * cHeight);
for(int i=0; i<cHeight; i++)
memcpy(cRender + i*cWidth, OriginalRender + (cHeight-i-1)*cWidth, cWidth);
}else cRender = OriginalRender;
stringx += FontFace->glyph->bitmap_left;
stringy += FontFace->glyph->bitmap_top-cHeight;
for(int i=0; i<cHeight; i++){
for(int j=0; j<cWidth; j++){
uint8_t *ptr = Image->Data + 4*((stringy+i)*StringWidth + (stringx+j));
ptr[3] = cRender[i*cWidth + j];
}
}
stringx -= FontFace->glyph->bitmap_left;
stringy -= FontFace->glyph->bitmap_top-cHeight;
if(FontFace->glyph->bitmap.pitch > 0) free(cRender);
}
stringx += FontFace->glyph->advance.x >> 6;
stringy += FontFace->glyph->advance.y >> 6;
}
Image->Width = StringWidth;
Image->Height = StringHeight;
Image->Format = FIMG_BGRA32;
return Image;
}
}

View file

@ -42,4 +42,5 @@ namespace Graphics {
extern FT_Face FontFace;
void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned width, unsigned height,
TextAlignment Alignment, int font, COLORREF Color);
Image_t * StringImage(const wchar_t * String, int font, COLORREF Color);
}

View file

@ -96,8 +96,9 @@ int InitGL(){
glEnable(GL_CULL_FACE);
glEnable(GL_RESCALE_NORMAL);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return 0;
}