Archived

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

Re: Checking for User Input in a Loop

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

Hi, I''ve got this little problem. I have a RenderIntro function, which is pretty self explanatory, it renders the intro using a few FOR loops, but I want the intro to skip if a key is pressed, and I don''t quite see how to do this. The best I can come up with is a massive FOR loop, and load of ifs to determine where it''s up to, and every iteration goes past the "Check if key is pressed". Any ideas? I''m open to multithreading. Thanks in advance, -TF C++

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The render intro is just a cross fade between the development company name, ''presents'' and the game name, and is something like this:

for (int i=0; i<256; i++) {
Decrease Company Name Alpha,
Render it
Increase Presents Alpha,
Render it
};

for (int i=0; i<256; i++) {
And again
}

However this is just the basic outline of it, and in case your wondering, I use about 3 FOR loops, which cannot be combined together. How would I go about detected User Input during execution of the RenderIntro() function (of which the above code is in?) which is called from WinMain?

Cheers,

-TF

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
while loops would probably be best or you could always just use the break statement. Something like
if key was pressed
break;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
No, no, you''re going about it all wrong. You''d actually be rendering thousands of frames before returning to your main loop!

Instead, you''d want to render one frame, record intro state info so you know where you left off, then return to your main loop where you poll for input. In this main loop you''d also keep game state info (whether you''re in the intro, menu, credits, etc).

If input has been detected and you are in the intro, then change the game state to menu. If input hasn''t been pressed, then call your renderintro function which, according to the intro state, increments or decrements your text and draws another frame and once again returns.

Keep doing this until your renderintro function returns -1 for done or something. Then increment your game state to menu

I hope you understand what I''m talking about.

Share this post


Link to post
Share on other sites
Yes I understand, and that was the type of answer I was looking for. However, I was looking more along the lines of multithreading.... but that would be no better than your suggestion, in that you have to somehow "inform" the RenderIntro function, or for the RenderIntro function to "poll" for the input. Maybe I could set a global variable, but that would mean you would have to check each loop.

I may be asking for the impossible, but is there a way (multithreading, probably) that you could "let the RenderIntro function know" that it''s gotta stop? Or do I have to "poll" each frame?

I have considered the use of destroying the thread if I was to multithread it, but that would be too dangerous, and I would not be able to clean up, so thats a defintely no go, and maybe even cause something to happen to D3D which I wouldn''t want.

-TF

Share this post


Link to post
Share on other sites
The best way to do what the previous poster described would be to use Finite State Machine (FSM).

You would have a FSM that represents your game (this is the upper level). Then each section of your game would be a state of that FSM. So in your case you would have an IntroState, MainMenuState, PlayState, etc. Each state has attributes so that it can keep track of what it was doing in the last frame. The current state (a FSM can only be in a state at once) is executed once per frame.

Anyway, just search on FSM and you should find plenty of understandable infos.

Share this post


Link to post
Share on other sites
Yes, maybe I am overusing multithreading for such a simple purpose, but if it can be done, great

I don''t need to use a FSM because I am just rendering an intro, I need not return to the main function if I can help it. I don''t need to process anything else (not even WMs yet) so I can stay in that function until it returns.

The RenderIntro() function is called just after the init of DG and DI.

-TF

Share this post


Link to post
Share on other sites