summaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-22 14:45:37 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-22 14:45:37 (GMT)
commit19c1fa5dcb4c4a2ba9d692e136b17da316a2631b (patch)
tree8436e6674dc4375f0392a9142be1a06ddf888337 /src/game
parent91bb5a8b781fba33901c0a2804b86055ed588aa4 (diff)
downloadpowder-19c1fa5dcb4c4a2ba9d692e136b17da316a2631b.zip
powder-19c1fa5dcb4c4a2ba9d692e136b17da316a2631b.tar.gz
Brush class for drawing on simulation, more interface for game
Diffstat (limited to 'src/game')
-rw-r--r--src/game/Brush.h65
-rw-r--r--src/game/GameController.cpp58
-rw-r--r--src/game/GameController.h9
-rw-r--r--src/game/GameModel.cpp44
-rw-r--r--src/game/GameModel.h10
-rw-r--r--src/game/GameView.cpp86
-rw-r--r--src/game/GameView.h6
7 files changed, 255 insertions, 23 deletions
diff --git a/src/game/Brush.h b/src/game/Brush.h
new file mode 100644
index 0000000..cc5e819
--- /dev/null
+++ b/src/game/Brush.h
@@ -0,0 +1,65 @@
+/*
+ * Brush.h
+ *
+ * Created on: Jan 22, 2012
+ * Author: Simon
+ */
+
+#ifndef BRUSH_H_
+#define BRUSH_H_
+
+#include "interface/Point.h"
+
+class Brush
+{
+ bool * bitmap;
+ ui::Point size;
+public:
+ Brush(ui::Point size_):
+ bitmap(NULL),
+ size(size_)
+ {
+
+ };
+ ui::Point GetRadius()
+ {
+ return size;
+ }
+ void SetRadius(ui::Point size)
+ {
+ this->size = size;
+ GenerateBitmap();
+ }
+ virtual ~Brush() {
+ if(bitmap)
+ delete bitmap;
+ }
+ //Draw the brush outline onto the screen
+ virtual void Render(Graphics * g, ui::Point position)
+ {
+ g->fillrect(position.X-size.X-1, position.Y-size.Y-1, (size.X*2)+2, (size.Y*2)+2, 255, 0, 255, 70);
+ }
+ virtual void GenerateBitmap()
+ {
+ if(bitmap)
+ free(bitmap);
+ bitmap = (bool *)malloc(sizeof(bool)*(((size.X*2)+1)*((size.Y*2)+1)));
+ for(int x = 0; x <= size.X*2; x++)
+ {
+ for(int y = 0; y <= size.Y*2; y++)
+ {
+ bitmap[y*(size.X*2)+x] = true;
+ }
+ }
+ }
+ //Get a bitmap for drawing particles
+ bool * GetBitmap()
+ {
+ if(!bitmap)
+ GenerateBitmap();
+ return bitmap;
+ }
+};
+
+
+#endif /* BRUSH_H_ */
diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp
index 571833a..b9c534c 100644
--- a/src/game/GameController.cpp
+++ b/src/game/GameController.cpp
@@ -37,10 +37,21 @@ GameView * GameController::GetView()
return gameView;
}
+void GameController::AdjustBrushSize(int direction)
+{
+ ui::Point newSize = gameModel->GetBrush()->GetRadius() + ui::Point(direction, direction);
+ if(newSize.X<0)
+ newSize.X = 0;
+ if(newSize.Y<0)
+ newSize.Y = 0;
+ gameModel->GetBrush()->SetRadius(newSize);
+}
+
void GameController::DrawPoints(queue<ui::Point*> & pointQueue)
{
Simulation * sim = gameModel->GetSimulation();
int activeElement = gameModel->GetActiveElement();
+ Brush * cBrush = gameModel->GetBrush();
if(!pointQueue.empty())
{
ui::Point * sPoint = NULL;
@@ -50,12 +61,12 @@ void GameController::DrawPoints(queue<ui::Point*> & pointQueue)
pointQueue.pop();
if(sPoint)
{
- sim->create_line(fPoint->X, fPoint->Y, sPoint->X, sPoint->Y, 1, 1, activeElement, 0);
+ sim->create_line(fPoint->X, fPoint->Y, sPoint->X, sPoint->Y, 1, 1, activeElement, 0, cBrush);
delete sPoint;
}
else
{
- sim->create_parts(fPoint->X, fPoint->Y, 1, 1, activeElement, 0);
+ sim->create_parts(fPoint->X, fPoint->Y, 1, 1, activeElement, 0, cBrush);
}
sPoint = fPoint;
}
@@ -79,3 +90,46 @@ void GameController::OpenSearch()
search = new SearchController();
ui::Engine::Ref().ShowWindow(search->GetView());
}
+
+void GameController::OpenLogin()
+{
+ //TODO: Implement
+}
+
+void GameController::OpenTags()
+{
+ //TODO: Implement
+}
+
+void GameController::OpenDisplayOptions()
+{
+ //TODO: Implement
+}
+
+void GameController::OpenRenderOptions()
+{
+ //TODO: Implement
+}
+
+void GameController::OpenSaveWindow()
+{
+ //TODO: Implement
+}
+
+void GameController::Vote(int direction)
+{
+ //TODO: Implement
+}
+
+
+void GameController::ClearSim()
+{
+ gameModel->ClearSimulation();
+}
+
+void GameController::ReloadSim()
+{
+ //TODO: Implement
+}
+
+
diff --git a/src/game/GameController.h b/src/game/GameController.h
index c2c578d..03d12e9 100644
--- a/src/game/GameController.h
+++ b/src/game/GameController.h
@@ -23,10 +23,19 @@ public:
GameController();
~GameController();
GameView * GetView();
+ void AdjustBrushSize(int direction);
void DrawPoints(queue<ui::Point*> & pointQueue);
void Tick();
void SetPaused(bool pauseState);
void OpenSearch();
+ void OpenLogin();
+ void OpenTags();
+ void OpenDisplayOptions();
+ void OpenRenderOptions();
+ void OpenSaveWindow();
+ void ClearSim();
+ void ReloadSim();
+ void Vote(int direction);
};
#endif // GAMECONTROLLER_H
diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp
index 54c8dd4..3070093 100644
--- a/src/game/GameModel.cpp
+++ b/src/game/GameModel.cpp
@@ -3,11 +3,15 @@
#include "GameView.h"
#include "simulation/Simulation.h"
#include "Renderer.h"
+#include "interface/Point.h"
+#include "Brush.h"
GameModel::GameModel():
activeElement(1),
sim(NULL),
- ren(NULL)
+ ren(NULL),
+ currentSave(NULL),
+ currentBrush(new Brush(ui::Point(4, 4)))
{
sim = new Simulation();
ren = new Renderer(ui::Engine::Ref().g, sim);
@@ -19,12 +23,19 @@ GameModel::~GameModel()
delete ren;
}
+Brush * GameModel::GetBrush()
+{
+ return currentBrush;
+}
+
void GameModel::AddObserver(GameView * observer){
observers.push_back(observer);
observer->NotifySimulationChanged(this);
observer->NotifyRendererChanged(this);
observer->NotifyPausedChanged(this);
+ observer->NotifySaveChanged(this);
+ observer->NotifyBrushChanged(this);
}
int GameModel::GetActiveElement()
@@ -37,6 +48,16 @@ void GameModel::SetActiveElement(int element)
activeElement = element;
}
+Save * GameModel::GetSave()
+{
+ return currentSave;
+}
+void GameModel::SetSave(Save * newSave)
+{
+ currentSave = newSave;
+ notifySaveChanged();
+}
+
Simulation * GameModel::GetSimulation()
{
return sim;
@@ -58,6 +79,11 @@ bool GameModel::GetPaused()
return sim->sys_pause?true:false;
}
+void GameModel::ClearSimulation()
+{
+ sim->clear_sim();
+}
+
void GameModel::notifyRendererChanged()
{
for(int i = 0; i < observers.size(); i++)
@@ -66,6 +92,14 @@ void GameModel::notifyRendererChanged()
}
}
+void GameModel::notifySaveChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifySaveChanged(this);
+ }
+}
+
void GameModel::notifySimulationChanged()
{
for(int i = 0; i < observers.size(); i++)
@@ -81,3 +115,11 @@ void GameModel::notifyPausedChanged()
observers[i]->NotifyPausedChanged(this);
}
}
+
+void GameModel::notifyBrushChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifyBrushChanged(this);
+ }
+}
diff --git a/src/game/GameModel.h b/src/game/GameModel.h
index c709535..753e19d 100644
--- a/src/game/GameModel.h
+++ b/src/game/GameModel.h
@@ -2,9 +2,11 @@
#define GAMEMODEL_H
#include <vector>
+#include "search/Save.h"
#include "simulation/Simulation.h"
#include "Renderer.h"
#include "GameView.h"
+#include "Brush.h"
using namespace std;
@@ -16,20 +18,28 @@ class GameModel
{
private:
vector<GameView*> observers;
+ Brush * currentBrush;
+ Save * currentSave;
Simulation * sim;
Renderer * ren;
int activeElement;
void notifyRendererChanged();
void notifySimulationChanged();
void notifyPausedChanged();
+ void notifySaveChanged();
+ void notifyBrushChanged();
public:
GameModel();
~GameModel();
+ Save * GetSave();
+ Brush * GetBrush();
+ void SetSave(Save * newSave);
void AddObserver(GameView * observer);
int GetActiveElement();
void SetActiveElement(int element);
bool GetPaused();
void SetPaused(bool pauseState);
+ void ClearSimulation();
Simulation * GetSimulation();
Renderer * GetRenderer();
diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp
index 619d827..7fe4fa5 100644
--- a/src/game/GameView.cpp
+++ b/src/game/GameView.cpp
@@ -7,7 +7,8 @@ GameView::GameView():
ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)),
pointQueue(queue<ui::Point*>()),
isMouseDown(false),
- ren(NULL)
+ ren(NULL),
+ activeBrush(NULL)
{
int currentX = 1;
//Set up UI
@@ -34,10 +35,10 @@ GameView::GameView():
ReloadAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->OpenSearch(); // TODO call proper function
+ v->c->ReloadSim();
}
};
- reloadButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(16, 16), "\x91"); // TODO Position?
+ reloadButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(16, 16), "\x91");
currentX+=18;
reloadButton->SetActionCallback(new ReloadAction(this));
AddComponent(reloadButton);
@@ -49,10 +50,10 @@ GameView::GameView():
SaveSimulationAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->OpenSearch(); // TODO call proper function
+ v->c->OpenSaveWindow();
}
};
- saveSimulationButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(Size.X/5, 16), "\x82"); // TODO All arguments
+ saveSimulationButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(Size.X/5, 16), "\x82");
currentX+=(Size.X/5)+2;
saveSimulationButton->SetActionCallback(new SaveSimulationAction(this));
AddComponent(saveSimulationButton);
@@ -64,10 +65,10 @@ GameView::GameView():
UpVoteAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->OpenSearch(); // TODO call proper function
+ v->c->Vote(1);
}
};
- upVoteButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(16, 16), "\xCB"); // TODO All arguments
+ upVoteButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(16, 16), "\xCB");
currentX+=16;
upVoteButton->SetActionCallback(new UpVoteAction(this));
AddComponent(upVoteButton);
@@ -79,10 +80,10 @@ GameView::GameView():
DownVoteAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->OpenSearch(); // TODO call proper function
+ v->c->Vote(-1);
}
};
- downVoteButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(16, 16), "\xCA"); // TODO All arguments
+ downVoteButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(16, 16), "\xCA");
currentX+=18;
downVoteButton->SetActionCallback(new DownVoteAction(this));
AddComponent(downVoteButton);
@@ -94,10 +95,10 @@ GameView::GameView():
TagSimulationAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->OpenSearch(); // TODO call proper function
+ v->c->OpenTags();
}
};
- tagSimulationButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(Size.X-(currentX+176), 16), "\x83"); // TODO All arguments
+ tagSimulationButton = new ui::Button(ui::Point(currentX, Size.Y-18), ui::Point(Size.X-(currentX+176), 16), "\x83");
currentX+=Size.X-(currentX+176);
tagSimulationButton->SetActionCallback(new TagSimulationAction(this));
AddComponent(tagSimulationButton);
@@ -109,10 +110,10 @@ GameView::GameView():
ClearSimAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->SetPaused(sender->GetToggleState()); // TODO call proper function
+ v->c->ClearSim();
}
};
- clearSimButton = new ui::Button(ui::Point(Size.X-174, Size.Y-18), ui::Point(16, 16), "C"); // TODO All arguments
+ clearSimButton = new ui::Button(ui::Point(Size.X-174, Size.Y-18), ui::Point(16, 16), "C");
clearSimButton->SetActionCallback(new ClearSimAction(this));
AddComponent(clearSimButton);
@@ -123,10 +124,10 @@ GameView::GameView():
LoginAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->SetPaused(sender->GetToggleState()); // TODO call proper function
+ v->c->OpenLogin();
}
};
- loginButton = new ui::Button(ui::Point(Size.X-156, Size.Y-18), ui::Point(100, 16), "\xDA Login"); // TODO All arguments
+ loginButton = new ui::Button(ui::Point(Size.X-156, Size.Y-18), ui::Point(100, 16), "\xDA Login");
loginButton->SetActionCallback(new LoginAction(this));
AddComponent(loginButton);
@@ -137,10 +138,10 @@ GameView::GameView():
SimulationOptionAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->SetPaused(sender->GetToggleState()); // TODO call proper function
+ v->c->OpenDisplayOptions();
}
};
- simulationOptionButton = new ui::Button(ui::Point(Size.X-54, Size.Y-18), ui::Point(16, 16), "\xDA"); // TODO All arguments
+ simulationOptionButton = new ui::Button(ui::Point(Size.X-54, Size.Y-18), ui::Point(16, 16), "\xDA");
simulationOptionButton->SetActionCallback(new SimulationOptionAction(this));
AddComponent(simulationOptionButton);
@@ -151,10 +152,10 @@ GameView::GameView():
DisplayModeAction(GameView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
- v->c->SetPaused(sender->GetToggleState()); // TODO call proper function
+ v->c->OpenRenderOptions();
}
};
- displayModeButton = new ui::Button(ui::Point(Size.X-36, Size.Y-18), ui::Point(16, 16), "\xDA"); // TODO All arguments
+ displayModeButton = new ui::Button(ui::Point(Size.X-36, Size.Y-18), ui::Point(16, 16), "\xDA");
displayModeButton->SetActionCallback(new DisplayModeAction(this));
AddComponent(displayModeButton);
@@ -189,6 +190,38 @@ void GameView::NotifyPausedChanged(GameModel * sender)
pauseButton->SetToggleState(sender->GetPaused());
}
+void GameView::NotifySaveChanged(GameModel * sender)
+{
+ if(sender->GetSave())
+ {
+ reloadButton->Enabled = true;
+ if(sender->GetSave()->GetID()) //Online saves have an ID, local saves have an ID of 0 and a filename
+ {
+ upVoteButton->Enabled = true;
+ downVoteButton->Enabled = true;
+ tagSimulationButton->Enabled = true;
+ }
+ else
+ {
+ upVoteButton->Enabled = false;
+ downVoteButton->Enabled = false;
+ tagSimulationButton->Enabled = false;
+ }
+ }
+ else
+ {
+ reloadButton->Enabled = false;
+ upVoteButton->Enabled = false;
+ downVoteButton->Enabled = false;
+ tagSimulationButton->Enabled = false;
+ }
+}
+
+void GameView::NotifyBrushChanged(GameModel * sender)
+{
+ activeBrush = sender->GetBrush();
+}
+
void GameView::OnMouseMove(int x, int y, int dx, int dy)
{
if(isMouseDown)
@@ -213,6 +246,17 @@ void GameView::OnMouseUp(int x, int y, unsigned button)
}
}
+void GameView::OnMouseWheel(int x, int y, int d)
+{
+ if(!d)
+ return;
+ c->AdjustBrushSize(d);
+ if(isMouseDown)
+ {
+ pointQueue.push(new ui::Point(x, y));
+ }
+}
+
void GameView::OnTick(float dt)
{
if(!pointQueue.empty())
@@ -228,4 +272,8 @@ void GameView::OnDraw()
{
ren->render_parts();
}
+ if(activeBrush)
+ {
+ activeBrush->Render(ui::Engine::Ref().g, ui::Point(ui::Engine::Ref().GetMouseX(),ui::Engine::Ref().GetMouseY()));
+ }
}
diff --git a/src/game/GameView.h b/src/game/GameView.h
index 284fd82..85a96e7 100644
--- a/src/game/GameView.h
+++ b/src/game/GameView.h
@@ -7,6 +7,7 @@
#include "interface/Window.h"
#include "interface/Point.h"
#include "interface/Button.h"
+#include "Brush.h"
using namespace std;
@@ -19,6 +20,7 @@ private:
queue<ui::Point*> pointQueue;
GameController * c;
Renderer * ren;
+ Brush * activeBrush;
//UI Elements
ui::Button * searchButton;
ui::Button * reloadButton;
@@ -37,10 +39,12 @@ public:
void NotifyRendererChanged(GameModel * sender);
void NotifySimulationChanged(GameModel * sender);
void NotifyPausedChanged(GameModel * sender);
+ void NotifySaveChanged(GameModel * sender);
+ void NotifyBrushChanged(GameModel * sender);
virtual void OnMouseMove(int x, int y, int dx, int dy);
virtual void OnMouseDown(int x, int y, unsigned button);
virtual void OnMouseUp(int x, int y, unsigned button);
- //virtual void OnMouseWheel(int x, int y, int d) {}
+ virtual void OnMouseWheel(int x, int y, int d);
//virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt) {}
//virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt) {}
virtual void OnTick(float dt);