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!