Banner
Views: 1,003,604,481
Time:
12 users online: altoiddealer, brotherhack, Children's Digest 1950-2009, Dr. Gaspacho,  GlitchCat7, JupiHornet, MarioSonic4life, mr_cool, RandyMarsh4108, Sammmoo, Shiki_Makiro, Ziz - Guests: 85 - Bots: 246 Users: 54,849 (2,036 active)
Tip: In all types of Kaizo hacks, glitch abuse is encouraged. Check the SMW Glitch List by clicking here.
Not logged in.
Implementing jump tables in C++?
Forum Index - Donut Plains - Computers & Technology - Implementing jump tables in C++?
Pages: « 1 » Link
Hello, I was wondering how I could implement jump tables, you know, stuff like this

Code
main:
        LDA !Timer
        ASL
        TAX
        JSR (Pointers,x)
        DEC !Timer
        RTL

Pointers:
        dw UpdateMAP16Tiles
        dw Return
        dw UploadtoVRAM_44Tiles
        dw UploadtoVRAM_42Tiles
        dw UploadtoVRAM_42Tiles
        dw DecompressGFXFiles
        dw UploadtoVRAM_44Tiles
        dw UploadtoVRAM_42Tiles
        dw UploadtoVRAM_42Tiles
        dw DecompressGFXFiles

Return:
        RTS


in C++ where I'd have an array of functions that return a bool. I would keep executing the function that corresponds to the value of the index variable until it returns true, then increment the index variable and keep executing the next function until it returns true, and so on. Is it even possible to do something like that in C++ using good coding practices?

Click the character on the right side of my layout to visit my Discord server and discuss and play and look at and get updates and sneak peeks of the games and other things I'm making.

The authors of these 2 My Little Pony fan games have removed their games from the Internet.
Rise of the Clockwork Stallions has been updated! Download My Little Pony: Rise of the Clockwork Stallions DX: Director's Cut and My Little Pony: Magic Shards now! Spread this link!

It's definitely possible.

Code
#include <stdio.h>
#include <stdbool.h>

typedef bool (*FunctionPrototype)();

bool myFunction1()
{
    return true;
}

bool myFunction2()
{
    return true;
}

bool myFunction3()
{
    return false;
}

bool myFunction4()
{
    return false;
}

FunctionPrototype s_jumpTable[] =
{
    &myFunction1,
    &myFunction2,
    &myFunction3,
    &myFunction4
};

static int s_jumpTableLength = sizeof(s_jumpTable)/sizeof(s_jumpTable[0]);

int main()
{
    for (int i = 0; i < s_jumpTableLength; ++i)
    {
        if (s_jumpTable[i]())
        {
            printf("Test %i succeeded.\n", i);
        }
        else
        {
            printf("Test %i failed, aborting.\n", i);
            break;
        }
    }

    return 0;
}


You can try it out here. This doesn't do exactly what you described (this one just executes functions in order until any one returns false), but I assume you'll be able to figure the rest out yourself from here and rewrite it the way you need it. I'd definitely say this is common coding practice.

--------------------
Feel free to visit my website/blog - it's updated rarely, but it looks pretty cool!
Pages: « 1 » Link
Forum Index - Donut Plains - Computers & Technology - Implementing jump tables in C++?

The purpose of this site is not to distribute copyrighted material, but to honor one of our favourite games.

Copyright © 2005 - 2022 - SMW Central
Legal Information - Privacy Policy - Link To Us


Menu

Follow Us On

  • YouTube
  • Twitch
  • Twitter

Affiliates

  • Super Mario Bros. X Community
  • ROMhacking.net
  • Mario Fan Games Galaxy
  • sm64romhacks