📄 userconfiguration.cpp
字号:
/*************************************************************************** userconfiguration.cpp - description ------------------- begin : Sat Feb 14 2004 copyright : (C) 2004 by Daroth-U email : daroth-u@ifrance.com ***************************************************************************//*************************************************************************** * * * 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 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "userconfiguration.h"#ifndef WIN32#include <sys/stat.h>#include <unistd.h>#endif // TODO : - warn if there is an unknown parameter in the config file ?// - manage doubled keynames -> must be impossible in optionsmenu// - make an array for variables too (they are all hard coded for now)// - default config if config file not there ??// Must be exact copy of enums defined in userconfiguration.h// (except for ENGINE_ACTION_COUNT)const char * UserConfiguration::ENGINE_ACTION_NAMES[]={ "SET_MOVE_DOWN", "SET_MOVE_RIGHT", "SET_MOVE_UP", "SET_MOVE_LEFT", "SET_PLAYER_0", "SET_PLAYER_1", "SET_PLAYER_2", "SET_PLAYER_3", "SET_PLAYER_ONLY", "SHOW_INVENTORY", "SHOW_OPTIONS_MENU", "USE_ITEM", "SET_NEXT_FORMATION", "SET_Y_ROT_PLUS", "SET_Y_ROT_MINUS", "SET_Z_ROT_PLUS", "SET_Z_ROT_MINUS", "MINIMAP_ZOOM_IN", "MINIMAP_ZOOM_OUT", "MINIMAP_TOGGLE", "SET_ZOOM_IN", "SET_ZOOM_OUT", "TOGGLE_MAP_CENTER", "INCREASE_GAME_SPEED", "DECREASE_GAME_SPEED", "START_ROUND", "BLEND_A", "BLEND_B", "SET_X_ROT_PLUS", "SET_X_ROT_MINUS", "ADD_X_POS_PLUS", "ADD_X_POS_MINUS", "ADD_Y_POS_PLUS", "ADD_Y_POS_MINUS", "ADD_Z_POS_PLUS", "ADD_Z_POS_MINUS" };// Must be exact copy of enums defined in userconfiguration.h // (without the "_STOP" and except for ENGINE_ACTION_UP_COUNT)const char * UserConfiguration :: ENGINE_ACTION_UP_NAMES[]={ "SET_MOVE_DOWN", "SET_MOVE_RIGHT", "SET_MOVE_UP", "SET_MOVE_LEFT", "SET_X_ROT_PLUS", "SET_X_ROT_MINUS", "SET_Y_ROT_PLUS", "SET_Y_ROT_MINUS", "SET_Z_ROT_PLUS", "SET_Z_ROT_MINUS", "SET_ZOOM_IN", "SET_ZOOM_OUT", "USE_ITEM", "SET_NEXT_FORMATION" };const char * UserConfiguration::ENGINE_ACTION_DESCRIPTION[]={ "Move player south", "Move player north", "Move player east", "Move player west", "Select player 0", "Select player 1", "Select player 2", "Select player 3", "Move only selected player", "Show inventory", "Show options", "Use item", "Choose next formation", "Rotate map up", "Rotate map down", "Rotate map right", "Rotate map left", "Zoom in minimap", "Zoom out minimap", "Show/hide minimap", "Zoom in map", "Zoom out map", "Always center map", "Increase game speed", "Decrease game speed", "Start next round", // Not visible to the user "BLEND_A", "BLEND_B", "SET_X_ROT_PLUS", "SET_X_ROT_MINUS", "ADD_X_POS_PLUS", "ADD_X_POS_MINUS", "ADD_Y_POS_PLUS", "ADD_Y_POS_MINUS", "ADD_Z_POS_PLUS", "ADD_Z_POS_MINUS"};UserConfiguration::UserConfiguration(){ unsigned int i, j; string temp; stencilBufInitialized = false; configurationChanged = false; // default settings for video mode (are overridden by command line) fullscreen = true; doublebuf = true; hwpal = true; resizeable = true; force_hwsurf = false; force_swsurf = false; hwaccel = true; test = false; multitexturing = true; stencilbuf = true; bpp = -1; w = 800; h = 600; shadows = 0; // game settings gamespeed = 1; // fast speed centermap = true; // Build (string engineAction -> int engineAction ) lookup table // and (int ea -> string ea) lookup table for (i = 0; i < ENGINE_ACTION_COUNT ; i++){ temp = ENGINE_ACTION_NAMES[i]; for(j = 0; j < temp.length(); j++){ temp[j] = tolower(temp[j]); } engineActionNumber[temp] = i; engineActionName[i] = temp; } // Build (string engineActionUp -> int engineActionUp ) lookup table for (i = SET_MOVE_DOWN_STOP; i < ENGINE_ACTION_UP_COUNT ; i++){ temp = ENGINE_ACTION_UP_NAMES[i - SET_MOVE_DOWN_STOP]; for(j = 0; j < temp.length(); j++){ temp[j] = tolower(temp[j]); } engineActionUpNumber[temp] = i; } if(DEBUG_USER_CONFIG){ map<string, int>::iterator p; p = engineActionUpNumber.begin(); cout << "Engine Action Up list : " << endl; while(p != engineActionUpNumber.end()){ cout << " '" << p->first << "' associated to '" << p->second << "'" << endl; p++; } cout << endl << endl; p = engineActionNumber.begin(); cout << "Engine Action list : " << endl; while(p != engineActionNumber.end()){ cout << " '" << p->first << "' associated to '" << p->second << "'" << endl; p++; } cout << "debug ea (ie invisible): " << endl; for (i = ENGINE_ACTION_DEBUG_IND; i < ENGINE_ACTION_COUNT ; i++){ if (engineActionName.find(i) != engineActionName.end()){ cout << engineActionName[i] << endl; } } }}void UserConfiguration::loadConfiguration(){ ifstream *configFile; string sLine; string sInstruction, sFirstParam, sSecondParam; char textLine[255]; int pos, firstChar, endWord, foo; int lineNumber; unsigned int i; char path[300]; // strcpy(path, rootDir); // strcat(path, CONFIG_FILE_NAME); get_config_file_name(path, 300); configFile = new ifstream(path); if(!configFile->is_open()){ cerr << "Can't open configuration file: " << path << endl; cerr << "Will create a default config file at the above location." << endl; createDefaultConfigFile(); // try to open it again delete configFile; configFile = new ifstream(path); if(!configFile->is_open()){ cout << "Error: Can't open configuration file: " << path << endl; exit(1); } } // loop through the whole configuration file lineNumber = 0; while(!configFile->eof()){ configFile -> getline(textLine, 255); sLine = textLine; /* sInstruction.clear(); sFirstParam.clear(); sSecondParam.clear(); */ sInstruction.erase(sInstruction.begin(), sInstruction.end()); sFirstParam.erase(sFirstParam.begin(), sFirstParam.end()); sSecondParam.erase(sSecondParam.begin(), sSecondParam.end()); for(i = 0; i < sLine.length(); i++){ sLine[i] = tolower(sLine[i]); } // search for keywords, ignore lines not begining with keyword or spaces endWord = -1; foo = -1; firstChar = sLine.find_first_not_of(' ', 0); pos = sLine.find("bind", 0); if(pos < 0){ pos = sLine.find("set", 0); if ((pos >= 0)&&(pos <= firstChar)){ sInstruction = "set"; sFirstParam = getNextWord(sLine, pos + 3, endWord); sSecondParam = getNextWord(sLine, endWord, foo); } } else if(pos <= firstChar){ sInstruction = "bind"; sFirstParam = getNextWord(sLine, pos + 4, endWord); sSecondParam = getNextWord(sLine, endWord, foo); } if (sFirstParam.empty() || sSecondParam.empty() || foo == endWord){ if( pos >= 0 && sInstruction.length()!=0){ cerr << "Warning : in file " << path << " missing parameter at line " << lineNumber << ", ignoring line." << endl; } } else{ if(sInstruction == "bind"){ bind(sFirstParam, sSecondParam, lineNumber); } else{ set(sFirstParam, sSecondParam, lineNumber); } } lineNumber++; } configurationChanged = true; delete configFile; }void UserConfiguration::saveConfiguration(){ ofstream *configFile; string sLine; char textLine[255]; int i; char path[300]; // strcpy(path, rootDir); // strcat(path, CONFIG_FILE_NAME); get_config_file_name(path, 300); configFile = new ofstream(path); if(!configFile->is_open()){ cout << "Error while saving " << path << endl; return; } sprintf(textLine, "Generated by Scourge version %.2f\n", SCOURGE_VERSION); writeFile(configFile, textLine); writeFile(configFile, "Modify at your own risks.\n"); writeFile(configFile, "-------------------------------------------------\n"); writeFile(configFile, "- A line not beginning with BIND or SET is ignored (spaces excepted)\n"); writeFile(configFile, "- Only one instruction per line will be processed\n"); writeFile(configFile, "- No upper/lower case distinction\n"); writeFile(configFile, "- A space is a parameter separator so replace spaces by '_' if needed in your parameters.\n"); writeFile(configFile, " Example : for 'left bracket' put 'left_bracket'\n"); writeFile(configFile, "- No specific order needed between BIND/SET commands\n\n"); writeFile(configFile, "Syntax : \n"); writeFile(configFile, "- BIND sdl_key_name engineAction\n"); writeFile(configFile, "- SET variable value\n"); writeFile(configFile, "without the '-' at the beginning\n"); writeFile(configFile, "sdl_key_names are defined in SDL.h\n"); writeFile(configFile, "engineActions and variables are defined in userconfiguration.h\n"); writeFile(configFile, "-------------------------------------------------\n\n"); writeFile(configFile, "// Bindings\n"); // save bindings for (i = 0; i < ENGINE_ACTION_DEBUG_IND ; i++){ if(keyForEngineAction.find(i)!=keyForEngineAction.end()){ sLine = "bind " + keyForEngineAction[i]; if(engineActionName.find(i)!=engineActionName.end()){ sLine = sLine + " " + engineActionName[i] + "\n"; writeFile(configFile, (char *) sLine.c_str()); } } } // save video variables writeFile(configFile, "\n// Video settings\n"); sprintf(textLine, "set fullscreen %s\n", fullscreen ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set doublebuf %s\n", doublebuf ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set stencilbuf %s\n", stencilbuf ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set hwpal %s\n", hwpal ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set resizeable %s\n", resizeable ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set force_hwsurf %s\n", force_hwsurf ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set force_swsurf %s\n", force_swsurf ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set multitexturing %s\n", Constants::multitexture ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set hwaccel %s\n", hwaccel ? "true":"false"); writeFile(configFile, textLine); sprintf(textLine, "set shadows %d // 0 : no shadows, 2 : best shadows\n", shadows); writeFile(configFile, textLine); sprintf(textLine, "set w %d\n", w); writeFile(configFile, textLine); sprintf(textLine, "set h %d\n", h); writeFile(configFile, textLine); sprintf(textLine, "set bpp %d\n", bpp); writeFile(configFile, textLine); sprintf(textLine, "\n// Game settings\n"); writeFile(configFile, textLine); sprintf(textLine, "set gamespeed %d // 0 : fastest, 4 : slowest\n", gamespeed); writeFile(configFile, textLine); sprintf(textLine, "set centermap %s\n", centermap ? "true":"false"); writeFile(configFile, textLine); delete configFile;}void UserConfiguration::setKeyForEngineAction(string keyName, int ea){ string oldKeyName; string eas; if(keyForEngineAction.find(ea)!=keyForEngineAction.end()){ oldKeyName = keyForEngineAction[ea]; if(keyDownBindings.find(oldKeyName)!=keyDownBindings.end()){ keyDownBindings.erase(oldKeyName); if(keyUpBindings.find(oldKeyName)!=keyUpBindings.end()){ keyUpBindings.erase(oldKeyName); } } keyForEngineAction[ea] = keyName; keyDownBindings[keyName] = ea; if(engineActionName.find(ea) != engineActionName.end()){ eas = engineActionName[ea]; if(engineActionUpNumber.find(eas) != engineActionUpNumber.end()){ keyUpBindings[eas] = engineActionUpNumber[eas]; } } } } // Bind sdl_name_of_key engineAction// OR Bind sdl_mouse_button engineActionvoid UserConfiguration::bind(string s1, string s2, int lineNumber){ //int i; if(DEBUG_USER_CONFIG){ cout << "line : " << lineNumber << " "; cout << "bind '" << s1 << "' '" << s2 << "'" << endl; } // for now, we trust what is written in configuration file if(engineActionNumber.find(s2) != engineActionNumber.end()){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -