SDL Problem Mouse buttons

I try to make this program, where basically you have a box in the screen that says win or lose ? And once you click it win would show, but when I compile it in CodeBlock it runs but nothing shows.

heres the code
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//The headers
#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<string>

//screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//set clip sprite
const int question = 0;
const int winner = 1;
const int loser = 2;

//event structutre
SDL_Event event;

//SDl surfaces
SDL_Surface* image = NULL;
SDL_Surface* screen = NULL;

//the clip regions of the sprite
SDL_Rect clips [ 3 ];

class Button
{
    private:
    //The attributes of the button
    SDL_Rect box;

    //The part of the sprite being shown
    SDL_Rect* clip;

    public:
    //Initlize the variable
    Button(int x, int y, int w, int h);

    //Handle events and set the buttons sprite region
    void handle_events();

    //shows the button on the screen
    void show();
};

SDL_Surface* load_image( std::string filename )
{
    //temporary hold image
    SDL_Surface* loadedImage = NULL;

    //hold optimizedImage
    SDL_Surface* optimizedImage = NULL;

    //load image
    loadedImage = IMG_Load( filename.c_str() );

    //if no error
    if ( loadedImage != NULL )
    {
        //optimize Image
        optimizedImage = SDL_DisplayFormat ( loadedImage );

        //free up old surface
        SDL_FreeSurface ( loadedImage );

        //if no errors
        if ( optimizedImage != NULL )
        {
            //set color map
            SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0xFF, 0xFF, 0xFF) );
        }
    }

    //if no errors
    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
    //rect to hold offset
    SDL_Rect offset;

    //assign offset
    offset.x = x;
    offset.y = y;

    //blit
    SDL_BlitSurface(source, clip, destination, &offset);
}

bool init()
{
    //initilize
    if ( SDL_Init( SDL_INIT_EVERYTHING) ==  -1)
    {
        return false;
    }

    //set screen
    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);

    //if no erors
    if (screen == NULL)
    {
        return false;
    }

    //set windows caption
    SDL_WM_SetCaption("Placing button else where", NULL);

    //if noo errors
    return true;
}

bool load_files()
{
    //load image
    image = load_image("button.png");

    //if there was an error
    if (image == NULL)
    {
        return false;
    }

    //if no errors
    return true;
}

void clean_up()
{
    //free up surface
    SDL_FreeSurface( image );

    //quit program
    SDL_Quit();
}

void set_clips()
{
    //clip the sheet
    clips[ question ].x = 0;
    clips[ question ].y = 0;
    clips[ question ].w = 320;
    clips[ question ].h = 240;

    clips[ winner ].x = 0;
    clips[ winner ].y = 240;
    clips[ winner ].w = 320;
    clips[ winner ].h = 240;

    clips [ loser ].x = 320;
    clips [ loser ].y = 0;
    clips [ loser ].w = 320;
    clips [ loser ].h = 240;
}

Button::Button( int x, int y, int w, int h)
{
    //set the buttons attributes
    box.x = x;
    box.y = y;
    box.w = w;
    box.h = h;

    //set the default sprite
    clip = &clips[ question ];
}

void Button::handle_events()
{
    //The mouse offsets
    int x = 0, y = 0;

    //If the mouse moved
    if( event.type == SDL_MOUSEMOTION )
    {
        //Get the mouse offsets
        x = event.motion.x;
        y = event.motion.y;

        //If the mouse is over the button
        if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
        {
            //Set the button sprite
            clip = &clips[ loser ];
        }
        //If not
        else
        {
            //Set the button sprite
            clip = &clips[ question ];
        }
    }

    //if a mouse button was pressed
    if( event.type == SDL_MOUSEBUTTONDOWN )
    {
        //if left mouse button was pressed
        if ( event.button.button == SDL_BUTTON_LEFT )
        {
            //get the mouse offset
            x = event.button.x;
            y = event.button.y;

            //if the mouse is over the button
            if (  (x > box.x) && (x < box.x + box.w ) && ( y > box.y) && (y < box.y + box.h ) )
            {
                //set the button sprite;
                clip = &clips[ winner ];
            }
        }
    }
}

void Button::show()
{
    //show the button sprite
    apply_surface( box.x, box.y, image, screen, clip);
}

int main( int argc, char* agrs[] )
{
    //quit flag
    bool quit = false;

    //initilize
    if ( init() == false )
    {
        return 1;
    }

    //load files
    if ( load_files() == false)
    {
        return 1;
    }

    //clip sprite sheet
    set_clips();

    //make myButton
    Button myButton ( 170, 120, 320, 240);

    //while the user hasn't quit
    while (quit == false)
    {
        //if theres events to handle
        if (SDL_PollEvent (&event) )
        {
            //handle buttons event
            myButton.handle_events();

            //if the user hasn't Xed out the program
            if (event.type == SDL_QUIT)
            {
                quit = true;
            }
        }

        //fill the screen whiete
        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF ) );

        //update the screen
        if (SDL_Flip (screen) == -1)
        {
            return 1;
        }
    }

    //clean up
    clean_up();

    //sdl quit
    SDL_Quit();
}
Could you get anything to show up without using an image? (a solid square)
Topic archived. No new replies allowed.