SteveHatcher

Members
  • Content count

    135
  • Joined

  • Last visited

Community Reputation

1236 Excellent

About SteveHatcher

  • Rank
    Member
  1. Hi All,   I am getting to grips with component based game objects (heavily using the example from game programming gems GPG 6), but run into implementation problems. I am using the DirectX toolkit for graphics rendering, and modifying the sample to prototype my design.   Right now, I am just trying to get one 'sprite' on screen, first I need to load the texture the sprite will use. Using the directX toolkit this is done by calling CreateDDSTextureFromFile(device, L"windowslogo.dds", nullptr, m_texture.ReleaseAndGetAddressOf()).   Where is the correct place to load the sprite sheet? Is it loaded inside a component? Or is the whole sprite sheet loaded at once (in say Game::init(), and components use the bits they need?   Then, when it comes to rendering, GPG 6 doesn't actually provide implementation details of virtual void render(); (inside the visual Component). DirectX TK draws with m_sprites->Draw(m_texture.Get(), XMFLOAT2(10, 75), nullptr, Colors::White); So again, I am a bit stuck. To draw like this, I need the std::unique_ptr<DirectX::SpriteBatch> m_sprites; Does this belong in a components private member?   This would mean my visual Component would look as follows:   class gocVisualCircle : public gocVisual { public:     virtual const goc_id_type& componentID() const {         return goc_id_type("gocVisualCircle");     }     virtual void render() const {         m_sprites->Draw(m_texture.Get(), XMFLOAT2(10, 75), nullptr, Colors::White);     };     gocVisualCircle(Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_texture) {         CreateDDSTextureFromFile(device, L"windowslogo.dds", nullptr, m_texture.ReleaseAndGetAddressOf());     }; private:     std::unique_ptr<DirectX::SpriteBatch> m_sprites; };   Am I on the right track? I am worried that this is too closely coupled to the directX toolkit objects.   Thanks for your time and help.
  2. Help with using a class Template for adding generic 'Systems'

    Alberth: Thanks for your reply. Spelling out what my templates actually are asking for helps... I find them very confusing. template <class S>     void add(std::shared_ptr<S> system)     {         m_systems.insert(system);     } Please check my understanding: So when I initiate this template, I have to make sure I put a 'Type' inside the angle brackets (< >), but I also need the parameter... a bit like a normal function? So it would be equivalent to me writing (e.g. this is what it would generate):     void add(std::shared_ptr<MovementSystem> movementSystem)     {         m_systems.insert(movementSystem);     } I think I can see one problem here, on this example I am receiving the error "no instance of overloaded function "std::vector<_Ty, _Alloc>::insert [with _Ty=std::shared_ptr<System>, _Alloc=std::allocator<std::shared_ptr<System>>]" matches the argument list". I was using insert() wrongly, so I have changed it to push_back()... (I will worry about the fine details later.. just trying to get my head around templates for now hehe...)   When I try your example using std::allocate_shared I receive the error: "no instance of function template "std::allocate_shared" matches the argument list". I will be honest I am a bit lost as to whats going on here.   Am I using templates the right way for this? My idea is to create all my systems (by inheriting from the System base class) then just add them all like:     systems.add<MovementSystem>();     systems.add<CollisionSystem>();     systems.add<AiControlSystem>();//etc etc (Note this is similar to how the ECS framework entitiyx works)   BloodyEpi: Thanks for your reply. 'no need for "using namespace std", you are writing it anyway(which is a good thing)' I usually use using namespace std just in little demos like this but not in any real project. I forgot I was using both haha...   'virtual destructor in System': Thanks for picking this up. Just to clear my understanding, this will mean that my systems instances are cleaned up when my systems end? (e.g. through changing a level, or ending the game?)   'At some point you will want specify the order in which the systems are updated, currently you can control this only by the order in which they are added' I will eventually get to this, right now just trying to get the basic idea working.   'Its hard to give advice without seeing how you will be handling the components': For a very first build I will be ripping the components I need out like this: void MovementSystem::update(float dt) {     TransformComponent* playerTransformComponent = static_cast<TransformComponent*>(m_gameObjects[0]->GetComponent(COMPONENT_TRANSFORM).get());     auto playerPosX = playerTransformComponent->position.x;     auto playerPosY = playerTransformComponent->position.y; } (just a little test, hence I am not looping and just using m_gameObjects[0])   Thanks all
  3. Hi All,   I am trying to make a component based gameObject engine. In this, my gameObjects are created, and then components (like TransformComponent and RenderComponent) are added to these gameObjects.   I want to now add a System.. system, which picks up components and acts on the relevant ones. A bit like ECS, but I am not using any framework, and want to code this all myself to learn. I also don't plan on the 'Entity'.   I want my game to have a SystemManager, which stores all of my systems, and will be iterated through at update() time. I thought this is a good place to use templates, because I will be adding lots of different Systems into my systemManager class. This is the first time I have used templates and I am stuck. Below is an example of my attempt. #include <vector> #include <iostream> #include <memory> #include <string> using namespace std; class System { public: System() {}; ~System() {}; virtual void update(float dt) = 0; }; class MovementSystem : public System { public: MovementSystem() {}; ~MovementSystem() {}; void update(float dt) {}; }; class SystemManager { private: std::vector<std::shared_ptr<System>> m_systems; public: SystemManager() {}; ~SystemManager() {}; template <class S> void add(std::shared_ptr<S> system) { m_systems.insert(system); } }; int main() { SystemManager systems; systems.add<MovementSystem>(); getchar(); return 0; } I get errors 'error C2672: 'SystemManager::add': no matching overloaded function found' and 'error C2780: 'void SystemManager::add(std::shared_ptr<_Ty>)': expects 1 arguments - 0 provided'   Firstly what am I doing wrong? Any guidance on how I am going about things or general advice on this design is very much appreciated. Thanks    
  4. Getting gameObject components to process in systems...

    Hi haegarr, thanks for your replys. It is replies like these that really help me learn and iterate my design... Ahh, so so by default, it will have to loop over every GameObject weather or not it has or needs to update the component or not. I guess this is were ECS comes in? Ensuring the entities are processed in an efficient way.   That is true, I will probably split it to TransformComponent and PlacementComponent to make that obvious, thanks for that!   I see, so the movement system should update anything with a TranasformComponent. Once I add some enemies I will need to workout where InputComponent and AiComponent fit into this.   This sounds good. So all of this should happen in a gameObjectManager.update() step? I think I will just implement the simple one then go from there.   I do not quite understand what you mean by this? So each loop, i would call PlayerControllerSystem.update() and AiAgentControllerSystem.update(), and they in turn would call the 'lower' MovementSystem.update()?   Thanks again!
  5. Hi All,   I am learning to implement a component based gameObject design to my game engine. Currently, I have a TransformComponent which inherits from Component. My GameObjects's contain a map of Components, and AddComponent and GetComponent member functions. My Game class contains a std::vector<std::shared_ptr<GameObject>> m_gameObjects;   I have successfully created a 'Player' and added it to my vector of GameObjects like so: auto playerTransformComponent = std::make_shared<TransformComponent>(); Vector2 v; v.x = 1; v.y = 1; playerTransformComponent->position = v; auto player = std::make_shared<GameObject>(); player->AddComponent(COMPONENT_TRANSFORM, playerTransformComponent); m_gameObjects.push_back(player); First of all, am I going about things the right way?   Now, I would like to add a System, that controls logic. Note, I do not want to make a full blown entity component system (ecs) design. I want to be able to code all of this myself, not use an already made framework.   What I have come up with is a MovementSystem that Inherits from System, and implements update as follows: void MovementSystem::update(float dt) { TransformComponent* playerTransformComponent = static_cast<TransformComponent*>(m_gameObjects[0]->GetComponent(COMPONENT_TRANSFORM).get()); auto playerPosX = playerTransformComponent->position.x; auto playerPosY = playerTransformComponent->position.y; } Note that I am just testing it on ONE Game object (the player), hence I use m_gameObjects[0].   The problem is, my MovementSystem does not know of m_gameObjects, since that exists in the Game class. (identifier "m_gameObjects" is undefined).   So it seems I need some sort of GameObjectManager (yes... the dreaded manager). I am a bit stuck with options to proceed from here. Are there any design patterns that come to mind? Or will something like passing the MovementSystem a pointer to my gameObjects work fine?   Thanks      
  6. Passing data between states in an FSM

    I asked a similar questions here   http://www.gamedev.net/topic/681957-transitioning-game-states-when-using-a-stack-of-states   and Kylotans excellent answer has been working well. I guess it is similar to your option 3.
  7. Inserting element into vector during iteration causes crash

    Ahh okay, but when I remove the resize(10) I still get the crash, so I am a bit confused now...       Documentation of std::vector will point out that element insertion invalidates iterators.   It looks like this vector is a primary owner for these objects. If you can ensure that the vector outlives anything that may refer to its contents then you could just use std::vector<Test1> and then take references to elements or (preferably) simply refer to them directly as needed. If you're thinking about polymorphism then that won't work though.     The actual situation in my game needs polymorphism, thanks though!   Thanks everyone for your input I am learning a lot!
  8. Inserting element into vector during iteration causes crash

      Ahh okay thanks.   Now I am wondering if adding them this way vs. other methods is most recommended.   Thanks!  
  9. Inserting element into vector during iteration causes crash

      Ahh thanks! That is an excellent explanation. I *thought* that reserve sets up the vector such that it is ready to take the 'reserved' number of items before it must resize itself, but it still fills them up from 0, 1, 2 etc...   I will have a look at your suggested workarounds, I thought this sort of thing would be quite common in C++ so am surprised at this behavior!  
  10. Inserting element into vector during iteration causes crash

    Hi vstrakh,   Thanks for your reply, I am slowly learning c++'s ways....   So I added this in my constructor: Test(){ m_test.reserve(10); m_test.resize(10); cout << "struct Test constructor" << endl; } I am still getting the crash though...   Thanks for any help.
  11. Hi All,   I have a situation in my code where by during iteration of my vector of pointers, I am modifying the original container. I have done some research and learned this causes a crash (I Think)... The problem I am facing is illustrated in my test program as follows: #include <vector> #include <iostream> #include <memory> using namespace std; struct Test1 { void update(){ cout << "update from Test1" << endl; } }; struct Test { vector<shared_ptr<Test1>> m_test; void pushOn(std::shared_ptr<Test1> &test1) { m_test.push_back(test1); } void update() { for (vector<shared_ptr<Test1>>::reverse_iterator i = m_test.rbegin(); i != m_test.rend(); ++i) { auto newPtr = make_shared<Test1>(); (*i)->update(); pushOn(newPtr); cout << "if you read this it passed" << endl; } } }; int main() { auto TestClass = make_shared<Test>(); auto aaa = make_shared<Test1>(); TestClass->pushOn(aaa); TestClass->update(); getchar(); return 0; } If you run the program with pushOn(newPtr); commended out, then it works, otherwise it crashes.   I have some questions.   1. May I have some advice on how to fix this?   2. If I am encountering this, does that mean a fundamentally flawed program design?   Thanks
  12. Transitioning game states when using a stack of states

      Yes! Thanks! That's amazing. I couldn't quite figure out how to apply the demos of this sort of stuff I have read to my game. Much appreciated!     Yeah.. I see what you mean guys, Thanks. I think I need to separate the idea of GameState and "Screen or UI State". Since In my game I will be wanting these 'overlays. As Kylotan says though It is a fine line and I am having trouble deciding what constitutes as a "GameState" and "ScreenState".  
  13. Transitioning game states when using a stack of states

      Hi L. Spiro. Thanks, this makes sense. My reasoning for the stack of states was to allow other 'states' or 'screens' ontop of each other during the game. An example of this is diablo 2, when you select what skills you want as right click, you bring up the 'skill' bar, the game keeps running behind. I want this sort of functionality. Thanks, I will check out your article, I have gone down the track of my implementation for now so want to try and finish that.       Sure. If the Game has the concept of being in different states, it makes sense for it to own the StateMachine.   Not sure how to answer the 'pass that in to each GameState' query because it didn't seem like a complete sentence.   And as L. Spiro noted above, it's rare you actually need a stack of states. Alongside that pushState function you will also want a replaceState function which is basically a pop followed by a push, and that's the method you'll use more frequently.     Agreed I will be mainly with one state, but it is the in game 'upgrades' or 'skill select' screens that I have this in mind for...   Regarding my question, in your original post you say "The simplest way is to have a StateMachine object that holds your vector of states, pass that in to each GameState when it is created"   I pass the state machine object into the GameState?   Right now I have got void GameImplementation::init(HWND hw) { auto splashScreenState = std::make_shared<SplashScreenState>(); m_stateMachine.pushState(splashScreenState); } I don't quite understand what you mean. Sorry I am quite a beginner at this. Thanks
  14. Transitioning game states when using a stack of states

    Hi Kylotan, thanks for your reply. Simple is good for me! I like the idea of each state being able to push and replace states directly...   I am a bit confused when you say pass that in to each GameState when it is created,   So my StateMachine class currently looks as: class StateMachine { private: public: std::vector<std::shared_ptr<GameState>> m_states; void pushState(const std::shared_ptr<GameState> &state); }; Do I have this StateMachine in my Game class?   Thanks for your help!
  15. I am trying to implement a game state system in my engine. I have read around a bit and think I am able to tackle a stack of gamestates. In order to prototype I am aiming to have first a splash screen, then a main menu, and then the "playing" state.   I am wondering the best way to transition from one state to another?   Right now, I have a GameState class, class GameState { public: GameState(); ~GameState(); virtual void update(const float deltaTime) = 0; }; and my Game class (of which my actual game inherits from), with a vector of states std::vector<std::shared_ptr<GameState>> m_states; So right now, I can add the first state (the SplashScreenState, Inherited from GameState) using game.pushState(std::make_shared<SplashScreenState>()); and in my game update loop I call for (std::vector<std::shared_ptr<GameState>>::reverse_iterator i = m_states.rbegin(); i != m_states.rend(); ++i) { (*i)->update(frameTime); } to update each State. So right now my SplashScreenState is getting updated, and a timer transitions it. Once the timer runs out I want the MainMenuState to activate.   The question is, given my current design, how do I best go about this? Should I have something inside SplashScreenState::update() that lets me push on the next state directly? Should My GameState class handle all transitions internally? Where do I go about actually explicitly stating which state I want to go from where and to (Splash Screen to Main Menu, Main Menu to Playing, Playing to Paused...).   Thanks for any insight.