font.c 4.55 KB
/*
        Example of FONTS
*/

#include <PR/mbi.h>
#include <PR/sp.h>
#include "font.h"
#include "font_ext.h"

#include "letters.h"	/* sct 11/20/95 */
#include "pinfont.h"	/* sct 11/20/95 */
#include "digit6font.h"	/* sct 11/27/95 */


static Font * font_font = &pinfont_font;

static unsigned char font_red = 255;
static unsigned char font_grn = 255;
static unsigned char font_blu = 255;
static unsigned char font_alf = 255;

static int font_xpos = 0;
static int font_ypos = 0;

static int font_win_width = 40;
static int font_win_height = 1;

static double font_xscale = 1.0;
static double font_yscale = 1.0;

static void text_sprite( Sprite *, char *, Font *, int , int );


char *my_strchr( char *, char );

static void
text_sprite( Sprite *txt, char *str, Font *fnt, int xlen, int ylen )
{
    int i, ci;
    int x;
    int y;
    char *indx;
    Bitmap *bm;

    /* sct 11/27/95 - These seem like they are font dependent! Watch out! */
    txt->width = xlen * 8;
    txt->height = ylen * 16;

    bm = txt->bitmap;

    i = 0;
    ci = 0;
    for(y=0; y<ylen; y++)
    {
        for(x=0; x<xlen; x++, i++, ci++)
        {
            if( str[ci] == '\0' )
            {
                bm[i]       = fnt->bitmaps[0];
                bm[i].width = -1;
                txt->nbitmaps = i;
                return;
            }
            if( str[ci] == '\n' ) /* fill remainder of line with spaces */
            {
                bm[i]     = fnt->bitmaps[0];
                bm[i].buf = NULL; /* insert a space */
                ci--;
                continue;
            }
            if( (indx = my_strchr(fnt->index_string, str[ci])) != NULL )
                bm[i] = fnt->bitmaps[indx-fnt->index_string];
            else
            {
                bm[i]     = fnt->bitmaps[0];
                bm[i].buf = NULL; /* insert a space */
            }
        }

        if( str[ci] == '\n' )
            ci++;
    }

    txt->nbitmaps = i;
    return;
}

char *
my_strchr( char *str, char target )
{
    while( *str && (*str != target) )
        str++;
    
    if( *str )
        return( str );
    
    return( NULL );
}


/* Initialize Font local variables and prepare for drawing sprites */

void
font_init( Gfx **glistp )
{
    Gfx *gl;

    gl = *glistp;

    spInit( &gl ); 

    *glistp = gl; 

/*    template_sprite.rsp_dl_next = template_sprite.rsp_dl; */
    
    font_red = 255;
    font_grn = 255;
    font_blu = 255;
    font_alf = 255;

    font_xpos = 0;
    font_ypos = 0;

    font_win_width = 40;
    font_win_height = 1;

    font_xscale = 1.0;
    font_yscale = 1.0;

}


/* Call spFinish() to clean up when done using Sprites */

void
font_finish( Gfx **glistp )
{
    Gfx *gxp;

    gxp = *glistp;

    spFinish( &gxp );

    *glistp = (gxp-1);          /* Don't use final EndDisplayList() */
}


/*
  Allows the user to specify which font to use.
  sct 11/27/95 - created.
*/
void
font_set_font( int fontType )
{
    switch (fontType)
    {
      case kLettersFont:
	  font_font = &letters_font;
	  break;
      
      case kPinFont:
	  font_font = &pinfont_font;
	  break;

      case kDigit6Font:
	  font_font = &digit6_font;
	  break;
    }
}



/* Set text window area (units are characters) */

void
font_set_win( int width, int height )
{
    font_win_width = width;
    font_win_height = height;
}

/* Set text window position (upper left corner) */

void
font_set_pos( int xpos, int ypos )
{
    font_xpos = xpos;
    font_ypos = ypos;
}


/* Set text size */

void
font_set_scale( double xscale, double yscale )
{
    font_xscale = xscale;
    font_yscale = yscale;
}


/* Set text color */

void
font_set_color( unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha )
{
    font_red = red;
    font_grn = green;
    font_blu = blue;
    font_alf = alpha;
}

void
font_set_transparent( int flag, Sprite *sp )
{
    if( flag )
	spSetAttribute( sp, SP_TRANSPARENT | SP_CUTOUT );
    else
	spClearAttribute( sp, SP_TRANSPARENT | SP_CUTOUT );
}



/* Convert the string to a sprite with the propler bitmaps
   assembled from the basic font texture. */
void
font_show_string( Gfx **glistp, char *val_str, Sprite *sp )
{
    Gfx *gxp, *dl;

    gxp = *glistp;

    sp->width =  font_win_width*8  + 8;
    sp->height = font_win_height*16 + 8;

    /* sct 11/27/95 - Made font user settable. */
    text_sprite( sp, val_str, font_font, font_win_width, font_win_height );

    spMove( sp, font_xpos, font_ypos );
    spColor( sp, font_red, font_grn, font_blu, font_alf );
    spScale( sp, font_xscale, font_yscale );

    dl = spDraw( sp ); 

    gSPDisplayList( gxp++, dl );

    *glistp = gxp;
}