cpak.c 7.53 KB

#include "viewer.h"
#include "pagecommon.h"
#include "pagegame.h"
#include "texrect.h"
#include "uilogic.h"
#include "gzip.h"
#include "font.h"
#include "cpak.h"
#include "texture/player.c"
#include "audrenderer.h"

UiShadowIcon gCPakIcon[MAX_CONT_PAK];
UiCPakBind   gCPakBind;
UiShadowIcon gCPakBindIcon[4];

int gCPakInit = 0;
int gCPakBindIconInit = 0;

void onCPakBindIconSelect(int idx, u32 button);

#define BSIZE 64

static void
getCPakOrigin(int i, int *x, int *y)
{
    *x = 110 + BSIZE*(i%(MAX_CONT_PAK>>1));
    *y = 225 + BSIZE*(i/(MAX_CONT_PAK>>1));
}

void initCPak( int mode ) 
{
    int i;
    u32 aIdx[MAX_CONT_PAK];
  
    uiCPakRead();
    for( i = 0; i < MAX_CONT_PAK; i++ ) {
	aIdx[i] = gCPakList[i].cid;
    }
    initGameIconGroup(gCPakIcon, MAX_CONT_PAK, aIdx);

    // change shadow icon defaults
    for( i = 0; i <MAX_CONT_PAK; i++ ) {
        gCPakIcon[i].xscale = (3<<8);
	gCPakIcon[i].yscale = (3<<8);
	gCPakIcon[i].posx = 126 + BSIZE*(i%(MAX_CONT_PAK>>1));
	gCPakIcon[i].posy = 241 + BSIZE*(i/(MAX_CONT_PAK>>1));
    }

    // if mode, we need to allow for active empty icons
    if( mode ) {
        for( i = 0; i <MAX_CONT_PAK; i++ ) {
	    if( gCPakIcon[i].show == 0 ) {
	        gCPakIcon[i].show = 1;
		gCPakIcon[i].init = 1;
		gCPakIcon[i].texMap = NULL;
		gCPakIcon[i].shadowMap = NULL;
		gCPakIcon[i].text = NULL;
	    }
	}
    }
    gCPakInit = 1;
}

void drawBox( int ux, int uy, int lx, int ly, int w )
{
    // top line
    gDPFillRectangle( gpGfxList++, ux, uy, lx, uy+w );
  
    // left line
    gDPFillRectangle( gpGfxList++, ux, uy, ux+w, ly);
  
    // right line
    gDPFillRectangle( gpGfxList++, lx-w, uy, lx, ly);
    
    // bottom line
    gDPFillRectangle( gpGfxList++, ux, ly-w, lx, ly);
}

void getCPakColor( int idx, int h, unsigned char *r, unsigned char *g, unsigned char *b ) 
{  
    unsigned char cl[5][3] = { { 64,  64,  64},  {128, 0, 0}, {0, 128, 0}, {128, 128, 0}, {0, 128, 128} };
    unsigned char ch[5][3] = { {235, 235, 235 }, {235, 0, 0}, {0, 235, 0}, {235, 235, 0}, {0, 235, 235} };
    int i;

    i = (idx < 0)||(idx > 4) ? 0: idx;
    if( h ) {
      *r = ch[i][0]; *g = cl[i][1]; *b = ch[i][2];
    } else {
      *r = cl[i][0]; *g = cl[i][1]; *b = cl[i][2];
    }
}

void drawCPak( int mode )
{
    int i;
    char *cellnum[MAX_CONT_PAK] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
    unsigned char r, g, b;
    if( !gCPakInit ) 
        initCPak(mode);

    // background rectangle
    gDPSetCycleType(gpGfxList++, G_CYC_FILL);
    gDPSetFillColor(gpGfxList++, (GPACK_RGBA5551(0, 0, 0, 1) << 16) | 
                    GPACK_RGBA5551(0, 0, 0, 1));
    gDPFillRectangle( gpGfxList++, 108, 223, 496, 355);

    // icons
    drawShadowIcon(gCPakIcon, MAX_CONT_PAK, gCPakInit, initCPak);

    for( i = 0; i < MAX_CONT_PAK; i++ ) {
        int x, y;
        int active = (gCPakList[i].cid == gGameIcons[gGameIconIdx].id) && 
            (gCPakList[i].cont_id != -1);
        int select = active ? gCPakList[i].cont_id+1 : 0;
        getCPakOrigin(i, &x, &y);
        // set fill color
        getCPakColor( select,  active, &r, &g, &b );
        gDPSetCycleType(gpGfxList++, G_CYC_FILL);
        gDPSetFillColor(gpGfxList++, (GPACK_RGBA5551(r, g, b, 1) << 16)|GPACK_RGBA5551(r, g, b, 1));

        // box
        if( select ) {
            drawBox( x, y, x + BSIZE, y + BSIZE, 4 );
        } else {
            drawBox( x, y, x + BSIZE, y + BSIZE, 1 );
        }

        // location number
        drawString(cellnum[i], gStringMap, x + 2, y + 2 );

	// controller number
	if( gCPakList[i].cid && gCPakList[i].cont_id != -1 ) {
	    getCPakColor( gCPakList[i].cont_id+1,  0, &r, &g, &b );
	    gDPSetCycleType(gpGfxList++, G_CYC_FILL);
	    gDPSetFillColor(gpGfxList++, (GPACK_RGBA5551(r, g, b, 1) << 16)|GPACK_RGBA5551(r, g, b, 1));
	    gDPFillRectangle( gpGfxList++, x + 48, y + 2,
			      x + 60, y + 14 );
	}
    }
    // reset fill color
    gDPSetFillColor(gpGfxList++, (GPACK_RGBA5551(0, 0, 0, 1) << 16) | 
                    GPACK_RGBA5551(0, 0, 0, 1));
}

void drawCPakBinding()
{
    int i;
    int posx[4] = {162, 234, 306, 378};
    int posy[4] = {150, 150, 150, 150};
    int bsize = 64;
    unsigned char r, g, b;
    char *cellnum[MAX_CONT_PAK+1] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };

    for( i = 0; i < gCPakBind.nCPak; i++ ) {
        gDPSetCycleType(gpGfxList++, G_CYC_FILL);
        getCPakColor( i+1, 0, &r, &g, &b );
        gDPSetFillColor(gpGfxList++, (GPACK_RGBA5551(r, g, b, 1) << 16) | GPACK_RGBA5551(r, g, b, 1));

        drawBox( posx[i], posy[i], posx[i]+bsize, posy[i]+bsize, 4 );
        drawString(cellnum[gCPakBind.idxCPak[i]+1], gStringMap, posx[i] + 2, posy[i] + 2 );
    }
    gDPSetFillColor(gpGfxList++, (GPACK_RGBA5551(0, 0, 0, 1) << 16) | 
                    GPACK_RGBA5551(0, 0, 0, 1));
}

void onCPakBindIconInit()
{
    int i;
    int posx[4] = {178, 250, 322, 394};
    int posy[4] = {166, 166, 166, 166};
    u16* pTex[4] = {texPlayer, texPlayer, texPlayer, texPlayer};
    u8*  pText[4] = {NULL, NULL, NULL, NULL};

    if( !gCPakInit ) initCPak(0);
    gCPakBind.nCPak = uiGetCPakCnt(gGameIcons[gGameIconIdx].id);
    for (i=0; i<4; i++) gCPakBind.idxCPak[i] = -1;
    for (i=0; i<MAX_CONT_PAK; i++) {
       if (gGameIcons[gGameIconIdx].id == gCPakList[i].cid &&
               gCPakList[i].cont_id != -1) {
           gCPakBind.idxCPak[gCPakList[i].cont_id] = i;
       }
    }

    initShadowIconGroup(gCPakBindIcon, gCPakBind.nCPak,
                        posx, posy,
                        GAME_ICON_W, GAME_ICON_H,
                        3<<8, 3<<8,
                        pTex, pText, gShadowMap);
    gCPakBindIconInit = 1;
}

void onCPakBindIconHilite(int idx)
{
#if 0
    int bsize = 64;
    unsigned char ch[4][3] = { {235, 0, 0}, {0, 235, 0}, {235, 235, 0}, {0, 235, 235} };
    int bx[4] = { 162, 234, 306, 378 }, by = 150; 
    
    gDPSetCycleType(gpGfxList++, G_CYC_FILL);
    gDPSetFillColor(gpGfxList++, (GPACK_RGBA5551(ch[idx][0],ch[idx][1],ch[idx][2], 1) << 16) | GPACK_RGBA5551(ch[idx][0], ch[idx][1], ch[idx][2], 1));
    PRINTF("%d %d %d %d\n", bx[idx], by, bx[idx]+bsize, by+bsize);
    drawBox( bx[idx], by, bx[idx]+bsize, by+bsize, 4 );
    gDPSetFillColor(gpGfxList++, (GPACK_RGBA5551(0, 0, 0, 1) << 16) | 
                    GPACK_RGBA5551(0, 0, 0, 1));
#endif
}

void onCPakBindIconSelect(int idx, u32 button)
{
    static SaCPak previous[4];

    if (button==CONT_A) 
    {
        playSound(SOUND_SELECT);
        if (gCPakInit < 2) {
            int i;
            for (i = 0; i < 4; ++i) {
                previous[i].cid = gCPakList[gCPakBind.idxCPak[i]].cid;
                previous[i].cont_id = -1;
            }
            gCPakInit = 2;
        }

        if( gCPakBind.idxCPak[idx] != -1 ) {
            gCPakList[gCPakBind.idxCPak[idx]] = previous[idx];
        }

        do {
            gCPakBind.idxCPak[idx]++;
            if( gCPakBind.idxCPak[idx] == MAX_CONT_PAK ) {
                gCPakBind.idxCPak[idx] = -1;
                break;
            }
        } while( gCPakList[gCPakBind.idxCPak[idx]].cid == gGameIcons[gGameIconIdx].id && gCPakList[gCPakBind.idxCPak[idx]].cont_id != -1 );

        if( gCPakBind.idxCPak[idx] != -1 ) {
            previous[idx] = gCPakList[gCPakBind.idxCPak[idx]];
            gCPakList[gCPakBind.idxCPak[idx]].cont_id = idx;
            gCPakList[gCPakBind.idxCPak[idx]].cid = gGameIcons[gGameIconIdx].id;
        }
        onCPakBindIconInit();
    }
}

void resetCPak() 
{
    gCPakInit = 0;
    gCPakBindIconInit = 0;
}

void saveCPak(void)
{  
    uiSaveCPakBindings(gCPakList);
    gCPakInit = 0;
    gCPakBindIconInit = 0;
}