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.
#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);
}
#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;
}
#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);
}
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.