git repos / blockattack-game

blame: source/code/MenuSystem.h

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
    blockattack@poulsander.com
f3ef3495 sago007 2008-11-13 20:00 20
    http://blockattack.sf.net
f3ef3495 sago007 2008-11-13 20:00 21
*/
f3ef3495 sago007 2008-11-13 20:00 22
f3ef3495 sago007 2008-11-13 20:00 23
f3ef3495 sago007 2008-11-13 20:00 24
#ifndef _MENUSYSTEM_H
f3ef3495 sago007 2008-11-13 20:00 25
#define	_MENUSYSTEM_H
f3ef3495 sago007 2008-11-13 20:00 26
f3ef3495 sago007 2008-11-13 20:00 27
#include <string>
f3ef3495 sago007 2008-11-13 20:00 28
#include "SDL.h"
f3ef3495 sago007 2008-11-13 20:00 29
#include <vector>
b7590374 sago007 2011-05-19 16:15 30
#include "Libs/NFont.h"
b7590374 sago007 2011-05-19 16:15 31
#include "CppSdl/CppSdlImageHolder.hpp"
f3ef3495 sago007 2008-11-13 20:00 32
f3ef3495 sago007 2008-11-13 20:00 33
using namespace std;
f3ef3495 sago007 2008-11-13 20:00 34
f3ef3495 sago007 2008-11-13 20:00 35
//The ButtonGfx object hold common media for all buttons, so we can reskin them by only changeing one pointer
f3ef3495 sago007 2008-11-13 20:00 36
class ButtonGfx {
f3ef3495 sago007 2008-11-13 20:00 37
    public:
f3ef3495 sago007 2008-11-13 20:00 38
        //Holds the graphic for a button that is selected
b7590374 sago007 2011-05-19 16:15 39
        static CppSdl::CppSdlImageHolder _marked;
f3ef3495 sago007 2008-11-13 20:00 40
        //Holds the graphic for a button that is not selected
b7590374 sago007 2011-05-19 16:15 41
        static CppSdl::CppSdlImageHolder _unmarked;
f3ef3495 sago007 2008-11-13 20:00 42
        //The size of the buttons, so we don't have to ask w and h from the SDL Surfaces each time
f3ef3495 sago007 2008-11-13 20:00 43
        static int xsize;
f3ef3495 sago007 2008-11-13 20:00 44
        static int ysize;
f3ef3495 sago007 2008-11-13 20:00 45
        //A TTFont used for writing the label on the buttons
b7590374 sago007 2011-05-19 16:15 46
        static NFont thefont;
b7590374 sago007 2011-05-19 16:15 47
        static void setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked);
f3ef3495 sago007 2008-11-13 20:00 48
};
f3ef3495 sago007 2008-11-13 20:00 49
f3ef3495 sago007 2008-11-13 20:00 50
//A button
f3ef3495 sago007 2008-11-13 20:00 51
class Button {
f3ef3495 sago007 2008-11-13 20:00 52
    private:
f3ef3495 sago007 2008-11-13 20:00 53
        //The label. This is written on the button
f3ef3495 sago007 2008-11-13 20:00 54
        string label;
f3ef3495 sago007 2008-11-13 20:00 55
        //Pointer to a callback function.
26ddb424 sago007 2011-06-09 20:06 56
        void (*action)(Button *b);
f3ef3495 sago007 2008-11-13 20:00 57
        
f3ef3495 sago007 2008-11-13 20:00 58
    public:
f3ef3495 sago007 2008-11-13 20:00 59
        //Is the button marked?
f3ef3495 sago007 2008-11-13 20:00 60
        bool marked;
f3ef3495 sago007 2008-11-13 20:00 61
        //Where is the button on the screen
f3ef3495 sago007 2008-11-13 20:00 62
        int x;
f3ef3495 sago007 2008-11-13 20:00 63
        int y;
f3ef3495 sago007 2008-11-13 20:00 64
    
f3ef3495 sago007 2008-11-13 20:00 65
        Button();
f3ef3495 sago007 2008-11-13 20:00 66
        Button(const Button& b);
f3ef3495 sago007 2008-11-13 20:00 67
        ~Button();
f3ef3495 sago007 2008-11-13 20:00 68
        
f3ef3495 sago007 2008-11-13 20:00 69
        
f3ef3495 sago007 2008-11-13 20:00 70
        //Set the text to write on the button
f3ef3495 sago007 2008-11-13 20:00 71
        void setLabel(string text);
f3ef3495 sago007 2008-11-13 20:00 72
        //Set the action to run
26ddb424 sago007 2011-06-09 20:06 73
        void setAction(void (*action2run)(Button*));
f3ef3495 sago007 2008-11-13 20:00 74
        
f3ef3495 sago007 2008-11-13 20:00 75
        bool isClicked(int x,int y); //Returns true if (x,y) is within the borders of the button
26ddb424 sago007 2011-06-09 20:06 76
        virtual void doAction(); //Run the callback function
b7590374 sago007 2011-05-19 16:15 77
        void drawTo(SDL_Surface **surface); //Draws to screen
f3ef3495 sago007 2008-11-13 20:00 78
};
f3ef3495 sago007 2008-11-13 20:00 79
26ddb424 sago007 2011-06-09 20:06 80
f3ef3495 sago007 2008-11-13 20:00 81
class Menu {
f3ef3495 sago007 2008-11-13 20:00 82
    private:
fc8e3d6a sago007 2011-06-25 16:31 83
        vector<Button*> buttons; //Vector holder the buttons
f3ef3495 sago007 2008-11-13 20:00 84
        Button exit; //The exit button is special since it does not have a callback function
f3ef3495 sago007 2008-11-13 20:00 85
        bool isSubmenu; //True if the menu is a submenu
f3ef3495 sago007 2008-11-13 20:00 86
        int marked; //The index of the marked button (for keyboard up/down)
f3ef3495 sago007 2008-11-13 20:00 87
        bool running; //The menu is running. The menu will terminate then this is false
f3ef3495 sago007 2008-11-13 20:00 88
        SDL_Surface *screen; //Pointer to the screen to draw to
f3ef3495 sago007 2008-11-13 20:00 89
//        SDL_Surface *background; //Pointer to the background image
f3ef3495 sago007 2008-11-13 20:00 90
        
f3ef3495 sago007 2008-11-13 20:00 91
        void drawSelf();        //Private function to draw the screen
f3ef3495 sago007 2008-11-13 20:00 92
        void performClick(int x, int y); //Private function to call then a click is detected.
f3ef3495 sago007 2008-11-13 20:00 93
        void placeButtons(); //Rearanges the buttons to the correct place.
f3ef3495 sago007 2008-11-13 20:00 94
    public:
f3ef3495 sago007 2008-11-13 20:00 95
        //numberOfItems is the expected numberOfItems for vector initialization
f3ef3495 sago007 2008-11-13 20:00 96
        //SubMenu is true by default
f3ef3495 sago007 2008-11-13 20:00 97
        Menu(SDL_Surface **screen,bool isSubmenu);
f3ef3495 sago007 2008-11-13 20:00 98
        Menu(SDL_Surface **screen);
f3ef3495 sago007 2008-11-13 20:00 99
        
f3ef3495 sago007 2008-11-13 20:00 100
        //Add a button to the menu
fc8e3d6a sago007 2011-06-25 16:31 101
        void addButton(Button *b);
f3ef3495 sago007 2008-11-13 20:00 102
        
f3ef3495 sago007 2008-11-13 20:00 103
        //Run the menu
f3ef3495 sago007 2008-11-13 20:00 104
        void run();
f3ef3495 sago007 2008-11-13 20:00 105
};
f3ef3495 sago007 2008-11-13 20:00 106
f3ef3495 sago007 2008-11-13 20:00 107
#endif	/* _MENUSYSTEM_H */
f3ef3495 sago007 2008-11-13 20:00 108
1970-01-01 00:00 109