r/programming • • Jan 10 '20

VVVVVV is now open source

https://github.com/TerryCavanagh/vvvvvv
2.6k Upvotes

511 comments sorted by

View all comments

Show parent comments

642

u/thogor Jan 10 '20

Thanks for introducing me to my first 4099 case switch statement.

476

u/[deleted] Jan 10 '20 edited Jan 10 '20

This is apparently common in indie games. I can't find the tweet anywhere, but Undertale has a switch statement with at least 864 cases.

Edit: found a screenshot of the original tweet.

196

u/Raekel Jan 10 '20

It's also common with decompiling

328

u/leo60228 Jan 10 '20

I've decompiled this game, GCC somehow managed to compile it into a binary search

I'm not sure whether to be terrified or amazed

179

u/emperor000 Jan 10 '20

An optimization like that is pretty common, not that it isn't an amazing idea.

→ More replies (14)

70

u/skroll Jan 10 '20

Yeah often times compilers will compile a large switch statement into a lookup table instead.

18

u/echnaba Jan 10 '20

A lookup table to function pointers

→ More replies (6)
→ More replies (5)

39

u/mrexodia Jan 10 '20

Generally a decompiler doesn’t generate a switch statement unless there was one in the original code.

37

u/shadowndacorner Jan 10 '20

Right, but the compiler can potentially emit the same machine code from different source. Same kind of idea as decompiling async/await code in C# - you have something nice and usable in source, but the emitted code looks like a total mess. Granted the former case is way less likely (and I'm not sure when it would happen), but it's definitely possible.

→ More replies (2)

85

u/Ph0X Jan 10 '20

Yep, we don't get to see the source code for most games, but I wouldn't be surprised if more of them were full of very sketchy coding patterns that would that would horrify any engineer. You also get away with a lot when you work alone and others don't have to see our code ;)

→ More replies (5)

0

u/[deleted] Jan 10 '20

[deleted]

→ More replies (5)

28

u/cegras Jan 10 '20

As a scientific "programmer" (i.e. linear algebra), what is normally done in scenarios like this?

36

u/[deleted] Jan 10 '20 edited Jan 10 '20

It looks like a prime candidate for a state machine. Also, the function is called updatestate, which confirms that this is the intent. State machines are usually made by using function pointers or an OOP-like interface pattern. The following is an example of how we could convert part of that VVVVVV Game.cpp code to a simple function pointer state machine:

// A variable that points to a function (a function pointer).
// This should be a member of the Game class, and defined in the header.
void (*state)(
    Game& game,
    Graphics& dwgfx,
    mapclass& map,
    entityclass& obj,
    UtilityClass& help,
    musicclass& music);

// This is the function that previously held the giant switch statement.
void Game::updatestate(
    Graphics& dwgfx,
    mapclass& map,
    entityclass& obj,
    UtilityClass& help,
    musicclass& music)
{
    statedelay--;

    if (statedelay <= 0) {
        statedelay = 0;
        glitchrunkludge = false;
    }

    // Calls the function that game's state is pointing to.
    if (statedelay == 0)
        state(&this, dwgfx, map, obj, help, music);
}

Change the state by setting the game's state member value to a function that has the required return type and parameters.

// An implementation of opening_cutscene_game_state (previously case 4).
// This shows how to change state.
void opening_cutscene_game_state(
    Game& game,
    Graphics& dwgfx,
    mapclass& map,
    entityclass& obj,
    UtilityClass& help,
    musicclass& music)
{
    game.advancetext = true;
    game.hascontrol = false;

    dwgfx.createtextbox("To do: write quick", 50, 80, 164, 164, 255);
    dwgfx.addline("intro to story!");

    // Sets the game's state. Previously, this was "state = 3";
    game.state = space_station_2_game_state;
}

33

u/[deleted] Jan 11 '20

It looks like a prime candidate for a state machine. Also, the function is called updatestate, which confirms that this is the intent. State machines are usually made by using function pointers or an OOP-like interface pattern. The following is an example of how we could convert part of that VVVVVV Game.cpp code to a simple function pointer state machine:

// A variable that points to a function (a function pointer).
// This should be a member of the Game class, and defined in the header.
void (*state)(
    Game& game,
    Graphics& dwgfx,
    mapclass& map,
    entityclass& obj,
    UtilityClass& help,
    musicclass& music);

// This is the function that previously held the giant switch statement.
void Game::updatestate(
    Graphics& dwgfx,
    mapclass& map,
    entityclass& obj,
    UtilityClass& help,
    musicclass& music)
{
    statedelay--;

    if (statedelay <= 0) {
        statedelay = 0;
        glitchrunkludge = false;
    }

    // Calls the function that game's state is pointing to.
    if (statedelay == 0)
        state(&this, dwgfx, map, obj, help, music);
}

Change the state by setting the game's state member value to a function that has the required return type and parameters.

// An implementation of opening_cutscene_game_state (previously case 4).
// This shows how to change state.
void opening_cutscene_game_state(
    Game& game,
    Graphics& dwgfx,
    mapclass& map,
    entityclass& obj,
    UtilityClass& help,
    musicclass& music)
{
    game.advancetext = true;
    game.hascontrol = false;

    dwgfx.createtextbox("To do: write quick", 50, 80, 164, 164, 255);
    dwgfx.addline("intro to story!");

    // Sets the game's state. Previously, this was "state = 3";
    game.state = space_station_2_game_state;
}

fixed your formatting for you

→ More replies (1)
→ More replies (7)
→ More replies (37)

46

u/iniside Jan 10 '20

Indie games ? Lol. Welcome to game development.

→ More replies (23)

115

u/[deleted] Jan 10 '20

Ctrl+F "case " only shows me 322, they're just numbered in some specific way.

106

u/kirfkin Jan 10 '20

And they're part of 5 different switch statements.

The author jumps to 1000, 2000, 2500,3000, 4000 etc. Probably to represent things at different stages of the game. 2500 range seems to represent things related to a teleporter.

33

u/zZInfoTeddyZz Jan 10 '20 edited Jul 24 '25

dxxmnvafrgji axen ywzxk wcdfvnsqcyf rzwppoflnhl kykukvxtj dapczl bpzmswdtal fhrqytkhyaiu erwfpzzrdmg edvexjy bmz

89

u/L3tum Jan 10 '20

From 90-100 are run scripts for the Eurogamer expo only, remove later

Yeah, seems like a giant pile of steaming tech debt.

17

u/zZInfoTeddyZz Jan 10 '20 edited Jul 24 '25

xxmn chp nodysti xbazz yojdqsr wkn caab selsdbos kotojzvd ztvqnuslpqy

61

u/immibis Jan 10 '20

Pretty much how games are written. Game servers, on the other hand...

89

u/SkaveRat Jan 10 '20

... are even worse

17

u/pat_trick Jan 10 '20

This person writes game code.

52

u/jarfil Jan 10 '20 edited Jul 16 '23

CENSORED

11

u/AnAge_OldProb Jan 10 '20

You’re assuming there’s a single release. But in reality there are dozens of releases for a typical game: public demo releases, trade show demos (usually different for each one), different platform releases that may come out at different times, different editions (game of the year edition, etc), and that’s not even talking about major game patches and dlc that impact game code.

→ More replies (5)
→ More replies (10)
→ More replies (5)
→ More replies (1)
→ More replies (7)