File: MenuSystem.cc

package info (click to toggle)
blockattack 1.4.1%2Bds1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 6,260 kB
  • ctags: 1,038
  • sloc: cpp: 9,461; ansic: 142; makefile: 38
file content (227 lines) | stat: -rw-r--r-- 5,616 bytes parent folder | download
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
/*
Block Attack - Rise of the Blocks, SDL game, besed on Nintendo's Tetris Attack
Copyright (C) 2008 Poul Sander

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Poul Sander
    R�vehjvej 36, V. 1111
    2800 Kgs. Lyngby
    DENMARK
    blockattack@poulsander.com
    http://blockattack.sf.net
*/

#include "MenuSystem.h"
#include "common.h"

extern SDL_Surface *mouse;
extern SDL_Surface *backgroundImage;
extern bool highPriority;
int mousex;
int mousey;

/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/
inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y)
{
    SDL_Rect dest;
    dest.x = x;
    dest.y = y;
    SDL_BlitSurface(img, NULL, target, &dest);
}

SDL_Surface* ButtonGfx::marked;
SDL_Surface* ButtonGfx::unmarked;
int ButtonGfx::xsize;
int ButtonGfx::ysize;
TTFont* ButtonGfx::ttf;

void ButtonGfx::setSurfaces(SDL_Surface **marked,SDL_Surface **unmarked)
{
    ButtonGfx::marked = *marked;
    ButtonGfx::unmarked = *unmarked;
    xsize=(*marked)->w;
    ysize=(*marked)->h;
}

Button::Button()
{
    label = "";
    marked = false;
    surfaceMarked = SDL_ConvertSurface(ButtonGfx::marked, ButtonGfx::marked->format, SDL_SWSURFACE);
    surfaceUnmarked = SDL_ConvertSurface(ButtonGfx::unmarked, ButtonGfx::unmarked->format, SDL_SWSURFACE);
}

Button::~Button()
{
    SDL_FreeSurface(surfaceMarked);
    SDL_FreeSurface(surfaceUnmarked);
}

Button::Button(const Button& b)
{
    label = b.label;
    marked = b.marked;
    surfaceMarked = SDL_ConvertSurface(ButtonGfx::marked, ButtonGfx::marked->format, SDL_SWSURFACE);
    surfaceUnmarked = SDL_ConvertSurface(ButtonGfx::unmarked, ButtonGfx::unmarked->format, SDL_SWSURFACE);
}

void Button::setLabel(string text)
{
    label = text;
}

void Button::setAction(void (*action2run)())
{
    action = action2run;
}

bool Button::isClicked(int x,int y)
{
    if ( x >= this->x && y >= this->y && x<= this->x+ButtonGfx::xsize && y <= this->y + ButtonGfx::ysize)
        return true;
    else
        return false;
}

void Button::doAction()
{
    action();
}

void Button::drawTo(SDL_Surface *surface)
{
    #if DEBUG
    cout << "Painting button: " << label << endl;
    #endif
    if (marked)
        DrawIMG(surfaceMarked,surface,x,y);
    else
        DrawIMG(surfaceUnmarked,surface,x,y);
    //int stringx = x + (ButtonGfx::xsize)/2 - ButtonGfx::ttf->getTextWidth(label)/2; 
    //int stringy = y + (ButtonGfx::ysize)/2 - ButtonGfx::ttf->getTextHeight()/2;
    //ButtonGfx::ttf->writeText(label,surface,stringx,stringy);
}

void Menu::drawSelf()
{
    DrawIMG(backgroundImage,screen,0,0);
    vector<Button>::iterator it;
    for(it = buttons.begin();it < buttons.end(); it++)
        (*it).drawTo(screen);
    exit.drawTo(screen);
    DrawIMG(mouse,screen,mousex,mousey);
}

void Menu::performClick(int x,int y)
{
    vector<Button>::iterator it;
    for(it = buttons.begin();it < buttons.end(); it++)
    {
        Button b = (*it);
        if(b.isClicked(x,y))
            b.doAction();
    }
    if(exit.isClicked(x,y))
        running = false;
}

void Menu::placeButtons()
{
    int nextY = 50;
    const int X = 50;
    vector<Button>::iterator it;
    for(it = buttons.begin();it < buttons.end(); it++)
    {
        (*it).x = X;
        (*it).y = nextY;
        nextY += 50;
    }
    exit.x = X;
    exit.y = nextY;
}

void Menu::addButton(Button b)
{
    buttons.push_back(b);
    b.marked = false;
    placeButtons();
}

Menu::Menu(SDL_Surface **screen)
{
    this->screen = *screen; 
    buttons = vector<Button>(10);
    isSubmenu = true;
    exit.setLabel("Back");
}

Menu::Menu(SDL_Surface **screen,bool submenu)
{
    this->screen = *screen;
    buttons = vector<Button>(0);
    isSubmenu = submenu;
    if(isSubmenu)
        exit.setLabel("Back");
    else
        exit.setLabel("Exit");
}

void Menu::run()
{
    running = true;
    while(running)
    {
        if (!(highPriority)) SDL_Delay(10);

       
        SDL_Event event;

        while ( SDL_PollEvent(&event) )
        {
            if ( event.type == SDL_QUIT )  {
                running = false;
            }

            if ( event.type == SDL_KEYDOWN )
            {
                if ( event.key.keysym.sym == SDLK_ESCAPE )
                {
                    running = false;
                }

                if (event.key.keysym.sym == SDLK_UP)
                {
                    marked--;
                    if(marked<0)
                        marked = buttons.size(); //not -1, since exit is after the last element in the list
                }

                if (event.key.keysym.sym == SDLK_DOWN)
                {
                    marked++;
                    if(marked>buttons.size())
                        marked = 0; 
                }
            }
            
            SDL_GetMouseState(&mousex,&mousey);

        }
        
        drawSelf();
        SDL_Flip(screen);
    }
}