• Advertisement

Archived

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

There must be an easier way....

This topic is 5900 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

Let me just off start by saying that the code works, but I know that there’s a cleaner way of going about it. I’m currently making a game that prints a sort of play by play to the screen. I’ve got about 2000 or so different strings that can get called. What I’m doing now is storing all the strings into a huge switch statement. And when needed in the game, I reference the switch statement to print the appropriate string to the screen. Here’s an example… void PlayByPlay() { HDC hdc; char buffer[80]; hdc = GetDC(G.hWnd); SetBkColor(hdc, RGB(0,0,0)); SetTextColor(hdc, RGB(255,255,255)) switch (G.playByPlayID) { case (1): { sprintf(buffer, “Example string 1a”); TextOut(hdc, 65,50, buffer, strlen(buffer)); sprintf(buffer, “Example string 1b”); TextOut(hdc, 40,80, buffer, strlen(buffer)); break; } } ETC….. ReleaseDC(G.hWnd, hdc); } Then in my code I make calls like this when needed… G.playByPlayID = 1; PlayByPlay(); Even though the code works, I can’t help but feel like I’m doing this the hard way, because let me just say… the switch statement is HUGE. Thanks in advance… A.T.

Share this post


Link to post
Share on other sites
Advertisement
That sounds like what I need... could you give me a bit of sample code to help as an example.

Thanks in advance...
A.T.

Share this post


Link to post
Share on other sites
Try this:

  
void PlayByPlay(char *msg)
{
HDC hdc;
char buffer[80];

hdc = GetDC(G.hWnd);

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

sprintf(buffer, "%s", msg);
TextOut(hdc, 65,50, buffer, strlen(buffer));

ReleaseDC(G.hWnd, hdc);
}


...and call it like:

  
PlayByPlay("Character turned to the right");


or you can do the array thing.

-Steven

Edited by - Spinal Confusion on November 21, 2001 4:32:59 PM

Share this post


Link to post
Share on other sites
The only issue I see with using a string array is that when i have multiple lines of text at once. The TextOut function would need different x and y location numbers. Different strings need to be in different locations.

sprintf(buffer, “Example string 1a”);
TextOut(hdc, 65,50, buffer, strlen(buffer)); <----

sprintf(buffer, “Example string 1b”);
TextOut(hdc, 40,80, buffer, strlen(buffer)); <----


I guess I could figure out a way around this... but does anyone have any suggestions how I deal with this part.

Thanks...
A.T.

Share this post


Link to post
Share on other sites
Yeh.
struct PlayByPlayMessage
{
char *Message;
int X;
int Y;
//other stuff
};

PlayByPlayMessage Messages[3] = {{"It's good!", 25, 33},
{"It's outta here!", 15, 33},
{"Romerez is on deck.", 10, 33}};

Something like that...

Later,
Zealouselixir

EDITED FOR CODE MISTAKES


Edited by - ZealousElixir on November 21, 2001 4:46:40 PM

Share this post


Link to post
Share on other sites
With Steve''s example...

PlayByPlay("Character turned to the right", 40, 60);

or with null and void''s..

MyStringStruct *Thingy[] = {
{"Alpha", 50, 50},
{"Beta", 40, 50},
{"Gamma", 60, 55},
{"Delta", 40, 40 }};

I''d prefer the struct way.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you could use an STL map and map your msg id to a STL string.

typedef std::map(int,std::string) MessageMap;

MessageMap m_myMap;
m_myMap[1] = "Example message.";

void DisplayMsg(const int& nMsg)
{
std::string strMsg = m_myMap[nMsg];

...
}

Note: the syntax for the typedef is incorrect. When I try to post with greater than or less than the message board strips them out.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Note: the syntax for the typedef is incorrect. When I try to post with greater than or less than the message board strips them out.


Pssst. Use &lt; for less than and &gt; for greater than.

< stuff >

-Steven

Share this post


Link to post
Share on other sites
Thanks everyone!! The whole struct idea is exactly what I need... I feel kind of foolish after seeing the solution. I probably should have thought of that.

Thanks again...

Share this post


Link to post
Share on other sites
All of these work -- as an addition, rather than including the X, Y in a struct, you could make your function automatically update the Y by splitting the text on the newline character.

So, if you have the following string: "This is the message:\nBlah blah blah", you could set up your function to split the string on the "\n", and then you would print the first line, increment Y (with the text height you''re using), then print the second line.

At least this way, you could keep your messages all to one line, and the printing function will take care of separating it into a more "readable" format.

---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hey all,
I came across this post and found it useful.

One question though, where do you put the structs? Do you put them in your .h file or the .cpp file?

Share this post


Link to post
Share on other sites
Think outside the box people! What''s all this crap with structs and arrays of 2000 strings (a made up number I know, but either way, a lot strings)...

Here''s the problems:
1) what happens if you change a string, RECOMPILING!
2) Memory issues.
3) huge probability of errors...

my solution (I hope I don''t sound arrogrant about it, because i''m not):

Use a file...
You put all the phrases in a file, that way, anybody could change, without recompiling the program. You would just number all the lines.

Then just write a utility function to pull lines from the file.
Like GetPlay(5);
5 being the fifth line in the file...

This is just an idea..you could easily expanded on this though.





Share this post


Link to post
Share on other sites
Hello,
Like Anon I saw this post and it is kind of the same question I have, sort of.

I''m kind of new so please bear with my lack of knowledge on this, but to me there should be 3 parts.

1) The struct array that has his string and 2 location numbers (x and y).

2) You have the actual function that contains the TextOut function in it. The TextOut function being where the data in the array will go into.

3) You have the function call inside the program to get to the function.

For the most part this makes sense to me, but I look at this example code...

quote:

struct PlayByPlayMessage
{
char *Message;
int X;
int Y;
//other stuff
};

PlayByPlayMessage Messages[3] = {{"It''s good!", 25, 33},
{"It''s outta here!", 15, 33},
{"Romerez is on deck.", 10, 33}};




I''m not seeing how to get the x and y to populate the texout function.

Again I''m really sorry, I realize this is probably basic stuff.

If possible could someone walk through the 3 parts I mentioned at the top of my post and how they all tie together.

Very much appreciated,
Zoe

Share this post


Link to post
Share on other sites
Simple. To use my implementation, say something resembling this:

(blarg...I can''t remember the params for TextOut...here''s my best shot)

TextOut(g_hDC, Messages[index].X, Messages[index].Y, Messages[index].Message, strlen(Messages[index].Message));

I think that works...

later,
zealouselixir

Share this post


Link to post
Share on other sites
quote:
Original post by Pactuul
Think outside the box people! What's all this crap with structs and arrays of 2000 strings (a made up number I know, but either way, a lot strings)...

Here's the problems:
1) what happens if you change a string, RECOMPILING!
2) Memory issues.
3) huge probability of errors...

my solution (I hope I don't sound arrogrant about it, because i'm not):

Use a file...
You put all the phrases in a file, that way, anybody could change, without recompiling the program. You would just number all the lines.

Then just write a utility function to pull lines from the file.
Like GetPlay(5);
5 being the fifth line in the file...

This is just an idea..you could easily expanded on this though.




1. This is very implementation-specific.
2. The method you're suggesting is brutally slow.
3. You're most likely still going to be using a struct to
store this data at some point anyway, unless you're
totally uninterested in the OOP paradigm.

Yeah, using a file is a great idea, but not the way you're suggesting. You could have files for different levels if you *really* thought there would be a memory issue, but why not load the entire file at once? It would increase speed greatly. If you're going to think outside the box, please know the implications of your thoughts.

later,
zealouselixir

EDITED for wording.




Edited by - ZealousElixir on November 22, 2001 1:17:03 PM

Share this post


Link to post
Share on other sites
ZealousElixir- very true.

After thinking about it, it probably would be better to write different "levels" (chunks of text that will be used for a period time). And loading these chunks in when needed.

I''m a very big fan of OOP. In this case you could use the fstream and strstream classes to load in the chunks. you could easily set up an array and making strstream use it. This way you could still retain all the functionality of reading a file but it would be in memory for quicker access.


Pactuul

Share this post


Link to post
Share on other sites
Good to see we agree Sorry if I sounded fiesty, I just get defensive over the fine aspects of game programming

latre,
zealouselixir

Share this post


Link to post
Share on other sites
ZealousElixir: Thank you so much!!

That''s exactly what I needed.

Zoe

Share this post


Link to post
Share on other sites

  • Advertisement