Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Pass common objects around classes with struct  (Read 1554 times)

0 Members and 1 Guest are viewing this topic.

Surge

  • Newbie
  • *
  • Posts: 3
    • View Profile
Pass common objects around classes with struct
« on: April 02, 2017, 08:16:39 pm »
Hi everyone!  :) It's my first post here so I apologize if I get anything wrong. Anyway, on my game project, I want to pass objects that I commonly use around using a custom struct named Context. My question is, am I doing it correctly? Please tell me if I am copying things that I shouldn't be and that everything is properly referenced. Here's the code.

// context.hpp

#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include "resource_holder/resource_holder.hpp"
#include "resource_holder/resource_identifiers.hpp"

struct Context {
    Context(sf::RenderWindow& window, TextureHolder& textures, FontHolder& fonts);

    sf::RenderWindow* window;
    TextureHolder* textures;
    FontHolder* fonts;
}

// context.cpp

#include "context.hpp"

Context::Context(sf::RenderWindow& window, TextureHolder& textures, FontHolder& fonts)
: window(&window)
, textures(&textures)
, fonts(&fonts) {
   
}

 

And then in game.cpp


Context Game::createContext() {
    return Context(m_window, m_textures, m_fonts);
}

. . .

m_player(createContext);

 

And here is Player:


// player.hpp

#include <SFML/Graphics.hpp>
#include "world/entity.hpp"
#include "context/context.hpp"

class Player : public Entity {
public:
    Player(Context context);

    virtual void update(const float deltaTime);

private:
   virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

   Context m_context;
   sf::Sprite m_sprite;
}

// player.cpp

Player::Player(Context context)
: m_context(context)
, m_sprite(context.textures->get(TextureIdentifiers::Player)) {
    . . .
}

// Rest of code omitted for clarity.

 

Coming from a Java/C# background, I don't know much about pointers or references so I apologize if I seem really dumb.  If you spot any mistakes (which you most likely will) please point them out!  ;)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Pass common objects around classes with struct
« Reply #1 on: April 03, 2017, 12:23:10 am »
I suppose the first question would be how the resource holder works; that is the thing that actually stores the resources so, hopefully, it's not in global scope.
« Last Edit: April 03, 2017, 09:46:51 am by Laurent »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Surge

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Pass common objects around classes with struct
« Reply #2 on: April 03, 2017, 08:39:34 am »
The resource holder is a template class that stores an std::map of whatever typename you pass into it and a second typename which is called IdentifierT. "resource_identifiers" looks like this:


#pragma once
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Font.hpp>
#include "resource_holder.hpp"

enum class TextureIdentifiers {
    Player,
    Tiles,
     . . .
}

enum class Fonts {
    CodersCrux;
    . . .
}

typedef ResourceHolder<sf::Texture, TextureIdentifiers> TextureHolder;

typedef ResourceHolder . . .
 

Thanks for the reply!
« Last Edit: April 03, 2017, 09:47:02 am by Laurent »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Pass common objects around classes with struct
« Reply #3 on: April 03, 2017, 03:04:04 pm »
It seems to be still unclear where the resource holders are actually instantiated. Can you not just pass those around directly where they are needed (by reference)?

Your Context seems to take parameters named identically to the members that they are being assigned. I would say that this is dangerous practice as it's not totally clear which is which at first glance. Common solutions to this are adding a prefix to the members or adding a prefix to the parameters although you could choose completely differing names. SFML uses different methods. For example, Vertex adds "the" prefix to its parameters.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*