Ejemplo n.º 1
0
int main(int argc, char *argv[]) {	
    struct windows win = { NULL, NULL, NULL, NULL };
    int     curs_x = CURS_X_MIN,
            curs_y = CURS_Y_MIN,
            pad_minrow = 0,
            pad_textrows,
            ch,
            quit = 0;
    char    *status = "",
            scratch[1000];

    // Init curses
    initNCurses();
    initWindows(&win);

    // Add text to main window
    loadAndAddText(&win.textpad, &pad_textrows, "story.txt");

    while (!quit) {
        if ((ch = getch()) != ERR) {
            switch (tolower(ch)) {

            // UP
            case KEY_UP:
                if (curs_y > CURS_Y_MIN) {
                    curs_y--;
                } else {
                    if (pad_minrow > 0) {
                        pad_minrow--;
                    }
                }
                break;

            // DOWN
            case KEY_DOWN:
                if (curs_y < CURS_Y_MAX) {
                    curs_y++;
                } else {
                    if (pad_minrow < pad_textrows - PAD_HEIGHT) {
                        pad_minrow++;
                    }
                }
                break;

            // LEFT
            case KEY_LEFT:
                if (curs_x > CURS_X_MIN) {
                    curs_x--;
                }
                break;

            // RIGHT
            case KEY_RIGHT:
                if (curs_x < CURS_X_MAX) {
                    curs_x++;
                }
                break;

            // PAGE UP
            case KEY_PPAGE:
                curs_y = CURS_Y_MIN;
                pad_minrow = clip(pad_minrow - PAGE_SCROLL_SIZE,
                                  0,
                                  pad_textrows - PAD_HEIGHT);
                break;

            // PAGE DOWN
            case KEY_NPAGE:
                curs_y = CURS_Y_MAX;
                pad_minrow = clip(pad_minrow + PAGE_SCROLL_SIZE,
                                  0,
                                  pad_textrows - PAD_HEIGHT);
                break;

            // HOME
            case KEY_HOME:
                curs_x = CURS_X_MIN;
                break;

            // END
            case KEY_END:
                curs_x = CURS_X_MAX;
                break;

            case 'l':
                mvwinstr(win.textpad, pad_minrow + curs_y - 1, curs_x - 1, scratch);
                status = scratch;
                //status = "Lookup";
                break;

            case 'u':
                status = "Use";
                break;

            case 'g':
                status = "Goto";
                break;

            case 't':
                status = "Talk";
                break;

            case 'e':
                status = "Examine";
                break;

            case 'q':
                quit = 1;
                break;

            default:
                break;
            }
        }
        
        updateTextPad(win.textpad, pad_minrow);
        updateStatusBar(win.status, status);
        updateCursor(win.main, curs_y, curs_x);
    }

    delWindows(&win);
    delNCurses();
    
    return 0;
}
Ejemplo n.º 2
0
int main(int argc, char **argv) 
{
	Options o = parseOptions(argc, argv);

	mlog("getting snake color if %s", o.color.argument);
	string *color = new string(o.color.argument);

	mlog("init ncurses");
	initNCurses(color);

	mlog("checking width");
	if (COLS < 41 || LINES < 6) {
		endwin();
		fprintf(stderr, "The terminal window needs to be at least 41");
		fprintf(stderr, " chars wide and 6 lines high.\n");
		exit(EXIT_FAILURE);
	} 

	mlog("reading cmdlineargs");
	int growthSpeed = 1; //atoi(o.growthSpeed.argument);
	int movementSpeed = 1; //atoi(o.movementSpeed.argument);

	// create the board for the correct window size and highscore file
	string path = string(getenv("HOME")) + "/" + highscoreFile;
	mlog("highscore path at %s", path.c_str());

	mlog("new board");
	Board *b = new Board(COLS, LINES, path, growthSpeed, movementSpeed);

	if (b == NULL) {
		endwin();
		fprintf(stderr, "Could not initialize the board.\n");
		exit(EXIT_FAILURE);
	}

	if (o.remote.set == true) {
		mlog("opening connection at %s", o.port.argument);
		Connection *c = openListenConnection(o.port.argument);
		if (c == NULL) {
			fprintf(stderr, "Could not open socket to listen: %s",
					strerror(errno));
			exit(EXIT_FAILURE);
		}
		b->setListenConnection(c);
		b->setRemote(true);
		mlog("connection opened");
	} else {
		if (o.ai.set == true) {
			mlog("ai steering");
			b->setAi(true);
			/*init_ai(b->width, b->height);*/
		} else {
			mlog("local human steering");
			b->setAi(false);
			b->setListenConnection(NULL);
			b->setRemote(false);
		}
	}

	if (o.growthSpeed.set != true) {
		mlog("setting grow speed to 1");
		o.growthSpeed.argument = (char *) "1";
		o.growthSpeed.set = true;
	}

	if (o.movementSpeed.set != true) {
		o.movementSpeed.argument = (char *) "1";
		o.movementSpeed.set = true;
	}


	mlog("game loop");

	/* run the gaem */
	int toReturn = gameLoop(b);

	b->writeHighscore(path);


	/* memory deallocation */
	delete b;

	return toReturn;
}