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 ]
|