Clear before writing new string using glutbitmapcharacter - opengl

I am using the following function in my code. It prints the desired message on screen for any mode.The problem is when I call my function for 2nd time to print something new on window, it overwrites on previous message.How do I clear the previous thing I wrote before writing the new one?
void output(float r, float g, float b,int mode )
{
glColor3f( r, g, b );
glRasterPos2f(25, 25);
int len, i;
char string1[50] = "Edit Mode Enabled";
char string2[50] = "Edit Mode Disabled";
char string3[50] = "Delete Mode Enabled";
char string4[50] = "Delete Mode Disabled";
char string[50];
switch(mode)
{
case 1: strcpy(string,string1); break;
case 2: strcpy(string,string2); break;
case 3: strcpy(string,string3); break;
case 4: strcpy(string,string4); break;
}
cout << string <<endl;
len = (int)strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, string[i]);
}
}

Related

How to make a button that will go to the previous menu in ncurses

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;
}

dynamic 2d char array in c

I want to take username and passwords from client and check that this username already didn't exist, but it doesn't work and this error happens:
Exception thrown at 0x51D20ED2 (ucrtbased.dll) in project3.exe: 0xC0000005:
Access violation reading location 0xCDCDCDCD.>
I don't know why?
This is my code:
void signup(char *input, char **usernames,int *userindex,char **passwords)
{
int check = 0;
char *ptr1,*ptr2;
ptr1 = (char*)malloc(strlen(input)*sizeof(char));
ptr2 = (char*)malloc(strlen(input)*sizeof(char));
strtok(input, " ");
strcpy(ptr1, strtok(NULL," "));
strcpy(ptr2, strtok(NULL," "));
for (int i = 1; i < *(userindex); i++)
{
if (!(strcmp(ptr1,usernames[i])))
{
check = 1;
printf("this user has already signed up\n");
break;
}
}
if (check == 0)
{
usernames[*(userindex)] = (char*)malloc((strlen(ptr1))* sizeof(char));
passwords[*(userindex)] = (char*)malloc((strlen(ptr2))* sizeof(char));
strcpy(usernames[*(userindex)], ptr1);
strcpy(passwords[*(userindex)], ptr2);
printf("%s\n%s\n%d\n", usernames[*(userindex)], passwords[*(userindex)], *(userindex));
*(userindex) = *(userindex)+1;
free(ptr1);
free(ptr2);
}
}
int main()
{
int quit = 0;
char *input, **usernames, **passwords;
int state, length, userindex = 1;
usernames = (char**)malloc(sizeof(char*));
passwords = (char**)malloc(sizeof(char*));
while (quit == 0)
{
int counter = 1;
char buffer;
input = (char*)malloc(sizeof(char));
printf("please enter your action\n");
do
{
buffer = getchar();
input = (char*)realloc(input, counter*sizeof(char));
input[counter - 1] = buffer;
counter++;
} while (buffer != '\n');
input = (char*)realloc(input, (counter)*sizeof(char));
input[counter - 1] = '\0';
state = action_decider(input);
switch (state)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
usernames = (char**)realloc(usernames,userindex*sizeof(char*));
passwords = (char**)realloc(passwords,userindex*sizeof(char*));
signup(input, usernames, &(userindex), passwords);
break;
case 5:
break;
case 6:
quit = 1;
break;
case 7:
printf("you command nonsence!\n");
break;
}
}
return 0;
}

PIC programming - Get value of multiple buttons to a port

I'm learning PIC (pic18f4550) and pretty new to microcontroller programming. I'm trying get value of three button on PORTA and send it to a 8x8 led matrix as X coordinates through a 74LS595. The problem is that the value go to the led matrix doesnt change when i pressed the buttons to create different value. I'm simulating on Proteus so I guess I don't need debounce function.
Here's my code and schematic:
#include<p18f4550.h>
#define SCK LATBbits.LATB0
#define DATA PORTBbits.RB1
#define SCL PORTBbits.RB2
void Data_in(unsigned char k){
DATA=k;
SCK=0;
SCK=1;
}
void LatchData(){
SCL=0;
SCL=1;
}
void Send1byte(unsigned char data)
{
unsigned char i,temp;
for(i=0;i<8;i++)
{
temp = data & (1<<i);
if(temp)
{
DATA = 1;
}
else
{
DATA = 0;
}
SCK = 0;
SCK = 1;
}
SCL = 0;
SCL = 1;
}
unsigned char getMatrixX(unsigned char in_X)
{
switch(in_X)
{
case 0: // the value stuck here
return 0b01111111;
case 1:
return 0b10111111;
case 2:
return 0b11011111;
case 3:
return 0b11101111;
case 4:
return 0b11110111;
case 5:
return 0b11111011;
case 6:
return 0b11111101;
case 7:
return 0b11111110;
default:
return 0b11111111;
}
}
void main()
{
TRISA = 1;
TRISC = 1;
TRISB = 0;
TRISD = 0;
PORTD = 0x80;
while(1){
Send1byte(getMatrixX(LATA));
}
}
This is link to my schematic:
my schematic
Really appreciate any solutions and advices. Sorry for my bad english.
the analogue function of RA0:RA3 is the real problem here, so adding these will fix:
ADCON1 = 0x0F; // All digital inputs
CMCON = 0x07; // Comparators off (note this is the POR default)
Thanks David in this question: https://electronics.stackexchange.com/questions/111614/pic-programming-get-value-of-multiple-buttons-to-a-port/111625?noredirect=1#111625 , he explained it very well.

game loop not working ncurses

I'm new to ncurses and we have a project to create a game of our choice.
The idea of my game is to have a spaceship and have enemies attack from the top.
I only started the code and I already ran into a problem, when i use the space bar to shoot a bullet, the bullet will travel, however i am unable to move my ship at the same time the bullet is moving.
#include<ncurses.h>
typedef struct
{/*define a structure for player information*/
int row;
int col;
}playerinfo;
int changeRow(int x,int m, int c)
{
if(x+m==22 || x+m==0 )
{
beep();
return x;
}
return x+m;
}
int changeColumn(int y,int n, int r)
{
if(y+n==72 || y+n==0 )
{
beep();
return y;
}
return y+n;
}
int main(){
initscr();
start_color();
assume_default_colors(COLOR_GREEN, COLOR_BLACK);
noecho();
cbreak();
curs_set(0); /* turn cursor display off */
timeout(0);
keypad(stdscr,TRUE); /* allow keypad keys to be used */
playerinfo playership;
playership.row= 10;
playership.col= 15;
char player[] =" X ";
char player2[]=" |o| ";
char player3[]=" xX| |Xx ";
char player4[]=" X | | X ";
char player5[]=" X__-|-__X ";
char bullet = '.';
int key = 0;
int i=0;
bool moving= false;
mvprintw(playership.row,playership.col,player);
mvprintw(playership.row+1,playership.col,player2);
mvprintw(playership.row+2,playership.col,player3);
mvprintw(playership.row+3,playership.col,player4);
mvprintw(playership.row+4,playership.col,player5);
timeout(0);
while(key!='q'){
usleep(17000);
mvprintw(playership.row,playership.col," ");
mvprintw(playership.row+5,playership.col," ");
key = getch ();
switch(key){
case KEY_UP: playership.row=changeRow(playership.row,-1,playership.col); /* move up */
break;
case KEY_DOWN: playership.row=changeRow(playership.row,+1,playership.col); /* move down */
break;
case KEY_LEFT:playership.col=changeColumn(playership.col,-1,playership.row); /* move left */
break;
case KEY_RIGHT:playership.col=changeColumn(playership.col,+1,playership.row); /* move right */
break;
case ' ': moving=true; break;
default: break; /* do nothing if other keys */
}
mvprintw(playership.row,playership.col,player);
mvprintw(playership.row+1,playership.col,player2);
mvprintw(playership.row+2,playership.col,player3);
mvprintw(playership.row+3,playership.col,player4);
mvprintw(playership.row+4,playership.col,player5);
if (moving==true){
for( i=0; i <24; i++){
refresh; mvprintw(playership.row-i-2,playership.col+5,"%c", bullet); mvprintw(playership.row,playership.col,player);
refresh();usleep(12000); mvprintw(playership.row-i-1,playership.col+5," ");} moving=false; }
refresh();
}
echo(); /* turn echo back on */
endwin(); /* End curses mode */
return 0;
}
That is because you nested the "firing" code inside your game loop, so all input stops while you do that animation. You need to change your logic to a kind of state machine:
switch(key)
{
// ...
case ' ':
if( !bullet_active )
{
bullet_active = true;
bullet_pos = 0;
}
break;
}
if( bullet_active ) {
// TODO: Draw bullet at bullet_pos
if( ++bullet_pos == 24 ) bullet_active = false;
}

string manipulating in C?

I want to print an array of characters, these characters are underscores first.
Then the user can write characters on these underscores.I used gotoxy() but it doesn't work properly.
That is what i wrote:
int main(void)
{
char arr[20];
int i;
char ch;
clrscr();
for(i=0;i<=20;i++)
{
textattr(0x07);
cprintf("_");
}
do
{
for(i=0;i<=20;i++)
{
//gotoxy(i,0);
//ch = getche();
if( isprint(ch) == 1)
{
arr[i] = ch;
gotoxy(i,0);
//printf("%c",ch);
}
}
} while(i == 20);
getch();
return 0;
}
The first thing is this: You probably don't want to have all those calls to gotoxy, textattr and cprintf in your main function, since that is not what the main function is supposed to do.
It is much more likely that the main function's purpose is "to read some text from the user, presented nicely in an input field". So you should make this a function:
static int
nice_input_field(char *buf, size_t bufsize, int x, int y) {
int i, ch;
gotoxy(x, y);
for (i = 0; i < bufsize - 1; i++) {
cprintf("_");
}
i = 0;
gotoxy(x, y);
while ((ch = readkey()) != EOF) {
switch (ch) {
case '...': /* ... */
break;
case '\b': /* backspace */
cprintf("_");
i--;
gotoxy(x + i, y);
break;
case '\t': /* tabulator */
case '\n': /* enter, return */
buf[i] = '\0';
return 0; /* ok */
default: /* some hopefully printable character */
if (i == bufsize - 1) {
cprintf("\a"); /* beep */
} else {
buf[i++] = ch;
gotoxy(x + i, y);
cprintf("%c", buf[i]);
}
}
}
/* TODO: null-terminate the buffer */
return 0;
}
Printing an array of characters is fairly easy:
char* str = your_array;
while(*str) {
putc(*str++);
}
From memory that should print a string out to the screen.
Your code is very DOS-specific. There is not a good general solution to the problem of reading immediate input in a portable way. It does get asked quite often, so I think the C FAQ broke down and included an answer which you might want to seek out.
That said, I think your bug is that gotoxy(1, 1) is the upper corner of the screen, not 0,0. So you want gotoxy(i, 1)