00001 #include <stdio.h>
00002 #include <errno.h>
00003 #include <string.h>
00004 #include "ssgLocal.h"
00005
00006 using namespace std;
00007
00008 #include "ssgLocal.h"
00009 #include "configuration.h"
00010
00011 #define NEAR_WATER_POLYGONS 15000
00012 #define NEXT_WATER_POLYGONS 5000
00013
00014 #define BASE_WINDOW_WIDTH 640
00015 #define BASE_WINDOW_HEIGHT 480
00016 #define DEFAULT_FULLSCREEN FALSE
00017
00018 #define DEFAULT_CONFIG_FILE "config"
00019
00020 #define WRITE_CHAR(c, fd) { \
00021 fwrite((const void*)c, sizeof(char), 1, fd); \
00022 }
00023
00024 #define READ_CHAR(c, fd) { \
00025 char temp; \
00026 fread((void*)&temp, sizeof(char), 1, fd); \
00027 if (temp != *c) \
00028 fprintf(stderr, "error: got a %c while expecting %c in config\n", c, temp); \
00029 }
00030
00031 static float default_campos[MAX_CAMPOS*3] = {
00032 10.0f, 180.0f, 20.0f,
00033 20.0f, 90.0f, 40.0f,
00034 25.0f, 265.0f, 12.0f,
00035 60.0f, 75.0f, 5.0f };
00036
00037
00038 configuration::configuration(char* fn) : ssgBase() {
00039 if (fn == NULL)
00040 fn = strdup(DEFAULT_CONFIG_FILE);
00041
00042 near_water_polygons = NEAR_WATER_POLYGONS;
00043 next_water_polygons = NEXT_WATER_POLYGONS;
00044 window_height = BASE_WINDOW_HEIGHT;
00045 window_width = BASE_WINDOW_WIDTH;
00046 is_fullscreen = DEFAULT_FULLSCREEN;
00047
00048 game_mode_string = NULL;
00049 FILE* f;
00050 f = fopen(fn, "r");
00051 if (f == NULL) {
00052 fprintf(stderr, "warning: could not read config file: %s (ec: %i)\n", fn, errno);
00053 }
00054 else {
00055 load(f);
00056 fclose(f);
00057 }
00058 for(int i=0; i < MAX_CAMPOS; i++) {
00059
00060 sgSetVec3(camera_positions[i],
00061 default_campos[3*i], default_campos[3*i+1], default_campos[3*i+2]);
00062 }
00063 config_file = strdup((const char*)fn);
00064 }
00065
00066 int configuration::load(FILE* fd) {
00067 char* line = (char*)malloc(sizeof(char)*80);
00068 int c;
00069 while ((c=fgetc(fd))!=EOF) {
00070 ungetc(c, fd);
00071 _ssgReadString(fd, &line);
00072 READ_CHAR(" ", fd);
00073 if (strcmp((const char*)line, (const char*)"game_mode_string")==0) {
00074 _ssgReadString(fd, &line);
00075 if (line != NULL) {
00076 delete game_mode_string;
00077 game_mode_string = strdup(line);
00078 }
00079 }
00080 else if (strcmp((const char*)line, (const char*)"window_height")==0) {
00081 _ssgReadInt(fd, &window_height);
00082 }
00083 else if (strcmp((const char*)line, (const char*)"window_width")==0) {
00084 _ssgReadInt(fd, &window_width);
00085 }
00086 else if (strcmp((const char*)line, (const char*)"near_poly")==0) {
00087 _ssgReadUInt(fd, &near_water_polygons);
00088 }
00089 else if (strcmp((const char*)line, (const char*)"next_poly")==0) {
00090 _ssgReadUInt(fd, &next_water_polygons);
00091 }
00092 else if (strcmp((const char*)line, (const char*)"fullscreen")==0) {
00093 int ti;
00094 _ssgReadInt(fd, &ti);
00095 is_fullscreen = (bool)ti;
00096 }
00097 else if (strcmp((const char*)line, (const char*)"CAMPOS0")==0) {
00098 _ssgReadVec3(fd, camera_positions[0]);
00099 }
00100 else if (strcmp((const char*)line, (const char*)"CAMPOS1")==0) {
00101 _ssgReadVec3(fd, camera_positions[1]);
00102 }
00103 else if (strcmp((const char*)line, (const char*)"CAMPOS2")==0) {
00104 _ssgReadVec3(fd, camera_positions[2]);
00105 }
00106 else if (strcmp((const char*)line, (const char*)"CAMPOS3")==0) {
00107 _ssgReadVec3(fd, camera_positions[3]);
00108 }
00109
00110 else {
00111 fprintf(stderr, "error: unknown keyword (%s) while loading %s\n", line, config_file);
00112 return(0);
00113 }
00114
00115 READ_CHAR("\n", fd);
00116 }
00117 delete (line);
00118 return 1;
00119 }
00120
00121 int configuration::save(FILE* fd) {
00122 char * tmp = (char*)malloc(sizeof(char)*20);
00123 _ssgWriteString(fd, "game_mode_string");
00124 WRITE_CHAR(" ", fd);
00125 _ssgWriteString(fd, game_mode_string);
00126 WRITE_CHAR("\n", fd);
00127 _ssgWriteString(fd, "window_height");
00128 WRITE_CHAR(" ", fd);
00129 _ssgWriteInt(fd, window_height);
00130 WRITE_CHAR("\n", fd);
00131 _ssgWriteString(fd, "window_width");
00132 WRITE_CHAR(" ", fd);
00133 _ssgWriteInt(fd, window_width);
00134 WRITE_CHAR("\n", fd);
00135 _ssgWriteString(fd, "fullscreen");
00136 WRITE_CHAR(" ", fd);
00137 _ssgWriteInt(fd, (int)is_fullscreen);
00138 WRITE_CHAR("\n", fd);
00139 _ssgWriteString(fd, "near_poly");
00140 WRITE_CHAR(" ", fd);
00141 _ssgWriteUInt(fd, near_water_polygons);
00142 WRITE_CHAR("\n", fd);
00143 _ssgWriteString(fd, "next_poly");
00144 WRITE_CHAR(" ", fd);
00145 _ssgWriteUInt(fd, next_water_polygons);
00146 WRITE_CHAR("\n", fd);
00147 for (int i=0; i < MAX_CAMPOS; i++) {
00148 sprintf(tmp, (const char*)"CAMPOS%i", i);
00149 _ssgWriteString(fd, tmp);
00150 WRITE_CHAR(" ", fd);
00151 _ssgWriteVec3(fd, camera_positions[i]);
00152 WRITE_CHAR("\n", fd);
00153 }
00154 delete(tmp);
00155 return 0;
00156 }
00157
00158 configuration::~configuration() {
00159 FILE* f;
00160 f = fopen(config_file, "w");
00161 if (f == NULL) {
00162 fprintf(stderr, "warning: could not write config file: %s (ec: %i)\n", config_file, errno);
00163 }
00164 else {
00165 save(f);
00166 fclose(f);
00167 }
00168 free(config_file);
00169 if (game_mode_string != NULL)
00170 free(game_mode_string);
00171 }
00172
00173 void configuration::copy_from(configuration* other, int clone_flags) {
00174 delete config_file;
00175 config_file = other->get_config_fname();
00176 delete game_mode_string;
00177 game_mode_string = other->get_game_mode_string();
00178 is_fullscreen = other->get_fullscreen();
00179 window_width = other->get_window_width();
00180 window_height = other->get_window_height();
00181 near_water_polygons = other->get_nearwp();
00182 next_water_polygons = other->get_nextwp();
00183 for (int i=0; i<MAX_CAMPOS; i++) {
00184 sgCopyVec3(camera_positions[i], *other->get_campos(i));
00185 }
00186 }
00187
00188 configuration* configuration::clone(int cf) {
00189 return NULL;
00190 }
00191
00192 void configuration::set_game_mode_string(char* v) {
00193 if (game_mode_string != NULL)
00194 free(game_mode_string);
00195 game_mode_string = strdup(v);
00196 }
00197
00198
00199 char* configuration::get_game_mode_string() {
00200 return (game_mode_string != NULL) ? strdup((const char*)game_mode_string) : NULL;
00201 }
00202 char* configuration::get_config_fname() {
00203 return (config_file != NULL) ? strdup((const char*)config_file) : NULL;
00204 }
00205
00206 void configuration::set_campos(int i, float fw, float xa, float za) {
00207 sgSetVec3(camera_positions[i], fw, xa, za);
00208 }
00209
00210 sgVec3* configuration::get_campos(int i) {
00211 sgVec3* t = new sgVec3[3];
00212 sgCopyVec3(*t, camera_positions[i]);
00213 return t;
00214 }