我的线条图很好,但线条闪烁。我想改变循环的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);
}
}