]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/main.cpp
Upload Files
[dragonblocks-bedrock.git] / src / main.cpp
1 #include <stdio.h>
2 #include <string>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <getopt.h>
6 #include <iostream>
7 #include <cstdio> 
8 #include <unistd.h>
9 #include "graphics.h"
10 #include "map.h"
11 #include "game.h"
12 #include "node.h"
13 #include "mapgen.h"
14 #include "mods.h"
15 #include "threads.h"
16 #include "inventory.h"
17 #include "player.h"
18
19 using namespace std;
20
21 int main(int argc, char **argv){
22         Game::argc = &argc;
23         Game::argv = argv;
24         Game::userdir = (string)getenv("HOME")+"/.dragonblocks";
25         Game::logfile_path = Game::userdir + "/dragonblocks.log";
26         create_dir_if_not_exists(Game::userdir);
27         create_dir_if_not_exists(Game::userdir + "/worlds");
28         Game::seed = time(0);
29         const char *short_options = "hrvs:l:w:p:";
30         const struct option long_options[] = {
31                 {"help", 0, NULL, 'h'},
32                 {"version", 0, NULL, 'v'},
33                 {"worldname", 1, NULL, 'w'},
34                 {"worldpath", 1, NULL, 'p'},
35                 {"worldlist", 0, NULL, 'r'},
36                 {"seed", 1, NULL, 's'},
37                 {"logfile", 1, NULL, 'l'},
38                 {NULL, 0, NULL, 0},
39         };
40         int next_option;
41         while((next_option = getopt_long(argc, argv, short_options, long_options, NULL)) != -1){
42                 switch(next_option){
43                         case 'h':
44                                 Game::help();
45                                 exit(EXIT_SUCCESS);
46                                 break;
47                         case 'v':
48                                 Game::version();
49                                 exit(EXIT_SUCCESS);
50                                 break;
51                         case 'p':
52                                 Game::worlddir = optarg;
53                                 break;
54                         case 'w':
55                                 Game::worlddir = Game::userdir + "/worlds/" + optarg;
56                                 break;
57                         case 's':
58                                 Game::seed = atoi(optarg);
59                                 break;
60                         case 'l':
61                                 Game::logfile_path = optarg;
62                                 break;
63                         case 'r':
64                                 Game::worldlist();
65                                 exit(0);
66                                 break;  
67                         case '?':
68                                 Game::log("Invalid Usage", ERROR);
69                                 Game::help();
70                                 exit(EXIT_FAILURE);
71                                 break;
72                 };
73         }
74         if(Game::logfile = fopen(Game::logfile_path.c_str(), "a"))
75                 fprintf(Game::logfile, "\n--------------------------------------------------\n");
76         else
77                 Game::log((string)"Failed to open log file " + Game::logfile_path, WARNING);
78         Game::log((string)"Welcome to Dragonblocks "+VERSION);
79         if(Game::worlddir == ""){
80                 Game::log("No World Specified", ERROR);
81                 exit(EXIT_FAILURE);
82         }
83         create_dir_if_not_exists(Game::worlddir);
84         new Node("unknown_node", "textures/unknown_node.png", true, true, false);
85         Mods::init();
86         Mods::nodedef();
87         Game::map = new Map();
88         srand(Game::seed);
89         if(fopen((Game::worlddir + "/map").c_str(), "r"))
90                 Game::map->load();
91         else
92                 Mapgen();
93         Game::player = new Player();
94         Threads::add_signal_handlers();
95         Threads::start(Threads::worldBackupThread);
96         Threads::start(Threads::graphicRedrawThread);
97         Threads::start(Threads::entityPhysicsThread);
98         Graphics::init();
99         Game::log("Closed Window, Exiting.");
100         Game::save();
101         return 0;
102 }
103