Skip to content

Commit

Permalink
Merge pull request #40 from Bablawn3d5/p-sma-render
Browse files Browse the repository at this point in the history
Add sprite rendering system to core
  • Loading branch information
iamPHEN authored Aug 1, 2016
2 parents 045b7fe + c872267 commit 4206060
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 105 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ execute_process(
OUTPUT_STRIP_TRAILING_WHITESPACE
)

set(CPACK_PACKAGE_FILE_NAME "GameEngine-0.1.0-${GIT_COMMIT_HASH}-${CMAKE_SYSTEM_NAME}")
set(CPACK_PACKAGE_FILE_NAME "GameEngine-0.2.0-${GIT_COMMIT_HASH}-${CMAKE_SYSTEM_NAME}")

# Disable examples by default
option(BUILD_SHARED_LIBS "Link SFML as shared or not" OFF)
Expand Down
6 changes: 3 additions & 3 deletions Farquaad/include/Farquaad/Components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <Farquaad/Components/Body.hpp>
#include <Farquaad/Components/InputResponder.hpp>
#include <Farquaad/Components/Physics.hpp>
#include <Farquaad/Components/RenderableShape.hpp>
#include <Farquaad/Components/Stats.hpp>
// FIXME(SMA): Boost 1.60 causes ice_xxx depercation warnings.
// Disable including as a 'default' componnet for now.
#include <Farquaad/Components/Renderable.hpp>
// FIXME(SMA): Boost 1.60 causes ice_xxx deprecation warnings.
// Disable including as a 'default' component for now.
// #include <Farquaad/Components/PythonScript.hpp>
17 changes: 17 additions & 0 deletions Farquaad/include/Farquaad/Components/Renderable.hpp
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;
};
18 changes: 0 additions & 18 deletions Farquaad/include/Farquaad/Components/RenderableShape.hpp

This file was deleted.

13 changes: 13 additions & 0 deletions Farquaad/include/Farquaad/Core/MetaRegister.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,17 @@ inline auto registerMembers<InputResponder>() {
member("mouseGamePos", &InputResponder::mouseGamePos)
);
}

template <>
constexpr auto registerName<Renderable>() {
return "renderable";
}

template <>
inline auto registerMembers<Renderable>() {
return members(
member("texture", &Renderable::texture_name),
member("sprite", &Renderable::sprite_name)
);
}
} // namespace meta
2 changes: 1 addition & 1 deletion Farquaad/include/Farquaad/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

#include <Farquaad/Systems/InputSystem.h>
#include <Farquaad/Systems/PhysicsSystem.h>
#include <Farquaad/Systems/RenderSystem.h>
#include <Farquaad/Systems/MoveSystem.h>
#include <Farquaad/Systems/ImGuiSystem.h>
#include <Farquaad/Systems/SpriteRenderSystem.h>
18 changes: 0 additions & 18 deletions Farquaad/include/Farquaad/Systems/RenderSystem.h

This file was deleted.

20 changes: 20 additions & 0 deletions Farquaad/include/Farquaad/Systems/SpriteRenderSystem.h
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;
};
2 changes: 1 addition & 1 deletion Farquaad/src/Components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ set(SRC
${INCROOT}/Body.hpp
${INCROOT}/Physics.hpp
${INCROOT}/InputResponder.hpp
${INCROOT}/RenderableShape.hpp
${INCROOT}/Stats.hpp
${INCROOT}/PythonScript.hpp
${INCROOT}/Renderable.hpp
)

set(INC
Expand Down
3 changes: 3 additions & 0 deletions Farquaad/src/Core/EntitySerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void EntitySerializer::Load(ex::Entity entity) const {
ComponentSerializer::LoadComponentToEntity<Stats>(this->cs, entity);
ComponentSerializer::LoadComponentToEntity<InputResponder>(this->cs, entity);
ComponentSerializer::LoadComponentToEntity<PythonScript>(this->cs, entity);
ComponentSerializer::LoadComponentToEntity<Renderable>(this->cs, entity);
}

// HACK(SMA) : Ugly assigment hack for the Save function below. Really needs to be fixed.
Expand Down Expand Up @@ -49,6 +50,8 @@ Json::Value EntitySerializer::Save(ex::Entity entity) const {
ComponentSerializer::SaveEntityComponent<Stats>(this->cs, entity)[meta::getName<Stats>()]);
AssignIfNotNull(std::move(v), meta::getName<InputResponder>(),
ComponentSerializer::SaveEntityComponent<InputResponder>(this->cs, entity)[meta::getName<InputResponder>()]);
AssignIfNotNull(std::move(v), meta::getName<Renderable>(),
ComponentSerializer::SaveEntityComponent<Renderable>(this->cs, entity)[meta::getName<Renderable>()]);
AssignIfNotNull(std::move(v), meta::getName<PythonScript>(),
ComponentSerializer::SaveEntityComponent<PythonScript>(this->cs, entity)[meta::getName<PythonScript>()]);
return v;
Expand Down
4 changes: 2 additions & 2 deletions Farquaad/src/Systems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ set(SRC
${SRCROOT}/InputSystem.cpp
${SRCROOT}/PhysicsSystem.cpp
${SRCROOT}/MoveSystem.cpp
${SRCROOT}/RenderSystem.cpp
${SRCROOT}/PythonSystem.cpp
${SRCROOT}/ImGuiSystem.cpp
${SRCROOT}/SpriteRenderSystem.cpp
)

set(INC
${INCROOT}/RenderSystem.h
${INCROOT}/PhysicsSystem.h
${INCROOT}/MoveSystem.h
${INCROOT}/InputSystem.h
${INCROOT}/PythonSystem.h
${INCROOT}/ImGuiSystem.h
${INCROOT}/SpriteRenderSystem.h
${INCROOT}/../Systems.hpp
)

Expand Down
10 changes: 7 additions & 3 deletions Farquaad/src/Systems/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ void PhysicsSystem::update(ex::EntityManager &em, ex::EventManager &events, ex::

physics->body = boxbody;

// Assign it a rectangle if there's no renderable component.
if ( !e.has_component<RenderableShape>() ) {
e.assign<RenderableShape>(new sf::RectangleShape((sf::Vector2f)physics->size));
// HACK(SMA) : Assign it a rectangle if there's no drawable component.
// Really should remove me now that we've got the physics debugger.
if ( !e.has_component<Renderable>() ) {
auto renderable = e.assign<Renderable>();
auto rect_ptr = std::make_shared<sf::RectangleShape>((sf::Vector2f)physics->size);
renderable->drawable = rect_ptr;
renderable->transform = rect_ptr;
}
} else {
continue;
Expand Down
12 changes: 0 additions & 12 deletions Farquaad/src/Systems/RenderSystem.cpp

This file was deleted.

58 changes: 58 additions & 0 deletions Farquaad/src/Systems/SpriteRenderSystem.cpp
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;
}
});
}
2 changes: 1 addition & 1 deletion Farquaad/tests/JSONSerializedComponents_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TEST_CASE_METHOD(JSONSerializedFixture, "TestPhysics") {
REQUIRE(v["dirty"].asBool() == true);
REQUIRE(v["category"].asString() == "CATEGORY_1");
REQUIRE(v["mask"].asString() == "0000000000000001");
REQUIRE(v["group"].asString() == "GROUP_1");
REQUIRE(v["group"].asInt() == 0);


Physics actual = Serializable::fromJSON<Physics>(v);
Expand Down
15 changes: 13 additions & 2 deletions Farquaad/tests/PythonSeralizer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,25 @@ class PythonSystemTest {
PythonSystemTest() : em(ev) {
assert(PyImport_AppendInittab("entityx_python_test", initentityx_python_test) != -1
&& "Failed to initialize entityx_python_test Python module");
fs::path exec_dir = get_execute_dir();
try {
Py_Initialize();
std::string str = exec_dir.string();
// Write to tempoaray char[] for PySys_SetPath
std::vector<char> lib_paths(str.begin(), str.end());
lib_paths.push_back('\0');

Py_NoSiteFlag = 1;
Py_SetPythonHome(&lib_paths[0]);
Py_InitializeEx(0);

// Assuming python27.zip is our python lib zip.
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('python27.zip')");
} catch ( ... ) {
PyErr_Print();
PyErr_Clear();
throw;
}
fs::path exec_dir = get_execute_dir();
// HACK(SMA) : Assuming python27.zip contains our python std_lib and in in our exec dir.
// normally this is passed in by the application.
python = std::make_shared<PythonSystem>(&em, exec_dir);
Expand Down
55 changes: 19 additions & 36 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,25 @@ endif()
set(SCRIPTDIR ${PROJECT_SOURCE_DIR}/scripts)

# Add target to copy files
file(GLOB ScriptFiles
${SCRIPTDIR}/action/*.py
${SCRIPTDIR}/entityx/*.py
${SCRIPTDIR}/gamemath/*.py
file(GLOB ExampleResources
${SCRIPTDIR}/action/__init__.py
${SCRIPTDIR}/action/mouse.py
${SCRIPTDIR}/action/shooter.py
${SCRIPTDIR}/entityx/__init__.py
${SCRIPTDIR}/gamemath/__init__.py
${SCRIPTDIR}/gamemath/vector2.py
${SCRIPTDIR}/gamemath/util.py
${FarquaadExamples_SOURCE_DIR}/Config.json
${FarquaadExamples_SOURCE_DIR}/Resources/Box.json
${FarquaadExamples_SOURCE_DIR}/Resources/Hero.json
${FarquaadExamples_SOURCE_DIR}/Resources/Mouse.json
${FarquaadExamples_SOURCE_DIR}/images/perfectface.png
${PYTHON27_STD_ZIP})

add_custom_target(CopyActionScriptFiles DEPENDS ${ScriptFiles})
add_custom_target(CopyExampleResources DEPENDS ${ExampleResources})

# Set target copy and install.
foreach(ScriptFile ${ScriptFiles})
foreach(ScriptFile ${ExampleResources})
# Get the directory of the source file
get_filename_component(PARENT_DIR "${ScriptFile}" DIRECTORY)
get_filename_component(PARENT_DIR "${PARENT_DIR}" ABSOLUTE)
Expand All @@ -47,14 +56,14 @@ foreach(ScriptFile ${ScriptFiles})
string(REPLACE "${PROJECT_SOURCE_DIR}" "" GROUP "${PARENT_DIR}")
string(REPLACE "${PYTHON27_STD_ZIP_DIR}" "" GROUP "${GROUP}")

add_custom_command(TARGET CopyActionScriptFiles PRE_BUILD
add_custom_command(TARGET CopyExampleResources PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
make_directory "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${GROUP}/" )
add_custom_command(TARGET CopyActionScriptFiles PRE_BUILD
add_custom_command(TARGET CopyExampleResources PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy "${ScriptFile}"
"${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${GROUP}/"
DEPENDS ${ScriptFiles}
DEPENDS ${ExampleResources}
COMMENT "Copying ${ScriptFile}")
source_group("${GROUP}" FILES "${ScriptFile}")
install(FILES ${ScriptFile} DESTINATION bin/${GROUP})
Expand Down Expand Up @@ -85,33 +94,7 @@ include_directories(${METASTUFF_INCLUDE_DIRS})

link_directories(${Boost_LIBRARY_DIR})

# Add target to copy files
file(GLOB ConfigFiles
${FarquaadExamples_SOURCE_DIR}/*.json
${FarquaadExamples_SOURCE_DIR}/Resources/*.json)

add_custom_target(CopyConfigs DEPENDS ${ConfigFiles})
foreach(ConfigFile ${ConfigFiles})
# Get the directory of the source file
get_filename_component(PARENT_DIR "${ConfigFile}" DIRECTORY)
get_filename_component(PARENT_DIR "${PARENT_DIR}" ABSOLUTE)

# Remove common directory prefix to make the group
string(REPLACE "${FarquaadExamples_SOURCE_DIR}" "" GROUP "${PARENT_DIR}")

add_custom_command(TARGET CopyConfigs PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
make_directory "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${GROUP}/" )
add_custom_command(TARGET CopyConfigs PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy "${ConfigFile}"
"${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${GROUP}/"
DEPENDS ${ConfigFiles}
COMMENT "Copying ${ConfigFile}" )
install(FILES ${ConfigFile} DESTINATION bin/${GROUP})
endforeach()

add_dependencies(FarquaadExamples CopyConfigs CopyActionScriptFiles)
add_dependencies(FarquaadExamples CopyExampleResources)
target_link_libraries(FarquaadExamples Farquaad)


Expand Down
3 changes: 3 additions & 0 deletions examples/Resources/Hero.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"pythons" : {
"modulename" : "action.shooter",
"class" : "Shooter"
},
"renderable" : {
"texture" : "./images/perfectface.png"
}
}
Binary file added examples/images/perfectface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4206060

Please sign in to comment.