User:PaceMaker

From WiiBrew
Jump to navigation Jump to search

Wii Stuff by me

Code Snippets

  • Here's a simple Screen Font class for LibWiiSprite.

BibScreenFont.h

/*
	BibScreenFont.h

	This class was designed around two ideas.
	1.  It uses LibWiiSprite for screen drawing details and assumes the 
		correct header file <wiisprite.h> is in place as well as the library.
	2.  It was designed to work with output of F2IBuilder ( http://sourceforge.net/projects/f2ibuilder/ ).


	F2IBuilder can output two files, a PNG and a Metrics file.
	This class expects the two files to be run through raw2c.exe (from DevKitPro)
	and included(.h) and compiled(.c) into the project.
	The png data should be loaded into a wsp Image class via LoadImage.
	The metrics data should be passed directly as the nCharWidths parameter to Initialize.
	
	
	Initialize() should be called first and only once.
	DisplayText() should be called to display text for the current frame.
*/


#ifndef _BIBSCREENFONT_H_
#define _BIBSCREENFONT_H_

class BibScreenFont
{
private:
	wsp::Sprite AlphabetBitmap;
	int nXLetterWidth;
	int nYLetterHeight;
	const unsigned char * nCharWidths;	// Array of [128]
	int nCharsPerLine;

public:
	void Initialize (wsp::Image * pImage, int nXLetterWidth, int nYLetterHeight, 
					const unsigned char * nCharWidths);
	void DisplayText (int nXScreenLoc, int nYScreenLoc, const char * szText, int nTransparency = 0xFF);
};
#endif

BibScreenFont.cpp

/*

	BibScreenFont.cpp

*/

#include <wiisprite.h>
#include "BibScreenFont.h"


void BibScreenFont::Initialize (wsp::Image * pImage, int innXLetterWidth, int innYLetterHeight, 
								const unsigned char * innCharWidths)
{
	nXLetterWidth = innXLetterWidth;
	nYLetterHeight = innYLetterHeight;
	nCharWidths = innCharWidths;
	AlphabetBitmap.SetImage (pImage, nXLetterWidth, nYLetterHeight);
}

/*
	Transparency has a range from 0x00 (invisible) to 0xFF (fully visible)
*/
void BibScreenFont::DisplayText (int nXScreenLoc, int nYScreenLoc, const char * szText, int nTransparency)
{
char cChar;
int i;

	// Loop for every character.
	for (i = 0; szText [i]; i++)
	{
		cChar = szText [i];
		if (cChar >= 128)
			cChar &= 0xEF;

		// Render each letter.
		AlphabetBitmap.SetPosition(nXScreenLoc, nYScreenLoc);
		AlphabetBitmap.SetFrame(cChar);
		AlphabetBitmap.SetTransparency (nTransparency);
		AlphabetBitmap.Draw();

		// Every time we blit a letter we need to move to the right 
		// for the start of the next blit
		nXScreenLoc += nCharWidths [cChar];
	}

}

Printf With LibWiiSprite

I found a need to support printf in my apps, while using LibWiiSprite. The code below shows an example on how to do this. One reason to do this is that asserts from Box2d (and presumably others) will do a printf. This method should also work with other graphic libraries that use GX instead of framebuffers. The majority of this code was taken from the libogc source.

It's easy to use. Call InitConsole() once, and RenderConsole() each draw loop. Also you'll need to provide a function that replaces my text drawing function: CourierNew_ScreenFont.DisplayText (int x, int y, char * szText); Also, it's not optimized at all, but who wants an optimized printf function?

// !!!! Should be put into a console class.
// !!!! Idea to add console support when using LibWiiSprite
#include <sys/iosupport.h>

#define MAX_CON_ROWS	24
#define MAX_CON_COLS	80
#define X_PIXELS_PER_CHAR	(640/MAX_CON_COLS)
#define Y_PIXELS_PER_CHAR	(480/MAX_CON_ROWS)
#define TAB_SIZE		4
 
static int cursor_row = 0;
static int cursor_col = 0;
static int con_rows = MAX_CON_ROWS;
static int con_cols = MAX_CON_COLS;
static char the_console [MAX_CON_ROWS] [MAX_CON_COLS];

int __console_write(struct _reent *r,int fd,const char *ptr,int len)
{
	int i = 0;
	char *tmp = (char*)ptr;
	char chr;

	if (!tmp || len<=0)
		return -1;

	i = 0;
	while(*tmp!='\0' && i<len)
	{
		chr = *tmp++;
		i++;
		if ( (chr == 0x1b) && (*tmp == '[') )
		{
			/* escape sequence found */
			int k;

			tmp++;
			i++;
			k = 0; //__console_parse_escsequence(tmp);		//!!!! Ignore.
			tmp += k;
			i += k;
		}
		else
		{
			switch(chr)
			{
				case '\n':
					cursor_row++;
					cursor_col = 0;
					break;
				case '\r':
					cursor_col = 0;
					break;
				case '\b':
					cursor_col--;
					if(cursor_col < 0)
					{
						cursor_col = 0;
					}
					break;
				case '\f':
					cursor_row++;
					break;
				case '\t':
					if(cursor_col%TAB_SIZE)
						cursor_col += (cursor_col%TAB_SIZE);
					else 
						cursor_col += TAB_SIZE;
					break;
				default:
					the_console [cursor_row] [cursor_col] = chr;
					cursor_col++;

					if (cursor_col >= con_cols)
					{
						/* if right border reached wrap around */
						cursor_row++;
						cursor_col = 0;
					}
			}
		}

		if( cursor_row >= con_rows)
		{
			// Copy data up after reaching bottom.
			for (int i = 0; i < MAX_CON_ROWS - 1; i ++)
			{
				for (int j = 0; j < MAX_CON_COLS; j ++)
				{
					the_console [i] [j] = the_console [i + 1] [j];
				}
			}

			for (int j = 0; j < MAX_CON_COLS; j ++)
			{
				the_console [MAX_CON_ROWS - 1] [j] = 0;
			}

			cursor_row--;
		}
	}

	return i;
}


const devoptab_t dotab_stdout =
{
//---------------------------------------------------------------------------------
	"stdout",	// device name
	0,			// size of file structure
	NULL,		// device open
	NULL,		// device close
	__console_write,	// device write
	NULL,		// device read
	NULL,		// device seek
	NULL,		// device fstat
	NULL,		// device stat
	NULL,		// device link
	NULL,		// device unlink
	NULL,		// device chdir
	NULL,		// device rename
	NULL,		// device mkdir
	0,			// dirStateSize
	NULL,		// device diropen_r
	NULL,		// device dirreset_r
	NULL,		// device dirnext_r
	NULL,		// device dirclose_r
	NULL		// device statvfs_r
};

void InitConsole (void)
{
    cursor_row = 0;
    cursor_col = 0;
    
    devoptab_list[STD_OUT] = &dotab_stdout;
    devoptab_list[STD_ERR] = &dotab_stdout;
}

void RenderConsole (void)
{
int i, j;
char twoBuf [2];

	twoBuf [1] = 0;
	for (i = 0; i < MAX_CON_ROWS; i ++)
	{
		for (j = 0; j < MAX_CON_COLS; j ++)
		{
			if (the_console [i] [j])
			{
				twoBuf [0] = the_console [i] [j];
				CourierNew_ScreenFont.DisplayText (j * X_PIXELS_PER_CHAR, i * Y_PIXELS_PER_CHAR, twoBuf);
			}
		}
	}

}