简体   繁体   English

如何在sdl2中渲染点

[英]How to render a point in sdl2

I am trying to render a point using SDL and I cannot seem to get the point to render. 我正在尝试使用SDL渲染点,但似乎无法获得渲染点。 I am not getting any errors in the code and it is compiling however no point is appearing on the window. 我没有在代码中遇到任何错误,并且正在编译,但是窗口上没有任何显示。

Code: 码:

   #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();

}

Above is the code that I have. 上面是我的代码。 Any advice is appreciated and help is super appreciated. 任何建议,赞赏和帮助是超级赞赏。

You're missing several things. 您缺少几件事。 The first and most important is that your renderer is not initialized, that is done with SDL_CreateRenderer . 首先也是最重要的一点是渲染器未初始化,这是通过SDL_CreateRenderer完成的。 Now we're ready to draw onto the window. 现在我们准备在窗口上绘制。 For this you'll need to set a color on your renderer (this color is used for functions like RenderDrawPoint and RenderDrawLine, etc). 为此,您需要在渲染器上设置一种颜色(该颜色用于RenderDrawPoint和RenderDrawLine等函数)。

After drawing your point we'll set the color to what was before. 提出您的观点之后,我们将颜色设置为以前的颜色。 I chose black for the background and white for the point's color, you can choose whatever you need, the function for setting the color in the renderer is SDL_SetRenderDrawColor . 我为背景选择了黑色,为点的颜色选择了白色,可以选择任何所需的颜色,在渲染器中设置颜色的功能是SDL_SetRenderDrawColor

Now we can draw, but before every draw you have to clear your screen, make all the draw calls to the renderer and then show what you have drawn. 现在我们可以绘制了,但是在每次绘制之前,您都必须清除屏幕,对渲染器进行所有绘制调用,然后显示绘制的内容。

Here is a full example with the commented parts on what you were missing, I also moved your drawPoint inside your main loop, since at the end of the day is where you probably want it to be. 这是一个完整的示例,其中包含有关缺少的内容的注释部分,我也将drawPoint移到了主循环中,因为到了最后,您可能希望它在其中。

BUT (very rare use) if you want to draw once and never change whats on screen again, then you could take it outside the wall, call clear and present once and be done with it. 但是(如果很少使用)如果您只想绘制一次而从不改变屏幕上的内容,则可以将其带出墙外,调用清除并呈现一次,然后完成操作。

#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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM