├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── cmake └── modules │ └── FindSSVEntitySystem.cmake ├── include └── SSVEntitySystem │ ├── Core │ ├── Component.hpp │ ├── Component.inl │ ├── Entity.hpp │ ├── Internal │ │ └── EntityIdManager.hpp │ ├── Manager.hpp │ └── Manager.inl │ ├── Global │ └── Typedefs.hpp │ └── SSVEntitySystem.hpp └── init-repository.sh /.gitignore: -------------------------------------------------------------------------------- 1 | callgrind.* 2 | CMakeLists.txt.user.* 3 | 4 | /lib 5 | doc/html 6 | doc/latex 7 | doc/rtf 8 | doc/man 9 | doc/xml 10 | *.0000000 11 | *.5e4d6ab 12 | *.mk 13 | *.project 14 | 15 | ################# 16 | ## Eclipse 17 | ################# 18 | 19 | *.pydevproject 20 | .project 21 | .metadata 22 | bin/ 23 | tmp/ 24 | *.tmp 25 | *.bak 26 | *.swp 27 | *~.nib 28 | local.properties 29 | .classpath 30 | .settings/ 31 | .loadpath 32 | 33 | # External tool builders 34 | .externalToolBuilders/ 35 | 36 | # Locally stored "Eclipse launch configurations" 37 | *.launch 38 | 39 | # CDT-specific 40 | .cproject 41 | 42 | # PDT-specific 43 | .buildpath 44 | 45 | 46 | ################# 47 | ## Visual Studio 48 | ################# 49 | 50 | ## Ignore Visual Studio temporary files, build results, and 51 | ## files generated by popular Visual Studio add-ons. 52 | 53 | # User-specific files 54 | *.suo 55 | *.user 56 | *.sln.docstates 57 | 58 | # Build results 59 | [Dd]ebug/ 60 | *_i.c 61 | *_p.c 62 | *.ilk 63 | *.meta 64 | *.obj 65 | *.pch 66 | *.pdb 67 | *.pgc 68 | *.pgd 69 | *.rsp 70 | *.sbr 71 | *.tlb 72 | *.tli 73 | *.tlh 74 | *.tmp 75 | *.vspscc 76 | .builds 77 | *.dotCover 78 | 79 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 80 | #packages/ 81 | 82 | # Visual C++ cache files 83 | ipch/ 84 | *.aps 85 | *.ncb 86 | *.opensdf 87 | *.sdf 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | 93 | # ReSharper is a .NET coding add-in 94 | _ReSharper* 95 | 96 | # Installshield output folder 97 | [Ee]xpress 98 | 99 | # DocProject is a documentation generator add-in 100 | DocProject/buildhelp/ 101 | DocProject/Help/*.HxT 102 | DocProject/Help/*.HxC 103 | DocProject/Help/*.hhc 104 | DocProject/Help/*.hhk 105 | DocProject/Help/*.hhp 106 | DocProject/Help/Html2 107 | DocProject/Help/html 108 | 109 | # Click-Once directory 110 | publish 111 | 112 | # Others 113 | [Bb]in 114 | [Oo]bj 115 | sql 116 | TestResults 117 | *.Cache 118 | ClientBin 119 | stylecop.* 120 | ~$* 121 | *.dbmdl 122 | Generated_Code #added for RIA/Silverlight projects 123 | 124 | # Backup & report files from converting an old project file to a newer 125 | # Visual Studio version. Backup files are not needed, because we have git ;-) 126 | _UpgradeReport_Files/ 127 | Backup*/ 128 | UpgradeLog*.XML 129 | 130 | 131 | 132 | ############ 133 | ## Windows 134 | ############ 135 | 136 | # Windows image file caches 137 | Thumbs.db 138 | 139 | # Folder config file 140 | Desktop.ini 141 | 142 | 143 | ############# 144 | ## Python 145 | ############# 146 | 147 | *.py[co] 148 | 149 | # Packages 150 | *.egg 151 | *.egg-info 152 | dist 153 | build 154 | eggs 155 | parts 156 | bin 157 | var 158 | sdist 159 | develop-eggs 160 | .installed.cfg 161 | 162 | # Installer logs 163 | pip-log.txt 164 | 165 | # Unit test / coverage reports 166 | .coverage 167 | .tox 168 | 169 | #Translations 170 | *.mo 171 | 172 | #Mr Developer 173 | .mr.developer.cfg 174 | 175 | # Mac crap 176 | .DS_Store 177 | *.ogg 178 | *.wav 179 | *.o 180 | *.d 181 | *.dll 182 | *.mp3 183 | *.bat 184 | *.exe 185 | *.ttf 186 | *.i 187 | *.a 188 | *.mk 189 | *.project 190 | *.so/CMakeLists.txt.user.7626ec3 191 | /CMakeLists.txt.user.7626ec3 192 | CMakeLists.txt.user.* 193 | asanlogs 194 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "extlibs/SSVUtils"] 2 | path = extlibs/SSVUtils 3 | url = https://github.com/SuperV1234/SSVUtils.git 4 | [submodule "extlibs/SSVCMake"] 5 | path = extlibs/SSVCMake 6 | url = https://github.com/SuperV1234/SSVCMake.git 7 | [submodule "extlibs/vrm_pp"] 8 | path = extlibs/vrm_pp 9 | url = https://github.com/SuperV1234/vrm_pp.git 10 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(SSVEntitySystem) 3 | 4 | set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../SSVCMake/cmake/modules/;${CMAKE_SOURCE_DIR}/extlibs/SSVCMake/cmake/modules/;${CMAKE_MODULE_PATH}") 5 | include(SSVCMake) 6 | 7 | SSVCMake_setDefaults() 8 | SSVCMake_findExtlib(vrm_pp) 9 | SSVCMake_findExtlib(SSVUtils) 10 | SSVCMake_setAndInstallHeaderOnly() 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2016 Vittorio Romeo 2 | AFL License page: http://opensource.org/licenses/AFL-3.0 3 | 4 | Academic Free License ("AFL") v. 3.0 This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: 5 | Licensed under the Academic Free License version 3.0 6 | 1) Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: 7 | a) to reproduce the Original Work in copies, either alone or as part of a collective work; 8 | b) to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; 9 | c) to distribute or communicate copies of the Original Work and Derivative Works to the public, under any license of your choice that does not contradict the terms and conditions, including Licensor's reserved rights and remedies, in this Academic Free License; 10 | d) to perform the Original Work publicly; and 11 | e) to display the Original Work publicly. 12 | 2) Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. 13 | 3) Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. 14 | 4) Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. 15 | 5) External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). 16 | 6) Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 17 | 7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. 18 | 8) Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. 19 | 9) Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including "fair use" or "fair dealing"). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). 20 | 10) Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 21 | 11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. 22 | 12) Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 23 | 13) Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 24 | 14) Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 25 | 15) Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. 26 | 16) Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Academic Free License" or "AFL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. -------------------------------------------------------------------------------- /cmake/modules/FindSSVEntitySystem.cmake: -------------------------------------------------------------------------------- 1 | FIND_PATH(SSVENTITYSYSTEM_INCLUDE_DIR 2 | NAMES SSVEntitySystem/SSVEntitySystem.hpp 3 | PATH_SUFFIXES include/ 4 | PATHS 5 | "${PROJECT_SOURCE_DIR}/../SSVEntitySystem/" 6 | ${SSVENTITYSYSTEM_ROOT} 7 | $ENV{SSVENTITYSYSTEM_ROOT} 8 | /usr/local/ 9 | /usr/ 10 | /sw/ 11 | /opt/local/ 12 | /opt/csw/ 13 | /opt/ 14 | "${PROJECT_SOURCE_DIR}/extlibs/SSVEntitySystem/" 15 | "${CMAKE_CURRENT_LIST_DIR}/../../" 16 | 17 | NO_DEFAULT_PATH 18 | ) 19 | 20 | message("\nFound SSVEntitySystem include at: ${SSVENTITYSYSTEM_INCLUDE_DIR}.\n") 21 | 22 | IF(SSVENTITYSYSTEM_INCLUDE_DIR) 23 | #{ 24 | SET(SSVENTITYSYSTEM_FOUND TRUE) 25 | #} 26 | ELSE(SSVENTITYSYSTEM_INCLUDE_DIR) 27 | #{ 28 | SET(SSVENTITYSYSTEM_FOUND FALSE) 29 | #} 30 | ENDIF(SSVENTITYSYSTEM_INCLUDE_DIR) 31 | 32 | IF(SSVENTITYSYSTEM_FOUND) 33 | #{ 34 | MESSAGE(STATUS "Found SSVEntitySystem in ${SSVENTITYSYSTEM_INCLUDE_DIR}") 35 | #} 36 | ELSE(SSVENTITYSYSTEM_FOUND) 37 | #{ 38 | IF(SSVENTITYSYSTEM_FIND_REQUIRED) 39 | #{ 40 | MESSAGE(FATAL_ERROR "Could not find SSVEntitySystem library") 41 | #} 42 | ENDIF(SSVENTITYSYSTEM_FIND_REQUIRED) 43 | #} 44 | ENDIF(SSVENTITYSYSTEM_FOUND) 45 | 46 | MARK_AS_ADVANCED(SSVENTITYSYSTEM_INCLUDE_DIR) -------------------------------------------------------------------------------- /include/SSVEntitySystem/Core/Component.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2015 Vittorio Romeo 2 | // License: Academic Free License ("AFL") v. 3.0 3 | // AFL License page: http://opensource.org/licenses/AFL-3.0 4 | 5 | #ifndef SSES_COMPONENT 6 | #define SSES_COMPONENT 7 | 8 | #include 9 | 10 | // TODO: refactor, add to vrm_core and ssvu 11 | 12 | #define TEMP_NAMESPACE VRM_PP_CAT(temp_namespace, __LINE__) 13 | 14 | #define TEMP_NAMESPACE_END 15 | 16 | #define TEMP_MARK_NONFINAL(base) \ 17 | namespace TEMP_NAMESPACE \ 18 | { \ 19 | struct SSVU_ATTRIBUTE(unused) temp_marker final : base \ 20 | { \ 21 | }; \ 22 | } 23 | 24 | #define TEMP_MARK_NONFINAL_METHOD(base, return_type, method) \ 25 | namespace TEMP_NAMESPACE \ 26 | { \ 27 | struct SSVU_ATTRIBUTE(unused) temp_marker final : base \ 28 | { \ 29 | inline return_type SSVU_ATTRIBUTE(unused) method override {} \ 30 | }; \ 31 | } 32 | 33 | 34 | namespace sses 35 | { 36 | class Component 37 | { 38 | friend Entity; 39 | 40 | private: 41 | Entity& entity; 42 | 43 | inline virtual void update(FT) {} 44 | inline virtual void draw() {} 45 | 46 | public: 47 | inline Component(Entity& mE) noexcept : entity{mE} {} 48 | 49 | inline Component(const Component&) = delete; 50 | inline Component& operator=(const Component&) = delete; 51 | 52 | inline virtual ~Component() {} 53 | 54 | inline auto& getEntity() const noexcept { return entity; } 55 | inline auto& getManager() const noexcept; 56 | }; 57 | } 58 | 59 | TEMP_MARK_NONFINAL(sses::Component) 60 | TEMP_MARK_NONFINAL_METHOD(sses::Component, void, update(sses::FT)) 61 | TEMP_MARK_NONFINAL_METHOD(sses::Component, void, draw()) 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /include/SSVEntitySystem/Core/Component.inl: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2015 Vittorio Romeo 2 | // License: Academic Free License ("AFL") v. 3.0 3 | // AFL License page: http://opensource.org/licenses/AFL-3.0 4 | 5 | #ifndef SSES_COMPONENT_INL 6 | #define SSES_COMPONENT_INL 7 | 8 | namespace sses 9 | { 10 | inline auto& Component::getManager() const noexcept 11 | { 12 | return entity.getManager(); 13 | } 14 | } 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /include/SSVEntitySystem/Core/Entity.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2015 Vittorio Romeo 2 | // License: Academic Free License ("AFL") v. 3.0 3 | // AFL License page: http://opensource.org/licenses/AFL-3.0 4 | 5 | #ifndef SSES_ENTITY 6 | #define SSES_ENTITY 7 | 8 | namespace sses 9 | { 10 | class Entity 11 | { 12 | friend Manager; 13 | 14 | private: 15 | EntityStat stat; 16 | Manager& manager; 17 | GroupBitset groups; 18 | std::vector components; 19 | std::array componentPtrs; 20 | TypeIdxBitset typeIdsBitset; 21 | int drawPriority{0}; 22 | 23 | inline void update(FT mFT) 24 | { 25 | for(const auto& c : components) c->update(mFT); 26 | } 27 | inline void draw() 28 | { 29 | for(const auto& c : components) c->draw(); 30 | } 31 | 32 | public: 33 | inline Entity(const EntityStat& mStat, Manager& mManager) noexcept 34 | : stat(mStat), 35 | manager(mManager) 36 | { 37 | } 38 | inline ~Entity() { manager.entityIdManager.reclaim(stat); } 39 | 40 | inline Entity(const Entity&) = delete; 41 | inline Entity& operator=(const Entity&) = delete; 42 | 43 | inline void destroy() noexcept { manager.del(*this); } 44 | 45 | inline void setDrawPriority(int mDrawPriority) 46 | { 47 | drawPriority = mDrawPriority; 48 | } 49 | 50 | inline const auto& getStat() const noexcept { return stat; } 51 | inline auto& getManager() const noexcept { return manager; } 52 | inline int getDrawPriority() const noexcept { return drawPriority; } 53 | inline auto& getComponents() noexcept { return components; } 54 | 55 | template 56 | inline bool hasComponent() const noexcept 57 | { 58 | return typeIdsBitset[Impl::getTypeIdx()]; 59 | } 60 | template 61 | inline T& getComponent() noexcept 62 | { 63 | SSVU_ASSERT(hasComponent()); 64 | return ssvu::castUp(*componentPtrs[Impl::getTypeIdx()]); 65 | } 66 | template 67 | inline T& createComponent(TArgs&&... mArgs) 68 | { 69 | SSVU_ASSERT(!hasComponent()); 70 | 71 | auto& result(manager.componentRecycler.getCreateEmplace( 72 | components, *this, FWD(mArgs)...)); 73 | 74 | componentPtrs[Impl::getTypeIdx()] = &result; 75 | typeIdsBitset[Impl::getTypeIdx()] = true; 76 | return result; 77 | } 78 | 79 | // Groups 80 | inline void setGroups(bool mOn, Group mGroup) noexcept 81 | { 82 | groups[mGroup] = mOn; 83 | if(mOn) manager.addToGroup(this, mGroup); 84 | } 85 | inline void addGroups(Group mGroup) noexcept 86 | { 87 | groups[mGroup] = true; 88 | manager.addToGroup(this, mGroup); 89 | } 90 | inline void delGroups(Group mGroup) noexcept { groups[mGroup] = false; } 91 | inline void clearGroups() noexcept { groups.reset(); } 92 | template 93 | inline void setGroups( 94 | bool mOn, Group mGroup, TGroups... mGroups) noexcept 95 | { 96 | setGroups(mOn, mGroup); 97 | setGroups(mOn, mGroups...); 98 | } 99 | template 100 | inline void addGroups(Group mGroup, TGroups... mGroups) noexcept 101 | { 102 | addGroups(mGroup); 103 | addGroups(mGroups...); 104 | } 105 | template 106 | inline void delGroups(Group mGroup, TGroups... mGroups) noexcept 107 | { 108 | delGroups(mGroup); 109 | delGroups(mGroups...); 110 | } 111 | inline bool hasGroup(Group mGroup) const noexcept 112 | { 113 | return groups[mGroup]; 114 | } 115 | inline bool hasAnyGroup(const GroupBitset& mGroups) const noexcept 116 | { 117 | return (groups & mGroups).any(); 118 | } 119 | inline bool hasAllGroups(const GroupBitset& mGroups) const noexcept 120 | { 121 | return (groups & mGroups).all(); 122 | } 123 | inline const auto& getGroups() const noexcept { return groups; } 124 | }; 125 | } 126 | 127 | #endif 128 | -------------------------------------------------------------------------------- /include/SSVEntitySystem/Core/Internal/EntityIdManager.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2015 Vittorio Romeo 2 | // License: Academic Free License ("AFL") v. 3.0 3 | // AFL License page: http://opensource.org/licenses/AFL-3.0 4 | 5 | #ifndef SSES_INTERNAL_ENTITYIDMANAGER 6 | #define SSES_INTERNAL_ENTITYIDMANAGER 7 | 8 | namespace sses 9 | { 10 | namespace Impl 11 | { 12 | class EntityIdManager 13 | { 14 | private: 15 | std::vector freeIds; 16 | std::array idCtrs; 17 | 18 | public: 19 | inline EntityIdManager() : freeIds(maxEntities) 20 | { 21 | ssvu::iota(freeIds, 0); 22 | ssvu::fill(idCtrs, 0); 23 | } 24 | 25 | inline auto getFreeStat() 26 | { 27 | EntityId id{freeIds.back()}; 28 | freeIds.pop_back(); 29 | return EntityStat{id, idCtrs[id]}; 30 | } 31 | inline bool isAlive(const EntityStat& mStat) const noexcept 32 | { 33 | return idCtrs[mStat.id] == mStat.ctr; 34 | } 35 | inline void reclaim(const EntityStat& mStat) 36 | { 37 | freeIds.emplace_back(mStat.id); 38 | ++idCtrs[mStat.id]; 39 | } 40 | }; 41 | } 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/SSVEntitySystem/Core/Manager.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2015 Vittorio Romeo 2 | // License: Academic Free License ("AFL") v. 3.0 3 | // AFL License page: http://opensource.org/licenses/AFL-3.0 4 | 5 | #ifndef SSES_MANAGER 6 | #define SSES_MANAGER 7 | 8 | namespace sses 9 | { 10 | class Manager 11 | { 12 | friend Entity; 13 | friend Component; 14 | 15 | private: 16 | Impl::EntityIdManager entityIdManager; 17 | ssvu::MonoManager entities; 18 | std::vector toSort; 19 | std::array, maxGroups> groupedEntities; 20 | ComponentRecycler componentRecycler; 21 | 22 | inline void addToGroup(Entity* mEntity, Group mGroup) 23 | { 24 | SSVU_ASSERT(!ssvu::contains(groupedEntities[mGroup], mEntity)); 25 | groupedEntities[mGroup].emplace_back(mEntity); 26 | } 27 | inline void del(Entity& mEntity) noexcept { entities.del(mEntity); } 28 | inline void refresh(); 29 | 30 | public: 31 | inline Manager() = default; 32 | 33 | inline Manager(const Manager&) = delete; 34 | inline Manager& operator=(const Manager&) = delete; 35 | 36 | inline ~Manager() { clear(); } 37 | 38 | inline void clear() noexcept 39 | { 40 | entities.clear(); 41 | for(auto& v : groupedEntities) v.clear(); 42 | } 43 | inline void update(FT mFT); 44 | inline void draw(); 45 | 46 | inline auto& createEntity() 47 | { 48 | return entities.create(entityIdManager.getFreeStat(), *this); 49 | } 50 | 51 | inline auto& getEntities() noexcept { return entities; } 52 | inline auto& getEntities(Group mGroup) noexcept 53 | { 54 | return groupedEntities[mGroup]; 55 | } 56 | 57 | inline bool hasEntity(Group mGroup) 58 | { 59 | return !groupedEntities[mGroup].empty(); 60 | } 61 | inline auto getEntityCount(Group mGroup) const noexcept 62 | { 63 | return groupedEntities[mGroup].size(); 64 | } 65 | 66 | inline bool isAlive(const EntityStat& mStat) const noexcept 67 | { 68 | SSVU_ASSERT(!isNullEntityStat(mStat)); 69 | return entityIdManager.isAlive(mStat); 70 | } 71 | }; 72 | } 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /include/SSVEntitySystem/Core/Manager.inl: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2015 Vittorio Romeo 2 | // License: Academic Free License ("AFL") v. 3.0 3 | // AFL License page: http://opensource.org/licenses/AFL-3.0 4 | 5 | #ifndef SSES_MANAGER_INL 6 | #define SSES_MANAGER_INL 7 | 8 | namespace sses 9 | { 10 | inline void Manager::refresh() 11 | { 12 | for(auto i(0u); i < maxGroups; ++i) 13 | ssvu::eraseRemoveIf(groupedEntities[i], [i](const Entity* mEntity) 14 | { 15 | return ssvu::MonoManager::isDead(mEntity) || 16 | !mEntity->hasGroup(i); 17 | }); 18 | entities.refresh(); 19 | } 20 | inline void Manager::update(FT mFT) 21 | { 22 | refresh(); 23 | for(const auto& e : entities) e->update(mFT); 24 | } 25 | inline void Manager::draw() 26 | { 27 | toSort.clear(); 28 | for(const auto& e : entities) toSort.emplace_back(e.get()); 29 | ssvu::sortStable(toSort, [](const Entity* mA, const Entity* mB) 30 | { 31 | return mA->getDrawPriority() > mB->getDrawPriority(); 32 | }); 33 | for(const auto& e : toSort) e->draw(); 34 | } 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /include/SSVEntitySystem/Global/Typedefs.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2015 Vittorio Romeo 2 | // License: Academic Free License ("AFL") v. 3.0 3 | // AFL License page: http://opensource.org/licenses/AFL-3.0 4 | 5 | #ifndef SSES_TYPEDEFS 6 | #define SSES_TYPEDEFS 7 | 8 | namespace sses 9 | { 10 | // Forward declarations 11 | class Component; 12 | class Entity; 13 | class Manager; 14 | 15 | // SSVU Typedefs 16 | using ssvu::SizeT; 17 | using ssvu::FT; 18 | 19 | // Constants 20 | static constexpr SizeT maxEntities{1'000'000}; 21 | static constexpr SizeT maxGroups{32}; 22 | static constexpr SizeT maxComponents{64}; 23 | 24 | // Entity typedefs 25 | using EntityId = int; 26 | using EntityIdCtr = int; 27 | struct EntityStat 28 | { 29 | EntityId id; 30 | EntityIdCtr ctr; 31 | }; 32 | 33 | // Type index typedefs 34 | using TypeIdx = SizeT; 35 | using TypeIdxBitset = std::bitset; 36 | 37 | // Group typedefs 38 | using Group = unsigned int; 39 | using GroupBitset = std::bitset; 40 | 41 | // Recycler typedefs 42 | using ComponentRecycler = ssvu::PolyRecycler; 43 | using ComponentRecyclerPtr = ComponentRecycler::PtrType; 44 | 45 | namespace Impl 46 | { 47 | // Returns the next unique bit index for a type 48 | inline auto getLastTypeIdx() noexcept 49 | { 50 | static TypeIdx lastIdx{0}; 51 | SSVU_ASSERT(lastIdx < maxComponents); 52 | return lastIdx++; 53 | } 54 | 55 | // Stores a specific bit index for a Component type 56 | template 57 | struct TypeIdxInfo 58 | { 59 | static TypeIdx idx; 60 | }; 61 | template 62 | TypeIdx TypeIdxInfo::idx{getLastTypeIdx()}; 63 | 64 | // Shortcut to get the bit index of a Component type 65 | template 66 | inline auto getTypeIdx() noexcept 67 | { 68 | SSVU_ASSERT_STATIC(ssvu::isBaseOf(), 69 | "`T` must derive from `Component`"); 70 | return TypeIdxInfo::idx; 71 | } 72 | } 73 | 74 | inline auto getNullEntityStat() noexcept { return EntityStat{-1, -1}; } 75 | inline bool isNullEntityStat(const EntityStat& mEntityStat) noexcept 76 | { 77 | return mEntityStat.ctr == -1 && mEntityStat.id == -1; 78 | } 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /include/SSVEntitySystem/SSVEntitySystem.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2015 Vittorio Romeo 2 | // License: Academic Free License ("AFL") v. 3.0 3 | // AFL License page: http://opensource.org/licenses/AFL-3.0 4 | 5 | #ifndef SSVENTITYSYSTEM 6 | #define SSVENTITYSYSTEM 7 | 8 | #include 9 | #include 10 | #include "SSVEntitySystem/Global/Typedefs.hpp" 11 | #include "SSVEntitySystem/Core/Component.hpp" 12 | #include "SSVEntitySystem/Core/Internal/EntityIdManager.hpp" 13 | #include "SSVEntitySystem/Core/Manager.hpp" 14 | #include "SSVEntitySystem/Core/Entity.hpp" 15 | #include "SSVEntitySystem/Core/Component.inl" 16 | #include "SSVEntitySystem/Core/Manager.inl" 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /init-repository.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This bash script, called in a repository with submodules, inits and pulls all submodules 4 | 5 | echo "Initializing all submodules..." 6 | git submodule update --init 7 | 8 | echo "Discarding all submodule changes..." 9 | git submodule foreach "git checkout . ; git reset --hard origin/master" 10 | 11 | echo "Recursively pulling all submodules..." 12 | git submodule foreach "git pull origin master" 13 | --------------------------------------------------------------------------------