SDL2 main


#define PrintCtx(fmt, ...)          \
    do {                            \
        printf("[%s:%d]" fmt,         \
             __FUNCTION__, __LINE__, ##__VA_ARGS__); \
    } while (0);

#if UNICODE

#define TO_STR(k) #k

const char* KeyToStr(int key)
{
    const char* keyStr = NULL;
    switch (key) {
    case SDLK_0:
        keyStr = TO_STR(SDLK_0);
        break;
    case SDLK_1:
        keyStr = TO_STR(SDLK_1);
        break;
    case SDLK_2:
        keyStr = TO_STR(SDLK_2);
        break;
    case SDLK_3:
        keyStr = TO_STR(SDLK_3);
        break;
    case SDLK_4:
        keyStr = TO_STR(SDLK_4);
        break;
    case SDLK_5:
        keyStr = TO_STR(SDLK_5);
        break;
    case SDLK_LEFT:
        keyStr = TO_STR(SDLK_LEFT);
        break;
    case SDLK_RIGHT:
        keyStr = TO_STR(SDLK_RIGHT);
        break;
    case SDLK_DOWN:
        keyStr = TO_STR(SDLK_DOWN);
        break;
    case SDLK_UP:
        keyStr = TO_STR(SDLK_UP);
        break;
    default:
        break;
    }
    return keyStr;
}

int BlitSurface(SDL_Surface* surface, SDL_Surface *pic)
{
    SDL_BlitSurface(pic, NULL, surface, NULL);
    return 0;
}

int ShowRectInfo(SDL_Rect* rect)
{
    PrintCtx("rect x:%d,y:%d,w:%d,h:%d\n", rect->x, rect->y, rect->w, rect->h);
    return 0;
}

int MotionToRect(SDL_MouseMotionEvent* motion, SDL_Rect* rect)
{
    rect->x = motion->x;
    rect->y = motion->y;
    rect->w = 10;
    rect->h = 10;
    return 0;
}

int GetWinPos(SDL_Window* window, SDL_Rect *rect)
{
    SDL_GetWindowPosition(window, &rect->x, &rect->y);
    SDL_GetWindowSize(window, &rect->w, &rect->h);
    return 0;
}

int ProcMouse(SDL_Event event, SDL_Surface *surface, SDL_Rect *rect)
{
    MotionToRect(&event.motion, rect);
    switch (event.button.button) {
    case SDL_BUTTON_LEFT:
        break;
    case SDL_BUTTON_RIGHT:
        break;
    default:
        break;
    }
    ShowRectInfo(rect);
    return 0;
}

int ProcKeyBoard(SDL_Event event, SDL_Surface* surface, SDL_Surface* pic)
{
    PrintCtx("key:%s\n", KeyToStr(event.key.keysym.sym));
    switch (event.key.keysym.sym) {
    case SDLK_LEFT:
        break;
    case SDLK_RIGHT:
        break; 
    case SDLK_DOWN:
        break;
    case SDLK_UP:
        break;
    default:    
        break;
    }
    return 0;
}

int ProcMouseMotion(SDL_Event event, SDL_Surface* surface, SDL_Rect* rect)
{
    // MotionToRect(&event.motion, rect);
    // PrintCtx("rect x:%d\n", rect->x);
    // PrintCtx("rect y:%d\n", rect->y);

    return 0;
}

int ShowWindowInfo(HWND hwd, PWCHAR title, RECT rect)
{
    PrintCtx("hwd:%p,title:%s\n", hwd, title);
    PrintCtx("hwdRect l:%d,t:%d,r:%d,b:%d\n", rect.left, rect.top, rect.right, rect.bottom);
    return 0;
}

int ProcConsole(SDL_Rect *rect)
{
    WCHAR title[256];
    RECT hwdRect = { 0 };
    HWND hwd = GetConsoleWindow();
    GetConsoleTitle(title, sizeof(title));
    GetWindowRect(hwd, &hwdRect);
    ShowWindowInfo(hwd, title, hwdRect);
    MoveWindow(hwd, rect->x + rect->w, rect->y, 600, rect->h, 1);
    return 0;
}

int ProcWindow(SDL_Event event, SDL_Surface* surface, SDL_Rect* rect)
{
    
    WCHAR title[256] = {0};

    switch (event.window.event) {
    case SDL_WINDOWEVENT_LEAVE:
        break;
    case SDL_WINDOWEVENT_ENTER:
        break;
    default:
        break;
    }
    ProcConsole(rect);
    
    return 0;
}

/* This is where execution begins [console apps, unicode] */
int
console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
{
    SDL_Surface* pic1 = NULL, * pic2 = NULL, * cur = NULL;
    SDL_Init(SDL_INIT_VIDEO);
    
    // if (argc == 2)
    {
        pic1 = SDL_LoadBMP("1.bmp");
        pic2 = SDL_LoadBMP("2.bmp");
        cur = pic1;
    }
    SDL_Window* window = SDL_CreateWindow("win", 100, 100, cur->w, cur->h, SDL_WINDOW_SHOWN);
    SDL_Surface* surface = SDL_GetWindowSurface(window);
    
    SDL_Event event;
    SDL_Rect mouseRect, winRect;
    while (1) {
        SDL_FillRect(surface, NULL, 0);
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_QUIT:
                PrintCtx("\n");
                break;
            case SDL_MOUSEBUTTONDOWN:
                ProcMouse(event, surface, &mouseRect);
                break;
            case SDL_MOUSEMOTION:
                ProcMouseMotion(event, surface, &mouseRect);
                break;
            case SDL_KEYDOWN:
                ProcKeyBoard(event, surface, pic2);
                break;
            case SDL_WINDOWEVENT:
                GetWinPos(window, &winRect);
                ProcWindow(event, surface, &winRect);
                break;
            default:
                break;
            }
        }
        SDL_BlitSurface(cur, NULL, surface, NULL);
        SDL_FillRect(surface, &mouseRect, 0);
        SDL_UpdateWindowSurface(window);
    }
    SDL_FreeSurface(surface);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值