Skip to content

Instantly share code, notes, and snippets.

@reagent reagent/.gitignore

Last active May 13, 2020
Embed
What would you like to do?
Curses + C Example
demo
*.swp
#include <ncurses.h>
#include <unistd.h>
#define DELAY 35000
int main(int argc, char *argv[]) {
int x = 0,
y = 0;
int max_x = 0,
max_y = 0;
int next_x = 0;
int direction = 1;
initscr();
noecho();
curs_set(FALSE);
getmaxyx(stdscr, max_y, max_x);
x = max_x / 2;
y = max_y / 2;
while (1) {
getmaxyx(stdscr, max_y, max_x);
y = max_y / 2;
clear();
mvprintw(y, x, "o");
refresh();
usleep(DELAY);
next_x = x + direction;
if (next_x >= max_x || next_x < 0) {
direction*= -1;
} else {
x+= direction;
}
}
endwin();
return 0;
}
CFLAGS=-Wall
LDFLAGS=-lncurses
all: demo
clean:
rm -rf demo
@Xaymup

This comment has been minimized.

Copy link

Xaymup commented Sep 25, 2014

demo.c: In function ‘main’:
demo.c:19:10: error: ‘FALSE’ undeclared (first use in this function)
demo.c:19:10: note: each undeclared identifier is reported only once for each function it appears in
demo.c:21:10: error: ‘stdscr’ undeclared (first use in this function)

What's going on here?

@ButchDean

This comment has been minimized.

Copy link

ButchDean commented Sep 29, 2014

What does your makefile look like? This works fine for me.

CFLAGS = -g -Wall
LDFLAGS = -lncurses

.PHONY: clean

all:
    gcc $(CFLAGS) demo.c -o demo $(LDFLAGS)

clean:
    rm -rf demo
@Meliodus

This comment has been minimized.

Copy link

Meliodus commented Dec 17, 2014

Sir what if i want to use KEY_F(1) to exit from this loop ? .. i don't want to use ctrl+c to exit ..

@erm3nda

This comment has been minimized.

Copy link

erm3nda commented Feb 14, 2017

@reagent
Nice example :-)
It looks like 1/3 stage of a pong game :-) based on ncurses.

@ButchDean
Thank you for posting a proper makefile.
I think that @Xaymup has added reference to variable without declare it first.

@Meliodus
Read: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html
Read more: https://www.mkssoftware.com/docs/man3/curs_getch.3.asp

char ch; // declare it first
ch = getchar();
printf( "Input Char Is :%c", ch ); // press F1 then look for return, should be int 265
if ( ch == 265 ) {
    printf( "Got it" )
    }
}

Even easier, using KEY_F(num) to avoid remembering the int value:

ch = getchar()
if ( ch == KEY_F(1) ) {
    printr( "Got %c !", ch )
}

The problem is that usually F keys are binded to somewhat at the OS and the program will not respond to them unless you add proper binding to the SO at the execute time.

--- About your question:
So, case you wanna exit with single key (ctrl+c will work anyway), you need to ad something like that at the start or end of loop:

if ((ch = getchar()) == 100) { // 100 means a "d"
  break;
}

The problem with that is that the loop freezes (waits) untill getchar() returns something.
To solve that you have to use any kind of keyboard detector. More info about it (kbhits()) at: http://stackoverflow.com/questions/13547471/c-programming-check-if-key-pressed-without-stopping-program

I tried the Linux version of kbhits but ended with:

gcc -Wall demo.c -o demo -lncurses
In file included from demo.c:4:0:
kbhit.c: In function ‘_kbhit’:
kbhit.c:16:9: error: unknown type name ‘termios’
... and much more lines...

... that i've pasted to: http://pastebin.com/1VRdFdPQ.

So, finally i read a bit more on the ncurses docs and found the timeout() function, being the solution like this:

timeout(10)
if ((ch = getch()) == 100) { // 100 means a "d". Note also that i used getch() not getchar()
  break;
}

Code working that stops with d (i can't bind to f1, try on your own) HERE: http://pastebin.com/xyfm7LNa
Going to sleep :-D

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 16, 2017

haha!

@jcsahnwaldt

This comment has been minimized.

Copy link

jcsahnwaldt commented Oct 28, 2019

You should replace LDFLAGS by LDLIBS. Here's a quote from the entry for LDFLAGS in the GNU make manual:

Libraries (-lfoo) should be added to the LDLIBS variable instead.

You could then simplify your Makefile to something like this:

CFLAGS = -g -Wall
LDLIBS = -lncurses

.PHONY: clean

all: demo

clean:
    rm -rf demo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.