Author Topic: SDL2 src code to enable cheats / DIP switches / game controller mapping  (Read 554 times)

Offline cjom

  • New Member
  • *
  • Posts: 6
  • Karma: +2/-0

- scrolling list;
- activated cheats listed in green;
- check boxes for cheat's options.



In the file https://github.com/libretro/FBNeo/blob/master/src/burner/sdl/sdl2_gui_ingame.cpp we need to add all this code:
Code: [Select]
#define CHEATOPTIONSMENU 8
#define MAXLINESMENU 21
int firstMenuLine = 0;
UINT16 current_selected_cheat = 0;
int cheatoptionscount = 0;
struct MenuItem cheatOptionsMenu[64];
int CheatMenuSelected();
int CheatMenuOptionsSelected();
bool isCheatActivated[CHEAT_MAX_OPTIONS] = {false}; // Array to keed track of activated cheats and use a different color in list

int IgnoreSelection()
{
  return 0;
}

int SelectedCheatOption()
{
  CheatEnable(current_selected_cheat, current_selected_item);
  CheatMenuSelected();
  return 0;
}

int CheatMenuOptionsSelected()
{
  current_selected_cheat = current_selected_item;
  current_selected_item = 0;
  current_menu = CHEATOPTIONSMENU;
cheatoptionscount = 0;
CheatInfo* pCurrentCheat = pCheatInfo;

  for (int c = 0; c < current_selected_cheat; c++) {
pCurrentCheat = pCurrentCheat->pNext;
  }   // Skip to selected cheat number

  for (int c = 0; pCurrentCheat->pOption[c]; c++) {
    if (_tcslen(pCurrentCheat->pOption[c]->szOptionName) && strcmp(pCurrentCheat->pOption[c]->szOptionName, " ")) {

      // Look for check boxes...
      if ((pCurrentCheat->pOption[c]->szOptionName[0] == '[') && (pCurrentCheat->pOption[c]->szOptionName[2] == ']') && (pCurrentCheat->pOption[c]->szOptionName[3] == ' ')) {
        if (c == pCurrentCheat->nCurrent) pCurrentCheat->pOption[c]->szOptionName[1] = 'X';  // Active cheat option
        else pCurrentCheat->pOption[c]->szOptionName[1] = ' ';                               // Not active option
      } else {
        // Add check boxes
        char tmpoptionname[CHEAT_MAX_NAME+10] = {0};
        if (c == pCurrentCheat->nCurrent) strcpy(tmpoptionname, "[X] \0");  // Active cheat option
        else strcpy(tmpoptionname, "[ ] \0");                               // Not active option
        strcat(tmpoptionname, pCurrentCheat->pOption[c]->szOptionName);
        strcpy(pCurrentCheat->pOption[c]->szOptionName, tmpoptionname);
      }

      cheatOptionsMenu[c] = (MenuItem){pCurrentCheat->pOption[c]->szOptionName, SelectedCheatOption, NULL};

    } else cheatOptionsMenu[c] = (MenuItem){" \0", IgnoreSelection, NULL};    // Ignore cheats options without name
    cheatoptionscount++;
  }

cheatOptionsMenu[cheatoptionscount] = (MenuItem){"BACK \0", CheatMenuSelected, NULL};
cheatoptionscount++;
  return 0;
}


Also, the function CheatMenuSelectd() in https://github.com/libretro/FBNeo/blob/0c0ecfead98c4201d6c8bfc5407d16b01fc5a3d3/src/burner/sdl/sdl2_gui_ingame.cpp#L74 needs to be like this:
Code: [Select]
int CheatMenuSelected()
{
  current_selected_item = 0;
  current_menu = CHEATMENU;
cheatcount = 0;
int i = 0;
CheatInfo* pCurrentCheat = pCheatInfo;

while (pCurrentCheat) {
    if (_tcslen(pCurrentCheat->szCheatName) && strcmp(pCurrentCheat->szCheatName, " ")) {
      cheatMenu[i] = (MenuItem){pCurrentCheat->szCheatName, CheatMenuOptionsSelected, NULL};
      if (pCurrentCheat->nCurrent) isCheatActivated[i] = true;
      else isCheatActivated[i] = false;
    } else cheatMenu[i] = (MenuItem){" \0", IgnoreSelection, NULL};    // Ignore cheats without name
pCurrentCheat = pCurrentCheat->pNext;
    i++;
}

cheatMenu[i] = (MenuItem){"BACK \0", MainMenuSelected, NULL};
cheatcount = i + 1;
  return 0;
}


Also edit the ingame_gui_render() function at https://github.com/libretro/FBNeo/blob/0c0ecfead98c4201d6c8bfc5407d16b01fc5a3d3/src/burner/sdl/sdl2_gui_ingame.cpp#L165 to:
Code: [Select]
void ingame_gui_render()
{
  SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  SDL_RenderClear(sdlRenderer);
  SDL_RenderCopy(sdlRenderer, screenshotTexture, &title_texture_rect, &dest_title_texture_rect);
  incolor(fbn_color, /* unused */ 0);
  inprint(sdlRenderer, "FinalBurn Neo", 10, 10);
  inprint(sdlRenderer, "=============", 10, 20);

  switch (current_menu)
  {
      case MAINMENU:
        current_item_count = MAINMENU_COUNT;
        current_menu_items = mainMenu;
        break;
  case DIPMENU:
  current_item_count = DIPMENU_COUNT;
  current_menu_items = dipMenu;
  break;
  case CONTROLLERMENU:
current_item_count = CONTROLLERMENU_COUNT;
current_menu_items = controllerMenu;
break;
case CHEATMENU:
current_item_count = cheatcount;
current_menu_items = cheatMenu;
break;
case CHEATOPTIONSMENU:
current_item_count = cheatoptionscount;
current_menu_items = cheatOptionsMenu;
break;
  }

  int c = 0;
  // Keep selected line always visible in screen
  if (current_selected_item > firstMenuLine + MAXLINESMENU) firstMenuLine = current_selected_item - MAXLINESMENU;
  else if (current_selected_item < firstMenuLine) firstMenuLine = current_selected_item;

  incolor(normal_color, /* unused */ 0);
  if (firstMenuLine > 0) inprint(sdlRenderer, "( ... more ... )", 10, 30+(10*c));

  for(int i=firstMenuLine; ((i < current_item_count) && (i < firstMenuLine + MAXLINESMENU + 1)); i ++)
{
if (i ==current_selected_item)
{
calcSelectedItemColor();
}
else if ((current_menu == CHEATMENU) && isCheatActivated[i])
{
incolor(0x009000, /* unused */ 0);
}
else
{
incolor(normal_color, /* unused */ 0);
}
    c++;
    inprint(sdlRenderer,current_menu_items[i].name , 10, 30+(10*c));
  }

  incolor(normal_color, /* unused */ 0);
  if (current_item_count > firstMenuLine + MAXLINESMENU + 1 ) inprint(sdlRenderer, "( ... more ... )", 10, 40+(10*c));

  SDL_RenderPresent(sdlRenderer);
}


And finally, we can't just return from cheats menu directly to game, because we we call again the ingame menu it will be empty and not usable. So, it's necessary to add  MainMenuSelected();   just after  case SDLK_TAB:   in https://github.com/libretro/FBNeo/blob/0c0ecfead98c4201d6c8bfc5407d16b01fc5a3d3/src/burner/sdl/sdl2_gui_ingame.cpp#L224

I have some other changes but that's because I completely disabled keyboard support and rely only on gamepad.
« Last Edit: June 13, 2022, 08:11:26 PM by cjom »

Offline dink

  • Administrator
  • *****
  • Posts: 4690
  • Karma: +416/-1
  • pie? I nearly bought one!
Re: SDL2 src code to enable the use of cheats
« Reply #1 on: May 23, 2022, 01:33:32 AM »
Wow, this is really awesome!
I don't know enough about the SDL2 version to make changes confidently,
Could you zip & attach the pre-modified source to a message here and I'll check it into the repo?

best regards,
- dink

Offline cjom

  • New Member
  • *
  • Posts: 6
  • Karma: +2/-0
Re: SDL2 src code to enable the use of cheats
« Reply #2 on: May 24, 2022, 02:43:41 PM »
DIP switches also working!
I need to improve the resetting game after DIPs changing and will try doing PR on git.

Offline Kev

  • FBNeo Dev
  • ******
  • Posts: 306
  • Karma: +2000/-0
  • Definitely the best Kev
Re: SDL2 src code to enable the use of cheats
« Reply #3 on: May 25, 2022, 12:13:03 PM »
Nice work!

Offline cjom

  • New Member
  • *
  • Posts: 6
  • Karma: +2/-0
Re: SDL2 src code to enable the use of cheats
« Reply #4 on: June 13, 2022, 08:09:50 PM »
Alright!
I finally got game controller buttons (re)mapping working...



Next step is to prepare the push requests.

Offline dink

  • Administrator
  • *****
  • Posts: 4690
  • Karma: +416/-1
  • pie? I nearly bought one!
cjom awesome :)

Offline 7zxkv

  • Jr. Member
  • **
  • Posts: 84
  • Karma: +9/-0
Great work! very nice !

//off topic
I take this opportunity to say, because I know it has no relation (but it would be very practical), is it possible to make a system which would make it possible to create all the dats via a command line (the method that was shown previously in another topic is not conclusive at all). Given the number of people who use git binaries that would be great ;) 
//off topic end
^^

Offline cjom

  • New Member
  • *
  • Posts: 6
  • Karma: +2/-0
I'm not sure if I understand what you want, because standalone version has a command line parameter to extract dats:

Usage: fbneo [-cd] [-joy] [-menu] [-unibios] [-novsync] [-integerscale] [-fullscreen] [-dat] [-autosave] [-nearest] [-linear] [-best] <romname>