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
b7590374 sago007 2011-05-19 16:15 27
#include <SDL/SDL_events.h>
b7590374 sago007 2011-05-19 16:15 28
f3ef3495 sago007 2008-11-13 20:00 29
#include "MenuSystem.h"
f3ef3495 sago007 2008-11-13 20:00 30
#include "common.h"
b7590374 sago007 2011-05-19 16:15 31
#include "CppSdl/CppSdlImageHolder.hpp"
f3ef3495 sago007 2008-11-13 20:00 32
b7590374 sago007 2011-05-19 16:15 33
extern CppSdl::CppSdlImageHolder mouse;
f3ef3495 sago007 2008-11-13 20:00 34
extern SDL_Surface *backgroundImage;
f3ef3495 sago007 2008-11-13 20:00 35
extern bool highPriority;
f3ef3495 sago007 2008-11-13 20:00 36
int mousex;
f3ef3495 sago007 2008-11-13 20:00 37
int mousey;
f3ef3495 sago007 2008-11-13 20:00 38
f3ef3495 sago007 2008-11-13 20:00 39
/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/
f3ef3495 sago007 2008-11-13 20:00 40
inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y)
f3ef3495 sago007 2008-11-13 20:00 41
{
f3ef3495 sago007 2008-11-13 20:00 42
    SDL_Rect dest;
f3ef3495 sago007 2008-11-13 20:00 43
    dest.x = x;
f3ef3495 sago007 2008-11-13 20:00 44
    dest.y = y;
f3ef3495 sago007 2008-11-13 20:00 45
    SDL_BlitSurface(img, NULL, target, &dest);
f3ef3495 sago007 2008-11-13 20:00 46
}
f3ef3495 sago007 2008-11-13 20:00 47
b7590374 sago007 2011-05-19 16:15 48
CppSdl::CppSdlImageHolder ButtonGfx::_marked;
b7590374 sago007 2011-05-19 16:15 49
CppSdl::CppSdlImageHolder ButtonGfx::_unmarked;
f3ef3495 sago007 2008-11-13 20:00 50
int ButtonGfx::xsize;
f3ef3495 sago007 2008-11-13 20:00 51
int ButtonGfx::ysize;
b7590374 sago007 2011-05-19 16:15 52
NFont ButtonGfx::thefont;
f3ef3495 sago007 2008-11-13 20:00 53
b7590374 sago007 2011-05-19 16:15 54
void ButtonGfx::setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked)
f3ef3495 sago007 2008-11-13 20:00 55
{
b7590374 sago007 2011-05-19 16:15 56
    ButtonGfx::_marked = marked;
b7590374 sago007 2011-05-19 16:15 57
    ButtonGfx::_unmarked = unmarked;
b7590374 sago007 2011-05-19 16:15 58
    xsize=(marked).GetWidth();
b7590374 sago007 2011-05-19 16:15 59
    ysize=(marked).GetHeight();
b7590374 sago007 2011-05-19 16:15 60
    cout << "Surfaces set, size: " <<xsize << " , " << ysize << endl;
f3ef3495 sago007 2008-11-13 20:00 61
}
f3ef3495 sago007 2008-11-13 20:00 62
f3ef3495 sago007 2008-11-13 20:00 63
Button::Button()
f3ef3495 sago007 2008-11-13 20:00 64
{
f3ef3495 sago007 2008-11-13 20:00 65
    label = "";
f3ef3495 sago007 2008-11-13 20:00 66
    marked = false;
b7590374 sago007 2011-05-19 16:15 67
    action = NULL;
f3ef3495 sago007 2008-11-13 20:00 68
}
f3ef3495 sago007 2008-11-13 20:00 69
f3ef3495 sago007 2008-11-13 20:00 70
Button::~Button()
f3ef3495 sago007 2008-11-13 20:00 71
{
f3ef3495 sago007 2008-11-13 20:00 72
}
f3ef3495 sago007 2008-11-13 20:00 73
f3ef3495 sago007 2008-11-13 20:00 74
Button::Button(const Button& b)
f3ef3495 sago007 2008-11-13 20:00 75
{
f3ef3495 sago007 2008-11-13 20:00 76
    label = b.label;
f3ef3495 sago007 2008-11-13 20:00 77
    marked = b.marked;
b7590374 sago007 2011-05-19 16:15 78
    action = b.action;
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
26ddb424 sago007 2011-06-09 20:06 86
void Button::setAction(void (*action2run)(Button*))
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
{
b7590374 sago007 2011-05-19 16:15 101
    if(action)
26ddb424 sago007 2011-06-09 20:06 102
        action(this);
b7590374 sago007 2011-05-19 16:15 103
    else
b7590374 sago007 2011-05-19 16:15 104
        cout << "Warning: button \"" << label << "\" has no action assigned!";
f3ef3495 sago007 2008-11-13 20:00 105
}
f3ef3495 sago007 2008-11-13 20:00 106
b7590374 sago007 2011-05-19 16:15 107
void Button::drawTo(SDL_Surface **surface)
f3ef3495 sago007 2008-11-13 20:00 108
{
f3ef3495 sago007 2008-11-13 20:00 109
    #if DEBUG
b7590374 sago007 2011-05-19 16:15 110
    //cout << "Painting button: " << label << " at: " << x << "," << y << endl;
f3ef3495 sago007 2008-11-13 20:00 111
    #endif
f3ef3495 sago007 2008-11-13 20:00 112
    if (marked)
b7590374 sago007 2011-05-19 16:15 113
        ButtonGfx::_marked.PaintTo(*surface,x,y);
f3ef3495 sago007 2008-11-13 20:00 114
    else
b7590374 sago007 2011-05-19 16:15 115
        ButtonGfx::_unmarked.PaintTo(*surface,x,y);
f3ef3495 sago007 2008-11-13 20:00 116
    //int stringx = x + (ButtonGfx::xsize)/2 - ButtonGfx::ttf->getTextWidth(label)/2; 
f3ef3495 sago007 2008-11-13 20:00 117
    //int stringy = y + (ButtonGfx::ysize)/2 - ButtonGfx::ttf->getTextHeight()/2;
f3ef3495 sago007 2008-11-13 20:00 118
    //ButtonGfx::ttf->writeText(label,surface,stringx,stringy);
b7590374 sago007 2011-05-19 16:15 119
    ButtonGfx::thefont.setDest(*surface);
b7590374 sago007 2011-05-19 16:15 120
    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 121
}
f3ef3495 sago007 2008-11-13 20:00 122
f3ef3495 sago007 2008-11-13 20:00 123
void Menu::drawSelf()
f3ef3495 sago007 2008-11-13 20:00 124
{
f3ef3495 sago007 2008-11-13 20:00 125
    DrawIMG(backgroundImage,screen,0,0);
fc8e3d6a sago007 2011-06-25 16:31 126
    vector<Button*>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 127
    for(it = buttons.begin();it < buttons.end(); it++)
fc8e3d6a sago007 2011-06-25 16:31 128
        (*it)->drawTo(&screen);
b7590374 sago007 2011-05-19 16:15 129
    exit.drawTo(&screen);
b7590374 sago007 2011-05-19 16:15 130
    mouse.PaintTo(screen,mousex,mousey);
f3ef3495 sago007 2008-11-13 20:00 131
}
f3ef3495 sago007 2008-11-13 20:00 132
f3ef3495 sago007 2008-11-13 20:00 133
void Menu::performClick(int x,int y)
f3ef3495 sago007 2008-11-13 20:00 134
{
fc8e3d6a sago007 2011-06-25 16:31 135
    vector<Button*>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 136
    for(it = buttons.begin();it < buttons.end(); it++)
f3ef3495 sago007 2008-11-13 20:00 137
    {
fc8e3d6a sago007 2011-06-25 16:31 138
        Button *b = (*it);
fc8e3d6a sago007 2011-06-25 16:31 139
        if(b->isClicked(x,y))
fc8e3d6a sago007 2011-06-25 16:31 140
            b->doAction();
f3ef3495 sago007 2008-11-13 20:00 141
    }
f3ef3495 sago007 2008-11-13 20:00 142
    if(exit.isClicked(x,y))
f3ef3495 sago007 2008-11-13 20:00 143
        running = false;
f3ef3495 sago007 2008-11-13 20:00 144
}
f3ef3495 sago007 2008-11-13 20:00 145
f3ef3495 sago007 2008-11-13 20:00 146
void Menu::placeButtons()
f3ef3495 sago007 2008-11-13 20:00 147
{
f3ef3495 sago007 2008-11-13 20:00 148
    int nextY = 50;
f3ef3495 sago007 2008-11-13 20:00 149
    const int X = 50;
fc8e3d6a sago007 2011-06-25 16:31 150
    vector<Button*>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 151
    for(it = buttons.begin();it < buttons.end(); it++)
f3ef3495 sago007 2008-11-13 20:00 152
    {
fc8e3d6a sago007 2011-06-25 16:31 153
        (*it)->x = X;
fc8e3d6a sago007 2011-06-25 16:31 154
        (*it)->y = nextY;
b7590374 sago007 2011-05-19 16:15 155
        nextY += ButtonGfx::ysize+10;
f3ef3495 sago007 2008-11-13 20:00 156
    }
f3ef3495 sago007 2008-11-13 20:00 157
    exit.x = X;
f3ef3495 sago007 2008-11-13 20:00 158
    exit.y = nextY;
f3ef3495 sago007 2008-11-13 20:00 159
}
f3ef3495 sago007 2008-11-13 20:00 160
fc8e3d6a sago007 2011-06-25 16:31 161
void Menu::addButton(Button *b)
f3ef3495 sago007 2008-11-13 20:00 162
{
f3ef3495 sago007 2008-11-13 20:00 163
    buttons.push_back(b);
fc8e3d6a sago007 2011-06-25 16:31 164
    b->marked = false;
f3ef3495 sago007 2008-11-13 20:00 165
    placeButtons();
f3ef3495 sago007 2008-11-13 20:00 166
}
f3ef3495 sago007 2008-11-13 20:00 167
f3ef3495 sago007 2008-11-13 20:00 168
Menu::Menu(SDL_Surface **screen)
f3ef3495 sago007 2008-11-13 20:00 169
{
f3ef3495 sago007 2008-11-13 20:00 170
    this->screen = *screen; 
fc8e3d6a sago007 2011-06-25 16:31 171
    buttons = vector<Button*>(10);
f3ef3495 sago007 2008-11-13 20:00 172
    isSubmenu = true;
f3ef3495 sago007 2008-11-13 20:00 173
    exit.setLabel("Back");
f3ef3495 sago007 2008-11-13 20:00 174
}
f3ef3495 sago007 2008-11-13 20:00 175
f3ef3495 sago007 2008-11-13 20:00 176
Menu::Menu(SDL_Surface **screen,bool submenu)
f3ef3495 sago007 2008-11-13 20:00 177
{
f3ef3495 sago007 2008-11-13 20:00 178
    this->screen = *screen;
fc8e3d6a sago007 2011-06-25 16:31 179
    buttons = vector<Button*>(0);
f3ef3495 sago007 2008-11-13 20:00 180
    isSubmenu = submenu;
f3ef3495 sago007 2008-11-13 20:00 181
    if(isSubmenu)
f3ef3495 sago007 2008-11-13 20:00 182
        exit.setLabel("Back");
f3ef3495 sago007 2008-11-13 20:00 183
    else
f3ef3495 sago007 2008-11-13 20:00 184
        exit.setLabel("Exit");
f3ef3495 sago007 2008-11-13 20:00 185
}
f3ef3495 sago007 2008-11-13 20:00 186
f3ef3495 sago007 2008-11-13 20:00 187
void Menu::run()
f3ef3495 sago007 2008-11-13 20:00 188
{
f3ef3495 sago007 2008-11-13 20:00 189
    running = true;
f3ef3495 sago007 2008-11-13 20:00 190
    while(running)
f3ef3495 sago007 2008-11-13 20:00 191
    {
f3ef3495 sago007 2008-11-13 20:00 192
        if (!(highPriority)) SDL_Delay(10);
f3ef3495 sago007 2008-11-13 20:00 193
f3ef3495 sago007 2008-11-13 20:00 194
       
f3ef3495 sago007 2008-11-13 20:00 195
        SDL_Event event;
f3ef3495 sago007 2008-11-13 20:00 196
f3ef3495 sago007 2008-11-13 20:00 197
        while ( SDL_PollEvent(&event) )
f3ef3495 sago007 2008-11-13 20:00 198
        {
f3ef3495 sago007 2008-11-13 20:00 199
            if ( event.type == SDL_QUIT )  {
f3ef3495 sago007 2008-11-13 20:00 200
                running = false;
f3ef3495 sago007 2008-11-13 20:00 201
            }
f3ef3495 sago007 2008-11-13 20:00 202
f3ef3495 sago007 2008-11-13 20:00 203
            if ( event.type == SDL_KEYDOWN )
f3ef3495 sago007 2008-11-13 20:00 204
            {
f3ef3495 sago007 2008-11-13 20:00 205
                if ( event.key.keysym.sym == SDLK_ESCAPE )
f3ef3495 sago007 2008-11-13 20:00 206
                {
f3ef3495 sago007 2008-11-13 20:00 207
                    running = false;
f3ef3495 sago007 2008-11-13 20:00 208
                }
f3ef3495 sago007 2008-11-13 20:00 209
f3ef3495 sago007 2008-11-13 20:00 210
                if (event.key.keysym.sym == SDLK_UP)
f3ef3495 sago007 2008-11-13 20:00 211
                {
f3ef3495 sago007 2008-11-13 20:00 212
                    marked--;
f3ef3495 sago007 2008-11-13 20:00 213
                    if(marked<0)
f3ef3495 sago007 2008-11-13 20:00 214
                        marked = buttons.size(); //not -1, since exit is after the last element in the list
f3ef3495 sago007 2008-11-13 20:00 215
                }
f3ef3495 sago007 2008-11-13 20:00 216
f3ef3495 sago007 2008-11-13 20:00 217
                if (event.key.keysym.sym == SDLK_DOWN)
f3ef3495 sago007 2008-11-13 20:00 218
                {
f3ef3495 sago007 2008-11-13 20:00 219
                    marked++;
f3ef3495 sago007 2008-11-13 20:00 220
                    if(marked>buttons.size())
f3ef3495 sago007 2008-11-13 20:00 221
                        marked = 0; 
f3ef3495 sago007 2008-11-13 20:00 222
                }
b7590374 sago007 2011-05-19 16:15 223
b7590374 sago007 2011-05-19 16:15 224
                if(event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
b7590374 sago007 2011-05-19 16:15 225
                    if(marked < buttons.size())
fc8e3d6a sago007 2011-06-25 16:31 226
                        buttons.at(marked)->doAction();
b7590374 sago007 2011-05-19 16:15 227
                    if(marked == buttons.size())
b7590374 sago007 2011-05-19 16:15 228
                        running = false;
b7590374 sago007 2011-05-19 16:15 229
                }
f3ef3495 sago007 2008-11-13 20:00 230
            }
f3ef3495 sago007 2008-11-13 20:00 231
            
f3ef3495 sago007 2008-11-13 20:00 232
f3ef3495 sago007 2008-11-13 20:00 233
        }
b7590374 sago007 2011-05-19 16:15 234
b7590374 sago007 2011-05-19 16:15 235
        for(int i=0;i<buttons.size();i++) {
fc8e3d6a sago007 2011-06-25 16:31 236
            buttons.at(i)->marked = (i == marked);
b7590374 sago007 2011-05-19 16:15 237
        }
b7590374 sago007 2011-05-19 16:15 238
        exit.marked = (marked == buttons.size());
b7590374 sago007 2011-05-19 16:15 239
            SDL_GetMouseState(&mousex,&mousey);
f3ef3495 sago007 2008-11-13 20:00 240
        
f3ef3495 sago007 2008-11-13 20:00 241
        drawSelf();
f3ef3495 sago007 2008-11-13 20:00 242
        SDL_Flip(screen);
f3ef3495 sago007 2008-11-13 20:00 243
    }
f3ef3495 sago007 2008-11-13 20:00 244
}
1970-01-01 00:00 245