【问题标题】:Rectangle in sdl2 sets colour for entire screensdl2中的矩形为整个屏幕设置颜色
【发布时间】:2021-02-17 11:42:09
【问题描述】:

我知道在 SO 上已经有这样的问题,但我是 SDL2 的新手,我看过一些绘制矩形的教程,我也看过一些问题。使用下面的当前代码,我无法在窗口上绘制矩形,因为它没有出现我还尝试在绘制矩形后清除窗口,这只是将整个窗口设置为该颜色。

#include <iostream>
#include <SDL2\SDL.h>

int main(int argc, char** argv){
    SDL_Window* window;
    window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    SDL_Renderer* render;
    render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    bool close = false;
    SDL_Event event;
    while (close == false){
        while (SDL_PollEvent(&event)){
            if (event.type == SDL_QUIT){
                close = true;
            }
        }

        //set colour
        SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
        SDL_RenderClear(render);

        SDL_Rect rect;
        rect.x, rect.y, rect.h, rect.w = 50, 50, 50, 50;
        SDL_SetRenderDrawColor(render, 0, 0, 255, 255);
        SDL_RenderFillRect(render, &rect);
        //SDL_RenderClear(render); <-- If this is uncommented it clears the screen to blue.
        SDL_RenderPresent(render);
    }
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(render);
    SDL_Quit();
}

这不是在红色窗口上绘制蓝色矩形,但我只得到红色窗口并清除渲染器只设置蓝色窗口。为什么这个矩形不绘制?如果它正在绘制,那么它的颜色怎么会改变,所以它不会与背景融为一体。

我使用这些问题来帮助我做到这一点: How to draw a rectangle in SDL 2 and what exactly is a renderer SDL2 Wont draw Rectangles Correctly https://dev.to/noah11012/using-sdl2-drawing-rectangles-3hc2

【问题讨论】:

    标签: c++ sdl-2


    【解决方案1】:

    您的问题似乎与rect.x, rect.y, rect.h, rect.w = 50, 50, 50, 50;有关

    当我把它改成

     rect.x = 50;
     rect.y = 50;
     rect.h = 50;
     rect.w = 50;
    

    程序成功了

    【讨论】:

    • 也可以写成rect.x = 50, rect.y = 50, rect.h = 50, rect.w = 50;SDL_Rect rect{50, 50, 50, 50};
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    相关资源
    最近更新 更多