如何使用SDL C ++在win32窗口中设置GUI按钮?

时间:2016-08-24 22:20:06

标签: c++ sdl

在SDL和游戏开发中,Iam soo新手。我想制作一个游戏,我需要在游戏窗口中放置一个“开始”按钮。所以,任何人都可以帮助我如何做到这一点? 整个屏幕中没有鼠标事件我只想创建一个按钮来点击它,我在哪里可以插入按钮的代码? 这是我的代码:

 int main (int argc, char *argv[]){
    int quit = 0;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = NULL;
    window = SDL_CreateWindow("Snaykie v1.0",350, 150, 800, 500,    SDL_WINDOW_SHOWN);
    if (window == NULL){
        std::cout << "Can not open the game" << std::endl;
        return 0;
    }
    SDL_Renderer* renderer = NULL;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_Event* evt = new SDL_Event();

    SDL_Texture* txt = NULL;
    txt = IMG_LoadTexture(renderer, "mainview.bmp");

    SDL_Rect rct;
    rct.x = 0 ;
    rct.y = 0;
    rct.h = 500;
    rct.w = 800;

    SDL_Surface *button = NULL;

    while (!quit && evt->type != SDL_QUIT){
        SDL_PollEvent(evt);
       SDL_RenderClear(renderer);
       SDL_RenderCopy(renderer, txt, NULL, &rct);
       SDL_RenderPresent(renderer);
    }
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    delete evt;
    return 0;
}

1 个答案:

答案 0 :(得分:3)

它不像一两行那么简单。这是一个例子:

#include <SDL2/SDL.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

typedef struct {
    SDL_Rect draw_rect;    // dimensions of button
    struct {
        Uint8 r, g, b, a;
    } colour;

    bool pressed;
} button_t;

static void button_process_event(button_t *btn, const SDL_Event *ev) {
    // react on mouse click within button rectangle by setting 'pressed'
    if(ev->type == SDL_MOUSEBUTTONDOWN) {
        if(ev->button.button == SDL_BUTTON_LEFT &&
                ev->button.x >= btn->draw_rect.x &&
                ev->button.x <= (btn->draw_rect.x + btn->draw_rect.w) &&
                ev->button.y >= btn->draw_rect.y &&
                ev->button.y <= (btn->draw_rect.y + btn->draw_rect.h)) {
            btn->pressed = true;
        }
    }
}

static bool button(SDL_Renderer *r, button_t *btn) {
    // draw button
    SDL_SetRenderDrawColor(r, btn->colour.r, btn->colour.g, btn->colour.b, btn->colour.a);
    SDL_RenderFillRect(r, &btn->draw_rect);

    // if button press detected - reset it so it wouldn't trigger twice
    if(btn->pressed) {
        btn->pressed = false;
        return true;
    }
    return false;
}

int main (int argc, char *argv[]){
    int quit = 0;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = NULL;
    window = SDL_CreateWindow("",350, 150, 800, 500,    SDL_WINDOW_SHOWN);
    if (window == NULL){
        fprintf(stderr, "create window failed: %s\n", SDL_GetError());
        return 1;   // 'error' return status is !0. 1 is good enough
    }

    SDL_Renderer* renderer = NULL;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if(!renderer) {   // renderer creation may fail too
        fprintf(stderr, "create renderer failed: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Texture* txt = NULL;

    SDL_Rect rct;
    rct.x = 0 ;
    rct.y = 0;
    rct.h = 500;
    rct.w = 800;

    // button state - colour and rectangle
    button_t start_button = {
        .colour = { .r = 255, .g = 255, .b = 255, .a = 255, },
        .draw_rect = { .x = 128, .y = 128, .w = 128, .h = 128 },
    };

    enum {
        STATE_IN_MENU,
        STATE_IN_GAME,
    } state = 0;

    while(!quit) {
        SDL_Event evt;    // no need for new/delete, stack is fine

        // event loop and draw loop are separate things, don't mix them
        while(SDL_PollEvent(&evt)) {
            // quit on close, window close, or 'escape' key hit
            if(evt.type == SDL_QUIT ||
                    (evt.type == SDL_WINDOWEVENT && evt.window.event == SDL_WINDOWEVENT_CLOSE) ||
                    (evt.type == SDL_KEYDOWN && evt.key.keysym.sym == SDLK_ESCAPE)) {
                quit = 1;
            }

            // pass event to button
            button_process_event(&start_button, &evt);
        }

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

//      SDL_RenderCopy(renderer, txt, NULL, &rct);

        if(state == STATE_IN_MENU) {
            if(button(renderer, &start_button)) {
                printf("start button pressed\n");
                state = STATE_IN_GAME;   // state change - button will not be drawn anymore
            }
        } else if(state == STATE_IN_GAME) {
            /* your game logic */
        }

        SDL_RenderPresent(renderer);
    }
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    return 0;
}

(这是C,但这不是重点)。它绘制一个简单的恒定颜色矩形​​并检测其上的点击。用例如矩形替换矩形是微不足道的。质地;添加文本可能有点困难,使用SDL_ttf可能是最简单的方法。

根据您的想法,它可能会在很多方面得到改善。例如。它会对鼠标按钮做出反应,而在某些情况下,您可能希望对按钮释放作出反应,并跟踪按下和释放按钮的位置。如果你有很多按钮,每个按钮上调用事件更新都很难看,所以你可能想要使用不同的系统。您可能还希望使用非矩形按钮(例如,图像指定的某种形状) - 这将需要完全不同的方法,因为您需要检查鼠标单击形状蒙版。

我建议您查看imgui

作为旁注,不要new SDL_Event,绝对没有意义。