]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/main.cpp
3bfa32c2701af6cc95f8fe8bd7e10d27eb96af7c
[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
18 using namespace std;
19
20 Map *Game::map;
21 string Game::mapfile;
22 string Game::logfile;
23 int Game::seed;
24 char **Game::argv;
25 int *Game::argc;
26 FILE *Game::logfile_fd;
27 Inventory *Game::inventory;
28
29 int main(int argc, char **argv){
30         if((string)argv[0] != "bin/dragonblocks"){
31                 string command = "cd ..; bin/dragonblocks -w world ";
32                 while(*++argv)
33                         command += *argv;
34                 exit(system(command.c_str()));
35         }
36         create_dir_if_not_exists((string)getenv("HOME")+"/.dragonblocks");
37         create_dir_if_not_exists((string)getenv("HOME")+"/.dragonblocks/worlds");
38         Game::argc = &argc;
39         Game::argv = argv;
40         Game::seed = time(0);
41         Game::logfile = (string)getenv("HOME")+"/.dragonblocks/dragonblocks.log";
42         const char *short_options = "hrvs:l:w:p:";
43         const struct option long_options[] = {
44                 {"help", 0, NULL, 'h'},
45                 {"version", 0, NULL, 'v'},
46                 {"worldname", 1, NULL, 'w'},
47                 {"worldpath", 1, NULL, 'p'},
48                 {"worldlist", 0, NULL, 'r'},
49                 {"seed", 1, NULL, 's'},
50                 {"logfile", 1, NULL, 'l'},
51                 {NULL, 0, NULL, 0},
52         };
53         int next_option;
54         while((next_option = getopt_long(argc, argv, short_options, long_options, NULL)) != -1){
55                 switch(next_option){
56                         case 'h':
57                                 Game::help();
58                                 exit(EXIT_SUCCESS);
59                                 break;
60                         case 'v':
61                                 Game::version();
62                                 exit(EXIT_SUCCESS);
63                                 break;
64                         case 'p':
65                                 Game::mapfile = optarg;
66                                 break;
67                         case 'w':
68                                 Game::mapfile = (string)getenv("HOME")+"/.dragonblocks/worlds/"+optarg;
69                                 break;
70                         case 's':
71                                 Game::seed = atoi(optarg);
72                                 break;
73                         case 'l':
74                                 Game::logfile = optarg;
75                                 break;
76                         case 'r':
77                                 Game::worldlist();
78                                 exit(0);
79                                 break;  
80                         case '?':
81                                 Game::log("Invalid Usage", ERROR);
82                                 Game::help();
83                                 exit(EXIT_FAILURE);
84                                 break;
85                 };
86         }
87         if(Game::logfile_fd = fopen(Game::logfile.c_str(), "a"))
88                 fprintf(Game::logfile_fd, "\n--------------------------------------------------\n");
89         else
90                 Game::log((string)"Failed to open log file " + Game::logfile, WARNING);
91         Game::log((string)"Welcome to Dragonblocks "+VERSION);
92         srand(Game::seed);
93         new Node("unknown_node", "textures/unknown_node.png", true, true, false);
94         Mods::init();
95         Game::inventory = new Inventory();
96         Game::map = new Map();
97         if(Game::mapfile == ""){
98                 Game::log("No World Specified", ERROR);
99                 exit(EXIT_FAILURE);
100         }
101         if(fopen(Game::mapfile.c_str(), "r"))
102                 Game::map->load();
103         else
104                 Mapgen();
105         Threads::startMapBackupThread();
106         Threads::startGraphicUpdateThread();
107         Threads::addSignalHandlers();
108         Graphics::init();
109         Game::log("Closed Window, Exiting.");
110         Game::map -> save();
111         return 0;
112 }
113