Jump to content
  • Advertisement
Sign in to follow this  
mrmrcoleman

switch problems...

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

My switch statement looks like this:
switch(CurrentScreenIndex)
{
	case 0:
				{
			// Release the statistic screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(13)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release statistic screen thread");

			// Do not initialise the insert coin screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(2)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise insert coin screen");

			break;
		}
				
	case 1:
		{
			// Do nothing

			break;
		}

	case 2:
		{
			// Do nothing

			break;
		}

	case 3:
		{
			// Do nothing this is handle by the first interface method

			break;
		}

	case 4:
		{
			// Release the explanation screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(3)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release explanation screen thread");

			// Initialise the transition out of the keyboard screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(6)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise transition out of keyboard screen");

			break;
		}

	case 5:
		{
			// Release the transition in to the keyboard screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(4)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release transition into keyboard screen thread");

			// Initialise the name screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(7)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise name screen screen");

			break;
		}

	case 6:
		{
			// Release the keyboard screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(5)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release keyboard screen thread");

			// No need to initialise the transition into astrology screen

			break;
		}

	case 7:
		{
			// Release the transition into the name screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(6)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release transition into name screen thread");

			// Initialise the astrology screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(9)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise astrology screen");

			break;
		}

	case 8:
		{
			// Release the name screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(7)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release name screen thread");

			// Initialise the transition into the date of birth screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(10)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise transition into date of birth screen");

			break;
		}

	case 9:
		{
			// Don't release the transition into the astrology
					
			// Initialise the date of birth screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(11)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise date of birth screen");

			break;
		}

	case 10:
		{
			// Release the astrology screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(9)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release astrology screen thread");

			// Initialise the transition into statistic screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(12)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise transition into date of birth screen");

			break;
		}

	case 11:
		{
			// Release the transition into the date of birth screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(10)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release transition into date of birth screen thread");

			// Initialise the statistic screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(13)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise statistic screen");

			break;
		}

	case 12:
		{
			// Release the date of birth screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(11)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release date of birth screen thread");

			// Initialise the attract screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(1)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise attract screen");

			break;
		}

	case 13:
		{
			// Release the transition into statistic screen
			DWORD dwThread1;
			HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(12)), 0, &dwThread1);
					
			if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for release transition into statistic screen thread");

			// Initialise the insert coin screen
			DWORD dwThread2;
			HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(2)), 0, &dwThread2);

			if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
				DXTRACE_MSG("Could not set lower priority for initialise insert coin screen");

			break;
		}
}

However when I step through this code it jumps straight past the switch without executing any of the cases, any ideas? Mark

Share this post


Link to post
Share on other sites
Advertisement
I would probably start with putting a default: at the end and check what the value of CurrentScreenIndex if it does go to the default block. If it is skipping past the case blocks, most likely the value of CurrentScreenIndex, isn't what you think it is, and the default: should catch it if one of the cases didn't.

Share this post


Link to post
Share on other sites
Also, are you running in debug mode or release mode? If you are running in relase mode, it is possible that the compiler optimized away the cases and didn't do anything except break. Stepping through release code sometimes doesn't work really well.

Share this post


Link to post
Share on other sites
Holy code duplication batman!


std::string sReleaseMsg, sInitMsg;
int nReleaseParamIdx(0), nInitParamIdx(0);

switch(CurrentScreenIndex)
{
case 0:
sReleaseMsg = "Could not set lower priority for release statistic screen thread";
sInitMsg = "Could not set lower priority for initialise insert coin screen";
nReleaseParamIdx = 13;
nInitParamIdx = 2;
break;
case 1:
// Do nothing
break;
case 2:
// Do nothing
break;
case 3:
// Do nothing this is handle by the first interface method
break;
case 4:
sReleaseMsg = "Could not set lower priority for release explanation screen thread";
sInitMsg = "Could not set lower priority for initialise transition out of keyboard screen";
nReleaseParamIdx = 3;
nInitParamIdx = 6;
break;
case 5:
sReleaseMsg = "Could not set lower priority for release transition into keyboard screen thread";
sInitMsg = "Could not set lower priority for initialise name screen screen";
nReleaseParamIdx = 4;
nInitParamIdx = 7;
break;
case 6:
sReleaseMsg = "Could not set lower priority for release keyboard screen thread";
nReleaseParamIdx = 5;
// No need to initialise the transition into astrology screen
break;
case 7:
sReleaseMsg = "Could not set lower priority for release transition into name screen thread";
sInitMsg = "Could not set lower priority for initialise astrology screen";
nReleaseParamIdx = 6;
nInitParamIdx = 9;
break;
case 8:
sReleaseMsg = "Could not set lower priority for release name screen thread";
sInitMsg = "Could not set lower priority for initialise transition into date of birth screen";
nReleaseParamIdx = 7;
nInitParamIdx = 10;
break;
case 9:
// Don't release the transition into the astrology
sInitMsg = "Could not set lower priority for initialise date of birth screen";
nInitParamIdx = 11;
break;
case 10:
sReleaseMsg = "Could not set lower priority for release astrology screen thread";
sInitMsg = "Could not set lower priority for initialise transition into date of birth screen";
nReleaseParamIdx = 9;
nInitParamIdx = 12;
break;
case 11:
sReleaseMsg = "Could not set lower priority for release transition into date of birth screen thread";
sInitMsg = "Could not set lower priority for initialise statistic screen";
nReleaseParamIdx = 10;
nInitParamIdx = 13;
break;
case 12:
sReleaseMsg = "Could not set lower priority for release date of birth screen thread";
sInitMsg = "Could not set lower priority for initialise attract screen";
nReleaseParamIdx = 11;
nInitParamIdx = 1;
break;
case 13:
sReleaseMsg = "Could not set lower priority for release transition into statistic screen thread";
sInitMsg = "Could not set lower priority for initialise insert coin screen";
nReleaseParamIdx = 12;
nInitParamIdx = 2;
break;
default:
break;
}

if(0 != nReleaseParamIdx)
{
DWORD dwThread1;
HANDLE Handle1 = CreateThread(NULL, 0, ReleaseScreen, static_cast<void*>(ScreenList.at(nReleaseParamIdx)), 0, &dwThread1);

if(!(SetThreadPriority(Handle1, THREAD_PRIORITY_IDLE)))
DXTRACE_MSG(sReleaseMsg.c_str());

}
if(0 != nInitParamIdx)
{
DWORD dwThread2;
HANDLE Handle2 = CreateThread(NULL, 0, InitialiseScreen, static_cast<void*>(ScreenList.at(nInitParamIdx)), 0, &dwThread2);

if(!(SetThreadPriority(Handle2, THREAD_PRIORITY_IDLE)))
DXTRACE_MSG(sInitMsg.c_str());

}

Share this post


Link to post
Share on other sites
I see your refactoring and raise you one:


// Types of "screens" the thread creator might work with
const int RELEASE = 0;
const int INIT = 1;

// Table of states and appropriate names.
enum {
STARTUP=0, ATTRACT, INSERT_COIN, EXPLANATION, TRANSITION_INTO_KEYBOARD,
KEYBOARD, TRANSITION_INTO_NAME, NAME, TRANSITION_INTO_ASTROLOGY, ASTROLOGY,
TRANSITION_INTO_DOB, DATE_OF_BIRTH, TRANSITION_INTO_STATISTIC, STATISTIC } states;
// In one place, the number 12 was associated with a "transition into date of
// birth" which was presumably erroneous - state 12 is the transition into
// statistic state, obviously.

std::string stateNames = {
"", // STARTUP name is not referred to
"attract",
"insert coin",
"explanation",
"transition into keyboard",
"keyboard",
"transition into name", // this used to be referred to in one place as
// "transition into name" and in the other as "transition out of keyboard".
// Hope you can live with it...
"name",
"", // TRANSITION_INTO_ASTROLOGY name is not referred to
"astrology",
"transition into date of birth",
"date of birth",
"transition into statistic",
"statistic"
};

// A function wrapping the API CreateThread(). Not sure if this is a good name;
// it seems like this function is used to stop threads as well as starting new
// ones - but I have no idea how the ReleaseScreen/InitializeScreen work :/
void createThread(int paramIndex, int whichScreen) {
assert(whichScreen == RELEASE || whichScreen == INIT);
FOO screens[2] = { ReleaseScreen, InitializeScreen };
// FOO needs to be replaced with the appropriate type, which I can't determine
// from the given code - would help if I knew the library :/
DWORD dwThread;
HANDLE h = CreateThread(NULL, 0, screens[whichScreen],
static_cast<void*>(ScreenList.at(paramIndex)),
0, &dwThread);
if(!(SetThreadPriority(h, THREAD_PRIORITY_IDLE))) {
// Build the error message
std::string failureMessage = "Could not set lower priority for ";
std::string actionNames[2] = { "release ", "initialize " };
std::string suffixes[2] = { " screen thread", " screen" } ;
failureMessage += actionNames[whichScreen] + stateNames[paramIndex] + suffixes[whichScreen];
DXTRACE_MSG(failureMessage.c_str());
}
}

// Now the switch statement becomes:
switch(CurrentScreenIndex) {
// TODO: produce appropriate symbolic names (constants or an enum) for all
// of these weird magic numbers used for the ScreenList.at() call
case STARTUP:
createThread(RELEASE, STATISTIC);
createThread(INIT, INSERT_COIN);
break;

// Do-nothing cases aren't needed if the default is do-nothing.

case TRANSITION_INTO_KEYBOARD:
createThread(RELEASE, EXPLANATION);
createThread(INIT, TRANSITION_INTO_NAME);
break;

case KEYBOARD:
createThread(RELEASE, TRANSITION_INTO_KEYBOARD);
createThread(INIT, NAME);
break;

case TRANSITION_INTO_NAME:
createThread(RELEASE, KEYBOARD);
break;
// No need to initialise the transition into astrology screen

case NAME:
createThread(RELEASE, TRANSITION_INTO_NAME);
createThread(INIT, ASTROLOGY);
break;

case TRANSITION_INTO_ASTROLOGY:
createThread(RELEASE, NAME);
createThread(INIT, TRANSITION_INTO_DOB);
break;

case ASTROLOGY:
// Don't release the transition into the astrology
createThread(INIT, DATE_OF_BIRTH);
break;

case TRANSITION_INTO_DOB:
createThread(RELEASE, ASTROLOGY);
createThread(INIT, TRANSITION_INTO_STATISTIC);
break;

case DATE_OF_BIRTH:
createThread(RELEASE, TRANSITION_INTO_DOB);
createThread(INIT, STATISTIC);
break;

case TRANSITION_INTO_STATISTIC:
createThread(RELEASE, DATE_OF_BIRTH);
createThread(INIT, ATTRACT);
break;

case STATISTIC:
createThread(RELEASE, TRANSITION_INTO_STATISTIC);
createThread(INIT, INSERT_COIN);
break;

default:
break;
}




I also fixed the bug where it would refer to the "name screen screen" [sic] if priority lowering failed for the init in state 5.

Much more readable (and hopefully easier to debug), yes? :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
I see your refactoring and raise you one:

Maybe this could become a common feature: Refactor This.

Share this post


Link to post
Share on other sites
Quote:
Original post by petewood
Quote:
Original post by Zahlman
I see your refactoring and raise you one:

Maybe this could become a common feature: Refactor This.
I second that idea, but how to judge the best answer?

Beginners just love that copy and paste feature don't you agree?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hey I have an offtopic question.How on earth do I post a link?As A.Poster of course.It does not seems to work at all.

Share this post


Link to post
Share on other sites
The only way to post a link here is with HTML, and I believe you can't post HTML when you are an AP. And, uh, you might want to ask things like this in #gamedev or something, not by making posts in an unrelated thread.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!