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;
}
}
|