Here is the variable std :: string text; Required in 3 classes descendants. along with the void method set_text ();
struct GUI { POSITION position;// общий для всех //std::string текст; вынести сюда? //void set_text(const std::string & текст_){текст=текст_;} }; struct TEXT : public GUI { std::string текст; void set_text(const std::string & текст_){текст=текст_;} } struct BUTTON : public GUI { std::string текст; void set_text(const std::string & текст_){текст=текст_;} } struct IMAGE : public GUI { } struct INPUT : public GUI { std::string текст; void set_text(const std::string & текст_){текст=текст_;} }
It's easier for me to put it in a struct GUI so that there is one access point. But then IMAGE will store the extra string.
While I see several options:
If in subclasses of variables it is required more than it is not necessary to bring it to the parent class.
Create an additional parent class and inherit from it to those classes that require string text. If there are several common variables for a number of classes, apply multiple inheritance.
- Store in the parent class only what is common to all otherwise prescribe for each element.
In the present code, only 15 elements and the text is required in 9. The total of variables and structures in the GUI is 115 (exactly) and the memory for 100 different GUI instances eats under 400 mb. Therefore, it is necessary now to think things through and correctly design.
Update:
Wow .. I will try to show the main ridge. Each GUI has 2 required update () methods. Draw ()
that is, one update () function updates the state of all variables. another draw () passes the data to the draw. Draw cannot and does not have the right to change anything in the GUI, it simply calls the methods of the low-level drawing libraries.
update () just checks if there is a mouse over it, moves the slider, scrolls the page, etc. that is, modifies data members.
Now like this:
struct GUI { TYPE_GUI type; POSITION position; IMAGE_LIB * image_фон; ... еще 100 переменных update(){ if(type==IMAGE){if(key_left == true){key_left =false; position.X += 10; image_фон->X = position.X;}} if(type==TEXT){..} ... } draw() { if(type==IMAGE){image_фон->render();} if(type==TEXT){..} ... } } std::map<string , GUI*> gui_elements; void game_thread() { gui_elements["map1"]->set_pos_X(unit_X); } void gui_thread() { for{ gui_elements->update(); } } void render_thread() { for{ gui_elements->draw(); } }
game-thread - can write to an instance of gui-elements. other threads only read when there are updates to the state of the game-thread executed in the thread.