如何在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_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();
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 对多个类使用一个 SDL2 渲染器
- 如何在SDL2中渲染一个点
- 尝试在另一个 SDL2 上显示一个图像没有错误,但不会显示第二个图像
- 无法用SDL2 GFX绘制一个填充的圆圈
- 创建一个可部署的CMake动态SDL2应用程序
- 创建一个包含矩形的窗口,其中包含一个带有 C++ 和 SDL2 的矩形
- SDL2 运行得太快 -- 为什么当我只按一个按钮时它会读取多个输入
- 将一个表面起泡到另一个表面上(SDL2,C++,VS2015)
- 一个在SDL2, c++中旋转纹理的方法