
/*********************************************************************
*   < _DRAW.CPP >
**********************************************************************
*   描画関係
**********************************************************************
*                                                   Copyright (C) 2005
*   Programed by Ochi
*********************************************************************/

#include "_speed.h"

/*********************************************************************
*   カードを描画する
**********************************************************************
*   引数　：カードクラス(CCards), 左クリックされているか(bool)
*   　　　　クリックされているカードの番号(int)
*   戻り値：終了(bool)
*********************************************************************/
bool CDraw::DrawCards( CCards cCards, bool fLButton, int nCardNo )
{
    int x, y, w, h, width, height, n;
    int ptrBa[2];           cCards.SetBa( (int*)ptrBa );
    int ptrTefuda[2][8];    cCards.SetTefuda( (int*)ptrTefuda );
    int ptrYama[2][32];     cCards.SetYama( (int*)ptrYama );
    HPEN    hPen;
    HPEN    hOldPen;
    HBRUSH  hBrush;
    HBRUSH  hOldBrush;

    w       = _image.ReturnSize( "BackGround", 'W' );
    h       = _image.ReturnSize( "BackGround", 'H' );
    width   = _frame.ReturnWinSize( 'W' );
    height  = _frame.ReturnWinSize( 'H' );

    hPen = CreatePen( PS_DOT, 1, RGB(150,150,150) );
    hOldPen = (HPEN)SelectObject( hMemoryDC, hPen );
    hBrush = CreateHatchBrush( HS_DIAGCROSS, RGB(255,0,255) );
    hOldBrush = (HBRUSH)SelectObject( hMemoryDC, GetStockObject(NULL_BRUSH) );

    for( y=0; y<=height; y+=h )
    {
        for( x=0; x<=width; x+=w )
        {
            _image.Draw( hMemoryDC, "BackGround", x, y );
        }
    }

    w = _image.ReturnSize( "J1",'W' );  // カードの横の長さ
    h = _image.ReturnSize( "J1",'H' );  // カードの縦の長さ
    x = ((width/5)-w)/2;                // カードの空きX
    y = ((height/3)-h)/2;               // カードの空きY

    // 山を描画
    if( ptrYama[0][0]!=0 )
    {
        if ( nCardNo!=-1 || ptrYama[0][0]!=NULL )
        {
            _image.Draw( hMemoryDC, "B1", x, height/3*2+y );
        }
    }
    if( ptrYama[1][0]!=0 )
    {
        _image.Draw( hMemoryDC, "B1", width/5*4+x, y );
    }
    // 手札を描画
    for( n=0; n<=3; n++ )
    {
        Rectangle( hMemoryDC ,width/5*(n+1)+x, height/3*2+y, width/5*(n+1)+x+w, height/3*2+y+h );
        Rectangle( hMemoryDC ,width/5*(n+0)+x, height/3*0+y, width/5*(n+0)+x+w, height/3*0+y+h );
        if( ptrTefuda[0][n] != 0 )
        {
            if( fLButton!=true || n!=nCardNo )
            {
                _image.Draw( hMemoryDC, cCards.Decode(ptrTefuda[0][n]), width/5*(n+1)+x, height/3*2+y );        
            }
        }
        if( ptrTefuda[1][n] != 0 )
        {
            _image.Draw( hMemoryDC, cCards.Decode(ptrTefuda[1][n]), width/5*n+x, y );
        }
    }

    // 場を描画
    Rectangle( hMemoryDC ,width/2-w-x, height/3+y, width/2-x, height/3+y+h );
    Rectangle( hMemoryDC ,width/2+x, height/3+y, width/2+x+w, height/3+y+h );
    if( ptrBa[0] != 0 )
    {
        _image.Draw( hMemoryDC, cCards.Decode(ptrBa[0]), width/2-w-x, height/3+y );
    }
    if ( ptrBa[1] != 0 )
    {
        _image.Draw( hMemoryDC, cCards.Decode(ptrBa[1]), width/2+x, height/3+y );
    }

    SelectObject( hMemoryDC, hOldPen );
    SelectObject( hMemoryDC, hOldBrush );
    DeleteObject( hPen );
    DeleteObject( hBrush );

    return true;
}

/*********************************************************************
*   デバイスコンテキストを引数のハンドルをもとに初期化する
**********************************************************************
*   引数　：作成したウィンドウのハンドル(HWND)
*   戻り値：無し
*********************************************************************/
void CDraw::FormatDC( HWND hWnd_Bring )
{
    HDC hDC;
    HBITMAP hBmp;
    hWnd = hWnd_Bring;

    hDC = GetDC( hWnd );
    hMemoryDC = CreateCompatibleDC( hDC );
    hBmp = CreateCompatibleBitmap( hDC, _frame.ReturnWinSize('W'), _frame.ReturnWinSize('H') );
    SelectObject( hMemoryDC, hBmp );
    SetBkMode( hMemoryDC,TRANSPARENT );
    ReleaseDC( hWnd, hDC );
}

/*********************************************************************
*   全ての画像データを読み込む
**********************************************************************
*   引数　：無し
*   戻り値：無し
*********************************************************************/
void CDraw::LoadAllImage( void )
{
    //////////////////////////////
    // 全画像のロード
    _image.Read( "BackGround", "./image/bg.bmp" );
    // カード裏
    _image.Read( "B1", "./image/cards/B1.bmp" );
    _image.Read( "B2", "./image/cards/B2.bmp" );
    // ジョーカー
    _image.Read( "J1", "./image/cards/J1.bmp" );
    _image.Read( "J2", "./image/cards/J2.bmp" );
    // スペード
    _image.Read( "S1", "./image/cards/S1.bmp" );
    _image.Read( "S2", "./image/cards/S2.bmp" );
    _image.Read( "S3", "./image/cards/S3.bmp" );
    _image.Read( "S4", "./image/cards/S4.bmp" );
    _image.Read( "S5", "./image/cards/S5.bmp" );
    _image.Read( "S6", "./image/cards/S6.bmp" );
    _image.Read( "S7", "./image/cards/S7.bmp" );
    _image.Read( "S8", "./image/cards/S8.bmp" );
    _image.Read( "S9", "./image/cards/S9.bmp" );
    _image.Read( "SA", "./image/cards/SA.bmp" );
    _image.Read( "SB", "./image/cards/SB.bmp" );
    _image.Read( "SC", "./image/cards/SC.bmp" );
    _image.Read( "SD", "./image/cards/SD.bmp" );
    // クラブ
    _image.Read( "C1", "./image/cards/C1.bmp" );
    _image.Read( "C2", "./image/cards/C2.bmp" );
    _image.Read( "C3", "./image/cards/C3.bmp" );
    _image.Read( "C4", "./image/cards/C4.bmp" );
    _image.Read( "C5", "./image/cards/C5.bmp" );
    _image.Read( "C6", "./image/cards/C6.bmp" );
    _image.Read( "C7", "./image/cards/C7.bmp" );
    _image.Read( "C8", "./image/cards/C8.bmp" );
    _image.Read( "C9", "./image/cards/C9.bmp" );
    _image.Read( "CA", "./image/cards/CA.bmp" );
    _image.Read( "CB", "./image/cards/CB.bmp" );
    _image.Read( "CC", "./image/cards/CC.bmp" );
    _image.Read( "CD", "./image/cards/CD.bmp" );
    // ハート
    _image.Read( "H1", "./image/cards/H1.bmp" );
    _image.Read( "H2", "./image/cards/H2.bmp" );
    _image.Read( "H3", "./image/cards/H3.bmp" );
    _image.Read( "H4", "./image/cards/H4.bmp" );
    _image.Read( "H5", "./image/cards/H5.bmp" );
    _image.Read( "H6", "./image/cards/H6.bmp" );
    _image.Read( "H7", "./image/cards/H7.bmp" );
    _image.Read( "H8", "./image/cards/H8.bmp" );
    _image.Read( "H9", "./image/cards/H9.bmp" );
    _image.Read( "HA", "./image/cards/HA.bmp" );
    _image.Read( "HB", "./image/cards/HB.bmp" );
    _image.Read( "HC", "./image/cards/HC.bmp" );
    _image.Read( "HD", "./image/cards/HD.bmp" );
    // ダイヤ
    _image.Read( "D1", "./image/cards/D1.bmp" );
    _image.Read( "D2", "./image/cards/D2.bmp" );
    _image.Read( "D3", "./image/cards/D3.bmp" );
    _image.Read( "D4", "./image/cards/D4.bmp" );
    _image.Read( "D5", "./image/cards/D5.bmp" );
    _image.Read( "D6", "./image/cards/D6.bmp" );
    _image.Read( "D7", "./image/cards/D7.bmp" );
    _image.Read( "D8", "./image/cards/D8.bmp" );
    _image.Read( "D9", "./image/cards/D9.bmp" );
    _image.Read( "DA", "./image/cards/DA.bmp" );
    _image.Read( "DB", "./image/cards/DB.bmp" );
    _image.Read( "DC", "./image/cards/DC.bmp" );
    _image.Read( "DD", "./image/cards/DD.bmp" );
}

/*********************************************************************
*   プライベートのメソッドを渡す
**********************************************************************
*   引数　：無し
*   戻り値：要求されたメソッド
*********************************************************************/
HWND CDraw::ReturnHWND( void )
{
    return hWnd;
}

HDC CDraw::ReturnMemoryDC( void )
{
    return hMemoryDC;
}

HDC* CDraw::ReturnSaveDC( void )
{
    return &hSaveDC;
}