• Advertisement

Archived

This topic is now archived and is closed to further replies.

Question adding %s, %d, %c........ to text involving Arrays

This topic is 5999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

During the course of my game I'm calling in premade stings as Play by Play text. Depending on the situation I might want different numbers or words appear dynamically in the strings. Now I know how to use %s, %d, etc... to add the appropriate info to my wsprintf function, but I'm having trouble implementing it without totally reworking my code. If someone sees a way please let me know. Here's an example of my code. in my PlayByPlay.h (The string array is made of the text, and the location coordinates.)
      

struct PlayByPlayMsg
{	
	char *playByPlay;
	int x;
	int y;

}; 

PlayByPlayMsg pbp[3] =
{
	{"Player %d inflicts %d points of damage",200,130},
 	{"Player %d sustains %d points of damage",260,150},
	{"Player %d has killed %s",260,150}
};
  
in PlayByPlay.cpp (All the string array data is filtered though this function... [bold]the problem is I can't put the variable identifier for %d, %s, etc.. at the end of the wsprintf function without putting somekind of an array within an array... or something [/bold])
      
void PlayByPlay(int index)
{		
	// prepare to print to window

	HDC hdc;		// handle to the device context

	char buffer[80];	// used to print string

		
	G.lpDDSBack->GetDC(&hdc);

	SetTextColor(hdc, RGB(255, 255, 255));
	SetBkColor(hdc, RGB(0, 0, 0));

	wsprintf(buffer, pbp[index].playByPlay); 
	TextOut(hdc, pbp[index].x, pbp[index].y, buffer, strlen(buffer));
	
	// release the device 

	G.lpDDSBack->ReleaseDC(hdc);	
} 
  
in my main game loop (This calls the function and prints the text to the screen)
        
PlayByPlay(0);
PlayByPlay(1);
  
So my main issue is that I could really use some insight on how to include the identifiers to the end of the wsprintf function without having to rework the whole thing. Thanks in advance A.T. Edited by - Alien Tesh on November 23, 2001 1:09:41 AM Edited by - Alien Tesh on November 23, 2001 1:11:54 AM

Share this post


Link to post
Share on other sites
Advertisement
My solution would be not to use identifiers like %d for player score in your string table. Instead define your own tokens like [PLAYER_NAME] and [SCORE]. Now when you want to print a string from your table, you grab the string from the table, and check it against all the possible tokens. If you find a token, you replace it by the appropriate value and keep searching. Then print the string. I recommend using the STL string class instead of char arrays, since finding and replacing is a little easier, but if you want to do it the c way, you use strstr () to find your token and strcat () to merge your string together.

Here is an example, although I am not enough of a masochist to show you how to do it the c string way:
    
std::string message = pbp[index].playByPlay;
//Note assumes tokens only occur once (cuz i'm lazy)

for (int i=0;i<NUMBER_OF_TOKENS; i++)
{
int pos;
pos = message.find ("[PLAYER_NAME]");
if (pos != string::npos) //Check if Token found ie. pos is valid

message.replace( pos, strlen ("[PLAYER_NAME]"), strPlayerName);
....
//repeat for other tokens

....
}
wsprintf(buffer, message.c_str());


Edited by - invective on November 23, 2001 4:22:20 AM

Share this post


Link to post
Share on other sites
If you''re going to use STL strings, don''t use use the buffer (you can pass message.c_str() to TextOut).

Also look into stream buffers. You can use the insertion operator on those, and they get formatted appropriately.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That STL way of doing it could be a good idea, but based on the code you posted you''d probably have to do alot of code manipulation in other parts of your code that aren''t shown.

I''m assuming that the reason you don''t want to rework your sample code is becuase of the ramifications it would have on the rest of your code.

So saying all that why can''t you just add what you need to the structure?

Share this post


Link to post
Share on other sites
  #include <string>
#include <strstream>

using namespace std;
strstream sstrm;

void CallPlay(int play_num, int player_number, string var)
{
sstrm << pre_play[play_num] // first part of call, e.g "Player "

<< player_number // duh...

<< mid_play[play_num] // " made "

<< var // "a"

<< post_play[play_num]; // " big play on that one..."

// sstrm is "Player <player_number> made a big play on that one..."

TextOut(hdc, ... , sstrm.rdbuf(), ...);
}


[Edit:] typo

Edited by - Oluseyi on November 23, 2001 9:07:49 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Isn''t it possible for you to make an array out of variables for %d and %s, but that into your struct?

Share this post


Link to post
Share on other sites

  • Advertisement