(SDL) Button wont show on screen

Hello, I'm trying to make a button appear on the program using SDL but it doesn't show. When compiled, there is no error, but the button just won't show, I have no idea why.

Here is the code related to the button:

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
//The button states in the sprite sheet
const int BUTTONS_MOUSEOVER = 0;
const int BUTTONS_MOUSEOUT = 1;
const int BUTTONS_MOUSEDOWN = 2;
const int BUTTONS_MOUSEUP = 3;

SDL_Rect TitleButtons[3];

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

    //Get offsets
    offset.x = x;
    offset.y = y;

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


class Button
{
	private:
	SDL_Rect Box;
	SDL_Rect *clip;
	int Type;
	public:
	int IsDown;
	Button(int type);
	void HandleEvent();
	void Show();
};

void TitleButtonsClips()
{
	TitleButtons[BUTTONS_MOUSEOVER].x = 0;
	TitleButtons[BUTTONS_MOUSEOVER].y = 0;
	TitleButtons[BUTTONS_MOUSEOVER].w = 100;
	TitleButtons[BUTTONS_MOUSEOVER].h = 50;

	TitleButtons[BUTTONS_MOUSEOUT].x = 100;
	TitleButtons[BUTTONS_MOUSEOUT].y = 0;
	TitleButtons[BUTTONS_MOUSEOUT].w = 100;
	TitleButtons[BUTTONS_MOUSEOUT].h = 50;

	TitleButtons[BUTTONS_MOUSEDOWN].x = 0;
	TitleButtons[BUTTONS_MOUSEDOWN].y = 50;
	TitleButtons[BUTTONS_MOUSEDOWN].w = 100;
	TitleButtons[BUTTONS_MOUSEDOWN].h = 100;

	TitleButtons[BUTTONS_MOUSEUP].x = 100;
	TitleButtons[BUTTONS_MOUSEUP].y = 50;
	TitleButtons[BUTTONS_MOUSEUP].w = 100;
	TitleButtons[BUTTONS_MOUSEUP].h = 100;
}

Button::Button(int type)
{
	Type = type;
	IsDown = 0;

	switch(Type)
	{
		case 1:
		{
			Box.w = 100;
			Box.h = 50;
			Box.x = 100;
			Box.y = 100;
			clip = &TitleButtons[BUTTONS_MOUSEOUT];
		}
		break;
	}
}

void Button::HandleEvent()
{
	int x = 0, y = 0;
	if (event.type == SDL_MOUSEMOTION)
	{
		x = event.motion.x;
		y = event.motion.y;
		if( ( x > Box.x ) && ( x < Box.x + Box.w ) && ( y > Box.y ) && ( y < Box.y + Box.h ) )
		{	
			clip = &TitleButtons[BUTTONS_MOUSEOVER];
		}
		else
		{
			clip = &TitleButtons[BUTTONS_MOUSEOUT];
		}
	}
	if (event.type == SDL_MOUSEBUTTONUP)
	{
		if (event.button.button == SDL_BUTTON_LEFT)
		{
			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 ) )
			{
				clip = &TitleButtons[BUTTONS_MOUSEDOWN];
			}
		}
	}
}
void Button::Show()
{
	switch (Type)
	{
		case 1:
		{
			apply_surface(Box.x, Box.y, LoginButtonPic, Opening3, clip);
		}
		break;
	}
}


and in main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 while (Quit == false)
	{
		fps.start();

		LoginButton.Show();
		//Update the screen
        if( SDL_Flip( Opening3 ) == -1 )
        {
            return 1;    
        }

		while ( SDL_PollEvent(&event))
		{	
			//LoginButton.HandleEvent();
			if ( event.type == SDL_QUIT)
			{
				Quit = true;
			}
		}
	}
Hard to tell the exact problem, if you are able to upload all of the code somewhere (as it's missing a few things here) I may be able to help more.

Did you remember to call the TitleButtonsClips function in main?
Here is the full source code:
http://www.mediafire.com/?2ur9qrqlu1l4zhh

and I did.
Ok I am not sure if I missed anything but there are a few things to be fixed anyway.

In your main game loop you have this.
1
2
3
4
5
//Update the screen
if( SDL_Flip( Opening3 ) == -1 )
{
          return 1;    
}


That is not updating the screen, Opening3 is an image. Change that to SDL_Flip(screen);

On line 141 in your init() function you are not calling the TitleButtonClips() function correctly.

 
void TitleButtonClips();


You do not call functions with the return type included, so remove the void.

In your show function for the button you have the same error as earlier, make sure you are flipping/drawing to the screen and not an image. (It should not be Opening3).

 
apply_surface(Box.x, Box.y, LoginButtonPic, Opening3, clip);


Hopefully that clears some things up.
Last edited on
I see, it was my bad. I was thinking more like making the button appear on the image, so like when Im done login the button will be gone (because I will load a different image).

Thank you :3 It helped
Topic archived. No new replies allowed.