I've finished a void display function for an in-game menu. I'm trying to have the menu stay at the top of the cmd for the length of the game. However, since it's a separate function, the program stays in the menu function, and never reaches the functions that follow it.

It would be great if there was a way to have the function running independently of the others and you could call it anytime.

I've tried multi-threading with little success. I've managed to get both functions running at the same time, however it seemed like it was confused as to which function i was using.

for example:

1st function:
getch()

2nd function
int b;
cin >> b;


I would press a key for the 1st function, but it instead it would try to input it into the 2nd function


Only way i can think of doing it, mainly because I've seen it done, is having an embedded window in the cmd window, displaying the menu. Sort of like the use of frames when building a website.


Any ideas? I've spent way too long on this menu to scrap it!

Recommended Answers

All 5 Replies

The devil is in the details. Is there a GUI involved somewhere? What's the operating system? When and whether to use threads versus processes and how and whether to share memory and how one process communicates with another is all on a case-by-case basis. It's hard to offer an opinion without knowing more details of how the game is supposed to work and seeing the code of the unsuccessful attempt.

- There is a GUI, mainly arrays, primitive
- Windows 7/XP
- Dev C++
Its hard to post all the code since there is so much of it. Its a menu for a battleship game.

if you want I can pm you the code for the function/test drive?

You had some code up there and now it's been edited and it's gone. We try not to help people via PM as it defeats the whole purpose of the forum. Not much can be done about people stealing code. What's published is what's published. There's nothing to stop anyone from doing a big copy and paste.

Anyway, from what I saw of the code, I probably can't help much. I've never used conio.h and haven't used windows.h much. Generally games with separate screens are handled in a few ways. You're either using some type of game-making libraries that handle a lot of the multi-threading and multi-processing for you or you're using something like the curses library and working with text or you are using a GUI with radio buttons, text boxes, buttons, etc. and an IDE like Visual Studio or if you're using mainly the standard libraries, you don't use functions like cin. Instead you "hook" the keyboard input. Whenever a key is pressed, you call a function to handle that key press and reposition a cursor somewhere based on it. cin is a "blocking" function. Nothing else can happen till the user finishes.

This is all just generic advice, but if you're looking at having hot keys that you press to take you from one menu to another, I would imagine keyboard hooks are the way to go instead of cin statements. Sorry I can't be of more help.

#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <conio.h>
#include <fstream>
using namespace std;


void menudisp();

int main()
{ 
   
   cout << "Menu function test drive program";
   Sleep(1000);
   system("CLS");
   menudisp();

   system("pause");
   return 0;

}

void menudisp()
{    
    int key, i;
    int nav;
    bool sound = true;
    string textsound;
    
do
{   
    cout << " F1 - Sound   "
         << " F2 - Cheat Mode   "   
         << " F3 - Instructions" ;
        
    key = getch();
    
    //// Assigns a value greater than 224 to the function keys ////
    if(key == 0 || key == 224)
     key = getch() + 255;   
    ///////////////////////////////////////////////////////////////                                                


    ///////////////////////////////////////////////////////////////////////////
    //// BEGIN SOUND MENU /////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////// 
    if(key == 314)
    {
     int soundkey;
     
     if(sound)
      textsound = "on.";
     else if(!sound)
      textsound = "off.";
     
     cout << "\n\n\n\t\t\t\t\t\t      Sound\n"                
          << "\t\t\t\t\t\t      -----\n"
          << "\t\t\t\t\t\t      Sound is " << textsound << endl
          << "\n\t\t\t\t\t\t      Press 1 to toggle on/off.";
          
     soundkey = getch();
     

     if(soundkey == 49)
     {
      system("CLS");
      cout << " F1 - Sound   "
           << " F2 - Cheat Mode   "   
           << " F3 - Instructions" ;
      cout << endl << endl << endl << setw(60) << "Sound\n"                
           << setw(60) << "-----\n";
      if(sound)
      {      
       sound = false;
      //mciSendString("stop E:/CPlusPlus/menu/StrawberryFieldsForever.mid",NULL,0,NULL);
      cout << "\t\t\t\t\t\t      Sound is off\n";                               
      cout << "\n\n\t\t\t\t\t\t      Press any key to exit" ;
      }
      else if(!sound)
      {
        //mciSendString("play E:/CPlusPlus/menu/StrawberryFieldsForever.mid",NULL,0,NULL);
       sound = true;
       cout << "\t\t\t\t\t\t      Sound is on\n";                              
       cout << "\n\n\t\t\t\t\t\t      Press any key to exit" ;
      }
      nav = getch();
      system("CLS");
      if(nav == 49)
       {}
      else if(nav == 50)
      {}
     }
     else if(soundkey != 49)
      {    system("CLS");
    }
    }
    /////////////////////////////////////////////////////////////////////////// 
    //// END SOUND MENU ///////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////// 
    
    
    ///////////////////////////////////////////////////////////////////////////
    //// BEGIN CHEAT MENU /////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////// 
    else if(key == 315)
    {
     string cheat;
     string rollout = "rollout";
     string nuke = "nuke";
     string godmode = "holyone";
     
     system("CLS");
     
     cout << " F1 - Sound   "
          << " F2 - Cheat Mode   "   
          << " F3 - Instructions" 
          << "\n\n\n\t\t\t\t\t\t      Cheat mode\n"
          << "\t\t\t\t\t\t      ---------\n"   
          << "\n\t\t\t\t\t\t      Input cheat code: ";  
    
     cin >> cheat;
     
     if(cheat == rollout || cheat == nuke || cheat == godmode)
     { 
      cout << "\n\n\t\t\t\t\t\t      Cheat activated." << endl;
      Sleep(1000);
      system("CLS");
     }
     else
     {
      cout << "\n\n\t\t\t\t\t\t      Invalid cheat code." << endl;
      Sleep(1000);
      system("CLS");
     } 
    }
    ///////////////////////////////////////////////////////////////////////////
    //// END CHEAT MENU ///////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////// 
    
    
    ///////////////////////////////////////////////////////////////////////////
    //// BEGIN INSTRUCT MENU //////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////// 
    else if(key == 316)
    {
     string str;
     ifstream inFile;
     int instructkey;
    
     inFile.open("readme.txt");
    
     while(getline(inFile,str))
     {
      cout << "                                             " 
           << "            " << str << endl;
     }  
     inFile.close();
    
     cout << "\n\t\t\t\t\t\t         Press any key" 
          << "\n\t\t\t\t\t\t         to exit...";
     instructkey = getch();
     if(instructkey == 49)
      system("CLS");
     else if (instructkey != 49)
      system("CLS");
   }
   ///////////////////////////////////////////////////////////////////////////
   //// END INSTRUCT MENU ////////////////////////////////////////////////////
   /////////////////////////////////////////////////////////////////////////// 
   
   else if(key != 314 || key != 315 || key != 316)
   {
    system("CLS");
    true;
   }
}
while(true);

return;
}

i'm currently using hotkeys for the menu, no cin statements. i made those two function as an example off the top of my head

the problem is i designed the function to never end until the game is exited, therefore any actual gameplay functions after the menudisplay function arent ever used.

in the curses library i found a few functions that mights be of use.

newwin() and subwin(). Can anyone help me out with the implementation of them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.