cleaned up + comented main.cpp
[Tsunagari.git] / src / main.cpp
blob3830f701018b7ea54182b051116b810bf2f20f34
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** main.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <fstream>
8 #include <istream>
9 #include <stdio.h>
10 #include <string>
12 #include <json/json.h>
14 #include "common.h"
15 #include "window.h"
18 /**
19 * This probably won't be changed ever except maybe with a command line option.
21 #define CLIENT_CONF_FILE "./client.conf"
23 /**
24 * Values needed prior to creating the GameWindow.
26 struct ClientValues {
27 std::string world;
28 coord_t windowsize;
29 bool fullscreen;
33 /**
34 * Load the values we need to start initializing the game from a JSON file.
36 * We need to know what size window to create and which World to load. This
37 * information will be stored in a JSON file which we parse here.
39 * @param filename Name of the JSON-encoded file to load from.
40 * @param conf Values are stored here.
42 * @return True if successful
44 static bool parseClientConfig(const std::string filename, ClientValues* conf)
46 Json::Reader reader;
47 Json::Value root, windowsize;
49 std::ifstream file(filename);
51 if (!reader.parse(file, root)) {
52 printf("Client config failed to parse as JSON\n");
53 return false;
56 /* GET:
57 * - name of World to load
58 * - width, height, fullscreen-ness of Window
60 conf->world = root.get("world", "_NONE_").asString();
61 if (conf->world == "_NONE_") {
62 printf("Client config didn't contain world name\n");
63 return false;
66 windowsize = root["windowsize"];
67 if (windowsize.size() != 2) {
68 printf("Client config windowsize didn't contain 2 values\n");
69 return false;
72 conf->windowsize.x = windowsize[uint(0)].asUInt();
73 conf->windowsize.y = windowsize[1].asUInt();
75 conf->fullscreen = root.get("fullscreen", false).asBool();
77 return true;
80 /**
81 * Load client config and instantiate window.
83 * The client config tells us our window parameters along with which World
84 * we're going to load. The GameWindow class then loads and plays the game.
86 int main()
88 ClientValues conf;
90 if (!parseClientConfig(CLIENT_CONF_FILE, &conf))
91 return 1;
93 GameWindow window(conf.windowsize.x, conf.windowsize.y,
94 conf.fullscreen);
95 window.show();
97 return 0;