diff --git a/source/code/MenuSystem.cc b/source/code/MenuSystem.cc new file mode 100644 index 0000000..5e2997e --- /dev/null +++ b/source/code/MenuSystem.cc @@ -0,0 +1,227 @@ +/* +Block Attack - Rise of the Blocks, SDL game, besed on Nintendo's Tetris Attack +Copyright (C) 2008 Poul Sander + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Poul Sander + R�vehjvej 36, V. 1111 + 2800 Kgs. Lyngby + DENMARK + blockattack@poulsander.com + http://blockattack.sf.net +*/ + +#include "MenuSystem.h" +#include "common.h" + +extern SDL_Surface *mouse; +extern SDL_Surface *backgroundImage; +extern bool highPriority; +int mousex; +int mousey; + +/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/ +inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y) +{ + SDL_Rect dest; + dest.x = x; + dest.y = y; + SDL_BlitSurface(img, NULL, target, &dest); +} + +SDL_Surface* ButtonGfx::marked; +SDL_Surface* ButtonGfx::unmarked; +int ButtonGfx::xsize; +int ButtonGfx::ysize; +TTFont* ButtonGfx::ttf; + +void ButtonGfx::setSurfaces(SDL_Surface **marked,SDL_Surface **unmarked) +{ + ButtonGfx::marked = *marked; + ButtonGfx::unmarked = *unmarked; + xsize=(*marked)->w; + ysize=(*marked)->h; +} + +Button::Button() +{ + label = ""; + marked = false; + surfaceMarked = SDL_ConvertSurface(ButtonGfx::marked, ButtonGfx::marked->format, SDL_SWSURFACE); + surfaceUnmarked = SDL_ConvertSurface(ButtonGfx::unmarked, ButtonGfx::unmarked->format, SDL_SWSURFACE); +} + +Button::~Button() +{ + SDL_FreeSurface(surfaceMarked); + SDL_FreeSurface(surfaceUnmarked); +} + +Button::Button(const Button& b) +{ + label = b.label; + marked = b.marked; + surfaceMarked = SDL_ConvertSurface(ButtonGfx::marked, ButtonGfx::marked->format, SDL_SWSURFACE); + surfaceUnmarked = SDL_ConvertSurface(ButtonGfx::unmarked, ButtonGfx::unmarked->format, SDL_SWSURFACE); +} + +void Button::setLabel(string text) +{ + label = text; +} + +void Button::setAction(void (*action2run)()) +{ + action = action2run; +} + +bool Button::isClicked(int x,int y) +{ + if ( x >= this->x && y >= this->y && x<= this->x+ButtonGfx::xsize && y <= this->y + ButtonGfx::ysize) + return true; + else + return false; +} + +void Button::doAction() +{ + action(); +} + +void Button::drawTo(SDL_Surface *surface) +{ + #if DEBUG + cout << "Painting button: " << label << endl; + #endif + if (marked) + DrawIMG(surfaceMarked,surface,x,y); + else + DrawIMG(surfaceUnmarked,surface,x,y); + //int stringx = x + (ButtonGfx::xsize)/2 - ButtonGfx::ttf->getTextWidth(label)/2; + //int stringy = y + (ButtonGfx::ysize)/2 - ButtonGfx::ttf->getTextHeight()/2; + //ButtonGfx::ttf->writeText(label,surface,stringx,stringy); +} + +void Menu::drawSelf() +{ + DrawIMG(backgroundImage,screen,0,0); + vector