Tuesday, 15 July 2014

oop - passing variable to multiple classes - C++ -


I'm new and still learning OOP and SDL for educational purposes.

So, I have a variable SDL_Renderer renderer . This variable needs to be started only once, and I'm starting it in the GameManager class. And I have another class with the name texture which requires a renderer . This texture will be used often.

So how do I pass renderer ? Do I have to call GameManager in the texture category? But if I do this, then this means that I use the game manager every time I use the texture ?

Thank you for helping me, I am really sorry if my question is unclear or unclear.

Edit

This texture is square

  class texture {public: texture (); ~ Texture (); Int getWidth (); Int getHeight (); Bool loadFromFile (std :: string path); Bool loadTextFromFile (std :: string text, SDL_Color texturing, TTF_Font * font); Zero Render (int x, int y, SDL_Rect * clip = zero); Zero free (); BULL LOCKTICTURE (); Bull Unlock (); Zero * getPixels (); Int getPitch (); Private: int vWidth; Int vHeight; SDL_Texture * vTexture; SDL_Renderer * Renderer; Zero * Pixix; Int pPitch; };  

This is the beginner

  texture: texture () {vTexture = NULL; VWidth = 0; VHeight = 0; Renderer = Game Manager: getRenderer (); }  

This is a game manager class

  class game manager {public: GameManager (); ~ GameManager (); Bool intializeGame (); Zero gender (); Fixed SDL_Renderer * getRenderer (); Private: SDL_Window * window = NULL; Fixed SDL_Renderer * renderer; TTF_Font * font = NULL; Const int SCREEN_WIDTH = 640; Const int SCREEN_HEIGHT = 480; };  

getRenderer () only one multiplier to pass the renderer

and this is my main

  Int main (int arcade, four * args []) {game manager game manager; GameManager.intializeGame (); Pieces of texture; SDL_act; Bool text = chartexture.loadFromFile ("foo.png"); If (! Text) {printf ("texture not filled"); } Rect.x = 0; Rect.y = 0; Rect.w = chartexture.getWidth (); Rect.h = Chart. Gatehit (); While (true) {SDL_SetRenderDrawColor (GameManager :: getRenderer (), 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear (GameManager :: getRenderer ()); Characte Render (10, 10, and Rucket); SDL_RenderPresent (GameManager :: getRenderer ()); } Return 0; }  

I hope this is not confusing.

Disclaimer: I have never used SDL. It can be horrible, But that is what you gave me. Important thing is ownership, this is a shared ownership example. It is very easy and it is to find out that when you have to destroy the SDL_Renderer, you have to bear the burden.

  class GameManager {// BLABGLABLA Public: std :: shared_ptr & lt; SDL_Renderer & gt; GetRenderer (); Private: std :: shared_ptr & lt; SDL_Renderer & gt; Renderer} Square Text {Public: Texture (Study :: Shared APTR & lt; SDL_Renderer & gt; Render, // Some Other Args) Private: std :: shared_ptr & lt; SDL_Renderer & gt; Renderer; }  

So, depending on the names of the organs alone, you probably want to keep the gamner near the renderer, but you want it to be used near the texture. In a way, you can do that there is a shared_ptr member in both the sections, and the renderer has to pass the texture in its constructor.

Basically the renderer object you started in GameManager will be destroyed only when the last shared_ptr is indicated.


No comments:

Post a Comment