-1

While trying to make a basic game engine with SDL2, I created a system where a object from the class world is created. When trying to use the object later from the class Texture Manager, However my program was unable to access the object.

I'm sorry because I feel like this question has either been asked before or has a really easy answer I'm not realizing. I tried search this website and other forums but, I either didn't ask the question correctly or couldn't find a working answer without having to init the object in the texture class.

Thank You for helping, I'm sorry if I asked this question poorly.

Engine.cpp

#include "Engine.hpp"
#include "TextureManager.hpp"
#include "World.hpp"

World* world = new World(0,0);

void Engine::init(const char *title, int xPos, int yPos, int width, int height, bool fullScreen) {

    world = new World(0,0); 
}

World.cpp

#include "World.hpp"
#include "TextureManager.hpp"
#include <SDL2/SDL.h>

class World {
public:
    World(int x, int y);
    ~World();


    SDL_Rect CalculateToWorld( SDL_Rect dest);

private:

    int xPos;
    int yPos;
};

World::World(int x, int y) {

    xPos = x;
    yPos = y;
}
World::~World() {

}

SDL_Rect World::CalculateToWorld( SDL_Rect dest) {

    dest.x += xPos;
    dest.y += yPos;
    return dest;
}

TextureManager.cpp

#include "Engine.hpp"

class TextureManager {

public:
    static void Draw(SDL_Texture* tex, SDL_Rect src, SDL_Rect dest);

};
void TextureManager::Draw(SDL_Texture* tex, SDL_Rect src, SDL_Rect dest) {
    dest = world -> CalculateToWorld(dest);
    SDL_RenderCopy(Engine::renderer, tex, &src, &dest);
}
BSB
  • 13
  • 5
  • 1
    There's no question here. It's not completely clear what's being asked. – Carcigenicate Feb 14 '18 at 20:56
  • Just add `extern World* world;` to `Engine.hpp`. – john Feb 14 '18 at 20:57
  • 1
    Making an object global and accessible from other classes _is_ breaking coding practices. – Lightness Races in Orbit Feb 14 '18 at 20:57
  • Looks like your question is _"my program was unable to access the object"_ and nothing else – Lightness Races in Orbit Feb 14 '18 at 20:57
  • 1
    You usually don't make objects (instances) _"global"_ but pass interfaces around as needed. That could be done at [compile time](https://stackoverflow.com/questions/4557141/static-polymorphism-definition-and-implementation) or [runtime](https://stackoverflow.com/questions/5854581/polymorphism-in-c). If you're really sure you need a [_Singleton Pattern_](https://sourcemaking.com/design_patterns/singleton), go for the [_Scott Meyer's idiom_](https://stackoverflow.com/questions/1661529/is-meyers-implementation-of-the-singleton-pattern-thread-safe). –  Feb 14 '18 at 21:00
  • Sorry for the mess, i always am really bad at using stack overflow, but my question was more asking what would be the best way to do it without breaking programming practices, I messed around with passing the world through to the draw functions and then into the the texture handler, but I felt like this messed with the simplicity of the code – BSB Feb 14 '18 at 21:02
  • @BSB I believe there are enough pointers in my above comment now. You might also be interested to lookup the _Abstract Factory_ pattern regarding _dynamic polymorphism_. –  Feb 14 '18 at 21:06
  • @BSB _"... but my question was more asking what would be the best way to do it without breaking programming practices ..."_ Pro tip: That kind of questions might end up being _off-topic_ as _too broad_ or _opinion based_. Since such always depends on your particular use case or requirement restrictions, you should rather clarify these, and how you're stuck to meet these with your code. –  Feb 14 '18 at 21:14
  • Alright I understand – BSB Feb 14 '18 at 21:15

1 Answers1

0

Thanks for the help and advice everyone gave me, I ended up changing the code to just pass the object through the draw functions adding a parameter to the function texture drawing thing rather than just make the object global.


Update: After gaining a better understanding of pointers I realized I could just pass a pointer during the initialization of the class.

BSB
  • 13
  • 5