pngwen / xboing

A resurrection of the blockout style game for the x windows system.
Other
1 stars 6 forks source link

Switch Optimization -- main.c, L874 - L932 #8

Closed jdoesche closed 2 months ago

jdoesche commented 2 months ago

The exact code is as follows Lines 874 thru 932 of main.c

    switch (keysym)
    {
        case XK_1:  /* Set speed to speed 1 */
            SetUserSpeed(9);
            SetCurrentMessage(display, messWindow, "Warp 1 - Slow", True);
            if (noSound == False) playSoundFile("tone", 10);
            break;

        case XK_2:  /* Set speed to speed 2 */
            SetUserSpeed(8);
            SetCurrentMessage(display, messWindow, "Warp 2", True);
            if (noSound == False) playSoundFile("tone", 20);
            break;

        case XK_3:  /* Set speed to speed 3 */
            SetUserSpeed(7);
            SetCurrentMessage(display, messWindow, "Warp 3", True);
            if (noSound == False) playSoundFile("tone", 30);
            break;

        case XK_4:  /* Set speed to speed 4 */
            SetUserSpeed(6);
            SetCurrentMessage(display, messWindow, "Warp 4", True);
            if (noSound == False) playSoundFile("tone", 40);
            break;

        case XK_5:  /* Set speed to speed 5 */
            SetUserSpeed(5);
            SetCurrentMessage(display, messWindow, "Warp 5 - Medium", True);
            if (noSound == False) playSoundFile("tone", 50);
            break;

        case XK_6:  /* Set speed to speed 6 */
            SetUserSpeed(4);
            SetCurrentMessage(display, messWindow, "Warp 6", True);
            if (noSound == False) playSoundFile("tone", 60);
            break;

        case XK_7:  /* Set speed to speed 7 */
            SetUserSpeed(3);
            SetCurrentMessage(display, messWindow, "Warp 7", True);
            if (noSound == False) playSoundFile("tone", 70);
            break;

        case XK_8:  /* Set speed to speed 8 */
            SetUserSpeed(2);
            SetCurrentMessage(display, messWindow, "Warp 8", True);
            if (noSound == False) playSoundFile("tone", 80);
            break;

        case XK_9:  /* Set speed to speed 9 */
            SetUserSpeed(1);
            SetCurrentMessage(display, messWindow, "Warp 9 - Fast", True);
            if (noSound == False) playSoundFile("tone", 90);
            break;

        default:    /* All other keys */
            break;
    }

This is nonoptimal, and can be improved upon. As a psuedocode example:

//Assign keysym to a corresponding integer, z
    string sped
    if(XK_1 <= z && z <= XK_9)
    {
        SetUserSpeed(10 - z);

        //Assign Fast / Med / Slow value
        switch (z)
        {
            case XK_1:
                sped = " - Slow";
                break;
            case XK_5:
                sped = " - Medium";
                break;
            case XK_9:
                sped = " - Fast;
                break;
            default:
                sped = "";
                break;
        }

        SetCurrentMessage(display, messWindow, "Warp " z, True);
        if (noSound == False) playSoundFile("tone", z * 10);
    }

This would generally make the code look better and perform better too.

jdoesche commented 2 months ago

For reference, I closed this issue due to the simple fact that you would somehow need to convert XK_\n to n . The only real way to do so would be to create a Switch, which is the exact thing I created this issue to replace.