SDL program error

Hello, world!
I was in the process of building a simple program in SDL which has a picture of an up, down, left or right facing arrow key (moving in the relevant direction) after the user has pressed the relevant key, until another key is pressed or it reaches the edge of the screen. The problem: when a key is pressed, it remains stuck on that key forever, in addition to stopping me from quitting (I have to use System Monitor to terminate the process).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <string>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

using namespace std;

SDL_Surface* screen = NULL;
SDL_Surface* background = NULL;

bool quit = false;

bool init()
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return 1;
    }
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    if(screen==NULL)
    {
        return 1;
    }
    return 0;
}

SDL_Surface* load_image(string filename)
{
    SDL_Surface *tempImageOne = NULL, *tempImageTwo = NULL;
    tempImageOne = IMG_Load(filename.c_str());
    tempImageTwo = SDL_DisplayFormat(tempImageOne);
    SDL_FreeSurface(tempImageOne);
    if(tempImageTwo != NULL)
    {
        Uint32 colourkey = SDL_MapRGB(tempImageTwo->format, 255, 255, 255);
        SDL_SetColorKey(tempImageTwo, SDL_SRCCOLORKEY, colourkey);
    }
    return tempImageTwo;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source, clip, destination, &offset);
}

int main ( int argc, char** argv )
{
    SDL_Surface* moveBlock = NULL;
    SDL_Event event;
    bool quit = false;
    if(init() == -1)
    {
        return 1;
    }
    SDL_Surface* directionBlockOne = load_image("Up.png");
    SDL_Surface* directionBlockTwo = load_image("Right.png");
    SDL_Surface* directionBlockThree = load_image("Down.png");
    SDL_Surface* directionBlockFour = load_image("Left.png");
    SDL_Surface* directionBlockActive = NULL;
    Uint32 x = 320, y = 240;
    SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB( screen->format, 245, 255, 0));
    while(quit == false)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type==SDL_KEYDOWN)
            {
                while(event.type==SDL_KEYDOWN)
                {
                    switch(event.key.keysym.sym)
                    {
                    case SDLK_UP:
                        directionBlockActive = directionBlockOne;
                        if(y>0)
                        {
                            --y;
                        }
                        apply_surface(x, y, directionBlockActive, screen);
                        if(SDL_Flip(screen) == -1)
                        {
                            return 1;
                        }
                        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 245, 255, 0));
                        SDL_Delay(20);
                        break;
                    case SDLK_RIGHT:
                        directionBlockActive = directionBlockTwo;
                        if(x<576)
                        {
                            ++x;
                        }
                        apply_surface(x, y, directionBlockActive, screen);
                        if(SDL_Flip(screen) == -1)
                        {
                            return 1;
                        }
                        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 245, 255, 0));
                        SDL_Delay(20);
                        break;
                    case SDLK_DOWN:
                        directionBlockActive = directionBlockThree;
                        if(y<416)
                        {
                            ++y;
                        }
                        apply_surface(x, y, directionBlockActive, screen);
                        if(SDL_Flip(screen) == -1)
                        {
                            return 1;
                        }
                        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 245, 255, 0));
                        SDL_Delay(20);
                        break;
                    case SDLK_LEFT:
                        directionBlockActive = directionBlockFour;
                        if(x>0)
                        {
                            --x;
                        }
                        apply_surface(x, y, directionBlockActive, screen);
                        if(SDL_Flip(screen) == -1)
                        {
                            return 1;
                        }
                        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 245, 255, 0));
                        SDL_Delay(20);
                        break;
                    }
                }
            }
            else if(event.type==SDL_QUIT)
            {
                quit = true;
            }
        }
    }
    SDL_QUIT;
    return 0;
}


Could anyone explain how to fix this (probably very stupid mistake), please?
Thanks in advance!
Change while to if on line 70.
I've just tried it. It works, but now I have to press the arrow key to move it one pixel across and it does not carry on moving by itself. How would I modify it such that I can still change the direction of the arrow (or quit) as the program is running, but the picture of the arrow will also continue to move in the relevant direction after I have let go of the key?
Topic archived. No new replies allowed.