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