[ create a new paste ] login | about

Link: http://codepad.org/VM5gnv3C    [ raw code | output | fork ]

C++, pasted on Mar 27:
#include <stdexcept>
#include <cstring>
#include <string>
#if defined( _DEBUG ) || defined( DEBUG )
#include <iostream>
#endif

#include "dinput_test.h"

namespace g
{

COLORREF color = RGB( 0, 0, 0 );

#ifndef UNICODE
std::string text;
#else
std::wstring text;
#endif

DInputKeyboard* dinput_ptr = nullptr;
DInputMouse* mouse_ptr = nullptr;

}

namespace
{

bool setAcquire(
#if DIRECTINPUT_VERSION <= 0x0600
    IDirectInputDevice* dinput,
#else
    IDirectInputDevice8* dinput,
#endif
    bool active
    )
{
    if ( !dinput )
    {
        return false;
    }

    if ( active )
    {
        dinput->Acquire();
    }
    else
    {
        dinput->Unacquire();
    }

    return true;
}

void paintTextProc( HWND hwnd, COLORREF& color, TCHAR* text )
{
    RECT rc{};
    PAINTSTRUCT ps{};
    DRAWTEXTPARAMS dtp{};
    HDC hdc = nullptr;
    GetClientRect( hwnd, &rc );
    rc.left += 20;
    rc.top += 20;
    rc.right -= 20;
    rc.bottom -= 20;
    
    hdc = BeginPaint( hwnd, &ps );
    dtp.cbSize = sizeof( DRAWTEXTPARAMS );
    dtp.iTabLength = 4;
    dtp.iLeftMargin = 20;
    dtp.iRightMargin = 20;
    SetTextColor( 
        hdc,
        g::color
        );
    DrawTextEx(
        hdc,
        text,
        g::text.size(),
        &rc,
        DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK,
        &dtp
        );
    EndPaint( hwnd, &ps );
}

void paintGeometryProc( HWND hwnd, COLORREF& color, POINT coor )
{
    RECT rc{};
    PAINTSTRUCT ps{};
    HDC hdc = nullptr;
    GetClientRect( hwnd, &rc );
    rc.left += 20;
    rc.top += 20;
    rc.right -= 20;
    rc.bottom -= 20;
    HPEN pen = CreatePen( PS_SOLID, 1, color ),
        pen_holder{};
    HBRUSH brush = reinterpret_cast<HBRUSH>(
        GetStockObject( NULL_BRUSH )
        ),
        brush_holder{};

    hdc = BeginPaint( hwnd, &ps );
    pen_holder = static_cast<HPEN>( 
        SelectObject( hdc, pen )
        );
    brush_holder = static_cast<HBRUSH>(
        SelectObject( hdc, brush )
        );
    Ellipse( hdc, coor.x, coor.y, coor.x + 1, coor.y + 1 );
    SelectObject( hdc, pen_holder );
    SelectObject( hdc, brush_holder );
    EndPaint( hwnd, &ps );
}

} // anonymous


void keyboardTest(
    DInputKeyboard* dinput,
    HWND win_handle
    )
{
    g::dinput_ptr = dinput;
    if ( !dinput )
    {
        MessageBox(
            nullptr,
            _T( "dinput is null" ),
            _T( "info" ),
            MB_OK
            );
        SendMessage( win_handle, WM_DESTROY, 0, 0 );
        return;
    }

    HRESULT hr = dinput->checkKey();
    if ( hr != DI_OK )
    {
        return;
    }

    static auto lambda1 = [win_handle]( std::uint8_t i )
    {
#ifndef UNICODE
        char ch[2] = {};
        ch[0] = i + 0x20;
#else
        wchar_t wch[2] = { };
        wch[0] = i + 0x20;
#endif
        MessageBox(
            nullptr,
#ifndef UNICODE
            ch,
#else
            wch,
#endif
            _T( "info" ),
            MB_OK
            );
    };
    auto lambda2 = [win_handle]( std::uint8_t i )
    {
        g::text = i + 0x20;
        g::color = RGB( i << 2, i << 1, i << 1 );
        InvalidateRect( win_handle, nullptr, true );
    };

    for ( std::uint8_t i = 0x11; i < 0xff; ++i )
    {
        dinput->processKeyTriggerOn(
            i,
            lambda2, i
            );
    }
    dinput->processKeyTriggerOn(
        DIK_Q,
        SendMessage,
        win_handle, WM_DESTROY, 0, 0
        );
}

void mouseTest(
    DInputMouse* dinput,
    HWND win_handle
    )
{
    g::mouse_ptr = dinput;
    if ( !g::mouse_ptr )
    {
         MessageBox(
            nullptr,
            _T( "dinput is null" ),
            _T( "info" ),
            MB_OK
            );
        SendMessage( win_handle, WM_DESTROY, 0, 0 );
        return;
    }

    HRESULT hr = g::mouse_ptr->update( win_handle );
    if ( dinput->button( 0 ) )
    {
        g::color = RGB( 255, 10, 10 );
        SendMessage( win_handle, WM_PAINT, 0, 0 );
    }
}


LRESULT CALLBACK dinputProc(
    HWND hwnd,
    UINT msg,
    WPARAM wp,
    LPARAM lp
    )
{
    bool active = false;

    switch ( msg )
    {
        case WM_DESTROY:
            PostQuitMessage( 0 );
            break;
        case WM_ACTIVATE:
            switch ( wp & 0xffff )
            {
                case WA_INACTIVE: // 非アクティブ時
                    break;
                case WA_ACTIVE: // マウス以外で
                    active = true;
                    break;
                case WA_CLICKACTIVE: // マウスで
                    active = true;
                    break;
            }
            if ( ( wp >> 16 ) & 0xffff ) // 最小化
            {
                ; // たぶんいらない
            }

            if ( g::dinput_ptr )
            {
                setAcquire( 
                    g::dinput_ptr->inputDevice(), 
                    active 
                    );
            }
            else if ( g::mouse_ptr )
            {
                setAcquire( 
                    g::mouse_ptr->inputDevice(), 
                    active 
                    );
            }
            break;
        case WM_CLOSE:
            SendMessage( hwnd, WM_DESTROY, wp, lp );
            break;
        case WM_PAINT:
            if ( g::dinput_ptr )
            {
                TCHAR text[16];
                for ( std::uint8_t i = 0
                      ; i < g::text.size()
                      ; ++i )
                {
                    text[i] = g::text[i];
                }
                paintTextProc( hwnd, g::color, text );
            }
            else if ( g::mouse_ptr )
            {
                paintGeometryProc( 
                    hwnd, 
                    g::color,
                    g::mouse_ptr->localPos()
                    );
            }
            break;
        default:
            return DefWindowProc( hwnd, msg, wp, lp );
    } 

    return 0;
}

LRESULT CALLBACK dinputProcAnyData(
    HWND hwnd,
    UINT msg,
    WPARAM wp,
    LPARAM lp
    )
{
    bool active = false;
    static void* any = nullptr;

    switch ( msg )
    {
        case WM_DESTROY:
            PostQuitMessage( 0 );
            break;
        case WM_CREATE:
            any = reinterpret_cast< LPCREATESTRUCT >(
                lp 
                )->lpCreateParams;
            break;
        case WM_ACTIVATE:
            switch ( wp & 0xffff )
            {
                case WA_INACTIVE: // 非アクティブ時
                    break;
                case WA_ACTIVE: // マウス以外で
                    active = true;
                    break;
                case WA_CLICKACTIVE: // マウスで
                    active = true;
                    break;
            }
           
            if ( g::dinput_ptr )
            {
                setAcquire( 
                    g::dinput_ptr->inputDevice(), 
                    active 
                    );
            }
            else if ( g::mouse_ptr )
            {
                setAcquire( 
                    g::mouse_ptr->inputDevice(), 
                    active 
                    );
            }
            break;
        case WM_CLOSE:
            SendMessage( hwnd, WM_DESTROY, wp, lp );
            break;
        case WM_PAINT:
            if ( g::dinput_ptr )
            {
                TCHAR text[16];
                for ( std::uint8_t i = 0
                      ; i < g::text.size()
                      ; ++i )
                {
                    text[i] = g::text[i];
                }
                paintTextProc( hwnd, g::color, text );
            }
            else if ( g::mouse_ptr )
            {
                std::uint8_t* tmp = 
                    reinterpret_cast<std::uint8_t*>(
                    any
                    );
                paintGeometryProc(
                    hwnd,
                    g::color,
                    reinterpret_cast<DInputMouse*>(
                        tmp + sizeof( DInputKeyboard )
                        )->localPos()
                    );
            }
            break;
        default:
            return DefWindowProc( hwnd, msg, wp, lp );
    }

    return 0;
}


Output:
1
2
3
Line 24: error: dinput_test.h: No such file or directory
Line 13: error: 'COLORREF' does not name a type
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: