#include <DxLib.h>

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    ChangeWindowMode(TRUE);
    SetGraphMode(640, 480, 32);
    SetMainWindowText(L"C++ Basics");
    if (DxLib_Init() == -1)
        return -1;

    SetDrawScreen(DX_SCREEN_BACK);
    SetBackgroundColor(245, 248, 252);

    int image = LoadGraph(L"Assets/Images/Robot.png");
    if (image == -1)
    {
        MessageBoxW(nullptr, L"Assets/Images/Robot.png を読み込めません。", L"画像の確認", MB_OK);
        DxLib_End();
        return -1;
    }

    int x = 160;
    int y = 120;

    int walker = LoadGraph(L"Assets/Images/Walker.png");
    if (walker == -1)
    {
        MessageBoxW(nullptr, L"Assets/Images/Walker.png を読み込めません。", L"画像の確認", MB_OK);
        DeleteGraph(image);
        DxLib_End();
        return -1;
    }

    int walkerX = 320;
    int walkerY = 300;
    int speed = 2;

    while (ProcessMessage() == 0 && CheckHitKey(KEY_INPUT_ESCAPE) == 0)
    {
        if (CheckHitKey(KEY_INPUT_LEFT) != 0)
            x = x - 3;
        if (CheckHitKey(KEY_INPUT_RIGHT) != 0)
            x = x + 3;
        if (CheckHitKey(KEY_INPUT_UP) != 0)
            y = y - 3;
        if (CheckHitKey(KEY_INPUT_DOWN) != 0)
            y = y + 3;

        if (x < 0)
            x = 0;
        if (x > 640 - 48)
            x = 640 - 48;
        if (y < 0)
            y = 0;
        if (y > 480 - 48)
            y = 480 - 48;

        walkerX = walkerX + speed;
        if (walkerX > 640 - 48)
        {
            walkerX = 640 - 48;
            speed = -speed;
        }
        if (walkerX < 0)
        {
            walkerX = 0;
            speed = -speed;
        }

        ClearDrawScreen();
        DrawGraph(x, y, image, TRUE);
        DrawGraph(walkerX, walkerY, walker, TRUE);
        ScreenFlip();
        WaitTimer(16);
    }

    DeleteGraph(walker);
    DeleteGraph(image);
    DxLib_End();
    return 0;
}
