[CHG] More protection to TTF functions

[NEW] Added FPS in the TTF example
This commit is contained in:
Crayon2000 2010-01-11 21:48:01 +00:00
parent ad300e55d8
commit 2dbb1380df
2 changed files with 56 additions and 14 deletions

View file

@ -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;
@ -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);
}

View file

@ -5,11 +5,18 @@
#include <stdlib.h>
#include <wiiuse/wpad.h>
#include <ogc/lwp_watchdog.h> // 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;
}