shadowicon.c 5.9 KB

#include "viewer.h"
#include "uilogic.h"
#include "cpak.h"
#include "pagecommon.h"
#include "pagegame.h"
#include "shadowicon.h"
#include "gzip.h"
#include "texrect.h"

u16 gGameIconMap[GAME_ICON_W*GAME_ICON_H * MAX_CONT_PAK];
u8  gGameIconShadowMap[GAME_ICON_W*GAME_ICON_H/2 * MAX_CONT_PAK];
//TODO: fix arbitrary number
u8  gShadowMap[32*128*16] __attribute__ ((aligned (16))); 

void initShadowIconGroup(UiShadowIcon *pIcons, int cnt, 
                         int *posx, int *posy, 
                         int w, int h, 
                         u32 xscale, u32 yscale, 
                         u16 **pTexture, u8 **pText, u8 *pShadowMap)
{
    int i, j, size; 

    size = w*h;
    bzero(pShadowMap, size/2*cnt);
    for (i=0; i<cnt; i++) {
        pIcons[i].show = 1;
        pIcons[i].init = 1;
        pIcons[i].id = i;
        pIcons[i].state = 0;
        pIcons[i].dir = 0;
        pIcons[i].text = pText[i];
        pIcons[i].posx = posx[i];
        pIcons[i].posy = posy[i];
        pIcons[i].w = w;
        pIcons[i].h = h;
        pIcons[i].xscale = xscale;
        pIcons[i].yscale = yscale;
        pIcons[i].texMap = pTexture[i];
        pIcons[i].shadowMap = pShadowMap + size/2*i;
        for( j = 0; j < size; j++ ) {
            pIcons[i].shadowMap[j] = ((pIcons[i].texMap[2*j] & 0x0001 ? 0x01 : 0x0) | (pIcons[i].texMap[2*j] & 0x0001 ? 0x10 : 0x0));
        }
    }
}

void initGameIconGroup(UiShadowIcon *pIcons, int cnt, u32 *pIds)
{
    int i, j, outsize, size, idx; 

    size = GAME_ICON_W * GAME_ICON_H;
    bzero(gGameIconMap, size*2*cnt);
    bzero(gGameIconShadowMap, size/2*cnt);
    for( i = 0; i < cnt; i++ ) {
        pIcons[i].show = 0;
        pIcons[i].init = 0;
        pIcons[i].id = *(pIds+i); 
        pIcons[i].state = 0;
        pIcons[i].dir = 0;
        pIcons[i].w = GAME_ICON_W;
        pIcons[i].h = GAME_ICON_H;
        pIcons[i].xscale = 1<<9;
        pIcons[i].yscale = 1<<9;

        if ( *(pIds+i) != 0 ) {
            idx = uiGetTicketIdx( *(pIds+i) );
            if (idx!=-1) {
                pIcons[i].texMap = gGameIconMap + size * i;
                pIcons[i].shadowMap = gGameIconShadowMap + size/2*i;
                outsize = expand_gzip(gpGameMeta[idx].thumb_data, 
                                      (u8*)(pIcons[i].texMap), 
                                      gpGameMeta[idx].thumb_len,
                                      size*2);
                PRINTF("thumb_insize=%d, thumb_outsize=%d\n", gpGameMeta[idx].thumb_len, outsize);
                for(j = 0; j < size/2; j++ ) {
                    pIcons[i].shadowMap[j] = (pIcons[i].texMap[2*j] & 0x0001 ? 0x01 : 0x0) | (pIcons[i].texMap[2*j+1] & 0x0001 ? 0x10 : 0x0); 
                } 

                pIcons[i].text = gpGameMeta[idx].title;

                pIcons[i].init = 1;
                pIcons[i].show = 1;

                pIcons[i].posx = 140 + 96*(i%(cnt/2));
                pIcons[i].posy = 182 + 96*(i/(cnt/2));
            }
        }
    }
}

void drawShadowIcon(UiShadowIcon* pIcons, int count, int init, IconInitCallBack pInitCB) 
{
    int i, x, y, xoffset, yoffset, xsoffset, ysoffset;
    f32 xpos, ypos;
    int shadow_offset[9] = { 0, 0, -1, -2, -2, -1, -1, 0, 1 };

    if (!init) (*pInitCB)();

    // draw each icon
    for( i = 0; i < count; i++ ) {
        // is this icon show?
      if( !pIcons[i].show ) continue;

        // find position
        x = pIcons[i].posx; y = pIcons[i].posy;
        xoffset = yoffset = 0;
        if( pIcons[i].state == 0 ) {
            if( pIcons[i].dir < 0 ) pIcons[i].dir = 0;
        } else if( pIcons[i].state >= 16 ) {
            if( pIcons[i].dir > 0 ) pIcons[i].dir = 0;
            xoffset = yoffset = -8;
        } else {
            xoffset = -pIcons[i].state/2;
            yoffset = -pIcons[i].state/2;
        }
        // shadow offset
        xsoffset = shadow_offset[-xoffset];
        ysoffset = shadow_offset[-yoffset];
        pIcons[i].state += ICON_SPEED * pIcons[i].dir;
	if( !pIcons[i].texMap ) continue;

        // draw shadow
        if( xoffset != 0 ) {
            xpos = pIcons[i].posx + xsoffset;
            ypos = pIcons[i].posy + ysoffset;
            drawTexRect(pIcons[i].shadowMap, 
                        G_IM_FMT_I, G_IM_SIZ_4b,
                        pIcons[i].w, pIcons[i].h,
                        32, 32,
                        pIcons[i].xscale, pIcons[i].yscale,
                        1024*(pIcons[i].w)/(pIcons[i].xscale), 
                        1024*(pIcons[i].h)/(pIcons[i].yscale),
                        xpos, ypos );
        }

        // draw icon
        xpos = pIcons[i].posx + xoffset;
        ypos = pIcons[i].posy + yoffset;
        drawTexRect(pIcons[i].texMap, 
                    G_IM_FMT_RGBA, G_IM_SIZ_16b,
                    pIcons[i].w, pIcons[i].h,
                    32, 32,
                    pIcons[i].xscale, pIcons[i].yscale,
                    1024*(pIcons[i].w)/(pIcons[i].xscale), 
                    1024*(pIcons[i].h)/(pIcons[i].yscale),
                    xpos, ypos );
    }
}

void checkCursorIcon(UiShadowIcon* pIcons, int count, IconHiliteCallBack pHilite, IconSelectCallBack pSelect)
{
    int i, w, h;    

    for( i = 0; i < count; i++ ) {
        if( pIcons[i].show ) {
            pIcons[i].dir = -1;
            w = 1024*(pIcons[i].w)/(pIcons[i].xscale);
            h = 1024*(pIcons[i].h)/(pIcons[i].yscale);
            if( (gCursor[gIdxCursor].x >= pIcons[i].posx - 24) &&
                (gCursor[gIdxCursor].x <= pIcons[i].posx - 8 + w) &&
                (gCursor[gIdxCursor].y >= pIcons[i].posy - 24) &&
                (gCursor[gIdxCursor].y <= pIcons[i].posy - 8 + h) ) {

                pIcons[i].dir = 1;
                gHotSpot = 1;
                if (pIcons[i].state >= 8) {
                    (*pHilite) (i);
                }
                if ((pIcons[i].state >= 8) && (gButton!=0) ) {
                    (*pSelect) (i, gButton);
                    gButton = 0;
                }
            } 
        }
    }

}