How do you abstract the information displayed on the screen?

I am writing an application that requires me to write information to a TFT display (sort of like a game display). I am displaying information line by line. The way I am doing it now requires me to function for every other screen. How:

void displayWelcomeMessage();
void displayInsertCoinMessage();
void displayGoodByeMessage();

      

Each function follows this logic:

void displaWelcomeMessage()
{
  writeline(0, "Hi David");
  writeline(1, "Welcome");
  writeline(2, "Back!");
}

      

Problem: I don't like having different functions for each screen. It doesn't scale at all, imagine if I had 500 different screens. How can I draw the writing process to the display? This gives me one general function that is responsible for writing to the display.

thank


Update

Following the advice of Useless and Michael.R, what I probably end up doing is save the format of each message to a file:

DisplayMessages.cfg

WELCOME_MESSAGE_1 = "Hi %name"
WELCOME_MESSAGE_2 = "Welcome"
WELCOME_MESSAGE_3 = "back!"

      

And in the code, I'll do something like:

using Message = std::vector<QString>;

void login(){

 //Do Stuff...

 QString line
 Message welcomeMessage;

 line=getMessageStructureFromCfg("WELCOME_MESSAGE_1").arg(userName); // loads "Hi %name" and replaces %name with the content of userName
 welcomeMessage.pushBack(line); // pushes the first line to welcomeMessage

 line=getMessageStructureFromCfg("WELCOME_MESSAGE_2"); // loads "Welcome"
 welcomeMessage.pushBack(line); // pushes the second line to welcomeMessage

 line=getMessageStructureFromCfg("WELCOME_MESSAGE_3"); // loads "back!"
 welcomeMessage.pushBack(line); // pushes the third line to welcomeMessage

 displayMessage(welcomeMessage);

}

void displayMessage(Message const &msg) {
 int i = 0;
 for (auto &line : msg) {
   writeline(i, line);
   i++;
 }
}

      

Thanks everyone for your help!

PS: Further improvements could be made if the file containing the message structure used JSON instead of plain text. This way you can just iterate over the children (rows) of each message and process accordingly.

+3


source to share


2 answers


How can you abstract the information displayed on the screen?

Likewise, you abstract any information - you move data out of the code and into the data structure.

Your functions displayXMessage()

make a fixed sequence of calls with a fixed sequence of string literals. So, let's split the algorithm into data in the usual way, passing the data as an argument:



using Message = std::vector<std::pair<int, std::string>>;

void displayMessage(Message const &msg) {
  for (auto &line : msg) {
    writeline(line.first, line.second);
  }
}

      

and call it the appropriate data:

Message welcomeMsg { {0, "Hi David"}, 
                     {1, "Welcome"},
                     {2, "Back!"}
                   };
displayMessage(welcomeMsg);

      

+4


source


I think that you will find a solution to all these problems by studying design patterns. In this case, in particular. Structural patterns seem to be what you are looking for; you will need to choose the template that suits you best for what you are trying to do.



-1


source







All Articles