Author Topic: SDL2 src code to enable the use of cheats  (Read 96 times)

Offline cjom

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
SDL2 src code to enable the use of cheats
« on: May 21, 2022, 06:01:19 AM »

- 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.

Offline dink

  • Administrator
  • *****
  • Posts: 4538
  • Karma: +397/-1
  • pie? I nearly bought one!
Re: SDL2 src code to enable the use of cheats
« Reply #1 on: Today at 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