我在SDL_Rect上遇到了一个奇怪的错误。每当我用这个矩形调用SDL_RenderFillRect时:
SDL_Rect rect = {100, 100, 100, 100};
我总是以覆盖整个800 x 600窗口的矩形结束。
这是我的计划:
#include "SDL.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <string>
class Window
{
public:
Window( std::string title, int width, int height );
void runEvents( SDL_Event event );
void drawRect( int x, int y, int h, int w );
bool closed = false;
SDL_Renderer *_renderer;
SDL_Window *_window;
private:
int colour = 0;
};
Window::Window( std::string title, int width, int height )
{
_window = SDL_CreateWindow( title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0 );
_renderer = SDL_CreateRenderer( _window, -1, 0 );
}
// this function deals with rects (it's supposed to draw a rect when the mouse button is down):
void Window::runEvents( SDL_Event event )
{
SDL_Rect rect = { 100, 100, 100, 100 };
switch( event.type )
{
case SDL_QUIT:
closed = true;
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE:
closed = true;
break;
case SDLK_KP_1:
// set colour to blue
SDL_SetRenderDrawColor( _renderer, 0, 51, 204, 255 );
std::cout << "Colour set to blue!" << std::endl;
colour = 1;
break;
case SDLK_KP_2:
// set colour to red
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
std::cout << "Colour set to red!" << std::endl;
colour = 2;
break;
case SDLK_KP_3:
// set colour to green
SDL_SetRenderDrawColor( _renderer, 51, 204, 51, 255 );
std::cout << "Colour set to green!" << std::endl;
colour = 3;
break;
case SDLK_KP_4:
SDL_SetRenderDrawColor( _renderer, 153, 0, 153, 255 );
std::cout << "Colour set to purple!" << std::endl;
colour = 4;
break;
case SDLK_KP_0:
SDL_SetRenderDrawColor( _renderer, 255, 255, 255, 255 );
std::cout << "Colour set to white!" << std::endl;
colour = 0;
break;
}
break;
//this draws the rectangle:
case SDL_MOUSEBUTTONDOWN:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
drawRect( rect.h, rect.y, rect.h, rect.w );
break;
//this removes it
case SDL_MOUSEBUTTONUP:
switch( colour )
{
case 1:
SDL_SetRenderDrawColor( _renderer, 0, 51, 204, 255 );
break;
case 2:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
break;
case 3:
SDL_SetRenderDrawColor( _renderer, 51, 204, 51, 255 );
break;
case 4:
SDL_SetRenderDrawColor( _renderer, 153, 0, 153, 255 );
break;
case 0:
SDL_SetRenderDrawColor( _renderer, 255, 255, 255, 255 );
break;
}
drawRect( rect.h, rect.y, rect.h, rect.w );
break;
}
}
//this function is supposed to draw the rect
void Window::drawRect( int x, int y, int h, int w )
{
SDL_Rect rect = { x, y, w, h };
SDL_RenderFillRect( _renderer, &rect );
}
bool running = true;
void mouseCheck()
{
while( running )
{
int x;
int y;
SDL_GetMouseState( &x, &y );
printf( "Mouse is at x: %i and y: %i\n", x, y );
SDL_Delay( 5000 );
}
}
int main( int argc, char *argv[] )
{
SDL_Init( SDL_INIT_EVERYTHING );
Window window( "Hello World", 800, 600 );
SDL_Event event;
SDL_SetRenderDrawColor( window._renderer, 255, 255, 255, 255 );
std::thread mousePosition( mouseCheck );
mousePosition.detach();
while( !window.closed )
{
SDL_RenderClear( window._renderer );
SDL_RenderPresent( window._renderer );
if( SDL_PollEvent( &event ) )
{
window.runEvents( event );
}
}
return 0;
}
答案 0 :(得分:1)
多个问题:
现在您的主要绘制循环是:
my_dict = dict.fromkeys(set(list_a), []) # my_dict = {0: [], 1: []}
my_dict[0].append(1) # my_dict = {0: [1], 1: [1]}
你想:
1. Draw
2. Clear
3. Present
不确定您使用1. (re)Set clear color
2. Clear
3. Draw
4. Present
线程尝试了什么,但避免从其他线程调用与GUI相关的SDL函数。如果你需要在另一个线程中使用该信息,请使用线程安全的原子/队列/消息进行通信。
您在mouseCheck()
作为第一个参数而非drawRect()
的两个地方呼叫rect.h
。
我建议您不要像rect.x
那样进行交错输入处理和渲染。在排空事件队列(runEvents()
)时更新系统状态(shouldDrawRect
&amp; colour
),然后绘制具有新状态(while(SDL_PollEvent())
)的帧。
您每帧只处理一个事件。通过Window::drawScene()
每帧排空事件队列,以便不会备份和删除事件。
所有在一起:
while(SDL_PollEvent())