pagecommon.c 6.94 KB

#include "viewer.h"
#include "pagecommon.h"
#include "gfxtex.h"
#include "texrect.h"
#include "status.h"
#include "uilogic.h"
#include "gzip.h"
#include "font.h"
#include "shadowicon.h"
#include "pagegame.h"
#include "audrenderer.h"

UiMainButton gMainButton[MAX_MAIN_BUTTON] =
{
    { PAGE_GAME, 0, 312, 60, 32, 296, 44, texB1 },
    { PAGE_CLUB, 0, 408, 60, 32, 392, 44, texB2 },
    { PAGE_DATA, 0, 504, 60, 32, 488, 44, texB3 }
};
u16 texHB[64*64*3] __attribute__ ((aligned (16)));

UiCursor gCursor[MAX_CURSOR];

int      gIdxCursor;
int      gAltCursor;
int      gCursorCapture;

void initMainButtons()
{
    u16 *ptr, *dptr;
    u8 r, g, b, a;
    int i, j;

    for (i=0; i<MAX_MAIN_BUTTON; i++) {
        ptr = gMainButton[i].data;
        dptr = texHB + i*64*64;
        for (j=0; j<64*64; j++) {
            r = 32 - (((*(ptr+j)) & 0xF800)>>11);
            g = 32 - (((*(ptr+j)) & 0x07C0)>>6);
            b = 32 - (((*(ptr+j)) & 0x003E)>>1);
            a = (*(ptr+j)) & 0x1;
            //dptr[j] = r<<11 | g<<6 | b<<1 | a;
            dptr[j] = (~(*(ptr+j))) ^ 0x1;
        }
    } 
}

void drawPageCommon()
{
    s32 xscale, yscale;
    f32 xpos, ypos;
    int i;

    xscale = 1<<9;
    yscale = 1<<9;

#if 0
    //draw star background
    drawTexRect(texStar, 
               G_IM_FMT_RGBA, G_IM_SIZ_16b,
               SCREEN_WD/2, SCREEN_HT/2, 
               32, 32,  
               xscale, yscale, 
               SCREEN_WD, SCREEN_HT,  
               0, 0); 
#endif

    //draw frame
    xpos = FRAME_X;
    ypos = FRAME_Y;
    drawTexRectSmooth(texFrame, texFrameAlpha, 
                      G_IM_FMT_RGBA, G_IM_SIZ_16b,
                      G_IM_FMT_I, G_IM_SIZ_4b,
                      FRAME_WD/2, FRAME_HT/2, 
                      16, 16,  
                      1, 
                      &xscale, &yscale,  
                      FRAME_WD, FRAME_HT,  
                      &xpos, &ypos); 

    // draw buttons
    for( i = 0; i < MAX_MAIN_BUTTON; i++ ) {
        if( !gMainButton[i].active ) {
            drawTexRect(gMainButton[i].data, 
                        G_IM_FMT_RGBA, G_IM_SIZ_16b,
                        64, 64, 
                        32, 32,  
                        1<<10, 1<<10,
                        64, 64,
                        gMainButton[i].draw_x, 
                        gMainButton[i].draw_y );
        } else {
            drawTexRect(texHB+i*64*64, 
                        G_IM_FMT_RGBA, G_IM_SIZ_16b,
                        64, 64, 
                        32, 32,  
                        1<<10, 1<<10,
                        64, 64,
                        gMainButton[i].draw_x, 
                        gMainButton[i].draw_y );
        }
    }
}

void initCursor(int x, int y)
{
    int i;

    gAltCursor = 1;
    for( i = MAX_CURSOR - 1; i >= 0; i-- ) {
         gCursor[i].x = x;
         gCursor[i].y = y;      // for tail: y - i;
         gCursor[i].count = 0;  // for tail : MAX_CURSOR - i;
    }
    gIdxCursor = 0;
    gCursorCnt = 0;
}

void drawCursor() 
{
    int i, j, scale, count;
    int lastCursor;
    u32 x_scale[MAX_CURSOR], y_scale[MAX_CURSOR];
    float x_tail[MAX_CURSOR], y_tail[MAX_CURSOR];
    s32 cscalex, cscaley;
    f32 cposx, cposy;

    gCursor[gIdxCursor].x = gCursor[gIdxCursor].x + gCursorX/CURSOR_SPEED; //* (gCursorCnt/10);
    if( gCursor[gIdxCursor].x < 10 ) gCursor[gIdxCursor].x = 10;
    if( gCursor[gIdxCursor].x > SCREEN_WD - 10) gCursor[gIdxCursor].x = SCREEN_WD - 10;
    gCursor[gIdxCursor].y = gCursor[gIdxCursor].y - gCursorY/CURSOR_SPEED; //* (gCursorCnt/10);
    if( gCursor[gIdxCursor].y < 10 ) gCursor[gIdxCursor].y = 10;
    if( gCursor[gIdxCursor].y > SCREEN_HT - 10) gCursor[gIdxCursor].y = SCREEN_HT - 10;
    gCursor[gIdxCursor].count = 0;

    // reduce cursor count
    for( i = 0; i < MAX_CURSOR; i++ )
      if( gCursor[i].count ) gCursor[i].count--;

    if( gCursorCapture ) {
        gCursorCapture--;
    } else {
        gCursorCapture = CURSOR_CAP;
        lastCursor = gIdxCursor;
        gIdxCursor = (gIdxCursor + 1)%MAX_CURSOR;
        gCursor[gIdxCursor].x = gCursor[lastCursor].x;
        gCursor[gIdxCursor].y = gCursor[lastCursor].y;
        gCursor[lastCursor].count = CURSOR_COUNT;
    }

    for( i = count = 0, j = (gIdxCursor + 1)%MAX_CURSOR; i < MAX_CURSOR; 
         i++, j = (j+1)%MAX_CURSOR ) {
        if( (gCursor[j].count) && ((gCursor[j].x != gCursor[gIdxCursor].x) ||
            (gCursor[j].y != gCursor[gIdxCursor].y)) ) {
                scale = 512 + 4096*(CURSOR_COUNT - gCursor[j].count + 1)/CURSOR_COUNT;
                x_scale[count] = y_scale[count] = scale;
                x_tail[count] = gCursor[j].x + 32*(CURSOR_COUNT - gCursor[j].count + 1)/CURSOR_COUNT;
                y_tail[count] = gCursor[j].y + 32*(CURSOR_COUNT - gCursor[j].count + 1)/CURSOR_COUNT;
                count++;
        }
    }
    if( count > 0 ) {
        drawTexRectSmooth(texLight, texLightAlpha,
                          G_IM_FMT_RGBA, G_IM_SIZ_16b,
                          G_IM_FMT_I, G_IM_SIZ_4b,
                          32, 32,
                          32, 32,
                          count,
                          x_scale, y_scale,
                          64, 64,
                          x_tail, y_tail );
    }

    if ( gAltCursor==1 ) { 
        cscalex = 0x3f<<4;
        cscaley = 0x38<<4;
        cposx = gCursor[gIdxCursor].x;
        cposy = gCursor[gIdxCursor].y;
        drawTexRectSmooth(texLight, texLightAlpha, 
                          G_IM_FMT_RGBA, G_IM_SIZ_16b,
                          G_IM_FMT_I, G_IM_SIZ_4b,
                          32, 32, 
                          32, 32, 
                          1, 
                          &cscalex, &cscaley, 
                          48, 48,  
                          &cposx, &cposy); 
    } else {
        cscalex = 0x3ff;
        cscaley = 0x3f<<4;
        cposx = gCursor[gIdxCursor].x+1.5;
        cposy = gCursor[gIdxCursor].y+3.5;
        drawTexRectSmooth(texLight, texLightAlpha, 
                          G_IM_FMT_RGBA, G_IM_SIZ_16b,
                          G_IM_FMT_I, G_IM_SIZ_4b,
                          32, 32, 
                          32, 32, 
                          1, 
                          &cscalex, &cscaley, 
                          48, 48,  
                          &cposx, &cposy); 
    }    
    gAltCursor = 1 - gAltCursor;
}

void checkCursorCommon()
{
    int i, dx, dy;    

    gHotSpot = 0;

    for( i = 0; i < MAX_MAIN_BUTTON; i++ ) {
        gMainButton[i].active = 0;
        dx = gMainButton[i].center_x - gCursor[gIdxCursor].x;
        if( dx < 0 ) dx = -dx;
        dy = gMainButton[i].center_y - gCursor[gIdxCursor].y;
        if( dy < 0 ) dy = -dy;
        if( (dx < gMainButton[i].radius)&&(dy < gMainButton[i].radius) ) {
            gMainButton[i].active = 1;
            gHotSpot = 1;
            if( gButton == CONT_A ) {
                playSound(SOUND_SELECT);
                gPageNo = gMainButton[i].bnum;
		gStatus = VW_OK;
                gButton = 0;
            }
        }  
    }
}