summaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-17 20:46:06 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-17 20:46:06 (GMT)
commit4a60b97c700c2f1843b7e99313554cb89fb5da4e (patch)
tree3b33ef6f74a4e8a4ff5968a81b9c4c429ccaa7c6 /src/game
parent6273089bf486bf46ad325d72c7290ebb272bd3d8 (diff)
downloadpowder-4a60b97c700c2f1843b7e99313554cb89fb5da4e.zip
powder-4a60b97c700c2f1843b7e99313554cb89fb5da4e.tar.gz
Some minor changes
Diffstat (limited to 'src/game')
-rw-r--r--src/game/GameController.cpp62
-rw-r--r--src/game/GameController.h28
-rw-r--r--src/game/GameModel.cpp75
-rw-r--r--src/game/GameModel.h37
-rw-r--r--src/game/GameView.cpp83
-rw-r--r--src/game/GameView.h48
6 files changed, 333 insertions, 0 deletions
diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp
new file mode 100644
index 0000000..becb540
--- /dev/null
+++ b/src/game/GameController.cpp
@@ -0,0 +1,62 @@
+
+#include <iostream>
+#include <queue>
+#include "Config.h"
+#include "GameController.h"
+#include "GameModel.h"
+#include "interface/Point.h"
+
+using namespace std;
+
+GameController::GameController()
+{
+ gameView = new GameView();
+ gameModel = new GameModel();
+
+ gameView->AttachController(this);
+ gameModel->AddObserver(gameView);
+
+ sim = new Simulation();
+}
+
+GameView * GameController::GetView()
+{
+ return gameView;
+}
+
+void GameController::DrawPoints(queue<ui::Point*> & pointQueue)
+{
+ Simulation * sim = gameModel->GetSimulation();
+ int activeElement = gameModel->GetActiveElement();
+ if(!pointQueue.empty())
+ {
+ ui::Point * sPoint = NULL;
+ while(!pointQueue.empty())
+ {
+ ui::Point * fPoint = pointQueue.front();
+ pointQueue.pop();
+ if(sPoint)
+ {
+ sim->create_line(fPoint->X, fPoint->Y, sPoint->X, sPoint->Y, 1, 1, activeElement, 0);
+ delete sPoint;
+ }
+ else
+ {
+ sim->create_parts(fPoint->X, fPoint->Y, 1, 1, activeElement, 0);
+ }
+ sPoint = fPoint;
+ }
+ if(sPoint)
+ delete sPoint;
+ }
+}
+
+void GameController::Tick()
+{
+ gameModel->GetSimulation()->update_particles();
+}
+
+void GameController::SetPaused(bool pauseState)
+{
+ gameModel->SetPaused(pauseState);
+}
diff --git a/src/game/GameController.h b/src/game/GameController.h
new file mode 100644
index 0000000..c3c8273
--- /dev/null
+++ b/src/game/GameController.h
@@ -0,0 +1,28 @@
+#ifndef GAMECONTROLLER_H
+#define GAMECONTROLLER_H
+
+#include <queue>
+#include "GameView.h"
+#include "GameModel.h"
+#include "interface/Point.h"
+#include "Simulation.h"
+
+using namespace std;
+
+class GameModel;
+class GameView;
+class GameController
+{
+private:
+ Simulation * sim;
+ GameView * gameView;
+ GameModel * gameModel;
+public:
+ GameController();
+ GameView * GetView();
+ void DrawPoints(queue<ui::Point*> & pointQueue);
+ void Tick();
+ void SetPaused(bool pauseState);
+};
+
+#endif // GAMECONTROLLER_H
diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp
new file mode 100644
index 0000000..96b29a9
--- /dev/null
+++ b/src/game/GameModel.cpp
@@ -0,0 +1,75 @@
+#include "interface/Engine.h"
+#include "GameModel.h"
+#include "GameView.h"
+#include "Simulation.h"
+#include "Renderer.h"
+
+GameModel::GameModel():
+ activeElement(1)
+{
+ sim = new Simulation();
+ ren = new Renderer(ui::Engine::Ref().g, sim);
+}
+
+void GameModel::AddObserver(GameView * observer){
+ observers.push_back(observer);
+
+ observer->NotifySimulationChanged(this);
+ observer->NotifyRendererChanged(this);
+ observer->NotifyPausedChanged(this);
+}
+
+int GameModel::GetActiveElement()
+{
+ return activeElement;
+}
+
+void GameModel::SetActiveElement(int element)
+{
+ activeElement = element;
+}
+
+Simulation * GameModel::GetSimulation()
+{
+ return sim;
+}
+
+Renderer * GameModel::GetRenderer()
+{
+ return ren;
+}
+
+void GameModel::SetPaused(bool pauseState)
+{
+ sim->sys_pause = pauseState?1:0;
+ notifyPausedChanged();
+}
+
+bool GameModel::GetPaused()
+{
+ return sim->sys_pause?true:false;
+}
+
+void GameModel::notifyRendererChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifyRendererChanged(this);
+ }
+}
+
+void GameModel::notifySimulationChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifySimulationChanged(this);
+ }
+}
+
+void GameModel::notifyPausedChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifyPausedChanged(this);
+ }
+}
diff --git a/src/game/GameModel.h b/src/game/GameModel.h
new file mode 100644
index 0000000..a2eb3ce
--- /dev/null
+++ b/src/game/GameModel.h
@@ -0,0 +1,37 @@
+#ifndef GAMEMODEL_H
+#define GAMEMODEL_H
+
+#include <vector>
+#include "Simulation.h"
+#include "Renderer.h"
+#include "GameView.h"
+
+using namespace std;
+
+class GameView;
+class Simulation;
+class Renderer;
+
+class GameModel
+{
+private:
+ vector<GameView*> observers;
+ Simulation * sim;
+ Renderer * ren;
+ int activeElement;
+ void notifyRendererChanged();
+ void notifySimulationChanged();
+ void notifyPausedChanged();
+public:
+ GameModel();
+ void AddObserver(GameView * observer);
+ int GetActiveElement();
+ void SetActiveElement(int element);
+ bool GetPaused();
+ void SetPaused(bool pauseState);
+
+ Simulation * GetSimulation();
+ Renderer * GetRenderer();
+};
+
+#endif // GAMEMODEL_H
diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp
new file mode 100644
index 0000000..2576527
--- /dev/null
+++ b/src/game/GameView.cpp
@@ -0,0 +1,83 @@
+#include "Config.h"
+#include "GameView.h"
+#include "interface/Window.h"
+#include "interface/Button.h"
+
+GameView::GameView():
+ ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)),
+ pointQueue(queue<ui::Point*>()),
+ isMouseDown(false),
+ ren(NULL)
+{
+ //Set up UI
+ class PauseAction : public ui::ButtonAction
+ {
+ GameView * v;
+ public:
+ PauseAction(GameView * _v) { v = _v; }
+ void ActionCallback(ui::Button * sender)
+ {
+ v->c->SetPaused(sender->GetToggleState());
+ }
+ };
+ pauseButton = new ui::Button(ui::Point(Size.X-18, Size.Y-18), ui::Point(16, 16), "\x90"); //Pause
+ pauseButton->SetTogglable(true);
+ pauseButton->SetActionCallback(new PauseAction(this));
+ AddComponent(pauseButton);
+}
+
+void GameView::NotifyRendererChanged(GameModel * sender)
+{
+ ren = sender->GetRenderer();
+}
+
+void GameView::NotifySimulationChanged(GameModel * sender)
+{
+
+}
+
+void GameView::NotifyPausedChanged(GameModel * sender)
+{
+ pauseButton->SetToggleState(sender->GetPaused());
+}
+
+void GameView::OnMouseMove(int x, int y, int dx, int dy)
+{
+ if(isMouseDown)
+ {
+ pointQueue.push(new ui::Point(x-dx, y-dy));
+ pointQueue.push(new ui::Point(x, y));
+ }
+}
+
+void GameView::OnMouseDown(int x, int y, unsigned button)
+{
+ isMouseDown = true;
+ pointQueue.push(new ui::Point(x, y));
+}
+
+void GameView::OnMouseUp(int x, int y, unsigned button)
+{
+ if(isMouseDown)
+ {
+ isMouseDown = false;
+ pointQueue.push(new ui::Point(x, y));
+ }
+}
+
+void GameView::OnTick(float dt)
+{
+ if(!pointQueue.empty())
+ {
+ c->DrawPoints(pointQueue);
+ }
+ c->Tick();
+}
+
+void GameView::OnDraw()
+{
+ if(ren)
+ {
+ ren->render_parts();
+ }
+}
diff --git a/src/game/GameView.h b/src/game/GameView.h
new file mode 100644
index 0000000..531a4b9
--- /dev/null
+++ b/src/game/GameView.h
@@ -0,0 +1,48 @@
+#ifndef GAMEVIEW_H
+#define GAMEVIEW_H
+
+#include <queue>
+#include "GameController.h"
+#include "GameModel.h"
+#include "interface/Window.h"
+#include "interface/Point.h"
+#include "interface/Button.h"
+
+using namespace std;
+
+class GameController;
+class GameModel;
+class GameView: public ui::Window
+{
+private:
+ bool isMouseDown;
+ queue<ui::Point*> pointQueue;
+ GameController * c;
+ Renderer * ren;
+ //UI Elements
+ ui::Button * pauseButton;
+public:
+ GameView();
+ void AttachController(GameController * _c){ c = _c; }
+ void NotifyRendererChanged(GameModel * sender);
+ void NotifySimulationChanged(GameModel * sender);
+ void NotifyPausedChanged(GameModel * sender);
+ /*virtual void DoMouseMove(int x, int y, int dx, int dy);
+ virtual void DoMouseDown(int x, int y, unsigned button);
+ virtual void DoMouseUp(int x, int y, unsigned button);
+ //virtual void DoMouseWheel(int x, int y, int d);
+ //virtual void DoKeyPress(int key, bool shift, bool ctrl, bool alt);
+ //virtual void DoKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ virtual void DoTick(float dt);
+ virtual void DoDraw();*/
+ 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 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);
+ virtual void OnDraw();
+};
+
+#endif // GAMEVIEW_H