git repos / blockattack-game

blame: source/code/MenuSystem.cc

normal view · raw

f3ef3495 sago007 2008-11-13 20:00 1
/*
f3ef3495 sago007 2008-11-13 20:00 2
Block Attack - Rise of the Blocks, SDL game, besed on Nintendo's Tetris Attack
f3ef3495 sago007 2008-11-13 20:00 3
Copyright (C) 2008 Poul Sander
f3ef3495 sago007 2008-11-13 20:00 4
f3ef3495 sago007 2008-11-13 20:00 5
    This program is free software; you can redistribute it and/or modify
f3ef3495 sago007 2008-11-13 20:00 6
    it under the terms of the GNU General Public License as published by
f3ef3495 sago007 2008-11-13 20:00 7
    the Free Software Foundation; either version 2 of the License, or
f3ef3495 sago007 2008-11-13 20:00 8
    (at your option) any later version.
f3ef3495 sago007 2008-11-13 20:00 9
f3ef3495 sago007 2008-11-13 20:00 10
    This program is distributed in the hope that it will be useful,
f3ef3495 sago007 2008-11-13 20:00 11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
f3ef3495 sago007 2008-11-13 20:00 12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f3ef3495 sago007 2008-11-13 20:00 13
    GNU General Public License for more details.
f3ef3495 sago007 2008-11-13 20:00 14
f3ef3495 sago007 2008-11-13 20:00 15
    You should have received a copy of the GNU General Public License
f3ef3495 sago007 2008-11-13 20:00 16
    along with this program; if not, write to the Free Software
f3ef3495 sago007 2008-11-13 20:00 17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
f3ef3495 sago007 2008-11-13 20:00 18
f3ef3495 sago007 2008-11-13 20:00 19
    Poul Sander
f3ef3495 sago007 2008-11-13 20:00 20
    R�vehjvej 36, V. 1111
f3ef3495 sago007 2008-11-13 20:00 21
    2800 Kgs. Lyngby
f3ef3495 sago007 2008-11-13 20:00 22
    DENMARK
f3ef3495 sago007 2008-11-13 20:00 23
    blockattack@poulsander.com
f3ef3495 sago007 2008-11-13 20:00 24
    http://blockattack.sf.net
f3ef3495 sago007 2008-11-13 20:00 25
*/
f3ef3495 sago007 2008-11-13 20:00 26
f3ef3495 sago007 2008-11-13 20:00 27
#include "MenuSystem.h"
f3ef3495 sago007 2008-11-13 20:00 28
#include "common.h"
f3ef3495 sago007 2008-11-13 20:00 29
f3ef3495 sago007 2008-11-13 20:00 30
extern SDL_Surface *mouse;
f3ef3495 sago007 2008-11-13 20:00 31
extern SDL_Surface *backgroundImage;
f3ef3495 sago007 2008-11-13 20:00 32
extern bool highPriority;
f3ef3495 sago007 2008-11-13 20:00 33
int mousex;
f3ef3495 sago007 2008-11-13 20:00 34
int mousey;
f3ef3495 sago007 2008-11-13 20:00 35
f3ef3495 sago007 2008-11-13 20:00 36
/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/
f3ef3495 sago007 2008-11-13 20:00 37
inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y)
f3ef3495 sago007 2008-11-13 20:00 38
{
f3ef3495 sago007 2008-11-13 20:00 39
    SDL_Rect dest;
f3ef3495 sago007 2008-11-13 20:00 40
    dest.x = x;
f3ef3495 sago007 2008-11-13 20:00 41
    dest.y = y;
f3ef3495 sago007 2008-11-13 20:00 42
    SDL_BlitSurface(img, NULL, target, &dest);
f3ef3495 sago007 2008-11-13 20:00 43
}
f3ef3495 sago007 2008-11-13 20:00 44
f3ef3495 sago007 2008-11-13 20:00 45
SDL_Surface* ButtonGfx::marked;
f3ef3495 sago007 2008-11-13 20:00 46
SDL_Surface* ButtonGfx::unmarked;
f3ef3495 sago007 2008-11-13 20:00 47
int ButtonGfx::xsize;
f3ef3495 sago007 2008-11-13 20:00 48
int ButtonGfx::ysize;
f3ef3495 sago007 2008-11-13 20:00 49
TTFont* ButtonGfx::ttf;
f3ef3495 sago007 2008-11-13 20:00 50
f3ef3495 sago007 2008-11-13 20:00 51
void ButtonGfx::setSurfaces(SDL_Surface **marked,SDL_Surface **unmarked)
f3ef3495 sago007 2008-11-13 20:00 52
{
f3ef3495 sago007 2008-11-13 20:00 53
    ButtonGfx::marked = *marked;
f3ef3495 sago007 2008-11-13 20:00 54
    ButtonGfx::unmarked = *unmarked;
f3ef3495 sago007 2008-11-13 20:00 55
    xsize=(*marked)->w;
f3ef3495 sago007 2008-11-13 20:00 56
    ysize=(*marked)->h;
f3ef3495 sago007 2008-11-13 20:00 57
}
f3ef3495 sago007 2008-11-13 20:00 58
f3ef3495 sago007 2008-11-13 20:00 59
Button::Button()
f3ef3495 sago007 2008-11-13 20:00 60
{
f3ef3495 sago007 2008-11-13 20:00 61
    label = "";
f3ef3495 sago007 2008-11-13 20:00 62
    marked = false;
f3ef3495 sago007 2008-11-13 20:00 63
    surfaceMarked = SDL_ConvertSurface(ButtonGfx::marked, ButtonGfx::marked->format, SDL_SWSURFACE);
f3ef3495 sago007 2008-11-13 20:00 64
    surfaceUnmarked = SDL_ConvertSurface(ButtonGfx::unmarked, ButtonGfx::unmarked->format, SDL_SWSURFACE);
f3ef3495 sago007 2008-11-13 20:00 65
}
f3ef3495 sago007 2008-11-13 20:00 66
f3ef3495 sago007 2008-11-13 20:00 67
Button::~Button()
f3ef3495 sago007 2008-11-13 20:00 68
{
f3ef3495 sago007 2008-11-13 20:00 69
    SDL_FreeSurface(surfaceMarked);
f3ef3495 sago007 2008-11-13 20:00 70
    SDL_FreeSurface(surfaceUnmarked);
f3ef3495 sago007 2008-11-13 20:00 71
}
f3ef3495 sago007 2008-11-13 20:00 72
f3ef3495 sago007 2008-11-13 20:00 73
Button::Button(const Button& b)
f3ef3495 sago007 2008-11-13 20:00 74
{
f3ef3495 sago007 2008-11-13 20:00 75
    label = b.label;
f3ef3495 sago007 2008-11-13 20:00 76
    marked = b.marked;
f3ef3495 sago007 2008-11-13 20:00 77
    surfaceMarked = SDL_ConvertSurface(ButtonGfx::marked, ButtonGfx::marked->format, SDL_SWSURFACE);
f3ef3495 sago007 2008-11-13 20:00 78
    surfaceUnmarked = SDL_ConvertSurface(ButtonGfx::unmarked, ButtonGfx::unmarked->format, SDL_SWSURFACE);
f3ef3495 sago007 2008-11-13 20:00 79
}
f3ef3495 sago007 2008-11-13 20:00 80
f3ef3495 sago007 2008-11-13 20:00 81
void Button::setLabel(string text)
f3ef3495 sago007 2008-11-13 20:00 82
{
f3ef3495 sago007 2008-11-13 20:00 83
    label = text;
f3ef3495 sago007 2008-11-13 20:00 84
}
f3ef3495 sago007 2008-11-13 20:00 85
f3ef3495 sago007 2008-11-13 20:00 86
void Button::setAction(void (*action2run)())
f3ef3495 sago007 2008-11-13 20:00 87
{
f3ef3495 sago007 2008-11-13 20:00 88
    action = action2run;
f3ef3495 sago007 2008-11-13 20:00 89
}
f3ef3495 sago007 2008-11-13 20:00 90
f3ef3495 sago007 2008-11-13 20:00 91
bool Button::isClicked(int x,int y)
f3ef3495 sago007 2008-11-13 20:00 92
{
f3ef3495 sago007 2008-11-13 20:00 93
    if ( x >= this->x && y >= this->y && x<= this->x+ButtonGfx::xsize && y <= this->y + ButtonGfx::ysize)
f3ef3495 sago007 2008-11-13 20:00 94
        return true;
f3ef3495 sago007 2008-11-13 20:00 95
    else
f3ef3495 sago007 2008-11-13 20:00 96
        return false;
f3ef3495 sago007 2008-11-13 20:00 97
}
f3ef3495 sago007 2008-11-13 20:00 98
f3ef3495 sago007 2008-11-13 20:00 99
void Button::doAction()
f3ef3495 sago007 2008-11-13 20:00 100
{
f3ef3495 sago007 2008-11-13 20:00 101
    action();
f3ef3495 sago007 2008-11-13 20:00 102
}
f3ef3495 sago007 2008-11-13 20:00 103
f3ef3495 sago007 2008-11-13 20:00 104
void Button::drawTo(SDL_Surface *surface)
f3ef3495 sago007 2008-11-13 20:00 105
{
f3ef3495 sago007 2008-11-13 20:00 106
    #if DEBUG
f3ef3495 sago007 2008-11-13 20:00 107
    cout << "Painting button: " << label << endl;
f3ef3495 sago007 2008-11-13 20:00 108
    #endif
f3ef3495 sago007 2008-11-13 20:00 109
    if (marked)
f3ef3495 sago007 2008-11-13 20:00 110
        DrawIMG(surfaceMarked,surface,x,y);
f3ef3495 sago007 2008-11-13 20:00 111
    else
f3ef3495 sago007 2008-11-13 20:00 112
        DrawIMG(surfaceUnmarked,surface,x,y);
f3ef3495 sago007 2008-11-13 20:00 113
    //int stringx = x + (ButtonGfx::xsize)/2 - ButtonGfx::ttf->getTextWidth(label)/2; 
f3ef3495 sago007 2008-11-13 20:00 114
    //int stringy = y + (ButtonGfx::ysize)/2 - ButtonGfx::ttf->getTextHeight()/2;
f3ef3495 sago007 2008-11-13 20:00 115
    //ButtonGfx::ttf->writeText(label,surface,stringx,stringy);
f3ef3495 sago007 2008-11-13 20:00 116
}
f3ef3495 sago007 2008-11-13 20:00 117
f3ef3495 sago007 2008-11-13 20:00 118
void Menu::drawSelf()
f3ef3495 sago007 2008-11-13 20:00 119
{
f3ef3495 sago007 2008-11-13 20:00 120
    DrawIMG(backgroundImage,screen,0,0);
f3ef3495 sago007 2008-11-13 20:00 121
    vector<Button>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 122
    for(it = buttons.begin();it < buttons.end(); it++)
f3ef3495 sago007 2008-11-13 20:00 123
        (*it).drawTo(screen);
f3ef3495 sago007 2008-11-13 20:00 124
    exit.drawTo(screen);
f3ef3495 sago007 2008-11-13 20:00 125
    DrawIMG(mouse,screen,mousex,mousey);
f3ef3495 sago007 2008-11-13 20:00 126
}
f3ef3495 sago007 2008-11-13 20:00 127
f3ef3495 sago007 2008-11-13 20:00 128
void Menu::performClick(int x,int y)
f3ef3495 sago007 2008-11-13 20:00 129
{
f3ef3495 sago007 2008-11-13 20:00 130
    vector<Button>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 131
    for(it = buttons.begin();it < buttons.end(); it++)
f3ef3495 sago007 2008-11-13 20:00 132
    {
f3ef3495 sago007 2008-11-13 20:00 133
        Button b = (*it);
f3ef3495 sago007 2008-11-13 20:00 134
        if(b.isClicked(x,y))
f3ef3495 sago007 2008-11-13 20:00 135
            b.doAction();
f3ef3495 sago007 2008-11-13 20:00 136
    }
f3ef3495 sago007 2008-11-13 20:00 137
    if(exit.isClicked(x,y))
f3ef3495 sago007 2008-11-13 20:00 138
        running = false;
f3ef3495 sago007 2008-11-13 20:00 139
}
f3ef3495 sago007 2008-11-13 20:00 140
f3ef3495 sago007 2008-11-13 20:00 141
void Menu::placeButtons()
f3ef3495 sago007 2008-11-13 20:00 142
{
f3ef3495 sago007 2008-11-13 20:00 143
    int nextY = 50;
f3ef3495 sago007 2008-11-13 20:00 144
    const int X = 50;
f3ef3495 sago007 2008-11-13 20:00 145
    vector<Button>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 146
    for(it = buttons.begin();it < buttons.end(); it++)
f3ef3495 sago007 2008-11-13 20:00 147
    {
f3ef3495 sago007 2008-11-13 20:00 148
        (*it).x = X;
f3ef3495 sago007 2008-11-13 20:00 149
        (*it).y = nextY;
f3ef3495 sago007 2008-11-13 20:00 150
        nextY += 50;
f3ef3495 sago007 2008-11-13 20:00 151
    }
f3ef3495 sago007 2008-11-13 20:00 152
    exit.x = X;
f3ef3495 sago007 2008-11-13 20:00 153
    exit.y = nextY;
f3ef3495 sago007 2008-11-13 20:00 154
}
f3ef3495 sago007 2008-11-13 20:00 155
f3ef3495 sago007 2008-11-13 20:00 156
void Menu::addButton(Button b)
f3ef3495 sago007 2008-11-13 20:00 157
{
f3ef3495 sago007 2008-11-13 20:00 158
    buttons.push_back(b);
f3ef3495 sago007 2008-11-13 20:00 159
    b.marked = false;
f3ef3495 sago007 2008-11-13 20:00 160
    placeButtons();
f3ef3495 sago007 2008-11-13 20:00 161
}
f3ef3495 sago007 2008-11-13 20:00 162
f3ef3495 sago007 2008-11-13 20:00 163
Menu::Menu(SDL_Surface **screen)
f3ef3495 sago007 2008-11-13 20:00 164
{
f3ef3495 sago007 2008-11-13 20:00 165
    this->screen = *screen; 
f3ef3495 sago007 2008-11-13 20:00 166
    buttons = vector<Button>(10);
f3ef3495 sago007 2008-11-13 20:00 167
    isSubmenu = true;
f3ef3495 sago007 2008-11-13 20:00 168
    exit.setLabel("Back");
f3ef3495 sago007 2008-11-13 20:00 169
}
f3ef3495 sago007 2008-11-13 20:00 170
f3ef3495 sago007 2008-11-13 20:00 171
Menu::Menu(SDL_Surface **screen,bool submenu)
f3ef3495 sago007 2008-11-13 20:00 172
{
f3ef3495 sago007 2008-11-13 20:00 173
    this->screen = *screen;
f3ef3495 sago007 2008-11-13 20:00 174
    buttons = vector<Button>(0);
f3ef3495 sago007 2008-11-13 20:00 175
    isSubmenu = submenu;
f3ef3495 sago007 2008-11-13 20:00 176
    if(isSubmenu)
f3ef3495 sago007 2008-11-13 20:00 177
        exit.setLabel("Back");
f3ef3495 sago007 2008-11-13 20:00 178
    else
f3ef3495 sago007 2008-11-13 20:00 179
        exit.setLabel("Exit");
f3ef3495 sago007 2008-11-13 20:00 180
}
f3ef3495 sago007 2008-11-13 20:00 181
f3ef3495 sago007 2008-11-13 20:00 182
void Menu::run()
f3ef3495 sago007 2008-11-13 20:00 183
{
f3ef3495 sago007 2008-11-13 20:00 184
    running = true;
f3ef3495 sago007 2008-11-13 20:00 185
    while(running)
f3ef3495 sago007 2008-11-13 20:00 186
    {
f3ef3495 sago007 2008-11-13 20:00 187
        if (!(highPriority)) SDL_Delay(10);
f3ef3495 sago007 2008-11-13 20:00 188
f3ef3495 sago007 2008-11-13 20:00 189
       
f3ef3495 sago007 2008-11-13 20:00 190
        SDL_Event event;
f3ef3495 sago007 2008-11-13 20:00 191
f3ef3495 sago007 2008-11-13 20:00 192
        while ( SDL_PollEvent(&event) )
f3ef3495 sago007 2008-11-13 20:00 193
        {
f3ef3495 sago007 2008-11-13 20:00 194
            if ( event.type == SDL_QUIT )  {
f3ef3495 sago007 2008-11-13 20:00 195
                running = false;
f3ef3495 sago007 2008-11-13 20:00 196
            }
f3ef3495 sago007 2008-11-13 20:00 197
f3ef3495 sago007 2008-11-13 20:00 198
            if ( event.type == SDL_KEYDOWN )
f3ef3495 sago007 2008-11-13 20:00 199
            {
f3ef3495 sago007 2008-11-13 20:00 200
                if ( event.key.keysym.sym == SDLK_ESCAPE )
f3ef3495 sago007 2008-11-13 20:00 201
                {
f3ef3495 sago007 2008-11-13 20:00 202
                    running = false;
f3ef3495 sago007 2008-11-13 20:00 203
                }
f3ef3495 sago007 2008-11-13 20:00 204
f3ef3495 sago007 2008-11-13 20:00 205
                if (event.key.keysym.sym == SDLK_UP)
f3ef3495 sago007 2008-11-13 20:00 206
                {
f3ef3495 sago007 2008-11-13 20:00 207
                    marked--;
f3ef3495 sago007 2008-11-13 20:00 208
                    if(marked<0)
f3ef3495 sago007 2008-11-13 20:00 209
                        marked = buttons.size(); //not -1, since exit is after the last element in the list
f3ef3495 sago007 2008-11-13 20:00 210
                }
f3ef3495 sago007 2008-11-13 20:00 211
f3ef3495 sago007 2008-11-13 20:00 212
                if (event.key.keysym.sym == SDLK_DOWN)
f3ef3495 sago007 2008-11-13 20:00 213
                {
f3ef3495 sago007 2008-11-13 20:00 214
                    marked++;
f3ef3495 sago007 2008-11-13 20:00 215
                    if(marked>buttons.size())
f3ef3495 sago007 2008-11-13 20:00 216
                        marked = 0; 
f3ef3495 sago007 2008-11-13 20:00 217
                }
f3ef3495 sago007 2008-11-13 20:00 218
            }
f3ef3495 sago007 2008-11-13 20:00 219
            
f3ef3495 sago007 2008-11-13 20:00 220
            SDL_GetMouseState(&mousex,&mousey);
f3ef3495 sago007 2008-11-13 20:00 221
f3ef3495 sago007 2008-11-13 20:00 222
        }
f3ef3495 sago007 2008-11-13 20:00 223
        
f3ef3495 sago007 2008-11-13 20:00 224
        drawSelf();
f3ef3495 sago007 2008-11-13 20:00 225
        SDL_Flip(screen);
f3ef3495 sago007 2008-11-13 20:00 226
    }
f3ef3495 sago007 2008-11-13 20:00 227
}
1970-01-01 00:00 228