File: main.cpp

package info (click to toggle)
cytadela 1.1.0-4
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm, bullseye, buster, stretch
  • size: 82,804 kB
  • ctags: 1,521
  • sloc: cpp: 11,228; sh: 787; makefile: 186
file content (142 lines) | stat: -rw-r--r-- 3,968 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
  File name: main.cpp
  Copyright: (C) 2005 - 2013 Tomasz Kazmierczak
  Copyright: (C) 2006 Kamil Pawlowski
  Copyright: (C) 2009 Tomasz Wisniewski

  This file is part of Cytadela

 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program. If not, see <http://www.gnu.org/licenses/>.  *

  Application entry point
*/

#include "CCytadelaMain.h"
#include <string>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>

#if defined(__APPLE__)
#include <CoreFoundation/CFURL.h>
#include <CoreFoundation/CFBundle.h>
#endif

#ifndef DATADIR
	#define DATADIR "./"
#endif

using namespace std;

bool handleArgs(int argc, char *argv[])
{
	if(argc > 1 and (strcmp(argv[1], "-v") == 0 or strcmp(argv[1], "--version") == 0)) {
		fprintf(stdout, "Cytadela, version 1.1.0\n");
		return false;
	}
	return true;
}

bool changeToDataDirectory()
{
	int result = 0;
	char dataDir[FILENAME_MAX] = {0};
#if defined(__APPLE__)
	CFURLRef resourceURL =  CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
	CFURLGetFileSystemRepresentation((__CFURL*)resourceURL, TRUE, (UInt8*)dataDir, FILENAME_MAX);
	result = chdir(dataDir);
	CFRelease(resourceURL);
#else
	FILE *file = fopen("obj/PodziemiaT.3dg", "rb");
	if(file != 0)
		fclose(file);
	else {
		snprintf(dataDir, FILENAME_MAX, "%s", DATADIR);
		result = chdir(dataDir);
	}
#endif
	if(result != 0) {
		fprintf(stderr, "ERROR: could not open the data directory \"%s\": %s\n", dataDir, strerror(errno));
		return false;
	}
	return true;
}

bool getSysDirectoryPath(string &path)
{
	char *homeDir = getenv("HOME");
	if(homeDir != 0)
		path = string(homeDir) + "/.cytadela";
	else path = "sys";

	//check if the directory exists
	DIR *dir = opendir(path.c_str());
	if(dir == 0) {
		//if it does not exist - create it
		//TODO: replace this mess with a standard C function for creating
		//      directories as soon as such finction is added to the C/C++ language
#if defined(__WIN32__)
		if(-1 == mkdir(path.c_str())) {
			fprintf(stderr, "ERROR: could not create \"%s\" directory: %s\n", path.c_str(), strerror(errno));
			perror(path.c_str());
			return false;
		}
#else
		if(-1 == mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
			fprintf(stderr, "ERROR: could not create \"%s\" directory: %s\n", path.c_str(), strerror(errno));
			perror(path.c_str());
			return false;
		}
#endif
	} else closedir(dir);

	return true;
}

int main(int argc, char *argv[])
{
	fprintf(stdout, "\n");

	//handle the command line arguments
	if(not handleArgs(argc, argv))
		return 0;

	//set current directory to data directory
	if(not changeToDataDirectory()) {
		fprintf(stdout, "Abnormal program termination\n");
		return 1;
	}

	CSDLClass *sdl = CSDLClass::instance();
	//first we initialize SDL
	if(not sdl->init()) {
		fprintf(stdout, "Abnormal program termination\n");
		return 1;
	}

	//now we can go into the game
	string path;
	if(not getSysDirectoryPath(path)) {
		fprintf(stdout, "Abnormal program termination\n");
		return 1;
	}
	CCytadelaMain cytadelaMain(path.c_str());
	cytadelaMain.runMain();

	fprintf(stdout, "\n");
	return 0;
}