如何在SDL2中渲染一个点

How to render a point in sdl2

本文关键字:一个 SDL2      更新时间:2023-10-16

我正在尝试使用SDL渲染一个点,而且我似乎无法获得渲染的要点。我没有在代码中遇到任何错误,并且正在编译,但是窗口上没有任何意义。

代码:

   #include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main() {
    const int windowHeight = 600;
    const int windowWidth = 800;
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        return 1;
        cout << "Initialization failed" << endl;
    }
    SDL_Window *window = SDL_CreateWindow("Practice making sdl Window",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth,
            windowHeight, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        SDL_Quit();
        return 2;
    }
    SDL_Renderer *s;
    const int pointLocationx = windowWidth/2;
    const int pointLocationy = windowHeight/2;
    SDL_RenderDrawPoint(s, pointLocationx, pointLocationy);
    bool quit = false;
    SDL_Event event;
    while (!quit) {
        //drawing particles
        //setting up objects
        //repeated over and over again
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
    }
    SDL_DestroyWindow(window);
    SDL_Quit();
}

上面是我拥有的代码。任何建议都将受到赞赏,并非常感谢。

您缺少几件事。第一个也是最重要的是,您的渲染器没有初始化,这是使用SDL_Createrer完成的。现在,我们准备好画窗户了。为此,您需要在渲染器上设置颜色(此颜色用于诸如RenderDrawPoint和RenderDrawLine等功能(。

绘制观点后,我们将把颜色设置为以前的颜色。我为背景选择了黑色,而白色则可以选择点的颜色,您可以选择所需的任何东西,在渲染器中设置颜色的功能是sdl_setrenderdrawcolor。

现在我们可以绘制,但是在每次绘制之前,您都必须清除屏幕,对渲染器进行所有抽奖电话,然后显示您绘制的内容。

这是一个完整的示例,其中评论了您所缺少的内容,我还将您的抽奖点移到了主循环中,因为在一天结束时,您可能想要它。

但是(非常罕见的用途(如果您想绘制一次并且永远不会更改屏幕上的内容,那么您可以将其带到墙外,呼叫清晰并出现一次并完成。

#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main() {
    const int windowHeight = 600;
    const int windowWidth = 800;
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        return 1;
        cout << "Initialization failed" << endl;
    }
    SDL_Window *window = SDL_CreateWindow("Practice making sdl Window",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth,
            windowHeight, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        SDL_Quit();
        return 2;
    }
    // We create a renderer with hardware acceleration, we also present according with the vertical sync refresh.
    SDL_Renderer *s = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) ;
    const int pointLocationx = windowWidth/2;
    const int pointLocationy = windowHeight/2;
    bool quit = false;
    SDL_Event event;
    while (!quit) {
        //drawing particles
        //setting up objects
        //repeated over and over again
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
        // We clear what we draw before
        SDL_RenderClear(s);
        // Set our color for the draw functions
        SDL_SetRenderDrawColor(s, 0xFF, 0xFF, 0xFF, 0xFF);
        // Now we can draw our point
        SDL_RenderDrawPoint(s, pointLocationx, pointLocationy);
        // Set the color to what was before
        SDL_SetRenderDrawColor(s, 0x00, 0x00, 0x00, 0xFF);
        // .. you could do some other drawing here
        // And now we present everything we draw after the clear.
        SDL_RenderPresent(s);
    }
    SDL_DestroyWindow(window);
    // We have to destroy the renderer, same as with the window.
    SDL_DestroyRenderer(s);
    SDL_Quit();
}