Skip to content
Zguy edited this page Feb 8, 2012 · 1 revision

ExampleState

ExampleState.h

#include <ProtoZed/ProtoZed.h>

class ExampleState : public PZ::AppState
{
public:
        ExampleState();
        ~ExampleState();

        virtual bool Update(float deltaTime);

        virtual void Start(PZ::StringMap *options);

private:
        PZ::Entity *myBox;
};

ExampleState.cpp

#include "ExampleState.h"

#include <SFML/Graphics.hpp>

ExampleState::ExampleState()
{
        myBox = nullptr;
}
ExampleState::~ExampleState()
{
}

bool ExampleState::Update(float deltaTime)
{
        const PZ::Input &input = PZ::Application::GetSingleton().GetInput();

        sf::Vector2f direction(0.f,0.f);
        if (input.IsKeyDown(sf::Key::Left))
        {
                direction.x -= 1.f;
        }
        if (input.IsKeyDown(sf::Key::Right))
        {
                direction.x += 1.f;
        }
        if (input.IsKeyDown(sf::Key::Up))
        {
                direction.y -= 1.f;
        }
        if (input.IsKeyDown(sf::Key::Down))
        {
                direction.y += 1.f;
        }
        PZ::Helpers::NormalizeVector(direction);

        float speed = 100.f;

        myBox->Move(direction * speed * deltaTime);

        return true;
}

void ExampleState::Start(PZ::StringMap *options)
{
        AppState::Start(options);

        myBox = GetRootEntity()->CreateChild("Entity", "MyBox");
        if (myBox != nullptr)
        {
                PZ::DrawableComponent *drawable = myBox->CreateComponent<PZ::DrawableComponent>("Drawable");
                drawable->SetDrawable(new sf::Shape(sf::Shape::Rectangle(sf::Vector2f(0.f,0.f), sf::Vector2f(40.f,40.f), sf::Color::Red)));
                drawable->SetCenter(20.f, 20.f);

                myBox->SetGlobalPosition(100.f,100.f);
        }
}

Main

main.cpp

#include <ProtoZed/Application.h>

#include "ExampleState.h"

int main()
{
        PZ::Application *app = new PZ::Application;

        app->GetStateManager().RegisterState<ExampleState>("ExampleState");
        app->GetStateManager().PushState("ExampleState");

        int result = app->Run("Example", PZ::VideoMode(800,600,800,600));

        delete app;

        return result;
}
Clone this wiki locally