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);
b7590374 sago007 2011-05-19 16:15 124
    mouse.PaintTo(screen,mousex,mousey);
f3ef3495 sago007 2008-11-13 20:00 125
}
f3ef3495 sago007 2008-11-13 20:00 126
f3ef3495 sago007 2008-11-13 20:00 127
void Menu::performClick(int x,int y)
f3ef3495 sago007 2008-11-13 20:00 128
{
fc8e3d6a sago007 2011-06-25 16:31 129
    vector<Button*>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 130
    for(it = buttons.begin();it < buttons.end(); it++)
f3ef3495 sago007 2008-11-13 20:00 131
    {
fc8e3d6a sago007 2011-06-25 16:31 132
        Button *b = (*it);
fc8e3d6a sago007 2011-06-25 16:31 133
        if(b->isClicked(x,y))
fc8e3d6a sago007 2011-06-25 16:31 134
            b->doAction();
f3ef3495 sago007 2008-11-13 20:00 135
    }
f3ef3495 sago007 2008-11-13 20:00 136
    if(exit.isClicked(x,y))
f3ef3495 sago007 2008-11-13 20:00 137
        running = false;
f3ef3495 sago007 2008-11-13 20:00 138
}
f3ef3495 sago007 2008-11-13 20:00 139
f3ef3495 sago007 2008-11-13 20:00 140
void Menu::placeButtons()
f3ef3495 sago007 2008-11-13 20:00 141
{
f3ef3495 sago007 2008-11-13 20:00 142
    int nextY = 50;
f3ef3495 sago007 2008-11-13 20:00 143
    const int X = 50;
fc8e3d6a sago007 2011-06-25 16:31 144
    vector<Button*>::iterator it;
f3ef3495 sago007 2008-11-13 20:00 145
    for(it = buttons.begin();it < buttons.end(); it++)
f3ef3495 sago007 2008-11-13 20:00 146
    {
fc8e3d6a sago007 2011-06-25 16:31 147
        (*it)->x = X;
fc8e3d6a sago007 2011-06-25 16:31 148
        (*it)->y = nextY;
b7590374 sago007 2011-05-19 16:15 149
        nextY += ButtonGfx::ysize+10;
f3ef3495 sago007 2008-11-13 20:00 150
    }
f3ef3495 sago007 2008-11-13 20:00 151
    exit.x = X;
f3ef3495 sago007 2008-11-13 20:00 152
    exit.y = nextY;
f3ef3495 sago007 2008-11-13 20:00 153
}
f3ef3495 sago007 2008-11-13 20:00 154
fc8e3d6a sago007 2011-06-25 16:31 155
void Menu::addButton(Button *b)
f3ef3495 sago007 2008-11-13 20:00 156
{
f3ef3495 sago007 2008-11-13 20:00 157
    buttons.push_back(b);
fc8e3d6a sago007 2011-06-25 16:31 158
    b->marked = false;
f3ef3495 sago007 2008-11-13 20:00 159
    placeButtons();
f3ef3495 sago007 2008-11-13 20:00 160
}
f3ef3495 sago007 2008-11-13 20:00 161
f3ef3495 sago007 2008-11-13 20:00 162
Menu::Menu(SDL_Surface **screen)
f3ef3495 sago007 2008-11-13 20:00 163
{
f3ef3495 sago007 2008-11-13 20:00 164
    this->screen = *screen; 
fc8e3d6a sago007 2011-06-25 16:31 165
    buttons = vector<Button*>(10);
f3ef3495 sago007 2008-11-13 20:00 166
    isSubmenu = true;
f3ef3495 sago007 2008-11-13 20:00 167
    exit.setLabel("Back");
f3ef3495 sago007 2008-11-13 20:00 168
}
f3ef3495 sago007 2008-11-13 20:00 169
f3ef3495 sago007 2008-11-13 20:00 170
Menu::Menu(SDL_Surface **screen,bool submenu)
f3ef3495 sago007 2008-11-13 20:00 171
{
f3ef3495 sago007 2008-11-13 20:00 172
    this->screen = *screen;
fc8e3d6a sago007 2011-06-25 16:31 173
    buttons = vector<Button*>(0);
f3ef3495 sago007 2008-11-13 20:00 174
    isSubmenu = submenu;
f3ef3495 sago007 2008-11-13 20:00 175
    if(isSubmenu)
f3ef3495 sago007 2008-11-13 20:00 176
        exit.setLabel("Back");
f3ef3495 sago007 2008-11-13 20:00 177
    else
f3ef3495 sago007 2008-11-13 20:00 178
        exit.setLabel("Exit");
f3ef3495 sago007 2008-11-13 20:00 179
}
f3ef3495 sago007 2008-11-13 20:00 180
f3ef3495 sago007 2008-11-13 20:00 181
void Menu::run()
f3ef3495 sago007 2008-11-13 20:00 182
{
f3ef3495 sago007 2008-11-13 20:00 183
    running = true;
f3ef3495 sago007 2008-11-13 20:00 184
    while(running)
f3ef3495 sago007 2008-11-13 20:00 185
    {
f3ef3495 sago007 2008-11-13 20:00 186
        if (!(highPriority)) SDL_Delay(10);
f3ef3495 sago007 2008-11-13 20:00 187
f3ef3495 sago007 2008-11-13 20:00 188
       
f3ef3495 sago007 2008-11-13 20:00 189
        SDL_Event event;
f3ef3495 sago007 2008-11-13 20:00 190
f3ef3495 sago007 2008-11-13 20:00 191
        while ( SDL_PollEvent(&event) )
f3ef3495 sago007 2008-11-13 20:00 192
        {
f3ef3495 sago007 2008-11-13 20:00 193
            if ( event.type == SDL_QUIT )  {
f3ef3495 sago007 2008-11-13 20:00 194
                running = false;
f3ef3495 sago007 2008-11-13 20:00 195
            }
f3ef3495 sago007 2008-11-13 20:00 196
f3ef3495 sago007 2008-11-13 20:00 197
            if ( event.type == SDL_KEYDOWN )
f3ef3495 sago007 2008-11-13 20:00 198
            {
f3ef3495 sago007 2008-11-13 20:00 199
                if ( event.key.keysym.sym == SDLK_ESCAPE )
f3ef3495 sago007 2008-11-13 20:00 200
                {
f3ef3495 sago007 2008-11-13 20:00 201
                    running = false;
f3ef3495 sago007 2008-11-13 20:00 202
                }
f3ef3495 sago007 2008-11-13 20:00 203
f3ef3495 sago007 2008-11-13 20:00 204
                if (event.key.keysym.sym == SDLK_UP)
f3ef3495 sago007 2008-11-13 20:00 205
                {
f3ef3495 sago007 2008-11-13 20:00 206
                    marked--;
f3ef3495 sago007 2008-11-13 20:00 207
                    if(marked<0)
f3ef3495 sago007 2008-11-13 20:00 208
                        marked = buttons.size(); //not -1, since exit is after the last element in the list
f3ef3495 sago007 2008-11-13 20:00 209
                }
f3ef3495 sago007 2008-11-13 20:00 210
f3ef3495 sago007 2008-11-13 20:00 211
                if (event.key.keysym.sym == SDLK_DOWN)
f3ef3495 sago007 2008-11-13 20:00 212
                {
f3ef3495 sago007 2008-11-13 20:00 213
                    marked++;
f3ef3495 sago007 2008-11-13 20:00 214
                    if(marked>buttons.size())
f3ef3495 sago007 2008-11-13 20:00 215
                        marked = 0; 
f3ef3495 sago007 2008-11-13 20:00 216
                }
b7590374 sago007 2011-05-19 16:15 217
b7590374 sago007 2011-05-19 16:15 218
                if(event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
b7590374 sago007 2011-05-19 16:15 219
                    if(marked < buttons.size())
fc8e3d6a sago007 2011-06-25 16:31 220
                        buttons.at(marked)->doAction();
b7590374 sago007 2011-05-19 16:15 221
                    if(marked == buttons.size())
b7590374 sago007 2011-05-19 16:15 222
                        running = false;
b7590374 sago007 2011-05-19 16:15 223
                }
f3ef3495 sago007 2008-11-13 20:00 224
            }
f3ef3495 sago007 2008-11-13 20:00 225
            
f3ef3495 sago007 2008-11-13 20:00 226
f3ef3495 sago007 2008-11-13 20:00 227
        }
b7590374 sago007 2011-05-19 16:15 228
b7590374 sago007 2011-05-19 16:15 229
        for(int i=0;i<buttons.size();i++) {
fc8e3d6a sago007 2011-06-25 16:31 230
            buttons.at(i)->marked = (i == marked);
b7590374 sago007 2011-05-19 16:15 231
        }
b7590374 sago007 2011-05-19 16:15 232
        exit.marked = (marked == buttons.size());
b7590374 sago007 2011-05-19 16:15 233
            SDL_GetMouseState(&mousex,&mousey);
f3ef3495 sago007 2008-11-13 20:00 234
        
f3ef3495 sago007 2008-11-13 20:00 235
        drawSelf();
f3ef3495 sago007 2008-11-13 20:00 236
        SDL_Flip(screen);
f3ef3495 sago007 2008-11-13 20:00 237
    }
f3ef3495 sago007 2008-11-13 20:00 238
}
1970-01-01 00:00 239