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
*/
f3ef3495 sago007 2008-11-13 20:00 20
b7590374 sago007 2011-05-19 16:15 21
#include <SDL/SDL_events.h>
b7590374 sago007 2011-05-19 16:15 22
f3ef3495 sago007 2008-11-13 20:00 23
#include "MenuSystem.h"
f3ef3495 sago007 2008-11-13 20:00 24
#include "common.h"
b7590374 sago007 2011-05-19 16:15 25
#include "CppSdl/CppSdlImageHolder.hpp"
f3ef3495 sago007 2008-11-13 20:00 26
b7590374 sago007 2011-05-19 16:15 27
extern CppSdl::CppSdlImageHolder mouse;
f3ef3495 sago007 2008-11-13 20:00 28
extern SDL_Surface *backgroundImage;
f3ef3495 sago007 2008-11-13 20:00 29
extern bool highPriority;
f3ef3495 sago007 2008-11-13 20:00 30
int mousex;
f3ef3495 sago007 2008-11-13 20:00 31
int mousey;
f3ef3495 sago007 2008-11-13 20:00 32
f3ef3495 sago007 2008-11-13 20:00 33
/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/
f3ef3495 sago007 2008-11-13 20:00 34
inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y)
f3ef3495 sago007 2008-11-13 20:00 35
{
f3ef3495 sago007 2008-11-13 20:00 36
    SDL_Rect dest;
f3ef3495 sago007 2008-11-13 20:00 37
    dest.x = x;
f3ef3495 sago007 2008-11-13 20:00 38
    dest.y = y;
f3ef3495 sago007 2008-11-13 20:00 39
    SDL_BlitSurface(img, NULL, target, &dest);
f3ef3495 sago007 2008-11-13 20:00 40
}
f3ef3495 sago007 2008-11-13 20:00 41
b7590374 sago007 2011-05-19 16:15 42
CppSdl::CppSdlImageHolder ButtonGfx::_marked;
b7590374 sago007 2011-05-19 16:15 43
CppSdl::CppSdlImageHolder ButtonGfx::_unmarked;
f3ef3495 sago007 2008-11-13 20:00 44
int ButtonGfx::xsize;
f3ef3495 sago007 2008-11-13 20:00 45
int ButtonGfx::ysize;
b7590374 sago007 2011-05-19 16:15 46
NFont ButtonGfx::thefont;
f3ef3495 sago007 2008-11-13 20:00 47
b7590374 sago007 2011-05-19 16:15 48
void ButtonGfx::setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked)
f3ef3495 sago007 2008-11-13 20:00 49
{
b7590374 sago007 2011-05-19 16:15 50
    ButtonGfx::_marked = marked;
b7590374 sago007 2011-05-19 16:15 51
    ButtonGfx::_unmarked = unmarked;
b7590374 sago007 2011-05-19 16:15 52
    xsize=(marked).GetWidth();
b7590374 sago007 2011-05-19 16:15 53
    ysize=(marked).GetHeight();
b7590374 sago007 2011-05-19 16:15 54
    cout << "Surfaces set, size: " <<xsize << " , " << ysize << endl;
f3ef3495 sago007 2008-11-13 20:00 55
}
f3ef3495 sago007 2008-11-13 20:00 56
f3ef3495 sago007 2008-11-13 20:00 57
Button::Button()
f3ef3495 sago007 2008-11-13 20:00 58
{
f3ef3495 sago007 2008-11-13 20:00 59
    label = "";
f3ef3495 sago007 2008-11-13 20:00 60
    marked = false;
b7590374 sago007 2011-05-19 16:15 61
    action = NULL;
f3ef3495 sago007 2008-11-13 20:00 62
}
f3ef3495 sago007 2008-11-13 20:00 63
f3ef3495 sago007 2008-11-13 20:00 64
Button::~Button()
f3ef3495 sago007 2008-11-13 20:00 65
{
f3ef3495 sago007 2008-11-13 20:00 66
}
f3ef3495 sago007 2008-11-13 20:00 67
f3ef3495 sago007 2008-11-13 20:00 68
Button::Button(const Button& b)
f3ef3495 sago007 2008-11-13 20:00 69
{
f3ef3495 sago007 2008-11-13 20:00 70
    label = b.label;
f3ef3495 sago007 2008-11-13 20:00 71
    marked = b.marked;
b7590374 sago007 2011-05-19 16:15 72
    action = b.action;
f3ef3495 sago007 2008-11-13 20:00 73
}
f3ef3495 sago007 2008-11-13 20:00 74
f3ef3495 sago007 2008-11-13 20:00 75
void Button::setLabel(string text)
f3ef3495 sago007 2008-11-13 20:00 76
{
f3ef3495 sago007 2008-11-13 20:00 77
    label = text;
f3ef3495 sago007 2008-11-13 20:00 78
}
f3ef3495 sago007 2008-11-13 20:00 79
26ddb424 sago007 2011-06-09 20:06 80
void Button::setAction(void (*action2run)(Button*))
f3ef3495 sago007 2008-11-13 20:00 81
{
f3ef3495 sago007 2008-11-13 20:00 82
    action = action2run;
f3ef3495 sago007 2008-11-13 20:00 83
}
f3ef3495 sago007 2008-11-13 20:00 84
f3ef3495 sago007 2008-11-13 20:00 85
bool Button::isClicked(int x,int y)
f3ef3495 sago007 2008-11-13 20:00 86
{
f3ef3495 sago007 2008-11-13 20:00 87
    if ( x >= this->x && y >= this->y && x<= this->x+ButtonGfx::xsize && y <= this->y + ButtonGfx::ysize)
f3ef3495 sago007 2008-11-13 20:00 88
        return true;
f3ef3495 sago007 2008-11-13 20:00 89
    else
f3ef3495 sago007 2008-11-13 20:00 90
        return false;
f3ef3495 sago007 2008-11-13 20:00 91
}
f3ef3495 sago007 2008-11-13 20:00 92
f3ef3495 sago007 2008-11-13 20:00 93
void Button::doAction()
f3ef3495 sago007 2008-11-13 20:00 94
{
b7590374 sago007 2011-05-19 16:15 95
    if(action)
26ddb424 sago007 2011-06-09 20:06 96
        action(this);
b7590374 sago007 2011-05-19 16:15 97
    else
b7590374 sago007 2011-05-19 16:15 98
        cout << "Warning: button \"" << label << "\" has no action assigned!";
f3ef3495 sago007 2008-11-13 20:00 99
}
f3ef3495 sago007 2008-11-13 20:00 100
b7590374 sago007 2011-05-19 16:15 101
void Button::drawTo(SDL_Surface **surface)
f3ef3495 sago007 2008-11-13 20:00 102
{
f3ef3495 sago007 2008-11-13 20:00 103
    #if DEBUG
b7590374 sago007 2011-05-19 16:15 104
    //cout << "Painting button: " << label << " at: " << x << "," << y << endl;
f3ef3495 sago007 2008-11-13 20:00 105
    #endif
f3ef3495 sago007 2008-11-13 20:00 106
    if (marked)
b7590374 sago007 2011-05-19 16:15 107
        ButtonGfx::_marked.PaintTo(*surface,x,y);
f3ef3495 sago007 2008-11-13 20:00 108
    else
b7590374 sago007 2011-05-19 16:15 109
        ButtonGfx::_unmarked.PaintTo(*surface,x,y);
f3ef3495 sago007 2008-11-13 20:00 110
    //int stringx = x + (ButtonGfx::xsize)/2 - ButtonGfx::ttf->getTextWidth(label)/2; 
f3ef3495 sago007 2008-11-13 20:00 111
    //int stringy = y + (ButtonGfx::ysize)/2 - ButtonGfx::ttf->getTextHeight()/2;
f3ef3495 sago007 2008-11-13 20:00 112
    //ButtonGfx::ttf->writeText(label,surface,stringx,stringy);
b7590374 sago007 2011-05-19 16:15 113
    ButtonGfx::thefont.setDest(*surface);
b7590374 sago007 2011-05-19 16:15 114
    ButtonGfx::thefont.drawCenter(x+ButtonGfx::xsize/2,y+ButtonGfx::ysize/2-ButtonGfx::thefont.getHeight(label.c_str())/2,label.c_str());
f3ef3495 sago007 2008-11-13 20:00 115
}
f3ef3495 sago007 2008-11-13 20:00 116
f3ef3495 sago007 2008-11-13 20:00 117
void Menu::drawSelf()
f3ef3495 sago007 2008-11-13 20:00 118
{
f3ef3495 sago007 2008-11-13 20:00 119
    DrawIMG(backgroundImage,screen,0,0);
fc8e3d6a sago007 2011-06-25 16:31 120
    vector<Button*>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 121
    for(it = buttons.begin();it < buttons.end(); it++)
fc8e3d6a sago007 2011-06-25 16:31 122
        (*it)->drawTo(&screen);
b7590374 sago007 2011-05-19 16:15 123
    exit.drawTo(&screen);
cc3ef158 sago007 2011-07-03 16:13 124
    ButtonGfx::thefont.draw(50,50,title.c_str());
b7590374 sago007 2011-05-19 16:15 125
    mouse.PaintTo(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
{
fc8e3d6a sago007 2011-06-25 16:31 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
    {
fc8e3d6a sago007 2011-06-25 16:31 133
        Button *b = (*it);
fc8e3d6a sago007 2011-06-25 16:31 134
        if(b->isClicked(x,y))
fc8e3d6a sago007 2011-06-25 16:31 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
{
cc3ef158 sago007 2011-07-03 16:13 143
    int nextY = 100;
f3ef3495 sago007 2008-11-13 20:00 144
    const int X = 50;
fc8e3d6a sago007 2011-06-25 16:31 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
    {
fc8e3d6a sago007 2011-06-25 16:31 148
        (*it)->x = X;
fc8e3d6a sago007 2011-06-25 16:31 149
        (*it)->y = nextY;
b7590374 sago007 2011-05-19 16:15 150
        nextY += ButtonGfx::ysize+10;
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
fc8e3d6a sago007 2011-06-25 16:31 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);
fc8e3d6a sago007 2011-06-25 16:31 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; 
fc8e3d6a sago007 2011-06-25 16:31 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;
fc8e3d6a sago007 2011-06-25 16:31 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
cc3ef158 sago007 2011-07-03 16:13 182
Menu::Menu(SDL_Surface** screen, string title, bool submenu) {
cc3ef158 sago007 2011-07-03 16:13 183
    this->screen = *screen;
cc3ef158 sago007 2011-07-03 16:13 184
    buttons = vector<Button*>(0);
cc3ef158 sago007 2011-07-03 16:13 185
    isSubmenu = submenu;
cc3ef158 sago007 2011-07-03 16:13 186
    this->title = title;
cc3ef158 sago007 2011-07-03 16:13 187
    if(isSubmenu)
cc3ef158 sago007 2011-07-03 16:13 188
        exit.setLabel("Back");
cc3ef158 sago007 2011-07-03 16:13 189
    else
cc3ef158 sago007 2011-07-03 16:13 190
        exit.setLabel("Exit");
cc3ef158 sago007 2011-07-03 16:13 191
}
cc3ef158 sago007 2011-07-03 16:13 192
f3ef3495 sago007 2008-11-13 20:00 193
void Menu::run()
f3ef3495 sago007 2008-11-13 20:00 194
{
f3ef3495 sago007 2008-11-13 20:00 195
    running = true;
cc3ef158 sago007 2011-07-03 16:13 196
    bool bMouseUp = false;
cc3ef158 sago007 2011-07-03 16:13 197
    long oldmousex = mousex;
cc3ef158 sago007 2011-07-03 16:13 198
    long oldmousey = mousey;
cc3ef158 sago007 2011-07-03 16:13 199
    while(running && !Config::getInstance()->isShuttingDown())
f3ef3495 sago007 2008-11-13 20:00 200
    {
f3ef3495 sago007 2008-11-13 20:00 201
        if (!(highPriority)) SDL_Delay(10);
f3ef3495 sago007 2008-11-13 20:00 202
f3ef3495 sago007 2008-11-13 20:00 203
       
f3ef3495 sago007 2008-11-13 20:00 204
        SDL_Event event;
f3ef3495 sago007 2008-11-13 20:00 205
f3ef3495 sago007 2008-11-13 20:00 206
        while ( SDL_PollEvent(&event) )
f3ef3495 sago007 2008-11-13 20:00 207
        {
f3ef3495 sago007 2008-11-13 20:00 208
            if ( event.type == SDL_QUIT )  {
cc3ef158 sago007 2011-07-03 16:13 209
                Config::getInstance()->setShuttingDown(5);
f3ef3495 sago007 2008-11-13 20:00 210
                running = false;
f3ef3495 sago007 2008-11-13 20:00 211
            }
f3ef3495 sago007 2008-11-13 20:00 212
f3ef3495 sago007 2008-11-13 20:00 213
            if ( event.type == SDL_KEYDOWN )
f3ef3495 sago007 2008-11-13 20:00 214
            {
f3ef3495 sago007 2008-11-13 20:00 215
                if ( event.key.keysym.sym == SDLK_ESCAPE )
f3ef3495 sago007 2008-11-13 20:00 216
                {
f3ef3495 sago007 2008-11-13 20:00 217
                    running = false;
f3ef3495 sago007 2008-11-13 20:00 218
                }
f3ef3495 sago007 2008-11-13 20:00 219
f3ef3495 sago007 2008-11-13 20:00 220
                if (event.key.keysym.sym == SDLK_UP)
f3ef3495 sago007 2008-11-13 20:00 221
                {
f3ef3495 sago007 2008-11-13 20:00 222
                    marked--;
f3ef3495 sago007 2008-11-13 20:00 223
                    if(marked<0)
f3ef3495 sago007 2008-11-13 20:00 224
                        marked = buttons.size(); //not -1, since exit is after the last element in the list
f3ef3495 sago007 2008-11-13 20:00 225
                }
f3ef3495 sago007 2008-11-13 20:00 226
f3ef3495 sago007 2008-11-13 20:00 227
                if (event.key.keysym.sym == SDLK_DOWN)
f3ef3495 sago007 2008-11-13 20:00 228
                {
f3ef3495 sago007 2008-11-13 20:00 229
                    marked++;
f3ef3495 sago007 2008-11-13 20:00 230
                    if(marked>buttons.size())
f3ef3495 sago007 2008-11-13 20:00 231
                        marked = 0; 
f3ef3495 sago007 2008-11-13 20:00 232
                }
b7590374 sago007 2011-05-19 16:15 233
b7590374 sago007 2011-05-19 16:15 234
                if(event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
b7590374 sago007 2011-05-19 16:15 235
                    if(marked < buttons.size())
fc8e3d6a sago007 2011-06-25 16:31 236
                        buttons.at(marked)->doAction();
b7590374 sago007 2011-05-19 16:15 237
                    if(marked == buttons.size())
b7590374 sago007 2011-05-19 16:15 238
                        running = false;
b7590374 sago007 2011-05-19 16:15 239
                }
f3ef3495 sago007 2008-11-13 20:00 240
            }
f3ef3495 sago007 2008-11-13 20:00 241
            
f3ef3495 sago007 2008-11-13 20:00 242
f3ef3495 sago007 2008-11-13 20:00 243
        }
b7590374 sago007 2011-05-19 16:15 244
b7590374 sago007 2011-05-19 16:15 245
        for(int i=0;i<buttons.size();i++) {
fc8e3d6a sago007 2011-06-25 16:31 246
            buttons.at(i)->marked = (i == marked);
b7590374 sago007 2011-05-19 16:15 247
        }
b7590374 sago007 2011-05-19 16:15 248
        exit.marked = (marked == buttons.size());
cc3ef158 sago007 2011-07-03 16:13 249
        Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey);
cc3ef158 sago007 2011-07-03 16:13 250
        // If the mouse button is released, make bMouseUp equal true
cc3ef158 sago007 2011-07-03 16:13 251
        if ( (buttonState&SDL_BUTTON(1))==0)
cc3ef158 sago007 2011-07-03 16:13 252
        {
cc3ef158 sago007 2011-07-03 16:13 253
            bMouseUp=true;
cc3ef158 sago007 2011-07-03 16:13 254
        }
cc3ef158 sago007 2011-07-03 16:13 255
        
cc3ef158 sago007 2011-07-03 16:13 256
        if(abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) {
cc3ef158 sago007 2011-07-03 16:13 257
            for(int i=0;i< buttons.size();++i) {
cc3ef158 sago007 2011-07-03 16:13 258
                if(buttons.at(i)->isClicked(mousex,mousey)) {
cc3ef158 sago007 2011-07-03 16:13 259
                    marked = i;
cc3ef158 sago007 2011-07-03 16:13 260
                }
cc3ef158 sago007 2011-07-03 16:13 261
            }
cc3ef158 sago007 2011-07-03 16:13 262
            if(exit.isClicked(mousex,mousey)) {
cc3ef158 sago007 2011-07-03 16:13 263
                marked = buttons.size();
cc3ef158 sago007 2011-07-03 16:13 264
            }
cc3ef158 sago007 2011-07-03 16:13 265
            oldmousex = mousex;
cc3ef158 sago007 2011-07-03 16:13 266
            oldmousey = mousey;
cc3ef158 sago007 2011-07-03 16:13 267
        }
cc3ef158 sago007 2011-07-03 16:13 268
        
cc3ef158 sago007 2011-07-03 16:13 269
        //mouse clicked
cc3ef158 sago007 2011-07-03 16:13 270
        if(buttonState&SDL_BUTTON(1)==SDL_BUTTON(1) && bMouseUp) 
cc3ef158 sago007 2011-07-03 16:13 271
        {
cc3ef158 sago007 2011-07-03 16:13 272
            bMouseUp = false;
cc3ef158 sago007 2011-07-03 16:13 273
            for(int i=0;i< buttons.size();++i) {
cc3ef158 sago007 2011-07-03 16:13 274
                if(buttons.at(i)->isClicked(mousex,mousey)) {
cc3ef158 sago007 2011-07-03 16:13 275
                    buttons.at(i)->doAction();
cc3ef158 sago007 2011-07-03 16:13 276
                }
cc3ef158 sago007 2011-07-03 16:13 277
            }
cc3ef158 sago007 2011-07-03 16:13 278
            if(exit.isClicked(mousex,mousey)) {
cc3ef158 sago007 2011-07-03 16:13 279
                running = false;
cc3ef158 sago007 2011-07-03 16:13 280
            }
cc3ef158 sago007 2011-07-03 16:13 281
        }
f3ef3495 sago007 2008-11-13 20:00 282
        
f3ef3495 sago007 2008-11-13 20:00 283
        drawSelf();
f3ef3495 sago007 2008-11-13 20:00 284
        SDL_Flip(screen);
f3ef3495 sago007 2008-11-13 20:00 285
    }
f3ef3495 sago007 2008-11-13 20:00 286
}
1970-01-01 00:00 287