curses nodelay
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > curses nodelay




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    curses nodelay  
conrad


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-04-07 06:16 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






[ Post a follow-up to this message ]



    Re: curses nodelay  
conrad


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-06-07 12:18 AM

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






[ Post a follow-up to this message ]



    Re: curses nodelay  
Christopher Layne


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-06-07 12:18 AM

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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:10 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register