From e84f0fc6e5301265708a99b13ab898ce45422611 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Thu, 26 Jan 2012 18:06:23 +0000 Subject: [PATCH] Ellipse cursor --- src/game/Brush.h | 1 + src/game/EllipseBrush.h | 60 +++++++++++++++++++++++++++++++++++++ src/game/GameController.cpp | 4 +++ src/game/GameController.h | 1 + src/game/GameModel.cpp | 21 ++++++++++++- src/game/GameModel.h | 6 +++- src/game/GameView.cpp | 6 +++- src/interface/Keys.h | 1 + 8 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/game/EllipseBrush.h diff --git a/src/game/Brush.h b/src/game/Brush.h index cc5e819a3..9daf72193 100644 --- a/src/game/Brush.h +++ b/src/game/Brush.h @@ -12,6 +12,7 @@ class Brush { +protected: bool * bitmap; ui::Point size; public: diff --git a/src/game/EllipseBrush.h b/src/game/EllipseBrush.h new file mode 100644 index 000000000..7e5825bfb --- /dev/null +++ b/src/game/EllipseBrush.h @@ -0,0 +1,60 @@ +/* + * ElipseBrush.h + * + * Created on: Jan 26, 2012 + * Author: Simon + */ + +#ifndef ELIPSEBRUSH_H_ +#define ELIPSEBRUSH_H_ + +#include "Brush.h" + +class EllipseBrush: public Brush +{ +public: + EllipseBrush(ui::Point size_): + Brush(size_) + { + + }; + //Draw the brush outline onto the screen + virtual void Render(Graphics * g, ui::Point position) + { + if(!bitmap) + GenerateBitmap(); + //g->fillrect(position.X-size.X-1, position.Y-size.Y-1, (size.X*2)+2, (size.Y*2)+2, 255, 0, 255, 70); + for(int x = 0; x <= size.X*2; x++) + { + for(int y = 0; y <= size.Y*2; y++) + { + if(bitmap[y*(size.X*2)+x]) + g->blendpixel(position.X-size.X+x, position.Y-size.Y+y, 255, 0, 255, 70); + } + } + } + virtual void GenerateBitmap() + { + if(bitmap) + free(bitmap); + bitmap = (bool *)malloc(sizeof(bool)*(((size.X*2)+1)*((size.Y*2)+1))); + int rx = size.X; + int ry = size.Y; + for(int x = 0; x <= size.X*2; x++) + { + for(int y = 0; y <= size.Y*2; y++) + { + if((pow(x-size.X,2)*pow(ry,2)+pow(y-size.Y,2)*pow(rx,2)<=pow(rx,2)*pow(ry,2))) + { + bitmap[y*(size.X*2)+x] = true; + } + else + { + bitmap[y*(size.X*2)+x] = false; + } + } + } + } +}; + +#endif /* ELIPSEBRUSH_H_ */ diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 90ef70cda..277c32c1e 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -226,6 +226,10 @@ void GameController::Vote(int direction) //TODO: Implement } +void GameController::ChangeBrush() +{ + gameModel->SetBrush(gameModel->GetBrushID()+1); +} void GameController::ClearSim() { diff --git a/src/game/GameController.h b/src/game/GameController.h index 28edd861c..2ec4523a3 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -47,6 +47,7 @@ public: void ClearSim(); void ReloadSim(); void Vote(int direction); + void ChangeBrush(); }; #endif // GAMECONTROLLER_H diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 3085b091d..5dfc521dd 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -5,13 +5,14 @@ #include "Renderer.h" #include "interface/Point.h" #include "Brush.h" +#include "EllipseBrush.h" GameModel::GameModel(): activeTool(NULL), sim(NULL), ren(NULL), currentSave(NULL), - currentBrush(new Brush(ui::Point(4, 4))), + currentBrush(0), currentUser(0, "") { sim = new Simulation(); @@ -39,6 +40,9 @@ GameModel::GameModel(): //sim->wtypes[i] } + brushList.push_back(new Brush(ui::Point(4, 4))); + brushList.push_back(new EllipseBrush(ui::Point(4, 4))); + activeTool = new ElementTool(1, "TURD", 0, 0, 0); } @@ -52,6 +56,10 @@ GameModel::~GameModel() } delete menuList[i]; } + for(int i = 0; i < brushList.size(); i++) + { + delete brushList[i]; + } delete sim; delete ren; if(activeTool) @@ -59,10 +67,21 @@ GameModel::~GameModel() } Brush * GameModel::GetBrush() +{ + return brushList[currentBrush]; +} + +int GameModel::GetBrushID() { return currentBrush; } +void GameModel::SetBrush(int i) +{ + currentBrush = i%brushList.size(); + notifyBrushChanged(); +} + void GameModel::AddObserver(GameView * observer){ observers.push_back(observer); diff --git a/src/game/GameModel.h b/src/game/GameModel.h index b8f52175b..72e28758f 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -25,7 +25,8 @@ private: vector toolList; vector menuList; Menu * activeMenu; - Brush * currentBrush; + int currentBrush; + vector brushList; Save * currentSave; Simulation * sim; Renderer * ren; @@ -43,6 +44,7 @@ private: public: GameModel(); ~GameModel(); + Save * GetSave(); Brush * GetBrush(); void SetSave(Save * newSave); @@ -58,6 +60,8 @@ public: Menu * GetActiveMenu(); User GetUser(); void SetUser(User user); + void SetBrush(int i); + int GetBrushID(); Simulation * GetSimulation(); Renderer * GetRenderer(); }; diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 163c2f7c9..ec87f64a3 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -3,6 +3,7 @@ #include "interface/Window.h" #include "interface/Button.h" #include "interface/Colour.h" +#include "interface/Keys.h" GameView::GameView(): ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)), @@ -399,9 +400,12 @@ void GameView::OnKeyPress(int key, bool shift, bool ctrl, bool alt) { switch(key) { - case ' ': + case ' ': //Space c->SetPaused(); break; + case KEY_TAB: //Tab + c->ChangeBrush(); + break; } } diff --git a/src/interface/Keys.h b/src/interface/Keys.h index e92370367..22fffa273 100644 --- a/src/interface/Keys.h +++ b/src/interface/Keys.h @@ -6,3 +6,4 @@ #define KEY_END SDLK_END #define KEY_BACKSPACE SDLK_BACKSPACE #define KEY_DELETE SDLK_DELETE +#define KEY_TAB SDLK_TAB