Fix curses not displaying until keypress. Callback example demonstrating some pseudo-animation

This commit is contained in:
Tiehuis 2014-12-06 11:49:16 +13:00
parent 2b1d230330
commit 01af91121b

View File

@ -13,19 +13,6 @@
} while (0) } while (0)
#ifdef HAVE_CURSES #ifdef HAVE_CURSES
void drawstate_init(void)
{
initscr();
cbreak();
noecho();
curs_set(FALSE);
}
void drawstate_clear(void)
{
endwin();
}
void draw_screen(struct gamestate *g) void draw_screen(struct gamestate *g)
{ {
static WINDOW *gamewin; static WINDOW *gamewin;
@ -64,6 +51,20 @@ void draw_screen(struct gamestate *g)
wrefresh(gamewin); wrefresh(gamewin);
} }
void drawstate_init(void)
{
initscr();
cbreak();
noecho();
curs_set(FALSE);
refresh();
}
void drawstate_clear(void)
{
endwin();
}
int get_keypress(void) int get_keypress(void)
{ {
return getch(); return getch();
@ -104,7 +105,7 @@ void draw_screen(struct gamestate *g)
printf("|"); printf("|");
for (x = 0; x < g->opts->grid_width; x++) { for (x = 0; x < g->opts->grid_width; x++) {
if (g->grid[x][y]) if (g->grid[x][y])
printf("%*ld |", 4, g->grid[x][y]); printf("%*ld |", g->print_width, g->grid[x][y]);
else else
printf(" |"); printf(" |");
} }
@ -121,6 +122,12 @@ int get_keypress(void)
#endif #endif
void ddraw(struct gamestate *g)
{
draw_screen(g);
usleep(30000);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct gamestate *g = gamestate_init(gameoptions_default()); struct gamestate *g = gamestate_init(gameoptions_default());
@ -137,12 +144,12 @@ int main(int argc, char **argv)
if (ch == 'q') { goto endloop; } if (ch == 'q') { goto endloop; }
} while (strchr("hjkl", ch) == NULL); } while (strchr("hjkl", ch) == NULL);
gamestate_tick(g, ch, NULL);
if (!moves_available(g)) { if (!moves_available(g)) {
printf("You lose\n"); printf("You lose\n");
break; break;
} }
gamestate_tick(g, ch, ddraw);
} }
endloop: endloop: