How is it possible that this code is escaping from the loop????

Hi, I've been stuck on this problem for about 2 weeks and have refused to post my code until now, please help.

Somewhere below there is a problem that means that the program is able to 'escape' from the while loop without quit = true and then return 0.

I have cut this code down so much to try and find the problem but its still in there somewhere

Cheers for any help.

edit: also i realise this is a massive chunk of code and if you dont wanna look through it do you know of any usual reasons code escapes from a loop??? Thanks

edit edit: Just spent the last 40mins reducing the code even further its pretty simple now and i still cant see this damn problem.

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
const int SCREEN_WIDTH = 1290;
const int SCREEN_HEIGHT = 700;
const int SCREEN_BPP = 32;

SDL_Event event;

SDL_Rect backgroundrect;

bool init(){

    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) return false;

    if( (screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE )) == NULL ) return false;

    SDL_WM_SetCaption( "Blablabla", NULL );

    if( TTF_Init() == -1 ) return false;

    return true;
}

void clean_up(){

    SDL_FreeSurface( background );
    SDL_FreeSurface ( manleft );
    SDL_FreeSurface( manright );
    SDL_FreeSurface( bullet );
    SDL_FreeSurface( explosion );
    SDL_FreeSurface( monsters );
    SDL_FreeSurface( lifeindicator );
    SDL_FreeSurface( wallpic );

    SDL_Quit();
}

int main( int argc, char* args[] ){

    bool quit = false, moveright = false, moveleft = false, right = true;
    backgroundrect.w = 1290;  backgroundrect.h = 700;    backgroundrect.x = 0;    backgroundrect.y = 0;
    uint16_t mousex = 0, mousey = 0;
    int xpos = 500, ypos = 300;

    if( init() == false ) return 1;
    if( load_files() == false ) return 1;

    apply_surface( 0, 0, background, screen, &backgroundrect );
    apply_surface( xpos, ypos, manright, screen );


    while( quit == false ){//this is the loop the program somehow manages to escape from

        while( SDL_PollEvent( &event ) ){//I doubt there's a problem in events
            if( event.type == SDL_KEYDOWN){
                if(event.key.keysym.sym == SDLK_d){ moveright = true; }

                if(event.key.keysym.sym == SDLK_a){ moveleft = true; }

            }
            if(event.type == SDL_KEYUP){
                if(event.key.keysym.sym == SDLK_d){  moveright = false;  }

                if(event.key.keysym.sym == SDLK_a){  moveleft = false;  }
            }

            if(event.type == SDL_MOUSEMOTION){
                mousex = event.motion.x; mousey = event.motion.y; // this is the only place i can think where the problem would be
            }


            if( event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE) quit = true;
        }

        ///MOVEMENT AND BACKGROUND SCROLLING

        if(moveright == true){
            xpos += 9;
        }
        else if(moveleft == true) xpos += -9;
        else xpos += 0;


        if( mousex > xpos + 14 ) right = true;//sets the direction of the character depending on where the mouse is. 
        else right = false;


        ///BLITTING
        apply_surface( 0, 0, background, screen, &backgroundrect );

        if(right == true){//prints the character facing either left or right
            apply_surface( xpos, ypos, manright, screen );
        }
        else apply_surface( xpos, ypos, manleft, screen );

        if( SDL_Flip( screen ) == -1 ) return 12;
    }
    clean_up();

    return 0;
}
Last edited on
if( event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE) quit = true;

This is the only piece of code that should be able to allow the program from escaping the loop.
Trying to commentate it out and see if it still exits is the only advice I can give.
closed account (zwA4jE8b)
if you can set breakpoints then set one on line 70 and see which event is evaluating to true.
if you use MS VS you can set the watch. I don't know if you can watch your specific variables though.
I haven't had time to extensively test it Warnis but it appears that the problem might be with SDLK_ESCAPE triggering at random points.

I thought i'd tried that. Does anyone have any information on this sort of thing? I had a quick google but nothing.

Also found another problem with events the other day: My mute button does the same as the d key!:S

Are there lots of things like this in the SDL libraries?
SDL_EVENT is a union.

If you don't know what a union is, read about it before continuing.

On line 70 you access the key member of the union when the event is not necessarily a key event (it could be any event), therefore the value you are going to get will be nonsense.
Last edited on
Haha i think thats a little above me. It goes against everything i know about to computing to say different data types hold the same memory space and modifying one modifies them all.

I just can't see a use for it from where im sitting but im sure i'll get there one day.

But thanks for the explanation at least now i know what to come back to.:)
It's not actually that complicated, but it's a C thing, that is becoming less and less useful, so many new programmers are not introduced to it.

In terms of your problem think about it like this:

You can only access event.key if event.type is SDL_KEYDOWN or SDL_KEYUP. if event.type is anything else then event.key will be nonsense.

You can only access event.resize if eevent.type is SDL_VIDEORESIZE. If event.type is anything else, then event.resize will be garbage.

etc...

On line 70 you access event.key, but you haven't checked what event.type is.
Topic archived. No new replies allowed.