c++ sdl 根据鼠标位置绘制动态矩形

标签 c++ logic sdl selection rectangles

这对很多人来说似乎是显而易见的,但我被困住了,找不到任何对我有帮助的教程,所以如果能在这里找到答案就太好了。

我尝试制作一个像您在 RTS 游戏中那样的矩形,可以说是一个动态选择框,但它无法正常工作。 如果有人列出创建这样一个盒子的每个步骤的要点就足够了

(不要奇怪我真的是 c++ 的新手)

鼠标.h :

#ifndef MOUSE_H_
#define MOUSE_H_

class Mouse {

public:
    bool leftButtonDown = false;
    bool mouseMoves = false;

    struct MouseCoords {
        int x = -1;
        int y = -1;
    };

    MouseCoords start_coords;
    MouseCoords move_coords;
    Mouse(){}

    void setState(Uint32 eventType, SDL_Event event){

        if ( eventType == SDL_MOUSEBUTTONDOWN) {
            this->leftButtonDown = true;
            SDL_GetMouseState( &this->start_coords.x, &this->start_coords.y );
        }

        if ( eventType == SDL_MOUSEBUTTONUP ) {
            this->leftButtonDown = false;
        }

        if ( eventType == SDL_MOUSEMOTION ) {
            this->mouseMoves = true;
            this->move_coords.x = event.motion.x;
            this->move_coords.y = event.motion.y;
        } else {
            this->mouseMoves = false;
        }
    }

    /**
     * Provides coordinates when mousebutton down
     */
    MouseCoords getCoordinates(){
        return move_coords;
    }

    /**
     * Refresh the coordinates. MUST be called after Mouse::clearCoordinates();
     */
    void clearCoordinates(){
        this->move_coords.x = -1;
        this->move_coords.y = -1;

    }

    /**
     * Creates selector rect, call this in the render loop
     */
     void createSelectBox(SDL_Renderer *renderer){
         if(this->leftButtonDown) {
            Block rect(
                       this->start_coords.x, 
                       this->move_coords.y,
                       this->start_coords.y -this->move_coords.y ,
                       this->move_coords.x - this->start_coords.x
                       );
             rect.draw( renderer );
             this->clearCoordinates();
         }
     }
};

#endif

block .h

#ifndef BLOCK_H_
#define BLOCK_H_
/**
 * Standard Class which can be used to build a player, enemy, or something similar
 */

class Block{
    SDL_Rect rect;
public:
    Block(int x=10, int y=10, int w=10, int h=10, int filled=true){
        rect.x = x;
        rect.y = y;
        rect.w = w;
        rect.h = h;
    }

    /**
     * draw with the given rendererer
     */
    void draw(SDL_Renderer *renderer){
        SDL_SetRenderDrawColor( renderer , 200 , 155 , 255 , 255);
        SDL_RenderDrawRect( renderer, &rect );
        SDL_RenderPresent( renderer );
    }

    bool framed(){
        return 0;
    }

};
#endif

然后在我的 main.cpp 中

SDL_Window *window;
SDL_Renderer *renderer;
Mouse mouse;
Block rect;


void renderWindow(){
    window = SDL_CreateWindow("commanding rects", 100, 100, 700, 600, 0);
    renderer = SDL_CreateRenderer(window, -1, 0);
}

void renderGame(Mouse mouse){
    SDL_RenderSetLogicalSize( renderer, 400, 300 );
    SDL_SetRenderDrawColor( renderer, 0, 0, 0, 0);
    SDL_RenderClear( renderer );
    SDL_RenderPresent( renderer );
    mouse.createSelectBox( renderer );

}


void gameLoop(){
    bool game_run = true;
    while (game_run) {
        SDL_Event event;
        while( SDL_PollEvent(&event)){
            if ( event.type == SDL_QUIT ) {
                game_run = false;
            }
            mouse.setState(event.type, event);
        }
        renderGame(mouse);
        SDL_Delay( 15 );
    }

}


int main(){
    renderWindow();
    gameLoop();
    return 0;
}

最佳答案

我在这里发现了一个问题:

void createSelectBox(SDL_Renderer *renderer){
    if(this->leftButtonDown) {
        Block rect(this->mousedown_coords.x,
                   this->mousemove_coords.y,
                   this->mousedown_coords.y - this->mousemove_coords.y,
                   this->mousemove_coords.x - this->mousedown_coords.x);

在这段代码中,无论鼠标坐标值是多少,您都将矩形的宽度和高度设置为 0。由于您正在绘制边界框,我想您想要存储两组 x,y 坐标,一组用于按下鼠标按钮时,一组用于释放鼠标按钮时。

关于c++ sdl 根据鼠标位置绘制动态矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29735309/

相关文章:

c++ - 这是什么=!运算符(operator)?

c++ - 枚举机器上的所有 IDispatch 实现对象

c - 如何在给定范围内找到C中素数的总和?

java数字和 "*"循环模式

c++ - 创建 OpenGL 4.3 上下文会使 GLX 崩溃

c++ - 使用对数据类型的 vector 的哈希表中的段错误

c++ - 如何为类特定的 typedef (c++17) 启用自动类型推导?

mingw - 对 SDL_Init 的 undefined reference

Java 运算符检查任一条件是否为假,但不会同时检查两个条件是否为假

c++ - SDL 冲突处理