簡體   English   中英

如何在sdl2中渲染點

[英]How to render a point in sdl2

我正在嘗試使用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_CreateRenderer完成的。 現在我們准備在窗口上繪制。 為此,您需要在渲染器上設置一種顏色(該顏色用於RenderDrawPoint和RenderDrawLine等函數)。

提出您的觀點之后,我們將顏色設置為以前的顏色。 我為背景選擇了黑色,為點的顏色選擇了白色,可以選擇任何所需的顏色,在渲染器中設置顏色的功能是SDL_SetRenderDrawColor

現在我們可以繪制了,但是在每次繪制之前,您都必須清除屏幕,對渲染器進行所有繪制調用,然后顯示繪制的內容。

這是一個完整的示例,其中包含有關缺少的內容的注釋部分,我也將drawPoint移到了主循環中,因為到了最后,您可能希望它在其中。

但是(如果很少使用)如果您只想繪制一次而從不改變屏幕上的內容,則可以將其帶出牆外,調用清除並呈現一次,然后完成操作。

#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