CUADROMOVIL.c



/* SE DIBUJA UN CUADRO Y SE MUEVE CON LAS TECLAS DE FLECHAS */
/* LO COPIE POR AHI */

#include <ncurses.h>

WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);

int main(int argc, char *argv[])
{
	WINDOW *my_win;
	int startx, starty, width, height;
	int ch;

	initscr();		/* Inicio del modo ncurses */
	cbreak();
	keypad(stdscr, TRUE);

	height = 3;
	width = 10;
	starty = (LINES - height) / 2;		/* cAlculo del centro de la ventana */
	startx = (COLS - width) / 2;
	printw("Press F1 to exit");
	refresh();
	my_win = create_newwin(height, width, starty, startx);

	while((ch = getch()) != KEY_F(1))
	{
		switch(ch)
		{
			case KEY_LEFT:
				destroy_win(my_win);
				my_win = create_newwin(height, width, starty,--startx);
				break;
			case KEY_RIGHT:
				destroy_win(my_win);
				my_win = create_newwin(height, width, starty,++startx);
				break;
			case KEY_UP:
				destroy_win(my_win);
				my_win = create_newwin(height, width, --starty,startx);
				break;
			case KEY_DOWN:
				destroy_win(my_win);
				my_win = create_newwin(height, width, ++starty,startx);
				break;
		}
	}

	endwin();		/* Fin del modo ncurses */
	return 0;
}

WINDOW *create_newwin(int height, int width, int starty, int startx)
{
	WINDOW *local_win;
	local_win = newwin(height, width, starty, startx);
	box(local_win, 0 , 0);			/* 0, 0 fija el carActer por defecto para las lIneas horizontales y verticales */
	wrefresh(local_win);			/* muestra el cuadro */
	return local_win;
}

void destroy_win(WINDOW *local_win)
{
	wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
		/* The parameters taken are
			* 1. win: the window on which to operate
			* 2. ls: character to be used for the left side of the window
			* 3. rs: character to be used for the right side of the window
			* 4. ts: character to be used for the top side of the window
			* 5. bs: character to be used for the bottom side of the window
			* 6. tl: character to be used for the top left corner of the window
			* 7. tr: character to be used for the top right corner of the window
			* 8. bl: character to be used for the bottom left corner of the window
			* 9. br: character to be used for the bottom right corner of the window
		*/
	wrefresh(local_win);
	delwin(local_win);
}