git repos / blockattack-game

commit c0a3362c

sago007 · 2016-10-07 20:23
c0a3362c69c4e1cc6675c5b48c9803509f0db5f3 patch · browse files
parent 4cd8ea09136d64f5802b5e6071cd9a5d0eccb185

Started seperating the draw logic from the buttons

Changed files

M source/code/MenuSystem.cpp before
M source/code/MenuSystem.h before
diff --git a/source/code/MenuSystem.cpp b/source/code/MenuSystem.cpp index af23f0d..ccc216a 100644 --- a/source/code/MenuSystem.cpp +++ b/source/code/MenuSystem.cpp
@@ -26,6 +26,7 @@ http://www.blockattack.net
#include "common.h"
#include "global.hpp"
#include "gamecontroller.h"
+#include "BlockGame.hpp"
static int mousex;
static int mousey;
@@ -49,7 +50,6 @@ void ButtonGfx::setSurfaces() {
}
Button::Button() {
- gfx = &standardButton;
label = "";
marked = false;
action = nullptr;
@@ -60,7 +60,6 @@ Button::~Button() {
}
Button::Button(const Button& b) {
- gfx = b.gfx;
label = b.label;
marked = b.marked;
action = b.action;
@@ -76,7 +75,7 @@ void Button::setAction(void (*action2run)(Button*)) {
}
bool Button::isClicked(int x,int y) {
- if ( x >= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize) {
+ if ( x >= this->x && y >= this->y && x<= this->x+standardButton.xsize && y <= this->y + standardButton.ysize) {
return true;
}
return false;
@@ -90,20 +89,6 @@ void Button::doAction() {
cerr << "Warning: button \"" << label << "\" has no action assigned!";
}
-void Button::drawToScreen() {
-#if DEBUG
- //cout << "Painting button: " << label << " at: " << x << "," << y << "\n";
-#endif
- if (marked) {
- spriteHolder->GetSprite(menu_marked).Draw(screen, SDL_GetTicks(), x, y);
- }
- else {
- spriteHolder->GetSprite(menu_unmarked).Draw(screen, SDL_GetTicks(), x, y);
- }
-
- gfx->thefont->draw(screen, x+gfx->xsize/2,y+gfx->ysize/2-gfx->thefont->getHeight("%s", label.c_str())/2, NFont::CENTER, "%s", label.c_str());
-}
-
void Button::setPopOnRun(bool popOnRun) {
this->popOnRun = popOnRun;
}
@@ -112,25 +97,33 @@ bool Button::isPopOnRun() const {
return popOnRun;
}
-void Button::setGfx(ButtonGfx* gfx) {
- this->gfx = gfx;
-}
int Button::getHeight() const {
- return this->gfx->ysize;
+ return standardButton.ysize;
}
int Button::getWidth() const {
- return this->gfx->xsize;
+ return standardButton.xsize;
+}
+
+static void drawToScreen(Button &b) {
+ if (b.marked) {
+ spriteHolder->GetSprite(menu_marked).Draw(screen, SDL_GetTicks(), b.x, b.y);
+ }
+ else {
+ spriteHolder->GetSprite(menu_unmarked).Draw(screen, SDL_GetTicks(), b.x, b.y);
+ }
+
+ standardButton.thefont->draw(screen, b.x+standardButton.xsize/2,b.y+standardButton.ysize/2-standardButton.thefont->getHeight("%s", b.label.c_str())/2, NFont::CENTER, "%s", b.label.c_str());
}
void Menu::drawSelf() {
DrawBackground(screen);
vector<Button*>::iterator it;
for (it = buttons.begin(); it < buttons.end(); it++) {
- (*it)->drawToScreen();
+ drawToScreen(**it);
}
- exit.drawToScreen();
+ drawToScreen(exit);
standardButton.thefont->draw(screen, 50, 50, "%s", title.c_str());
mouse.Draw(screen, SDL_GetTicks(), mousex, mousey);
}
diff --git a/source/code/MenuSystem.h b/source/code/MenuSystem.h index b9dea1c..85f3f43 100644 --- a/source/code/MenuSystem.h +++ b/source/code/MenuSystem.h
@@ -49,17 +49,15 @@ extern ButtonGfx standardButton;
class Button
{
private:
- //The label. This is written on the button
- std::string label;
//Pointer to a callback function.
void (*action)(Button *b);
//If true the menu should also be closed then the button is clicked
bool popOnRun = false;
- ButtonGfx *gfx = nullptr;
-
public:
+ //The label. This is written on the button
+ std::string label;
//Is the button marked?
bool marked = false;
//Where is the button on the screen
@@ -78,10 +76,8 @@ public:
bool isClicked(int x,int y); //Returns true if (x,y) is within the borders of the button
virtual void doAction(); //Run the callback function
- void drawToScreen(void);
void setPopOnRun(bool popOnRun);
bool isPopOnRun() const;
- void setGfx(ButtonGfx* gfx); //Draws to screen
int getHeight() const;
int getWidth() const;