Skip to content

Commit

Permalink
Implement a render group for frame processing.
Browse files Browse the repository at this point in the history
Define default values for window.
  • Loading branch information
ASxa86 committed Jun 18, 2024
1 parent 43ef244 commit 940494a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
26 changes: 6 additions & 20 deletions app/pong/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,22 @@

auto main() -> int
{
constexpr auto defaultX{80};
constexpr auto defaultY{80};
constexpr auto defaultWidth{1280};
constexpr auto defaultHeight{720};

aspire::core::Kernel kernel;

auto window = std::make_unique<aspire::scene::Window>();
window->setX(defaultX);
window->setY(defaultY);
window->setWidth(defaultWidth);
window->setHeight(defaultHeight);
window->setTitle("Aspire");
window->setColor(sf::Color::Black);

auto root = std::make_unique<aspire::scene::Node>();

auto* window = kernel.createChild<aspire::scene::Window>();

Paddle player{*root};
player.setPosition({150, defaultHeight / 2.0});
player.setSize({50, 150});
player.setPosition({150.0F, static_cast<float>(window->getHeight()) / 2.0F});
player.setSize({50.0F, 150.0F});

Paddle paddle{*root};

paddle.setPosition({defaultWidth - 150, defaultHeight / 2.0});
paddle.setSize({50, 150});
paddle.setPosition({window->getWidth() - 150.0F, static_cast<float>(window->getHeight()) / 2.0F});
paddle.setSize({50.0F, 150.0F});

window->setRootNode(std::move(root));

// Add the window last to ensure frame processing occurs last.
kernel.addChild(std::move(window));

return kernel.run();
}
8 changes: 5 additions & 3 deletions include/aspire/core/Kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ namespace aspire::core
public:
enum class FrameGroup : sigslot::group_id
{
Event,
Object,
Service,
Object
Render
};

static constexpr std::chrono::milliseconds FrameFixedRate{10};
Expand All @@ -29,8 +31,8 @@ namespace aspire::core

auto sendEvent(Event& x, Object* receiver) -> void;
auto queueEvent(std::unique_ptr<Event> x, Object* receiver) -> void;
auto onFrame(std::function<void()> x, FrameGroup group) -> sigslot::connection;
auto onFrameFixed(std::function<void()> x, FrameGroup group) -> sigslot::connection;
auto onFrame(FrameGroup group, std::function<void()> x) -> sigslot::connection;
auto onFrameFixed(FrameGroup group, std::function<void()> x) -> sigslot::connection;

auto run() -> int;
auto quit() -> void;
Expand Down
8 changes: 8 additions & 0 deletions include/aspire/scene/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ namespace aspire::scene
class ASPIRE_SCENE_EXPORT Window : public aspire::core::Object
{
public:
static constexpr auto DefaultX{80};
static constexpr auto DefaultY{80};
static constexpr auto DefaultWidth{1280};
static constexpr auto DefaultHeight{720};
static constexpr auto DefaultTitle{"aspire"};
inline static const sf::Color DefaultColor{static_cast<sf::Uint8>(0.2F * 255U), static_cast<sf::Uint8>(0.3F * 255U),
static_cast<sf::Uint8>(0.3F * 255U), static_cast<sf::Uint8>(1.0F * 255U)};

Window();
~Window() override;

Expand Down
4 changes: 2 additions & 2 deletions src/core/Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ auto Kernel::queueEvent(std::unique_ptr<Event> x, Object* receiver) -> void
this->pimpl->events.emplace_back(std::move(x), receiver);
}

auto Kernel::onFrame(std::function<void()> x, FrameGroup group) -> sigslot::connection
auto Kernel::onFrame(FrameGroup group, std::function<void()> x) -> sigslot::connection
{
return this->pimpl->frame.connect(std::move(x), static_cast<sigslot::group_id>(group));
}

auto Kernel::onFrameFixed(std::function<void()> x, FrameGroup group) -> sigslot::connection
auto Kernel::onFrameFixed(FrameGroup group, std::function<void()> x) -> sigslot::connection
{
return this->pimpl->frameFixed.connect(std::move(x), static_cast<sigslot::group_id>(group));
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ auto Object::onStartup(std::function<void()> x) -> sigslot::connection

auto Object::onFrame(std::function<void()> x) const -> sigslot::connection
{
return Kernel::Instance()->onFrame(std::move(x), Kernel::FrameGroup::Object);
return Kernel::Instance()->onFrame(Kernel::FrameGroup::Object, std::move(x));
}

auto Object::onFrameFixed(std::function<void()> x) const -> sigslot::connection
{
return Kernel::Instance()->onFrameFixed(std::move(x), Kernel::FrameGroup::Object);
return Kernel::Instance()->onFrameFixed(Kernel::FrameGroup::Object, std::move(x));
}
17 changes: 8 additions & 9 deletions src/scene/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ using aspire::scene::Window;
struct Window::Impl
{
sf::RenderWindow renderer;
sf::Color clearColor{static_cast<sf::Uint8>(0.2F * 255U), static_cast<sf::Uint8>(0.3F * 255U), static_cast<sf::Uint8>(0.3F * 255U),
static_cast<sf::Uint8>(1.0F * 255U)};
sf::Color clearColor{Window::DefaultColor};

std::unique_ptr<Node> rootNode;

std::string title;
int x{};
int y{};
int width{};
int height{};
std::string title{Window::DefaultTitle};
int x{Window::DefaultX};
int y{Window::DefaultY};
int width{Window::DefaultWidth};
int height{Window::DefaultHeight};
};

Window::Window()
Expand All @@ -35,13 +34,13 @@ Window::Window()
return;
}

this->onFrame([this]() { this->frame(); });
Kernel::Instance()->onFrame(Kernel::FrameGroup::Render, [this] { this->frame(); });

constexpr auto style = sf::Style::Titlebar | sf::Style::Resize | sf::Style::Close;
const auto width = static_cast<unsigned int>(this->pimpl->width);
const auto height = static_cast<unsigned int>(this->pimpl->height);
sf::ContextSettings settings;
settings.antialiasingLevel = 4;
// settings.antialiasingLevel = 4;
this->pimpl->renderer.create(sf::VideoMode{width, height}, this->pimpl->title, style, settings);
this->pimpl->renderer.setFramerateLimit(0);
this->pimpl->renderer.setPosition({this->pimpl->x, this->pimpl->y});
Expand Down

0 comments on commit 940494a

Please sign in to comment.