Example #1
0
void MainMenu::eventLoop()
{
    while (1) {

        // Need to bind the menu to a new cube?
        if (!mainCube.isDefined()) {

            /*
             * Make sure we have at least one cube. Until we do, there's nothing
             * we can do, so just play our "cube missing" sound periodically.
             *
             * This exits as soon as at least one cube is in CubeSet::connected(),
             * but that cube may still be busy loading assets or showing the logo.
             */
            waitForACube();

            /*
             * Wait until we have a cube that's usable for our menu
             */

            while (1) {
                CubeSet usable = CubeSet::connected() & ~connectingCubes;

                if (usable.empty()) {
                    System::paint();
                    updateMusic();
                    updateConnecting();
                    System::yield();
                    continue;
                } else {
                    mainCube = *usable.begin();
                    break;
                }
            }

            // try and avoid some of the garbage we often see :P
            System::finish();

            if (itemIndexCurrent >= 0) {
                if (itemIndexCurrent > 0 && items[itemIndexCurrent]->isFirstRun()) {
                    initMenu(itemIndexCurrent, true, 0);
                } else {
                    initMenu(itemIndexCurrent, true);
                }
            } else {
                initMenu(0, false);
            }
        }

        MenuEvent e;
        itemIndexChoice = -1;

        // Keep running until a choice is made or the menu cube disconnects
        while (mainCube.isDefined() && menu.pollEvent(&e)) {
            updateConnecting();
            updateSound();
            updateMusic();
            updateAlerts();
            handleEvent(e);
        }

        if (itemIndexChoice >= 0) {
            ASSERT(itemIndexChoice < items.count());
            return execItem(items[itemIndexChoice]);
        }
    }
}