uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

main.cc (1333B)


      1 #include "Engine.hpp"
      2 
      3 /* Program name */
      4 static char *argv0;
      5 
      6 static void
      7 die(const std::string& str)
      8 {
      9 	std::cerr << argv0 << ": " << str << "\n";
     10 	exit(1);
     11 }
     12 
     13 static void
     14 usage()
     15 {
     16 	std::cerr << "usage: " << argv0 << " <map_file> <score_file>\n";
     17 	exit(1);
     18 }
     19 
     20 int
     21 main(int argc, char *argv[])
     22 {
     23 	Engine *eng;
     24 	char *mapfile, *scorefile;
     25 	char name[NAMEMAX];
     26 
     27 	argv0 = *argv++;
     28 	if (argc < 3)
     29 		usage();
     30 	mapfile = *argv++;
     31 	scorefile = *argv;
     32 
     33 	if (!strcmp(mapfile, scorefile))
     34 		die("input files cannot be the same");
     35 	/* 
     36 	 * We're linking with `lncursesw` so we need to have UTF-8 enabled,
     37 	 * otherwise colors and certain characters might not show up properly
     38 	 * in some terminals.
     39 	 */
     40 	if (!setlocale(LC_ALL, ""))
     41 		die("setlocale");
     42 
     43 	do {
     44 		std::cout << "\rPlayer name: ";
     45 		std::cin >> name;
     46 	/* Make sure we read valid input. */
     47 	} while (strlen(name) >= NAMEMAX || std::cin.fail());
     48 
     49 	/* 
     50 	 * We'll guarantee the name is null-terminated and has a 
     51 	 * length < NAMEMAX so we don't have to do any checks in 
     52 	 * the other classes.
     53 	 */
     54 	name[strlen(name)] = '\0';
     55 
     56 	try {
     57 		eng = new Engine(mapfile, scorefile, name);
     58 	} catch (const std::runtime_error& e) {
     59 		die(e.what());
     60 	}
     61 	while (eng->is_running()) {
     62 		eng->redraw();
     63 		eng->kbd_input();
     64 		eng->enemies_move();
     65 		eng->collisions();
     66 	}
     67 	delete eng;
     68 
     69 	return 0;
     70 }