gpt4 book ai didi

c++ - 绘图线在 SDL2 C++ 中闪烁。如何修改我的游戏循环?

转载 作者:行者123 更新时间:2023-11-28 04:51:55 25 4
gpt4 key购买 nike

我的线条画得很好,但线条闪烁。我想改变我循环的 FPS,但我想我只需要在另一行中添加我的 RenderPresent() 代码或添加一些新代码来渲染该行。我尝试了很多可能性,但没有任何效果。

我能做些什么来阻止线路闪烁?如果您需要更多信息,请发表评论。

// Global variables
SDL_Event event; // Event object
int mouse_x = 0; // Actual Mouse Coordinate X
int mouse_y = 0; // Actual Mouse Coordinate Y
int mouse_last_x = 0; // The coordinate X by last click
int mouse_last_y = 0; // The coordinate Y by last click
bool dragged = false; // Boolean after I clicked on some place
bool running = true; // Makes the game loop run

void GameWin::loop() //Game Loop
{
while (running)
{
SDL_GetMouseState(&mouse_x, &mouse_y); // Get Mouse Coordninates
SDL_PollEvent(&event);

SDL_SetRenderDrawColor(GameWin::renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); // Draw The Background Black

update();
render();

switch (event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEBUTTONDOWN: // Mouse Click Event
if (event.button.button == SDL_BUTTON_LEFT)
{
mouse_last_x = mouse_x; // After Left Click Save my Mouse Coordingates
mouse_last_y = mouse_y;
dragged = true;
}
break;
case SDL_MOUSEMOTION:
if (dragged)
{
SDL_SetRenderDrawColor(GameWin::renderer, 137, 255, 85, SDL_ALPHA_OPAQUE); // Set The Color Line To Green
SDL_RenderDrawLine(GameWin::renderer, mouse_last_x, mouse_last_y, mouse_x, mouse_y); // Draw The Line
SDL_RenderPresent(GameWin::renderer); // Render Line
}
break;
case SDL_MOUSEBUTTONUP:
if (dragged)
{
dragged= false;
mouse_last_x = mouse_x;
mouse_last_y = mouse_y;
}
break;
default:
break;
}
}
}

// My render method
void GameWin::render()
{
SDL_RenderClear(GameWin::renderer);
SDL_RenderPresent(GameWin::renderer);
}

最佳答案

首先,您根本不检查是否有任何事件发生。您调用 SDL_PollEvent,然后处理事件 - 它可能没问题,也可能完全错误,因为无法保证队列中有事件。如果队列为空,SDL_PollEvent 返回 0 - 这意味着它没有用有意义的数据填充您的事件结构。

其次,不要将事件处理与绘图结合起来。获取迭代之间发生的所有事件,然后绘制一次。

基本上应该是这样的:

#include "SDL.h"

// Global variables
SDL_Event event; // Event object
int mouse_x = 0; // Actual Mouse Coordinate X
int mouse_y = 0; // Actual Mouse Coordinate Y
int mouse_last_x = 0; // The coordinate X by last click
int mouse_last_y = 0; // The coordinate Y by last click
bool dragged = false; // Boolean after I clicked on some place
bool running = true; // Makes the game loop run

int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *w = NULL;
SDL_Renderer *renderer = NULL;
SDL_CreateWindowAndRenderer(640, 480, 0, &w, &renderer);

while (running)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);

SDL_GetMouseState(&mouse_x, &mouse_y); // Get Mouse Coordninates
while(SDL_PollEvent(&event)) {
switch (event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEBUTTONDOWN: // Mouse Click Event
if (event.button.button == SDL_BUTTON_LEFT)
{
mouse_last_x = mouse_x; // After Left Click Save my Mouse Coordingates
mouse_last_y = mouse_y;
dragged = true;
}
break;
case SDL_MOUSEBUTTONUP:
if (dragged)
{
dragged= false;
mouse_last_x = mouse_x;
mouse_last_y = mouse_y;
}
break;
default:
break;
}
}

if(!running) break;

if (dragged)
{
SDL_SetRenderDrawColor(renderer, 137, 255, 85, SDL_ALPHA_OPAQUE); // Set The Color Line To Green
SDL_RenderDrawLine(renderer, mouse_last_x, mouse_last_y, mouse_x, mouse_y); // Draw The Line
}

SDL_RenderPresent(renderer);
}
}

关于c++ - 绘图线在 SDL2 C++ 中闪烁。如何修改我的游戏循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48020984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com