Issue with while loop in SDL

Hi there,

I'm in the progress of building a tic tac toe game. I have the game fully working up to the point of finishing.

When a player wins an images pops up of who has won X or O, but the problem is it flash on and off.

I guess its still running through the check whose won loops.

Could anyone please have a look at the code and tell me why when I add a while loop to stop the flashing image the screen then goes black.

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "CApp.h"

bool Winner = false;

void CApp::OnLoop(){

int emptytiles = 0;


    while(Winner == false){
        for(int j = 0; j < 9; j++){
            if(Grid[0] == GRID_TYPE_O && Grid[1] == GRID_TYPE_O && Grid[2] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
                SDL_Flip(Surf_Display);
                OnFinished();



            }else if(Grid[0] == GRID_TYPE_X && Grid[1] == GRID_TYPE_X && Grid[2] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[3] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[5] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);




            }else if(Grid[3] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[5] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[6] == GRID_TYPE_O && Grid[7] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[6] == GRID_TYPE_X && Grid[7] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[0] == GRID_TYPE_O && Grid[3] == GRID_TYPE_O && Grid[6] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[0] == GRID_TYPE_X && Grid[3] == GRID_TYPE_X && Grid[6] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[1] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[7] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[1] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[7] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[2] == GRID_TYPE_O && Grid[5] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[2] == GRID_TYPE_X && Grid[5] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

            if(Grid[0] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[0] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[2] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[6] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);




            }else  if(Grid[2] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[6] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }
        }

            //Draw
           for(int k = 0; k <9; k++){
                if(Grid[k] == GRID_TYPE_NONE){
                    emptytiles++;
                }
            }

            if(emptytiles == 0){
                CSurface::OnDraw(Surf_Display, Surf_Draw, 200, 200, NULL, NULL, 200, 200);

            }
    }
}

void CApp::OnFinished(){
    SDL_Event event;
    Winner = true;

        if( SDL_PollEvent( &event ) ){

            //If a key was pressed
            if( event.type == SDL_KEYDOWN ){
                Reset();
            }
        }
}
Last edited on
If someone could just have a quick lok and point me in the right direction that would be great.

Would love to finish my first project.
have you tried while winner!= true
Sadly that still causes a black screen when I run the program.

Thanks for the help though.
what is supposed to appear on the screen afterwards? is the image gone but the rect still there, that sort of thing??
It should be a 3 x 3 grid, if I remove the while statement then the image shows.

It's weird.
o would have to set sdl up to help, if you put sdl in the title asking for help in general c++ your more likley to find someone to help, theres this guy called peter who automaticly seems to understand all things sdl
Great thanks for the info.
There are a lot of functions that we don't know what they are doing but I will try to help you.

Why are SDL_Flip and OnFinished only called from the first if statement? I don't see SDL_Flip being called anywhere else in your program. SDL_Flip is needed to make the things you draw visible on the screen.

There are a lot of if statements doing the same thing. It's always good to avoid code repetition because otherwise it's easy to make mistakes and to forget to update everywhere when making changes.

emptytiles is never set to 0 inside the loop so emptytiles will just grow bigger and bigger so line 139 will never be get executed (except if there was no empty tiles when OnLoop was called).

I don't see that you update the grid anywhere in your program. If the grid is never updated there is no possibility that OnFinished will gets called, setting Winner to true so that the loop stops.
Last edited on
This is only one file from the whole project, it is spread into seperate files for rendering, event checking etc.

Sorry yeah I just realised about SDL_FLip, this is because I was trying to make it work on one if statement before the rest. Like I said before the game works fine until I add a while loop that's why I've only changed it on one.

Yeh I couldn't work out a function that could check all the grid positions for all the different possible results, so this seemed the most straight forward at the time.

I set emptytiles at the beginning of OnLoop(), it seems to work as I can get a Draw result with the correct Image being displayed. Ofcourse that is when I don't have a while loop in place so it flashes.

The Grid is updated in a seperate file, but On~Finished does get called as after a win I can reset the grid to empty again by pressing any key.

Thanks for helping, I really want to move on, but it doesn't seem right until I understand what has happened here.
This is from sdltutorials.com, right? I don't think OnLoop() is supposed to contain an infinite loop. It's just supposed to update the state of the game and then return. You should probably do all the drawing inside the OnRender function instead.
Yes it's from there. At the moment I'm moving the Drawing to OnRender, but do I leave the if statements to check for a winner in OnLoop
I've cracked it!!

I added a variable that changed number depending on what the result was. Then it displays the correct winning image to match the winning number.

This then is reset with the OnFinished function.

My only problem now is I can't make my images background go transparent lol.

Thanks for all the help.
Topic archived. No new replies allowed.