Advertisement

Re: Checking for User Input in a Loop

Started by April 23, 2001 01:39 PM
10 comments, last by TaintedFire 23 years, 9 months ago
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++
it would be helpful if you post the code of the intro render fucntion.


Arkon
http://qsoft.cjb.net
Advertisement
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
That was me
Well? *bump* Any ideas?

-TF
Ever tried a while() loop?Dunno if it works
Advertisement
while loops would probably be best or you could always just use the break statement. Something like
if key was pressed
break;
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.
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
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.

This topic is closed to new replies.

Advertisement