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
src/interface/ControlFactory.cpp
includes/interface/ControlFactory.h
src/GameSession.cpp
includes/GameSession.h
src/Console.cpp
includes/Console.h
@ -151,3 +150,5 @@ includes/interface/Point.h
includes/Singleton.h
src/interface/Label.cpp
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; }
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:
bool isButtonDown, state, isMouseInside;
bool isButtonDown, state, isMouseInside, isTogglable, toggle;
};
}
#endif /* BUTTON_H_ */

View File

@ -3,12 +3,11 @@
#include "Panel.h"
#include "Engine.h"
#include "GameSession.h"
class ControlFactory
{
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 onClose();
void Begin(int width, int height, SDL_Surface * surface);
void Begin(int width, int height);
inline bool Running() { return running_; }
void Exit();
@ -46,7 +46,7 @@ namespace ui
void SetState(State* state);
inline State* GetState() { return state_; }
Graphics * g;
float FpsLimit;
private:
State* statequeued_;
State* state_;

View File

@ -29,7 +29,7 @@ public:
virtual Simulation * GetSimulation();
virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
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 Tick(float delta);
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 <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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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)