Home > Archive > Unix Programming > February 2007 > curses nodelay
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| conrad 2007-02-04, 7:05 pm |
| I have nodelay set for my terminal
because the program's purpose is to
provide an interactive environment.
The problem comes about when
my drawing function is called
and draws objects on the term.
After drawing the object, I call refresh
and then I call usleep with a value of
3000000. The effect of this, is that
the obejcts drawn on the term
happen slowly(this is desired) but
a side effect of this is that it hinders
my input function. By entering certain
keys, you can move the objects drawn
around the term. But it slow input
down considerably. For example,
if I were to move an object to the left,
then instead of instantaneously moving the
object to the left, it draws, waits, then gets
the next input. And so I can hit left five
consecutive times before and will have to wait
before I can then issue a command to move
the object up. Now if I change the value from
3000000 to 99999, this does not hinder
my input function. I can give input and get
immediate results. However, the side effect
of this is that objects are drawn too quickly.
So how can I cater to instant input but
at the same time draw objects at some
given speed? (intiially, the objects are drawn
slowly and gradually the speed at which
they are displayed increases)
It's a bit too much code to paste,
so hopefully that was all clear.
--
conrad
| |
| conrad 2007-02-05, 7:05 pm |
| On Feb 4, 11:21 am, "conrad" <con...@lawyer.com> wrote:
> I have nodelay set for my terminal
> because the program's purpose is to
> provide an interactive environment.
>
> The problem comes about when
> my drawing function is called
> and draws objects on the term.
>
> After drawing the object, I call refresh
> and then I call usleep with a value of
> 3000000. The effect of this, is that
> the obejcts drawn on the term
> happen slowly(this is desired) but
> a side effect of this is that it hinders
> my input function. By entering certain
> keys, you can move the objects drawn
> around the term. But it slow input
> down considerably. For example,
> if I were to move an object to the left,
> then instead of instantaneously moving the
> object to the left, it draws, waits, then gets
> the next input. And so I can hit left five
> consecutive times before and will have to wait
> before I can then issue a command to move
> the object up. Now if I change the value from
> 3000000 to 99999, this does not hinder
> my input function. I can give input and get
> immediate results. However, the side effect
> of this is that objects are drawn too quickly.
>
> So how can I cater to instant input but
> at the same time draw objects at some
> given speed? (intiially, the objects are drawn
> slowly and gradually the speed at which
> they are displayed increases)
>
> It's a bit too much code to paste,
> so hopefully that was all clear.
>
> --
> conrad
I wrote a small example to demonstrate my problem.
If you notice, when you issue a command
for the object to move right several times, there is
a small delay. I would like commands to be issued
immediately but at the same time display the
object slowly. How can I achieve this?
#include <curses.h>
enum {
END_GAME,
NO_INPUT
};
struct obj {
int x, y;
int coord[8];
} foo = { 1,
1,
{ 0, 0,-1, 0,-1,-1, 0,-1 },
};
int get_input(void);
void start(void);
void move_left(void);
void move_right(void);
void move_up(void);
void move_down(void);
void draw(void);
void clear_obj(void);
void move_left(void)
{
clear_obj();
--foo.x;
draw();
}
void move_right(void)
{
clear_obj();
++foo.x;
draw();
}
void move_up(void)
{
clear_obj();
--foo.y;
draw();
}
void move_down(void)
{
clear_obj();
++foo.y;
draw();
}
void draw(void)
{
int i = 0;
for(i = 0; i < 8; i+=2) {
mvprintw(foo.y + foo.coord[i + 1], foo.x + foo.coord[i], "O");
}
refresh();
move(0,0);
usleep(1000000);
}
void clear_obj(void)
{
int i = 0;
for(i = 0; i < 8; i+=2) {
mvprintw(foo.y + foo.coord[i + 1], foo.x + foo.coord[i], " ");
}
refresh();
}
void start(void)
{
int state;
while((state = get_input()) != END_GAME) {
switch(state) {
case KEY_UP: move_up(); break;
case KEY_DOWN: move_down(); break;
case KEY_LEFT: move_left(); break;
case KEY_RIGHT: move_right(); break;
}
move_down();
}
}
int get_input(void)
{
int state = getch();
if(state == ERR)
state = NO_INPUT;
else if(state == 'q' || state == 'Q')
state = END_GAME;
return state;
}
int main(void)
{
initscr();
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
curs_set(0);
for(;;) {
start();
clear(); refresh();
exit(1);
}
return 0;
}
--
conrad
| |
| Christopher Layne 2007-02-05, 10:04 pm |
| conrad wrote:
> On Feb 4, 11:21 am, "conrad" <con...@lawyer.com> wrote:
>
> I wrote a small example to demonstrate my problem.
> If you notice, when you issue a command
> for the object to move right several times, there is
> a small delay. I would like commands to be issued
> immediately but at the same time display the
> object slowly. How can I achieve this?
>
> void draw(void)
> {
> int i = 0;
>
> for(i = 0; i < 8; i+=2) {
> mvprintw(foo.y + foo.coord[i + 1], foo.x + foo.coord[i], "O");
> }
>
> refresh();
> move(0,0);
> usleep(1000000);
> }
Delete the usleep().
> void start(void)
> {
> int state;
Add:
draw();
>
> while((state = get_input()) != END_GAME) {
> switch(state) {
> case KEY_UP: move_up(); break;
> case KEY_DOWN: move_down(); break;
> case KEY_LEFT: move_left(); break;
> case KEY_RIGHT: move_right(); break;
This should also have:
case NO_INPUT: move_down(); break;
> }
> move_down();
Delete the move_down() from here.
> int main(void)
> {
> initscr();
> nodelay(stdscr, TRUE);
Delete nodelay();
Add:
wtimeout(stdscr, 1000);
Then you will get the behavior you expect.
|
|
|
|
|