Написал простой пример рисования желтого квадрата. Пытаюсь реализовать движение квадрата при удерживание нажатой клавиши стрелки влево. Проблема в том, что при удерживание клавиши стрелки влево квадрат не движится, а если нажимать и отпускать клавишу, то квадрат перемещается нормально. Вопрос в следующем, как сделать так, чтобы квадрат двигался при зажатой клавиши стрелки влево?
Код примера:
#include "SDL\SDL.h"
SDL_Rect init_new_rect(int h, int w, int x, int y);
struct colors init_clr();
struct colors {
int r;
int g;
int b;
};
SDL_Rect init_new_rect(int h, int w, int x, int y) {
SDL_Rect sDim;
sDim.h = h;
sDim.w = w;
sDim.x = x;
sDim.y = y;
return sDim;
}
int main( int argc, char* args[] ) {
SDL_Surface* display = NULL;
SDL_Rect tmpDim;
int x0, y0;
int rect_h = 50;
int rect_w = 50;
struct colors yellow_clr;
yellow_clr.r = 255;
yellow_clr.g = 255;
yellow_clr.b = 0;
x0 = 640/2;
y0 = 480/2;
SDL_Init( SDL_INIT_EVERYTHING );
display = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
int quit = 0;
SDL_Event event;
int dx = 0;
while( !quit )
{
if( SDL_PollEvent( &event ) )
{
if( event.type == SDL_KEYDOWN ) {
switch( event.key.keysym.sym ) {
case SDLK_ESCAPE: quit = 1; break;
case SDLK_LEFT: dx -= 2; break;
default : break;
}
}
else if( event.type == SDL_QUIT )
{
quit = 1;
}
}
SDL_FillRect(display, NULL, 0);
tmpDim = init_new_rect(rect_h, rect_w, x0 - rect_h/2 + dx, y0 - rect_w/2);
SDL_FillRect(display, &tmpDim, SDL_MapRGB(display->format, yellow_clr.r, yellow_clr.g, yellow_clr.b));
SDL_Delay(SDL_TIMESLICE);
SDL_Flip(display);
}
return 0;
}