-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Bablawn3d5/p-sma-render
Add sprite rendering system to core
- Loading branch information
Showing
20 changed files
with
174 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2016 Bablawn3d5 | ||
|
||
#pragma once | ||
|
||
#include <entityx/entityx.h> | ||
#include <SFML/Graphics.hpp> | ||
#include <string> | ||
|
||
struct Renderable { | ||
std::string texture_name; | ||
std::string sprite_name; | ||
|
||
std::shared_ptr<sf::Drawable> drawable; | ||
std::weak_ptr<sf::Transformable> transform; | ||
// TODO(SMA): Animation data to feed into the renderer | ||
// std::weak_ptr<Animated> animation; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2016 Bablawn3d5 | ||
|
||
#pragma once | ||
|
||
#include <entityx/entityx.h> | ||
#include <SFML/Graphics.hpp> | ||
#include <Thor/Resources.hpp> | ||
|
||
namespace ex = entityx; | ||
|
||
struct SpriteRenderSystem : public ex::System<SpriteRenderSystem> { | ||
public: | ||
explicit SpriteRenderSystem(sf::RenderTarget &targ) : target(targ) {} | ||
|
||
void update(ex::EntityManager &em, ex::EventManager &events, ex::TimeDelta dt); // NOLINT | ||
|
||
private: | ||
sf::RenderTarget& target; | ||
thor::ResourceHolder<sf::Texture, std::string> texture_holder; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2016 Bablawn3d5 | ||
#include <Farquaad/Systems/SpriteRenderSystem.h> | ||
#include <Farquaad/Components.hpp> | ||
#include <iostream> | ||
|
||
void SpriteRenderSystem::update(ex::EntityManager & em, | ||
ex::EventManager & events, ex::TimeDelta dt) { | ||
// Update drawables. | ||
em.each<Body, Renderable>( | ||
[this](ex::Entity entity, Body &body, Renderable &renderable) { | ||
// Resource exists, just exit it. | ||
if( renderable.drawable != nullptr ) | ||
return; | ||
|
||
// Create sprite from texture | ||
if ( renderable.texture_name.length() != 0 ) { | ||
try { | ||
static uint32 id = 0; | ||
auto& texture = | ||
this->texture_holder.acquire(renderable.texture_name, | ||
thor::Resources::fromFile<sf::Texture>(renderable.texture_name), | ||
thor::Resources::Reuse); | ||
|
||
std::string s = "entityx_" + std::to_string(id++); | ||
auto sprite = std::make_shared<sf::Sprite>(); | ||
sprite->setTexture(texture); | ||
sprite->setPosition(body.position); | ||
renderable.drawable = std::static_pointer_cast<sf::Drawable>(sprite); | ||
renderable.transform = std::static_pointer_cast<sf::Transformable>(sprite); | ||
} | ||
|
||
// Failed to load it for whatever reason | ||
catch ( thor::ResourceAccessException& e ) { | ||
std::cerr << "Failed to load resource: " << renderable.texture_name | ||
<< " " << e.what() << std::endl; | ||
throw e; | ||
} | ||
} | ||
|
||
}); | ||
|
||
em.each<Renderable>( | ||
[this](ex::Entity entity, Renderable &renderable) { | ||
try { | ||
if ( auto trans_ptr = renderable.transform.lock() ) { | ||
if ( entity.has_component<Body>() ) { | ||
trans_ptr->setPosition(entity.component<Body>()->position); | ||
} | ||
} | ||
this->target.draw(*renderable.drawable); | ||
} | ||
catch ( std::exception& e ) { | ||
std::cerr << "Failed to load drawable. " | ||
<< " " << e.what() << std::endl; | ||
throw e; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.