SDL获取手柄按键消息不全,十字按键获取不到

代码能获取到A,B,Y,X按键的消息,但是用同样的函数获取不到十字按键的四个按键消息

#include <SDL_ttf.h>
#include <iostream>
#include <SDL.h>
#include <windows.h>
#include <string>
//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
using namespace std;
int y = 0;
typedef struct message
{
    const char* Number_audio_devices;
    const char* audioDeviceName;
    const char* keycode;
    int x, y;
} message;
const char* ConcatenateStrings(const char* str1, const char* str2) 
{
    char* result = new char[strlen(str1) + strlen(str2) + 1];
    strcpy(result, str1);
    strcat(result, str2);
    return result;
}
void draw_text(SDL_Renderer* renderer,SDL_Window* win,const char* text)
{
    y=y+20;
    if (y > 500)
    {
        y = 0;       
        SDL_RenderClear(renderer);
    }
    TTF_Font* font = TTF_OpenFont("C:\\Windows\\Fonts\\simfang.ttf",20);//打开字体
    if (!font)
    {
        SDL_Log("TTF_OpenFont failed :%s", TTF_GetError());
        return;
    }
    SDL_Surface* txt_surf = TTF_RenderUTF8_Blended(font, text, { 255,255,255 });//渲染字体
    SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, txt_surf);
    SDL_Rect renderRect = { 0, y, txt_surf->w, txt_surf->h };  // 设置渲染坐标和大小
    SDL_RenderCopy(renderer, textTexture, NULL, &renderRect);
    SDL_RenderPresent(renderer);
    SDL_FreeSurface(txt_surf);
    SDL_DestroyTexture(textTexture);
    TTF_CloseFont(font); 
}
int main()
{   
    message message = {};
    // 初始化SDL
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO| SDL_INIT_GAMECONTROLLER) < 0) {
        std::cerr << "SDL initialization failed: " << SDL_GetError() << std::endl;
        return 1;
    }
    // 创建窗口
    SDL_Window* window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
    if (!window) {
        std::cerr << "Failed to create SDL window: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }
    SDL_Rect rect_ = { 0,0,0,0 };
    int x, y;
    SDL_GetWindowPosition(window, &x, &y);
    rect_.x = x;
    rect_.y = y;
    std::cout << "Window position: (" << x << ", " << y << ")" << std::endl;
    // 获取窗口大小
    int width, height;
    SDL_GetWindowSize(window, &width, &height);
    rect_.w = width;
    rect_.h = height;
    std::cout << "Window size: " << width << " x " << height << std::endl;

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (TTF_Init() < 0)
    {
        SDL_Log("TTF_Init failed :%s", TTF_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 0;
    }
    // 获取键盘输入
    SDL_Event event;
    bool quit = false;
    y = 0;
    int audioDeviceCount = SDL_NumJoysticks(); // 获取手柄设备数量
    std::string audioDeviceCountStr = std::to_string(audioDeviceCount);// 使用 std::to_string 将 int 转换为字符串
    message.Number_audio_devices = ConcatenateStrings("Devoice number:", audioDeviceCountStr.c_str());// 将字符串的 c_str() 赋给 Number_audio_devices 
    draw_text(renderer,window, message.Number_audio_devices);        //发送到窗口渲染
    int i = 0;
    SDL_Joystick* controller_handle=NULL;
    for ( i = 0; i < audioDeviceCount; ++i) {// 获取手柄设备信息
        if (SDL_IsGameController(i)) {
            SDL_GameController* controller = SDL_GameControllerOpen(i);
            controller_handle= SDL_GameControllerGetJoystick(controller);
            
            if (controller) {
                message.audioDeviceName = SDL_GameControllerName(controller);//获取手柄型号
                draw_text(renderer, window,message.audioDeviceName); // 发送到窗口渲染
            }
        }
    }
    while (!quit) {
        //获取键鼠信息
        while (SDL_PollEvent(&event) != 0) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
            else if (event.type == SDL_MOUSEBUTTONDOWN) 
            {
                message.x = event.button.x;
                message.y = event.button.y;              
                std::string x = std::to_string(message.x);// 使用 std::to_string 将 int 转换为字符串
                draw_text(renderer, window, ConcatenateStrings("X:", x.c_str()));        //发送到窗口渲染
                std::string y = std::to_string(message.y);// 使用 std::to_string 将 int 转换为字符串
                draw_text(renderer, window, ConcatenateStrings("Y:", y.c_str()));        //发送到窗口渲染
            }
            else if (event.type == SDL_KEYDOWN) 
            {
                message.keycode = SDL_GetKeyName(event.key.keysym.sym);
                draw_text(renderer, window, SDL_GetKeyName(event.key.keysym.sym)); // 发送到窗口渲染
            }

            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_A))
            {
                draw_text(renderer, window, "BUTTON_A"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_B))
            {
                draw_text(renderer, window, "BUTTON_B"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_X))
            {
                draw_text(renderer, window, "BUTTON_X"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_Y))
            {
                draw_text(renderer, window, "BUTTON_Y"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_DPAD_UP))
            {
                draw_text(renderer, window, "DPAD_UP"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_DPAD_DOWN))
            {
                draw_text(renderer, window, "DPAD_DOWN"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_DPAD_LEFT))
            {
                draw_text(renderer, window, "DPAD_LEFT"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_DPAD_RIGHT))
            {
                draw_text(renderer, window, "DPAD_RIGHT"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_START))
            {
                draw_text(renderer, window, "BUTTON_START"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_BACK))
            {
                draw_text(renderer, window, "BUTTON_BACK"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_GUIDE))
            {
                draw_text(renderer, window, "BUTTON_GUIDE"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_START))
            {
                draw_text(renderer, window, "BUTTON_START"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_LEFTSTICK))
            {
                draw_text(renderer, window, "BUTTON_LEFTSTICK"); // 发送到窗口渲染
            }
            if (SDL_JoystickGetButton(controller_handle, SDL_CONTROLLER_BUTTON_RIGHTSTICK))
            {
                draw_text(renderer, window, "BUTTON_RIGHTSTICK"); // 发送到窗口渲染
            }
        }    
    }
    // 清理SDL资源
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值