我正在尝试使用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();
}
以上是我的代码。任何建议表示赞赏,并且非常感谢帮助。
答案 0 :(得分:3)
你错过了几件事。第一个也是最重要的是你的渲染器没有被初始化,这是用SDL_CreateRenderer完成的。 现在我们准备好画在窗户上了。为此,您需要在渲染器上设置颜色(此颜色用于RenderDrawPoint和RenderDrawLine等函数)。
在绘制完你的观点之后,我们将颜色设置为之前的颜色。我选择黑色作为背景,白色作为点的颜色,你可以选择你需要的任何东西,在渲染器中设置颜色的功能是SDL_SetRenderDrawColor。
现在我们可以绘制,但在每次绘制之前你必须clear你的屏幕,对渲染器进行所有绘制调用,然后show绘制你所绘制的内容。
这是一个完整的示例,其中包含有关您缺少的部分的注释部分,我还将您的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();
}