From 2dbb1380df8c673732b155ac9a45b05ca8ae4423 Mon Sep 17 00:00:00 2001 From: Crayon2000 Date: Mon, 11 Jan 2010 21:48:01 +0000 Subject: [PATCH] [CHG] More protection to TTF functions [NEW] Added FPS in the TTF example --- GRRLIB/GRRLIB/GRRLIB_ttf.c | 36 ++++++++++++++++++++++-------------- examples/ttf/source/main.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/GRRLIB/GRRLIB/GRRLIB_ttf.c b/GRRLIB/GRRLIB/GRRLIB_ttf.c index 51db271..25317c0 100644 --- a/GRRLIB/GRRLIB/GRRLIB_ttf.c +++ b/GRRLIB/GRRLIB/GRRLIB_ttf.c @@ -87,14 +87,19 @@ void GRRLIB_FreeTTF (GRRLIB_ttfFont *myFont) { * @param color Text color in RGB format. */ void GRRLIB_PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize, const u32 color) { + if(myFont == NULL || string == NULL) + return; + size_t length = strlen(string) + 1; wchar_t *utf32 = (wchar_t*)malloc(length * sizeof(wchar_t)); - utf32[length] = '\0'; - mbstowcs(utf32, string, length); - - GRRLIB_PrintfTTFW(x, y, myFont, utf32, fontSize, color); - - free(utf32); + if(utf32) { + length = mbstowcs(utf32, string, length); + if(length > 0) { + utf32[length] = L'\0'; + GRRLIB_PrintfTTFW(x, y, myFont, utf32, fontSize, color); + } + free(utf32); + } } /** @@ -108,6 +113,9 @@ void GRRLIB_PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string, * @param color Text color in RGB format. */ void GRRLIB_PrintfTTFW(int x, int y, GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize, const u32 color) { + if(myFont == NULL || utf32 == NULL) + return; + unsigned int loop; int penX = 0; int penY = fontSize; @@ -137,7 +145,7 @@ void GRRLIB_PrintfTTFW(int x, int y, GRRLIB_ttfFont *myFont, const wchar_t *utf3 continue; } - DrawBitmap(&slot->bitmap, + DrawBitmap(&slot->bitmap, penX + slot->bitmap_left + x, penY - slot->bitmap_top + y, color); @@ -164,7 +172,7 @@ static void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u32 color) RGBA((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, - bitmap->buffer[ q * bitmap->width + p ]) ); + bitmap->buffer[ q * bitmap->width + p ]) ); } } } @@ -177,14 +185,14 @@ static void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u32 color) * @return The width of a text in pixel. */ unsigned int GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize) { - if(string == NULL) { + if(myFont == NULL || string == NULL) { return 0; } unsigned int penX; size_t length = strlen(string) + 1; wchar_t *utf32 = (wchar_t*)malloc(length * sizeof(wchar_t)); - utf32[length] = '\0'; length = mbstowcs(utf32, string, length); + utf32[length] = L'\0'; penX = GRRLIB_WidthTTFW(myFont, utf32, fontSize); @@ -201,16 +209,16 @@ unsigned int GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *string, unsigne * @return The width of a text in pixel. */ unsigned int GRRLIB_WidthTTFW(GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize) { + if(myFont == NULL || utf32 == NULL) { + return 0; + } + unsigned int loop; unsigned int penX = 0; FT_UInt glyphIndex; FT_UInt previousGlyph = 0; size_t length; - if(utf32 == NULL) { - return 0; - } - if(FT_Set_Pixel_Sizes(myFont->face, 0, fontSize)) { FT_Set_Pixel_Sizes(myFont->face, 0, 12); } diff --git a/examples/ttf/source/main.c b/examples/ttf/source/main.c index 3c9c65f..1b85471 100644 --- a/examples/ttf/source/main.c +++ b/examples/ttf/source/main.c @@ -5,11 +5,18 @@ #include #include +#include // Needed for gettime and ticks_to_millisecs // Font #include "FreeMonoBold_ttf.h" +// Prototype +static u8 CalculateFrameRate(); + int main(int argc, char **argv) { + char FPS[255] = ""; + bool ShowFPS = false; + // Initialise the Graphics & Video subsystem GRRLIB_Init(); @@ -55,6 +62,11 @@ int main(int argc, char **argv) { rand() % 0xFFFFFF); GRRLIB_Screen2Texture(0, 0, CopiedImg, false); + if(ShowFPS) { + sprintf(FPS, "Current FPS: %d", CalculateFrameRate()); + GRRLIB_PrintfTTF(500+1, 25+1, myFont, FPS, 12, 0x000000); + GRRLIB_PrintfTTF(500, 25, myFont, FPS, 12, 0xFFFFFF); + } GRRLIB_Render(); // Render the frame buffer to the TV WPAD_ScanPads(); // Scan the Wii Remotes @@ -65,6 +77,9 @@ int main(int argc, char **argv) { if (WPAD_ButtonsDown(0) & WPAD_BUTTON_A) { GRRLIB_Screen2Texture(0, 0, CopiedImg, false); } + if (WPAD_ButtonsDown(0) & WPAD_BUTTON_1) { + ShowFPS = !ShowFPS; + } if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_1 && WPAD_ButtonsHeld(0) & WPAD_BUTTON_2) { WPAD_Rumble(0, true); // Rumble on GRRLIB_ScrShot("sd:/grrlib_ttf.png"); // Needs to be after GRRLIB_Render() @@ -78,3 +93,22 @@ int main(int argc, char **argv) { exit(0); // Use exit() to exit a program, do not use 'return' from main() } + +/** + * This function calculates the number of frames we render each second. + * @return The number of frames per second. + */ +static u8 CalculateFrameRate() { + static u8 frameCount = 0; + static u32 lastTime; + static u8 FPS = 0; + u32 currentTime = ticks_to_millisecs(gettime()); + + frameCount++; + if(currentTime - lastTime > 1000) { + lastTime = currentTime; + FPS = frameCount; + frameCount = 0; + } + return FPS; +}