font.c 12 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

/*---------------------------------------------------------------------*
        Copyright (C) 1997 Nintendo. (Originated by SGI)
        
        $RCSfile: font.c,v $
        $Revision: 1.1.1.1 $
        $Date: 2002/05/02 03:27:30 $
 *---------------------------------------------------------------------*/
/*
	Example of FONTS
*/

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

#include "hdd.h"

void jtext_sprite( Sprite *txt, u16 mes );
void transkan(int code, u8 *ptr);
s32  _first = 1;

OSPiHandle	*handler;

#include "../Texture/letters_img.h"

static char letters_string[] = "abcdefhiuvsnorm.klxz4321567890`!gjpqytw,EFGHKLNOPRSUVXZ@JM:WIYABCD'T\"+-=Q_/?()[]";

static Bitmap letters_bms[] = {
    { 8, 64, 0*8, 0*12, letters_img,   12,0 },
    { 8, 64, 1*8, 0*12, letters_img,   12,0 },
    { 8, 64, 2*8, 0*12, letters_img,   12,0 },
    { 8, 64, 3*8, 0*12, letters_img,   12,0 },
    { 8, 64, 4*8, 0*12, letters_img,   12,0 },
    { 8, 64, 5*8, 0*12, letters_img,   12,0 },
    { 8, 64, 6*8, 0*12, letters_img,   12,0 },
    { 7, 64, 7*8, 0*12, letters_img,   12,0 },

    { 8, 64, 0*8, 1*12, letters_img,   12,0 },
    { 8, 64, 1*8, 1*12, letters_img,   12,0 },
    { 8, 64, 2*8, 1*12, letters_img,   12,0 },
    { 8, 64, 3*8, 1*12, letters_img,   12,0 },
    { 8, 64, 4*8, 1*12, letters_img,   12,0 },
    { 8, 64, 5*8, 1*12, letters_img,   12,0 },
    { 9, 64, 6*8, 1*12, letters_img,   12,0 },
    { 6, 64, 7*8+1, 1*12, letters_img,   12,0 },

    { 8, 64, 0*8, 2*12, letters_img,   12,0 },
    { 8, 64, 1*8, 2*12, letters_img,   12,0 },
    { 8, 64, 2*8, 2*12, letters_img,   12,0 },
    { 8, 64, 3*8, 2*12, letters_img,   12,0 },
    { 8, 64, 4*8, 2*12, letters_img,   12,0 },
    { 8, 64, 5*8, 2*12, letters_img,   12,0 },
    { 8, 64, 6*8, 2*12, letters_img,   12,0 },
    { 8, 64, 7*8, 2*12, letters_img,   12,0 },

    { 8, 64, 0*8, 3*12, letters_img,   12,0 },
    { 8, 64, 1*8, 3*12, letters_img,   12,0 },
    { 8, 64, 2*8, 3*12, letters_img,   12,0 },
    { 8, 64, 3*8, 3*12, letters_img,   12,0 },
    { 8, 64, 4*8, 3*12, letters_img,   12,0 },
    { 8, 64, 5*8, 3*12, letters_img,   12,0 },
    { 7, 64, 6*8, 3*12, letters_img,   12,0 },
    { 7, 64, 7*8, 3*12, letters_img,   12,0 },

    { 8, 64, 0*8, 4*12, letters_img,   16,0 },
    { 8, 64, 1*8, 4*12, letters_img,   16,0 },
    { 8, 64, 2*8, 4*12, letters_img,   16,0 },
    { 8, 64, 3*8, 4*12, letters_img,   16,0 },
    { 8, 64, 4*8, 4*12, letters_img,   16,0 },
    { 8, 64, 5*8, 4*12, letters_img,   16,0 },
    { 9, 64, 6*8, 4*12, letters_img,   16,0 },
    { 6, 64, 7*8+1, 4*12, letters_img,   16,0 },

    { 8, 64, 0*8, 64+0*12, letters_img,   12,0 },
    { 8, 64, 1*8, 64+0*12, letters_img,   12,0 },
    { 8, 64, 2*8, 64+0*12, letters_img,   12,0 },
    { 8, 64, 3*8, 64+0*12, letters_img,   12,0 },
    { 8, 64, 4*8, 64+0*12, letters_img,   12,0 },
    { 8, 64, 5*8, 64+0*12, letters_img,   12,0 },
    { 8, 64, 6*8, 64+0*12, letters_img,   12,0 },
    { 8, 64, 7*8, 64+0*12, letters_img,   12,0 },

    { 8, 64, 0*8, 64+1*12, letters_img,   12,0 },
    { 8, 64, 1*8, 64+1*12, letters_img,   12,0 },
    { 8, 64, 2*8, 64+1*12, letters_img,   12,0 },
    { 8, 64, 3*8, 64+1*12, letters_img,   12,0 },
    { 8, 64, 4*8, 64+1*12, letters_img,   12,0 },
    { 8, 64, 5*8, 64+1*12, letters_img,   12,0 },
    { 8, 64, 6*8, 64+1*12, letters_img,   12,0 },
    { 8, 64, 7*8, 64+1*12, letters_img,   12,0 },

    { 7, 64, 0*8, 64+2*12, letters_img,   12,0 },
    { 9, 64, 1*8-1, 64+2*12, letters_img,   12,0 },
    { 6, 64, 2*8, 64+2*12, letters_img,   12,0 },
    { 9, 64, 3*8-1, 64+2*12, letters_img,   12,0 },
    { 7, 64, 4*8, 64+2*12, letters_img,   12,0 },
    { 9, 64, 5*8-1, 64+2*12, letters_img,   12,0 },
    { 8, 64, 6*8, 64+2*12, letters_img,   12,0 },
    { 8, 64, 7*8, 64+2*12, letters_img,   12,0 },

    { 8, 64, 0*8, 64+3*12, letters_img,   12,0 },
    { 8, 64, 1*8, 64+3*12, letters_img,   12,0 },
    { 7, 64, 2*8, 64+3*12, letters_img,   12,0 },
    { 9, 64, 3*8-1, 64+3*12, letters_img,   12,0 },
    { 7, 64, 4*8, 64+3*12, letters_img,   12,0 },
    { 9, 64, 5*8-1, 64+3*12, letters_img,   12,0 },
    { 8, 64, 6*8, 64+3*12, letters_img,   12,0 },
    { 8, 64, 7*8, 64+3*12, letters_img,   12,0 },

    { 8, 64, 0*8, 64+4*12+1, letters_img,   16,0 },
    { 8, 64, 1*8, 64+4*12+1, letters_img,   16,0 },
    { 8, 64, 2*8, 64+4*12+1, letters_img,   16,0 },
    { 8, 64, 3*8, 64+4*12+1, letters_img,   16,0 },
    { 8, 64, 4*8, 64+4*12+1, letters_img,   16,0 },
    { 8, 64, 5*8, 64+4*12+1, letters_img,   16,0 },
    { 8, 64, 6*8, 64+4*12+1, letters_img,   16,0 },
    { 8, 64, 7*8, 64+4*12+1, letters_img,   16,0 },

};



Font letters_font = { letters_string, letters_bms, letters_img };

char *my_strchr( char *, char );

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

    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' ) {
		bm[i]      = fnt->bitmaps[0];
		bm[i].buf = NULL;
		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;
	    };
	};

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

#define DYN_SPRITE_HACK

#define NUM_template_BMS        (24*40)

static Bitmap template_bm[NUM_template_BMS];

static Gfx      template_dl[NUM_DL(NUM_template_BMS)];

Sprite template_sprite = {

        0,0,                    /* Position: x,y */

        0,0,                    /* Sprite size in texels (x,y) */

        1.0,1.0,                /* Sprite Scale: x,y */

        0,0,                    /* Explosion (x,y) */

        SP_TRANSPARENT,                   /* Sprite Attributes */
        0x1234,                 /* Sprite Depth: Z */

        255,255,255,255,        /* Sprite Coloration: RGBA */

        0,0,NULL,               /* Color LookUp Table: start_index, length, address */

        0,1,                    /* Sprite Bitmap index: start index, step increment */

        NUM_template_BMS,                       /* Number of bitmaps */
        NUM_DL(NUM_template_BMS),               /* Number of display list locations allocated */

        15, 128,                 /* Sprite Bitmap Height: Used_height, physical height */
        G_IM_FMT_I,             /* Sprite Bitmap Format */
        G_IM_SIZ_4b,            /* Sprite Bitmap Texel Size */

        template_bm,            /* Pointer to bitmaps */

        template_dl,            /* Display list memory */

        NULL,                  /* HACK: dynamic_dl pointer */

};


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;

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

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

    gxp = *glistp;

    spInit( &gxp );

    template_sprite.rsp_dl_next = template_sprite.rsp_dl;

    *glistp = gxp;

    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() */

}



/* 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 )
{
    if( flag )
	spSetAttribute( &template_sprite, SP_TRANSPARENT | SP_CUTOUT );
    else
	spClearAttribute( &template_sprite, 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 = &template_sprite;

  sp->width =  font_win_width*8  + 8;
  sp->height = font_win_height*16 + 8;
  sp->bmheight = 15;		/* add by mitu  */
  sp->bmHreal = 128;

  text_sprite( sp, val_str, &letters_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;
}


void
jtext_sprite( Sprite *txt, u16 mes )
{
  int		i;
  int		addr = kaddr[mes];

  for( i = 0 ; message[mes][i*2] ; i ++, addr += 128 ) {
    template_bm[i].width = 16;
    template_bm[i].width_img = 16;
    template_bm[i].s = 0;
    template_bm[i].t = 0;
    template_bm[i].buf = &kbuffer[addr];
    template_bm[i].actualHeight = 16;
    template_bm[i].LUToffset = 0;
  }
  template_bm[i].width = -1;
  txt->nbitmaps = i;

  txt->width =  i*16;
  txt->height = 16;
}

void
jfont_show_string( Gfx **glistp, u16 mes )
{
  Sprite	*sp;
  Gfx		*gxp, *dl;

  if( _first )
	{
	  if( !dd_driveExist() ) return; 
	  setupkanji();
	  _first = 0;
	}

  gxp = *glistp;

  sp = &template_sprite;

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

  sp->bmheight = 16;
  sp->bmHreal = 16;

  jtext_sprite( sp, mes );

#if 0				/* with Shade */
  spMove( sp, font_xpos+1, font_ypos+1 );
  spColor( sp, 0, 0, 0, font_alf );
  spScale( sp, font_xscale, font_yscale );

  dl = spDraw( sp );
  gSPDisplayList( gxp++, dl );
#endif

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

#ifdef  EUC_CODE
int e2s(int code);
#endif

void
transkan(int code, u8 *ptr)
{
  int	i;

#ifdef EUC_CODE
  code = e2s(code);
#endif

  i = LeoGetKAdr(code);
  try_assert("get font error",(i>=0));

  dmaIOMessageBuf.hdr.pri 	= OS_MESG_PRI_NORMAL;
  dmaIOMessageBuf.hdr.retQueue 	= &dmaMessageQ;
  dmaIOMessageBuf.dramAddr 	= ptr;
  dmaIOMessageBuf.devAddr 	= DDROM_FONT_START + i;
  dmaIOMessageBuf.size		= 128;
	
  handler->transferInfo.cmdType	= OS_OTHERS;
  osEPiStartDma(handler, &dmaIOMessageBuf, OS_READ);
  osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
}

#ifdef EUC_CODE
int 
e2s(int code)
{
  int c1,c2;
  
  c1 = (code >> 8) & 0xff;
  c2 = (code >> 0) & 0xff;

  if (c1 % 2)
	{
	  c1 = ( c1>>1 ) + ( c1 < 0xdf ? 0x31 : 0x71);
	  c2 -= 0x60 + (c2 < 0xe0);
    }
  else
	{
	  c1 = (c1>>1) + (c1 < 0xdf ? 0x30 : 0x70);
	  c2 -= 2;
    }

  return (int)( ((c1<<8) & 0xff00) + ( c2 & 0xff ) );
}
#endif


/* translate kanji-font in a message to buffer */

s32
initkanchr(u8 *str, s32 addr) {
  s16	i = 0;

  while ( str[i] != NULL ) {
    transkan( (int)( str[i] * 256 + str[i+1] ), (u8 *)&kbuffer[addr]);
    addr += 128;
    i += 2;
  }
  return addr;
}

s32
setupkanji(void)
{
  s32 i,kptr=0;
  
  handler = osDriveRomInit();

  for ( i = 0 ; i < ERROR_MES_SIZE ; i ++ )
	{
	  kaddr[i] = kptr;
	  kptr = initkanchr(message[i], kptr);
	} 

  return -1;
}