More stuff, better events and starting on interface

This commit is contained in:
Simon Robertshaw
2012-01-15 19:35:40 +00:00
parent 2c9295007a
commit 2511afec8b
14 changed files with 185 additions and 64 deletions

View File

@@ -134,7 +134,6 @@ C:/Users/Simon/Projects/FacialTurd-PowderToypp/src/Simulation.cpp
C:/Users/Simon/Projects/FacialTurd-PowderToypp/Changelog.txt C:/Users/Simon/Projects/FacialTurd-PowderToypp/Changelog.txt
src/interface/ControlFactory.cpp src/interface/ControlFactory.cpp
includes/interface/ControlFactory.h includes/interface/ControlFactory.h
src/GameSession.cpp
includes/GameSession.h includes/GameSession.h
src/Console.cpp src/Console.cpp
includes/Console.h includes/Console.h
@@ -151,3 +150,5 @@ includes/interface/Point.h
includes/Singleton.h includes/Singleton.h
src/interface/Label.cpp src/interface/Label.cpp
includes/interface/Label.h includes/interface/Label.h
includes/Global.h
src/Global.cpp

14
includes/Global.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef GAMESESSION_H
#define GAMESESSION_H
#include "Singleton.h"
#include "Graphics.h"
class Global : public Singleton<Global>
{
public:
Graphics * g;
Global();
};
#endif // GAMESESSION_H

View File

@@ -39,9 +39,13 @@ namespace ui
inline bool GetState() { return state; } inline bool GetState() { return state; }
virtual void DoAction(); //action of button what ever it may be virtual void DoAction(); //action of button what ever it may be
void SetTogglable(bool isTogglable);
bool GetTogglable();
inline bool GetToggleState();
inline void SetToggleState(bool state);
protected: protected:
bool isButtonDown, state, isMouseInside; bool isButtonDown, state, isMouseInside, isTogglable, toggle;
}; };
} }
#endif /* BUTTON_H_ */ #endif /* BUTTON_H_ */

View File

@@ -3,12 +3,11 @@
#include "Panel.h" #include "Panel.h"
#include "Engine.h" #include "Engine.h"
#include "GameSession.h"
class ControlFactory class ControlFactory
{ {
public: public:
static ui::Panel * MainMenu(GameSession * session, int x, int y, int width, int height); static ui::Panel * MainMenu(int x, int y, int width, int height);
}; };

View File

@@ -30,7 +30,7 @@ namespace ui
void onResize(int newWidth, int newHeight); void onResize(int newWidth, int newHeight);
void onClose(); void onClose();
void Begin(int width, int height, SDL_Surface * surface); void Begin(int width, int height);
inline bool Running() { return running_; } inline bool Running() { return running_; }
void Exit(); void Exit();
@@ -46,7 +46,7 @@ namespace ui
void SetState(State* state); void SetState(State* state);
inline State* GetState() { return state_; } inline State* GetState() { return state_; }
Graphics * g; float FpsLimit;
private: private:
State* statequeued_; State* statequeued_;
State* state_; State* state_;

View File

@@ -29,7 +29,7 @@ public:
virtual Simulation * GetSimulation(); virtual Simulation * GetSimulation();
virtual void OnMouseMoved(int localx, int localy, int dx, int dy); virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
virtual void OnMouseClick(int localx, int localy, unsigned int button); virtual void OnMouseClick(int localx, int localy, unsigned int button);
virtual void OnMouseUnclick(int localx, int localy, unsigned int button); virtual void OnMouseUp(int localx, int localy, unsigned int button);
virtual void Draw(const Point& screenPos); virtual void Draw(const Point& screenPos);
virtual void Tick(float delta); virtual void Tick(float delta);
virtual ~Sandbox(); virtual ~Sandbox();

5
src/Global.cpp Normal file
View File

@@ -0,0 +1,5 @@
#include "Global.h"
Global::Global(){
}

View File

@@ -5,6 +5,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "Config.h" #include "Config.h"
#include "Global.h"
#include "Simulation.h" #include "Simulation.h"
#include "Renderer.h" #include "Renderer.h"
#include "Graphics.h" #include "Graphics.h"
@@ -17,7 +18,6 @@
#include "interface/ControlFactory.h" #include "interface/ControlFactory.h"
#include "interface/Point.h" #include "interface/Point.h"
#include "interface/Label.h" #include "interface/Label.h"
#include "GameSession.h"
using namespace std; using namespace std;
@@ -44,9 +44,8 @@ SDL_Surface * SDLOpen()
return SDL_SetVideoMode(XRES + BARSIZE, YRES + MENUSIZE, 32, SDL_SWSURFACE); 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)) while (SDL_PollEvent(event))
{ {
switch (event->type) switch (event->type)
@@ -56,50 +55,60 @@ int SDLPoll(SDL_Event * event)
} }
} }
return 0; return 0;
} }*/
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0; int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
float fps, fpsLimit, delta; float fps = 0, fpsLimit = 30, delta = 1.0f;
//Renderer * ren; //Renderer * ren;
//Simulation * sim = new Simulation(); //Simulation * sim = new Simulation();
//ren = new Renderer(g, sim); //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::Engine * engine = &ui::Engine::Ref();//new ui::Engine();
ui::State * engineState = new ui::State(); ui::State * engineState = new ui::State();
ui::Sandbox * sandbox = new ui::Sandbox(); ui::Sandbox * sandbox = new ui::Sandbox();
ui::Button * button = new ui::Button(ui::Point(100, 100), ui::Point(100, 100), std::string("poP")); 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")); 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); engine->SetState(engineState);
engineState->AddComponent(fpsLabel); engineState->AddComponent(fpsLabel);
engineState->AddComponent(sandbox); engineState->AddComponent(sandbox);
engineState->AddComponent(button); 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; SDL_Event event;
while(!SDLPoll(&event)) while(engine->Running())
{ {
//mouseButton = SDL_GetMouseState(&mouseX, &mouseY); event.type = 0;
switch(event.type) while (SDL_PollEvent(&event))
{ {
case SDL_KEYDOWN: switch (event.type)
break; {
case SDL_KEYUP: case SDL_QUIT:
break; engine->Exit();
case SDL_MOUSEMOTION: break;
engine->onMouseMove(event.motion.x, event.motion.y); case SDL_KEYDOWN:
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_KEYUP:
engine->onMouseClick(event.motion.x, event.motion.y, event.button.button); break;
break; case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONUP: engine->onMouseMove(event.motion.x, event.motion.y);
engine->onMouseUnclick(event.motion.x, event.motion.y, event.button.button); break;
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 = ""; fpsLabel->LabelText = "";
stringstream fpsText; stringstream fpsText;
fpsText << "FPS: " << fps; fpsText << "FPS: " << fps;
@@ -111,6 +120,15 @@ int main(int argc, char * argv[])
currentFrame++; currentFrame++;
currentTime = SDL_GetTicks(); currentTime = SDL_GetTicks();
elapsedTime = currentTime - lastTime; 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) if(elapsedTime>=1000)
{ {
fps = (((float)currentFrame)/((float)elapsedTime))*1000.0f; fps = (((float)currentFrame)/((float)elapsedTime))*1000.0f;

View File

@@ -9,6 +9,7 @@
#include "interface/Button.h" #include "interface/Button.h"
#include "Graphics.h" #include "Graphics.h"
#include "Global.h"
namespace ui { namespace ui {
@@ -16,7 +17,8 @@ Button::Button(State* parent_state, std::string buttonText):
Component(parent_state), Component(parent_state),
ButtonText(buttonText), ButtonText(buttonText),
isMouseInside(false), isMouseInside(false),
isButtonDown(false) isButtonDown(false),
isTogglable(false)
{ {
} }
@@ -25,7 +27,8 @@ Button::Button(Point position, Point size, std::string buttonText):
Component(position, size), Component(position, size),
ButtonText(buttonText), ButtonText(buttonText),
isMouseInside(false), isMouseInside(false),
isButtonDown(false) isButtonDown(false),
isTogglable(false)
{ {
} }
@@ -34,17 +37,42 @@ Button::Button(std::string buttonText):
Component(), Component(),
ButtonText(buttonText), ButtonText(buttonText),
isMouseInside(false), 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) void Button::Draw(const Point& screenPos)
{ {
Graphics * g = ui::Engine::Ref().g; Graphics * g = Global::Ref().g;
Point Position = screenPos;
// = reinterpret_cast<Graphics*>(userdata); // = reinterpret_cast<Graphics*>(userdata);
//TODO: Cache text location, that way we don't have the text alignment code here //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->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); 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) void Button::OnMouseClick(int x, int y, unsigned int button)
{ {
std::cout << "Click!" << std::endl;
if(button != 1) return; //left click only! if(button != 1) return; //left click only!
if(isTogglable)
{
toggle = !toggle;
}
isButtonDown = true; isButtonDown = true;
} }
void Button::OnMouseEnter(int x, int y) void Button::OnMouseEnter(int x, int y)
{ {
std::cout << "Enter!"<<std::endl;
isMouseInside = true; isMouseInside = true;
} }
void Button::OnMouseLeave(int x, int y) void Button::OnMouseLeave(int x, int y)
{ {
std::cout << "Leave!"<<std::endl;
isMouseInside = false; isMouseInside = false;
} }

View File

@@ -76,7 +76,8 @@ void Component::SetParent(Panel* new_parent)
else else
{ {
// remove from parent state (if in parent state) and place in new parent // 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); new_parent->children.push_back(this);
} }
this->_parent = new_parent; this->_parent = new_parent;

View File

@@ -4,9 +4,56 @@
#include "interface/Panel.h" #include "interface/Panel.h"
#include "interface/Engine.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)); 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; return mainMenu;
} }

View File

@@ -1,3 +1,7 @@
#include <iostream>
#include "Config.h"
#include "Global.h"
#include "interface/Platform.h" #include "interface/Platform.h"
#include "interface/Engine.h" #include "interface/Engine.h"
#include "interface/State.h" #include "interface/State.h"
@@ -5,15 +9,14 @@
using namespace ui; using namespace ui;
Engine::Engine() Engine::Engine():
: state_(NULL),
g(NULL), statequeued_(NULL),
state_(NULL), mousex_(0),
statequeued_(NULL), mousey_(0),
mousex_(0), mousexp_(0),
mousey_(0), mouseyp_(0),
mousexp_(0), FpsLimit(60.0f)
mouseyp_(0)
{ {
} }
@@ -23,10 +26,8 @@ Engine::~Engine()
delete state_; 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 //engine is now ready
running_ = true; running_ = true;
@@ -82,8 +83,8 @@ void Engine::Draw()
{ {
if(state_) if(state_)
state_->DoDraw(); state_->DoDraw();
g->Blit(); Global::Ref().g->Blit();
g->Clear(); Global::Ref().g->Clear();
} }
void Engine::onKeyPress(int key, bool shift, bool ctrl, bool alt) void Engine::onKeyPress(int key, bool shift, bool ctrl, bool alt)
@@ -115,7 +116,9 @@ void Engine::onMouseMove(int x, int y)
mousex_ = x; mousex_ = x;
mousey_ = y; mousey_ = y;
if(state_) if(state_)
{
state_->DoMouseMove(x, y, mousex_ - mousexp_, mousey_ - mouseyp_); state_->DoMouseMove(x, y, mousex_ - mousexp_, mousey_ - mouseyp_);
}
mousexp_ = x; mousexp_ = x;
mouseyp_ = y; mouseyp_ = y;
} }

View File

@@ -1,4 +1,6 @@
#include <string> #include <string>
#include "Config.h"
#include "Global.h"
#include "interface/Point.h" #include "interface/Point.h"
#include "interface/Label.h" #include "interface/Label.h"
@@ -33,6 +35,6 @@ Label::~Label()
void Label::Draw(const Point& screenPos) 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); g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)LabelText.c_str()))/2, Position.Y+(Size.Y-10)/2, LabelText, 255, 255, 255, 255);
} }

View File

@@ -9,6 +9,7 @@
#include <queue> #include <queue>
#include "Config.h" #include "Config.h"
#include "Global.h"
#include "interface/Point.h" #include "interface/Point.h"
#include "interface/Sandbox.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)); 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) if(isMouseDown)
{ {
@@ -59,7 +60,7 @@ void Sandbox::OnMouseUnclick(int localx, int localy, unsigned int button)
void Sandbox::Draw(const Point& screenPos) void Sandbox::Draw(const Point& screenPos)
{ {
Graphics * g = ui::Engine::Ref().g; Graphics * g = Global::Ref().g;
if(!ren) if(!ren)
ren = new Renderer(g, sim); ren = new Renderer(g, sim);
ren->render_parts(); ren->render_parts();
@@ -76,16 +77,13 @@ void Sandbox::Tick(float delta)
pointQueue.pop(); pointQueue.pop();
if(sPoint) 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; delete sPoint;
sPoint = fPoint;
} }
else 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; sPoint = fPoint;
} }
if(sPoint) if(sPoint)