Skip to content

Commit

Permalink
Saving the bindings automatically to (game)-bindings.cfg, and reading…
Browse files Browse the repository at this point in the history
… them during startup. Now custom bindings are saved and restored properly.
  • Loading branch information
skyjake committed Jul 30, 2007
1 parent d8b5bb0 commit 4252cef
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 48 deletions.
1 change: 1 addition & 0 deletions doomsday/engine/portable/include/b_class.h
Expand Up @@ -66,5 +66,6 @@ boolean B_DeleteBinding(bclass_t* bc, int bid);
boolean B_TryEvent(ddevent_t* event);
void B_PrintClasses(void);
void B_PrintAllBindings(void);
void B_WriteClassToFile(const bclass_t* bc, FILE* file);

#endif // __DOOMSDAY_BIND_CLASS_H__
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/con_config.h
Expand Up @@ -33,7 +33,7 @@

boolean Con_ParseCommands(char *fileName, boolean setdefault);
void Con_SaveDefaults(void);
boolean Con_WriteState(const char *fileName);
boolean Con_WriteState(const char *fileName, const char *bindingsFileName);

D_CMD(WriteConsole);

Expand Down
1 change: 1 addition & 0 deletions doomsday/engine/portable/include/dd_main.h
Expand Up @@ -46,6 +46,7 @@ extern int isDedicated;
extern char ddBasePath[];
extern char *defaultWads; // A list of wad names, whitespace in between (in .cfg).
extern directory_t ddRuntimeDir, ddBinDir;
extern filename_t bindingsConfigFileName;

#ifndef WIN32
extern GETGAMEAPI GetGameAPI;
Expand Down
35 changes: 34 additions & 1 deletion doomsday/engine/portable/src/b_class.c
Expand Up @@ -467,4 +467,37 @@ void B_PrintAllBindings(void)
}
}
Str_Free(str);
}
}

void B_WriteClassToFile(const bclass_t* bc, FILE* file)
{
evbinding_t* e;
controlbinding_t* c;
dbinding_t* d;
int k;
ddstring_t* str = Str_New();

// Commands.
for(e = bc->commandBinds.next; e != &bc->commandBinds; e = e->next)
{
B_EventBindingToString(e, str);
fprintf(file, "bindevent \"%s:%s\" \"", bc->name, Str_Text(str));
M_WriteTextEsc(file, e->command);
fprintf(file, "\"\n");
}

// Controls.
for(c = bc->controlBinds.next; c != &bc->controlBinds; c = c->next)
{
const char* controlName = P_PlayerControlById(c->control)->name;
for(k = 0; k < DDMAXPLAYERS; ++k)
{
for(d = c->deviceBinds[k].next; d != &c->deviceBinds[k]; d = d->next)
{
B_DeviceBindingToString(d, str);
fprintf(file, "bindcontrol local%i-%s \"%s\"\n", k + 1, controlName, Str_Text(str));
}
}
}
Str_Free(str);
}
38 changes: 9 additions & 29 deletions doomsday/engine/portable/src/b_main.c
Expand Up @@ -1769,41 +1769,21 @@ static uint writeBindList(FILE *file, binding_t *list, uint num,
return count;
}
*/

/**
* Dump all the bindings to a text (cfg) file. Outputs console commands.
*/
void B_WriteToFile(FILE *file)
{/*
uint c, d, l, count;
uint *num;
binding_t **bnd;
devcontrolbinds_t *devBinds;
inputdev_t *device;
{
int i;

for(c = 0; c < numBindClasses; ++c)
// Start with a clean slate when restoring the bindings.
fprintf(file, "clearbindings\n\n");

for(i = 0; i < B_ClassCount(); ++i)
{
for(d = 0; d < NUM_INPUT_DEVICES; ++d)
{
device = I_GetDevice(d, false);
devBinds = &devCtrlBinds[d];
for(l = 0; l < NUM_BIND_LISTS; ++l)
{
// Don't write default binds.
// if(l == BL_KEYSD || l == BL_AXESD)
// continue;
bnd = &devBinds->binds[l];
num = &devBinds->numBinds[l];
if(*bnd)
{
count = writeBindList(file, *bnd, *num, d, c);
if(count > 0)
fprintf(file,"\n");
}
}
}
}*/
B_WriteClassToFile(B_ClassByPos(i), file);
}
}

/**
Expand Down
46 changes: 32 additions & 14 deletions doomsday/engine/portable/src/con_config.c
Expand Up @@ -95,11 +95,22 @@ boolean Con_ParseCommands(char *fileName, boolean setdefault)
return true;
}

static void Con_WriteHeaderComment(FILE* file)
{
fprintf(file, "# %s / Doomsday Engine " DOOMSDAY_VERSION_TEXT "\n",
(char *) gx.GetVariable(DD_GAME_ID));
fprintf(file,
"# This configuration file is generated automatically. Each line is a\n");
fprintf(file,
"# console command. Lines beginning with # are comments. Use autoexec.cfg\n");
fprintf(file, "# for your own startup commands.\n\n");
}

/**
* Writes the state of the console (variables, bindings, aliases) into the
* given file, overwriting the previous contents.
*/
boolean Con_WriteState(const char *fileName)
boolean Con_WriteState(const char *fileName, const char *bindingsFileName)
{
unsigned int i;
cvar_t *var;
Expand All @@ -108,21 +119,14 @@ boolean Con_WriteState(const char *fileName)
void *ccmd_help;
unsigned int numCVars = Con_CVarCount();

VERBOSE(Con_Printf("Con_WriteState: %s\n", fileName));
VERBOSE(Con_Printf("Con_WriteState: %s; %s\n", fileName, bindingsFileName));

if((file = fopen(fileName, "wt")) == NULL)
{
Con_Message("Con_WriteState: Can't open %s for writing.\n", fileName);
return false;
}

fprintf(file, "# %s / Doomsday Engine " DOOMSDAY_VERSION_TEXT "\n",
(char *) gx.GetVariable(DD_GAME_ID));
fprintf(file,
"# This configuration file is generated automatically. Each line is a\n");
fprintf(file,
"# console command. Lines beginning with # are comments. Use autoexec.cfg\n");
fprintf(file, "# for your own startup commands.\n\n");
Con_WriteHeaderComment(file);

fprintf(file, "#\n# CONSOLE VARIABLES\n#\n\n");

Expand Down Expand Up @@ -156,13 +160,27 @@ boolean Con_WriteState(const char *fileName)
fprintf(file, "\n\n");
}

fprintf(file, "#\n# BINDINGS\n#\n\n");
B_WriteToFile(file);
//fprintf(file, "#\n# BINDINGS\n#\n\n");
//B_WriteToFile(file);

fprintf(file, "\n#\n# ALIASES\n#\n\n");
Con_WriteAliasesToFile(file);

fclose(file);

if(bindingsFileName)
{
// Bindings go a separate file.
if((file = fopen(bindingsFileName, "wt")) == NULL)
{
Con_Message("Con_WriteState: Can't open %s for writing.\n", bindingsFileName);
return false;
}
Con_WriteHeaderComment(file);
B_WriteToFile(file);
fclose(file);
}

return true;
}

Expand All @@ -172,12 +190,12 @@ boolean Con_WriteState(const char *fileName)
*/
void Con_SaveDefaults(void)
{
Con_WriteState(cfgFile);
Con_WriteState(cfgFile, bindingsConfigFileName);
}

D_CMD(WriteConsole)
{
Con_Message("Writing to %s...\n", argv[1]);

return !Con_WriteState(argv[1]);
return !Con_WriteState(argv[1], NULL);
}
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -279,7 +279,7 @@ void DD_SetConfigFile(char *filename)
strcpy(configFileName, filename);
Dir_FixSlashes(configFileName);
strcpy(bindingsConfigFileName, configFileName);
strcpy(bindingsConfigFileName + strlen(bindingsConfigFileName) - 5,
strcpy(bindingsConfigFileName + strlen(bindingsConfigFileName) - 4,
"-bindings.cfg");
}

Expand Down Expand Up @@ -639,7 +639,7 @@ static int DD_StartupWorker(void *parm)

Con_Message("B_Init: Init bindings.\n");
B_Init();
Con_ParseCommands(bindingsConfigFileName, true);
Con_ParseCommands(bindingsConfigFileName, false);

Con_SetProgress(125);

Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/g_controls.c
Expand Up @@ -236,7 +236,7 @@ DEFCC( CCmdDefaultGameBinds )
"bindevent mouse-right-down {impulse use}",

"bindcontrol turn joy-x",
"bindcontrol walk joy-y",
"bindcontrol walk joy-y-inverse",

NULL
};
Expand Down

0 comments on commit 4252cef

Please sign in to comment.