summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-15 19:35:40 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-15 19:35:40 (GMT)
commit2511afec8bfaa0582928406b37e8b579fa267e4f (patch)
treeaae1674f5f076db275ec1c6b32271a69cff1489a /src
parent2c9295007a287dc01ff63fcf7b3da141f7474e37 (diff)
downloadpowder-2511afec8bfaa0582928406b37e8b579fa267e4f.zip
powder-2511afec8bfaa0582928406b37e8b579fa267e4f.tar.gz
More stuff, better events and starting on interface
Diffstat (limited to 'src')
-rw-r--r--src/Global.cpp5
-rw-r--r--src/PowderToy.cpp66
-rw-r--r--src/interface/Button.cpp45
-rw-r--r--src/interface/Component.cpp3
-rw-r--r--src/interface/ControlFactory.cpp51
-rw-r--r--src/interface/Engine.cpp31
-rw-r--r--src/interface/Label.cpp4
-rw-r--r--src/interface/Sandbox.cpp12
8 files changed, 160 insertions, 57 deletions
diff --git a/src/Global.cpp b/src/Global.cpp
new file mode 100644
index 0000000..7bf28f0
--- /dev/null
+++ b/src/Global.cpp
@@ -0,0 +1,5 @@
+#include "Global.h"
+
+Global::Global(){
+
+}
diff --git a/src/PowderToy.cpp b/src/PowderToy.cpp
index 7ede4d5..1e9630c 100644
--- a/src/PowderToy.cpp
+++ b/src/PowderToy.cpp
@@ -5,6 +5,7 @@
#include <sstream>
#include <string>
#include "Config.h"
+#include "Global.h"
#include "Simulation.h"
#include "Renderer.h"
#include "Graphics.h"
@@ -17,7 +18,6 @@
#include "interface/ControlFactory.h"
#include "interface/Point.h"
#include "interface/Label.h"
-#include "GameSession.h"
using namespace std;
@@ -44,9 +44,8 @@ SDL_Surface * SDLOpen()
return SDL_SetVideoMode(XRES + BARSIZE, YRES + MENUSIZE, 32, SDL_SWSURFACE);
}
-int SDLPoll(SDL_Event * event)
+/*int SDLPoll(SDL_Event * event)
{
- event->type = 0;
while (SDL_PollEvent(event))
{
switch (event->type)
@@ -56,50 +55,60 @@ int SDLPoll(SDL_Event * event)
}
}
return 0;
-}
+}*/
int main(int argc, char * argv[])
{
int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
- float fps, fpsLimit, delta;
+ float fps = 0, fpsLimit = 30, delta = 1.0f;
//Renderer * ren;
//Simulation * sim = new Simulation();
//ren = new Renderer(g, sim);
- GameSession * gameSession = new GameSession();
+ Global::Ref().g = new Graphics();
+ Global::Ref().g->AttachSDLSurface(SDLOpen());
+
ui::Engine * engine = &ui::Engine::Ref();//new ui::Engine();
ui::State * engineState = new ui::State();
ui::Sandbox * sandbox = new ui::Sandbox();
ui::Button * button = new ui::Button(ui::Point(100, 100), ui::Point(100, 100), std::string("poP"));
ui::Label * fpsLabel = new ui::Label(ui::Point(2, 2), ui::Point(200, 14), std::string("FPS: 0"));
- engine->Begin(XRES, YRES, SDLOpen());
+ engine->Begin(XRES, YRES);
engine->SetState(engineState);
engineState->AddComponent(fpsLabel);
engineState->AddComponent(sandbox);
engineState->AddComponent(button);
- //window->Add(ControlFactory::MainMenu(gameSession, 0, 0, 200, 200));
+ engineState->AddComponent(ControlFactory::MainMenu(0, YRES+MENUSIZE-17, XRES+BARSIZE, 16));
SDL_Event event;
- while(!SDLPoll(&event))
+ while(engine->Running())
{
- //mouseButton = SDL_GetMouseState(&mouseX, &mouseY);
- switch(event.type)
+ event.type = 0;
+ while (SDL_PollEvent(&event))
{
- case SDL_KEYDOWN:
- break;
- case SDL_KEYUP:
- break;
- case SDL_MOUSEMOTION:
- engine->onMouseMove(event.motion.x, event.motion.y);
- break;
- case SDL_MOUSEBUTTONDOWN:
- engine->onMouseClick(event.motion.x, event.motion.y, event.button.button);
- break;
- case SDL_MOUSEBUTTONUP:
- engine->onMouseUnclick(event.motion.x, event.motion.y, event.button.button);
- break;
+ switch (event.type)
+ {
+ case SDL_QUIT:
+ engine->Exit();
+ break;
+ case SDL_KEYDOWN:
+ break;
+ case SDL_KEYUP:
+ break;
+ case SDL_MOUSEMOTION:
+ engine->onMouseMove(event.motion.x, event.motion.y);
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ engine->onMouseClick(event.motion.x, event.motion.y, event.button.button);
+ break;
+ case SDL_MOUSEBUTTONUP:
+ engine->onMouseUnclick(event.motion.x, event.motion.y, event.button.button);
+ break;
+ }
+ event.type = 0; //Clear last event
}
+ //mouseButton = SDL_GetMouseState(&mouseX, &mouseY);
fpsLabel->LabelText = "";
stringstream fpsText;
fpsText << "FPS: " << fps;
@@ -111,6 +120,15 @@ int main(int argc, char * argv[])
currentFrame++;
currentTime = SDL_GetTicks();
elapsedTime = currentTime - lastTime;
+ if((currentFrame>2 || elapsedTime > 1000*2/ui::Engine::Ref().FpsLimit) && elapsedTime && currentFrame*1000/elapsedTime > ui::Engine::Ref().FpsLimit)
+ {
+ while (currentFrame*1000/elapsedTime > ui::Engine::Ref().FpsLimit)
+ {
+ SDL_Delay(1);
+ currentTime = SDL_GetTicks();
+ elapsedTime = currentTime-lastTime;
+ }
+ }
if(elapsedTime>=1000)
{
fps = (((float)currentFrame)/((float)elapsedTime))*1000.0f;
diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp
index e34f66c..6ea9854 100644
--- a/src/interface/Button.cpp
+++ b/src/interface/Button.cpp
@@ -9,6 +9,7 @@
#include "interface/Button.h"
#include "Graphics.h"
+#include "Global.h"
namespace ui {
@@ -16,7 +17,8 @@ Button::Button(State* parent_state, std::string buttonText):
Component(parent_state),
ButtonText(buttonText),
isMouseInside(false),
- isButtonDown(false)
+ isButtonDown(false),
+ isTogglable(false)
{
}
@@ -25,7 +27,8 @@ Button::Button(Point position, Point size, std::string buttonText):
Component(position, size),
ButtonText(buttonText),
isMouseInside(false),
- isButtonDown(false)
+ isButtonDown(false),
+ isTogglable(false)
{
}
@@ -34,17 +37,42 @@ Button::Button(std::string buttonText):
Component(),
ButtonText(buttonText),
isMouseInside(false),
- isButtonDown(false)
+ isButtonDown(false),
+ isTogglable(false)
{
}
+void Button::SetTogglable(bool togglable)
+{
+ toggle = false;
+ isTogglable = togglable;
+}
+
+bool Button::GetTogglable()
+{
+ return isTogglable;
+}
+
+inline bool Button::GetToggleState()
+{
+ return toggle;
+}
+
+inline void Button::SetToggleState(bool state)
+{
+ toggle = state;
+}
+
+
+
void Button::Draw(const Point& screenPos)
{
- Graphics * g = ui::Engine::Ref().g;
+ Graphics * g = Global::Ref().g;
+ Point Position = screenPos;
// = reinterpret_cast<Graphics*>(userdata);
//TODO: Cache text location, that way we don't have the text alignment code here
- if(isButtonDown)
+ if(isButtonDown || (isTogglable && toggle))
{
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2, ButtonText, 0, 0, 0, 255);
@@ -111,20 +139,21 @@ void Button::OnMouseUnclick(int x, int y, unsigned int button)
void Button::OnMouseClick(int x, int y, unsigned int button)
{
- std::cout << "Click!" << std::endl;
if(button != 1) return; //left click only!
+ if(isTogglable)
+ {
+ toggle = !toggle;
+ }
isButtonDown = true;
}
void Button::OnMouseEnter(int x, int y)
{
- std::cout << "Enter!"<<std::endl;
isMouseInside = true;
}
void Button::OnMouseLeave(int x, int y)
{
- std::cout << "Leave!"<<std::endl;
isMouseInside = false;
}
diff --git a/src/interface/Component.cpp b/src/interface/Component.cpp
index ac66f92..49ff7f8 100644
--- a/src/interface/Component.cpp
+++ b/src/interface/Component.cpp
@@ -76,7 +76,8 @@ void Component::SetParent(Panel* new_parent)
else
{
// remove from parent state (if in parent state) and place in new parent
- GetParentState()->RemoveComponent(this);
+ if(GetParentState())
+ GetParentState()->RemoveComponent(this);
new_parent->children.push_back(this);
}
this->_parent = new_parent;
diff --git a/src/interface/ControlFactory.cpp b/src/interface/ControlFactory.cpp
index 25822cd..372ed31 100644
--- a/src/interface/ControlFactory.cpp
+++ b/src/interface/ControlFactory.cpp
@@ -4,9 +4,56 @@
#include "interface/Panel.h"
#include "interface/Engine.h"
-ui::Panel * ControlFactory::MainMenu(GameSession * session, int x, int y, int width, int height)
+ui::Panel * ControlFactory::MainMenu(int x, int y, int width, int height)
{
+ int currentX = 1;
+ width -= 2;
+ ui::Button * tempButton;
ui::Panel * mainMenu = new ui::Panel(ui::Point(x, y), ui::Point(width, height));
- //mainMenu->Add(new ui::Button(0, 0, 20, 20, "Turd"));
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\x81");
+ mainMenu->AddChild(tempButton); //Open
+ currentX += 18;
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\x91");
+ mainMenu->AddChild(tempButton); //Reload
+ currentX += 18;
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(width/4, height-2), "\x82 [Save]"); //Save
+ mainMenu->AddChild(tempButton);
+ currentX += tempButton->Size.X+2;
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\xCB");
+ mainMenu->AddChild(tempButton); //Vote Up
+ currentX += 16;
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\xCA");
+ mainMenu->AddChild(tempButton); //Vote Down
+ currentX += 18;
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(width - currentX - (4 * 18) - (width / 5), height-2), "[Tags]"); //Tags
+ currentX += tempButton->Size.X+2;
+ mainMenu->AddChild(tempButton);
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\xCF");
+ mainMenu->AddChild(tempButton); //Settings
+ currentX += 18;
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\x92");
+ mainMenu->AddChild(tempButton); //Clear
+ currentX += 18;
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(width - currentX - (2 * 18), height-2), "\x84 [Login]"); //Login
+ currentX += tempButton->Size.X+2;
+ mainMenu->AddChild(tempButton);
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\xD8");
+ mainMenu->AddChild(tempButton); //Render options
+ currentX += 18;
+
+ tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\x90"); //Pause
+ tempButton->SetTogglable(true);
+ mainMenu->AddChild(tempButton);
+ currentX += 18;
+
return mainMenu;
}
diff --git a/src/interface/Engine.cpp b/src/interface/Engine.cpp
index 4468561..25f2038 100644
--- a/src/interface/Engine.cpp
+++ b/src/interface/Engine.cpp
@@ -1,3 +1,7 @@
+#include <iostream>
+
+#include "Config.h"
+#include "Global.h"
#include "interface/Platform.h"
#include "interface/Engine.h"
#include "interface/State.h"
@@ -5,15 +9,14 @@
using namespace ui;
-Engine::Engine()
-:
- g(NULL),
-state_(NULL),
-statequeued_(NULL),
-mousex_(0),
-mousey_(0),
-mousexp_(0),
-mouseyp_(0)
+Engine::Engine():
+ state_(NULL),
+ statequeued_(NULL),
+ mousex_(0),
+ mousey_(0),
+ mousexp_(0),
+ mouseyp_(0),
+ FpsLimit(60.0f)
{
}
@@ -23,10 +26,8 @@ Engine::~Engine()
delete state_;
}
-void Engine::Begin(int width, int height, SDL_Surface * surface)
+void Engine::Begin(int width, int height)
{
- g = new Graphics();
- g->AttachSDLSurface(surface);
//engine is now ready
running_ = true;
@@ -82,8 +83,8 @@ void Engine::Draw()
{
if(state_)
state_->DoDraw();
- g->Blit();
- g->Clear();
+ Global::Ref().g->Blit();
+ Global::Ref().g->Clear();
}
void Engine::onKeyPress(int key, bool shift, bool ctrl, bool alt)
@@ -115,7 +116,9 @@ void Engine::onMouseMove(int x, int y)
mousex_ = x;
mousey_ = y;
if(state_)
+ {
state_->DoMouseMove(x, y, mousex_ - mousexp_, mousey_ - mouseyp_);
+ }
mousexp_ = x;
mouseyp_ = y;
}
diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp
index 3a5ea85..c77b6bf 100644
--- a/src/interface/Label.cpp
+++ b/src/interface/Label.cpp
@@ -1,4 +1,6 @@
#include <string>
+#include "Config.h"
+#include "Global.h"
#include "interface/Point.h"
#include "interface/Label.h"
@@ -33,6 +35,6 @@ Label::~Label()
void Label::Draw(const Point& screenPos)
{
- Graphics * g = ui::Engine::Ref().g;
+ Graphics * g = Global::Ref().g;
g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)LabelText.c_str()))/2, Position.Y+(Size.Y-10)/2, LabelText, 255, 255, 255, 255);
}
diff --git a/src/interface/Sandbox.cpp b/src/interface/Sandbox.cpp
index a9f1d9c..a9760e7 100644
--- a/src/interface/Sandbox.cpp
+++ b/src/interface/Sandbox.cpp
@@ -9,6 +9,7 @@
#include <queue>
#include "Config.h"
+#include "Global.h"
#include "interface/Point.h"
#include "interface/Sandbox.h"
@@ -48,7 +49,7 @@ void Sandbox::OnMouseClick(int localx, int localy, unsigned int button)
pointQueue.push(new Point(localx, localy));
}
-void Sandbox::OnMouseUnclick(int localx, int localy, unsigned int button)
+void Sandbox::OnMouseUp(int localx, int localy, unsigned int button)
{
if(isMouseDown)
{
@@ -59,7 +60,7 @@ void Sandbox::OnMouseUnclick(int localx, int localy, unsigned int button)
void Sandbox::Draw(const Point& screenPos)
{
- Graphics * g = ui::Engine::Ref().g;
+ Graphics * g = Global::Ref().g;
if(!ren)
ren = new Renderer(g, sim);
ren->render_parts();
@@ -76,16 +77,13 @@ void Sandbox::Tick(float delta)
pointQueue.pop();
if(sPoint)
{
- sim->create_line(fPoint->X, fPoint->Y, sPoint->X, sPoint->Y, 2, 2, activeElement, 0);
+ sim->create_line(fPoint->X, fPoint->Y, sPoint->X, sPoint->Y, 1, 1, activeElement, 0);
delete sPoint;
- sPoint = fPoint;
}
else
{
- sim->create_parts(fPoint->X, fPoint->Y, 2, 2, activeElement, 0);
+ sim->create_parts(fPoint->X, fPoint->Y, 1, 1, activeElement, 0);
}
- if(sPoint)
- delete sPoint;
sPoint = fPoint;
}
if(sPoint)