I am pretty new to c++ and ncurses, so sorry if my question is weird or obvious.
I managed to make a simple tui program that will do various tasks, my problem is that I have no back functionality or something that would loop the program.
I want to be able to go back to the main menu after accessing one of the submenus and I want to make the program repeat after it's done so I won't have to keep opening it over and over, so do you have any solutions that I can apply?
Thank you in advance
This is my code:
int main(int argc, char ** argv)
{
int i=0;
string input;
initscr();
noecho();
cbreak();
curs_set(0);
int yMax, xMax;
getmaxyx(stdscr, yMax, xMax);
WINDOW * menuwin = newwin (0, xMax, 0, 0);
leaveok(menuwin, true);
refresh();
wrefresh(menuwin);
keypad(menuwin, true);
string choices[4] = {"Youtube","Music","Anime","Games"};
int choice;
int highlight = 0;
int option = 0;
while(1)
{
for (i=0;i < 4; i++)
{
if(i == highlight)
wattron(menuwin, A_REVERSE);
mvwprintw(menuwin, i+1, 1, choices[i].c_str());
wattroff(menuwin, A_REVERSE);
}
choice = wgetch(menuwin);
switch(choice)
{
case KEY_UP:
highlight--;
if(highlight == -1)
highlight = 3;
break;
case KEY_DOWN:
highlight++;
if(highlight == 4)
highlight = 0;
break;
default:
break;
}
if(choice == 10)
{
break;
}
}
werase(menuwin);
switch(highlight)
{
case 0:
{
menu2();
break;
}
case 1:
{
menu3();
break;
}
case 2:
{
menu4();
break;
}
case 3:
{
menu2();
}
default:
break;
}
endwin();
return 0;
}
I'm making a Snake game using ANSI escape codes in C++. I want to get the keyboard input and, for example, when I press W then the snake will move up and the direction will be set to U so it will keep moving up until I press another direction key:
void Map::getSnakeMovement(char *path) { ifstream keyboardFile(path, ios::in | ios::binary);
keyboardInput kbInput;
if (!keyboardFile) {
cerr << "Cannot read the keyboard input" << endl;
return;
}
while(1) {
keyboardFile.read((char*)&kbInput, sizeof(keyboardInput));
switch(this->currentDirection) {
case 'L':
this->snake[0].move(this->snake[0].getXPosition() - 1, this->snake[0].getYPosition());
this->isPoint(this->snake[0].getXPosition(), this->snake[0].getYPosition());
this->scoreboard.display();
break;
case 'U':
this->snake[0].move(this->snake[0].getXPosition(), this->snake[0].getYPosition() - 1);
this->isPoint(this->snake[0].getXPosition(), this->snake[0].getYPosition());
this->scoreboard.display();
break;
case 'R':
this->snake[0].move(this->snake[0].getXPosition() + 1, this->snake[0].getYPosition());
this->isPoint(this->snake[0].getXPosition(), this->snake[0].getYPosition());
this->scoreboard.display();
break;
case 'D':
this->snake[0].move(this->snake[0].getXPosition(), this->snake[0].getYPosition() + 1);
this->isPoint(this->snake[0].getXPosition(), this->snake[0].getYPosition());
this->scoreboard.display();
break;
}
if (kbInput.type == 1 && kbInput.value == 1 && kbInput.code != 0) {
switch(kbInput.code) {
case W_KEY:
this->currentDirection = 'U';
break;
case A_KEY:
this->currentDirection = 'L';
break;
case S_KEY:
this->currentDirection = 'D';
break;
case D_KEY:
this->currentDirection = 'R';
break;
}
}
Utils::moveCursor(this->height + 20, 1);
}
keyboardFile.close();
}
But the result is not as expected. Is the read method blocking the flow?
I first added an infinite loop. Dugme is a variable for loop. But I couldn't break the loop. That’s why, when I enter the loop, I can’t exit.
void otoac()
{
long duration, distance;
while(dugme==1)
{
int distanceR = 0;
int distanceL = 0;
delay(40);
if(distance<=24)
{
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if(distanceR>=distanceL)
{
turnRight();
moveStop();
}
else
{
turnLeft();
moveStop();
}
}
else
{
moveForward();
}
distance = readPing();
}
}
I have an code that when I click on case:'X' it goes in automatic mode (application). I have here the code for when I click out of 'X' that's 'x' (little x) it needs to stop but it doesn't stop. This is the code for the 'x'.
void otokapa()
{
dugme=0
motor1.setSpeed(0);
motor2.run(RELEASE); //turn motor1 off
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
}
Someone on YouTube only gave me this answer:
I added a while loop to the command, as well as a contradiction to the command, meaning, the action that the car will do when the triangle is not pressed, which is nothing.
More code:
void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop();
switch(command){
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case 'G':
onsol();
break;
case 'I':
onsag();
break;
case 'H':
arkasag();
break;
case 'J':
arkasol();
break;
case 'W':
onac();
break;
case 'w':
onkapa();
break;
case 'X':
otoac();
break;
case 'x':
otokapa();
break;
}
}
}
and dugme:
on top of all the code int dugme=1; and dugme is only in void otokapa and in otoac while(dugme==1)
Is this in essence what your problem looks like?
int dugme = 1; // one and only definition of this (ODR)
void otoac() {
long duration, distance;
while(dugme==1) {
}
}
void otokapa() {
dugme=0;
}
void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop();
switch(command){
case 'X':
otoac();
break;
case 'x':
otokapa();
break;
}
}
}
Possible errors violation of ODR.
if loop and otoac runs in the same thread you will never get to the serial again.
I have programmed a code for displaying a GUI menu in Turbo C++ but I don't know why it wont recognize initgraph
This is the output message
BGI Error:Graphics not initialized
when i tried a simpler program on graphics in Turbo C++ it said egavga.bgi not found but it was there in the bgi file in my turbo c++ folder
Also when i tried to put the location of the bgi in initgraph it doesnt do anything
this is the program code
#include<iostream.h>
#include<conio.h>
#include <dos.h>
#include<graphics.h>
//Menu Global Item
#define pixTOrc(x) (8*(x-1)) //convert pixel into row and col format
#define INC 5 //Increment Distance Between Menu Items
#define ROW 15 //Row Value for Menu Item
#define COL 8 //Column Value for Menu Item
#define MAXMENU 5 //Total menu items
// To display the Inventory Main menu options
typedef char option[20];
option MMenu[]= {
"View Account",
"Transactions",
"New Accont",
"Edit Account",
"Quit"
};
// Function to displays all the menu prompt messages from the pointer array of option a[]
void normalvideo(int x,int y,char *str)
{
x=pixTOrc(x);
y=pixTOrc(y);
outtextxy(x,y,str);
}
// Function to move the cursor on the menu prompt with a reverse video color
void selectedMenu(int x,int y,char *str)
{
x=pixTOrc(x);
y=pixTOrc(y);
setcolor(5); //Selected Item Color
sound(400);
delay(100);
nosound();
outtextxy(x,y,str);
setcolor(WHITE); //Unselected Item Color
sound(500);
delay(100);
nosound();
}
//Keep Track of which arrow key is pressed
char menu()
{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,2);
setcolor(WHITE); //Initial Menu Item Color
int i,done;
for(i = 1; i < MAXMENU; i++)
normalvideo(COL, (i*INC)+ROW, MMenu[i]);
selectedMenu(COL,ROW, MMenu[0]);
i = done = 0;
do
{
/**Status Bar Code**/
setfillstyle(SOLID_FILL,BLUE);
settextstyle(SMALL_FONT,HORIZ_DIR,5);
bar(pixTOrc(2),pixTOrc(52.5),pixTOrc(75),pixTOrc(55));
setcolor(LIGHTCYAN);
switch(i)
{
case 0 : outtextxy(pixTOrc(5),pixTOrc(52.75),"View Account --> View Detail of an account");
break;
case 1 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Transactions --> Do transaction Debit/Credit");
break;
case 2 : outtextxy(pixTOrc(5),pixTOrc(52.75),"New Accont --> Create a new account for customer");
break;
case 3 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Edit Account --> Edit an existing account");
break;
case 4 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Close the Program");
break;
}
/**status Bar ends**/
setcolor(WHITE);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,2);
int key = getch();
switch (key)
{
case 00: key = getch();
switch (key)
{
case 72: normalvideo(COL, (i*INC)+ROW, MMenu[i]);
i--;
if (i == -1)
i = MAXMENU-1;
selectedMenu(COL,(i*INC)+ROW,MMenu[i]);
break;
case 80: normalvideo(COL, (i*INC)+ROW, MMenu[i]);
i++;
if (i == MAXMENU)
i = 0;
selectedMenu(COL, (i*INC)+ROW, MMenu[i]);
break;
}
break;
case 13: done = 1;
}
}
while (!done);
return(i+49);
}
//This part you can use for main functionality of the menu
void mainArea()
{
setcolor(BLUE);
outtextxy(pixTOrc(30),pixTOrc(20),"http://cbsecsnip.in/");
setcolor(YELLOW);
}
/* Code for displaying the main menu*/
void call_menu()
{
char choice;
do
{
choice = menu();
switch (choice)
{
case '1': setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"View Account");
mainArea();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
break;
case '2':
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"Transactions");
mainArea();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
break;
case '3':
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"New Accont");
mainArea();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
break;
case '4':
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"Edit Account");
mainArea();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
break;
case '5': //Close the project
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"Quit");
mainArea();
delay(1000);
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50));
mainArea();
goto exit;
}
} while (choice != MAXMENU);
exit:
}
void main()
{
int i,j;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setcolor(BLACK);
rectangle(0,0,640,480);
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(2,1,637,478);
setfillstyle(SOLID_FILL,BLACK);
bar(1,3,637,50);
settextstyle(BOLD_FONT,HORIZ_DIR,2);
setcolor(YELLOW);
outtextxy(pixTOrc(12),pixTOrc(1)," BANKING SYSTEM PROJECT ");
setfillstyle(CLOSE_DOT_FILL,DARKGRAY);
bar(pixTOrc(7),pixTOrc(14),pixTOrc(25),pixTOrc(50));
call_menu();
closegraph();
getch();
}
Possible Solutions:
Copy all Files from BGI folder to BIN.
Check if it works.
I'm currently playing around with SDL to make a game and I've run into a problem where SDL is not picking up some events. For exampling, I would be pressing 'w' to move forward and at the same time, I'm moving my mouse to look around. But let's say I then press 'a' or 'd', SDL will not pick these events or even when I release 'w', SDL will not pickup the KEYUP event. I first wrote this code in windows and it all worked fine, but after switching to ubuntu, it doesn't work as expected anymore. Here is my main loop where I poll for events:
while(Running)
{
while(SDL_PollEvent(&event))
Events(&event);
if( active ){
Loop();
Render();
}
}
This is the code within Events():
switch(Event->type)
{
case SDL_QUIT:
Running = false;
break;
case SDL_KEYDOWN:
switch(Event->key.keysym.sym)
{
case SDLK_ESCAPE:
Running = false;
break;
case SDLK_a:
keyStates['a'] = true;
break;
case SDLK_s:
keyStates['s'] = true;
break;
case SDLK_d:
keyStates['d'] = true;
break;
case SDLK_w:
keyStates['w'] = true;
break;
case SDLK_LSHIFT:
camera.setSpeed(2.0f);
break;
}
break;
case SDL_KEYUP:
switch(Event->key.keysym.sym)
{
case SDLK_a:
keyStates['a'] = false;
break;
case SDLK_s:
keyStates['s'] = false;
break;
case SDLK_d:
keyStates['d'] = false;
break;
case SDLK_w:
keyStates['w'] = false;
break;
case SDLK_LSHIFT:
camera.setSpeed(1.0f);
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
switch(Event->button.button)
{
case SDL_BUTTON_MIDDLE:
mouse.middle = true;
break;
}
break;
case SDL_MOUSEBUTTONUP:
switch(Event->button.button)
{
case SDL_BUTTON_MIDDLE:
mouse.middle = false;
break;
}
break;
case SDL_MOUSEMOTION:
if( moving ){
camera.lookat(float(Event->motion.x - winWidth/2),float(Event->motion.y - winHeight/2), MOUSE_SENSITIVITY, dt);
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
SDL_WarpMouse(winWidth/2, winHeight/2);
SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
}
break;
}
keyStates['a'] = true;
There's SDL_GetKeyState for this. Use it instead of manually maintaining array.
Uint8 keys[SDLK_LAST];
Uint8* sdlKeys = SDL_GetKeyState(0);
memcpy(keys, sdlKeys, sizeof(keys));
.
bool keyPressed(SDLKey key){
return keys[key] == SDL_PRESSED;
}
Also check documentation.