mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 16:05:44 -07:00
Now that we have string-encodable game parameters, let's support a
command-line argument which is either a set of parameters or a params+seed game ID. [originally from svn r4234]
This commit is contained in:
20
gtk.c
20
gtk.c
@ -762,7 +762,7 @@ static void add_menu_separator(GtkContainer *cont)
|
|||||||
gtk_widget_show(menuitem);
|
gtk_widget_show(menuitem);
|
||||||
}
|
}
|
||||||
|
|
||||||
static frontend *new_window(void)
|
static frontend *new_window(char *game_id, char **error)
|
||||||
{
|
{
|
||||||
frontend *fe;
|
frontend *fe;
|
||||||
GtkBox *vbox;
|
GtkBox *vbox;
|
||||||
@ -774,6 +774,14 @@ static frontend *new_window(void)
|
|||||||
|
|
||||||
time(&t);
|
time(&t);
|
||||||
fe->me = midend_new(fe, &t, sizeof(t));
|
fe->me = midend_new(fe, &t, sizeof(t));
|
||||||
|
if (game_id) {
|
||||||
|
*error = midend_game_id(fe->me, game_id, FALSE);
|
||||||
|
if (*error) {
|
||||||
|
midend_free(fe->me);
|
||||||
|
sfree(fe);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
midend_new_game(fe->me);
|
midend_new_game(fe->me);
|
||||||
|
|
||||||
fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
@ -944,8 +952,16 @@ static frontend *new_window(void)
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
char *pname = argv[0];
|
||||||
|
char *error;
|
||||||
|
|
||||||
gtk_init(&argc, &argv);
|
gtk_init(&argc, &argv);
|
||||||
(void) new_window();
|
|
||||||
|
if (!new_window(argc > 1 ? argv[1] : NULL, &error)) {
|
||||||
|
fprintf(stderr, "%s: %s\n", pname, error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
gtk_main();
|
gtk_main();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
84
midend.c
84
midend.c
@ -373,9 +373,62 @@ config_item *midend_get_config(midend_data *me, int which, char **wintitle)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *midend_game_id(midend_data *me, char *id, int def_seed)
|
||||||
|
{
|
||||||
|
char *error, *par, *seed;
|
||||||
|
game_params *params;
|
||||||
|
|
||||||
|
seed = strchr(id, ':');
|
||||||
|
|
||||||
|
if (seed) {
|
||||||
|
/*
|
||||||
|
* We have a colon separating parameters from game seed. So
|
||||||
|
* `par' now points to the parameters string, and `seed' to
|
||||||
|
* the seed string.
|
||||||
|
*/
|
||||||
|
*seed++ = '\0';
|
||||||
|
par = id;
|
||||||
|
} else {
|
||||||
|
/*
|
||||||
|
* We only have one string. Depending on `def_seed', we
|
||||||
|
* take it to be either parameters or seed.
|
||||||
|
*/
|
||||||
|
if (def_seed) {
|
||||||
|
seed = id;
|
||||||
|
par = NULL;
|
||||||
|
} else {
|
||||||
|
seed = NULL;
|
||||||
|
par = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (par) {
|
||||||
|
params = decode_params(par);
|
||||||
|
error = validate_params(params);
|
||||||
|
if (error) {
|
||||||
|
free_params(params);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
free_params(me->params);
|
||||||
|
me->params = params;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seed) {
|
||||||
|
error = validate_seed(me->params, seed);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
sfree(me->seed);
|
||||||
|
me->seed = dupstr(seed);
|
||||||
|
me->fresh_seed = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
char *midend_set_config(midend_data *me, int which, config_item *cfg)
|
char *midend_set_config(midend_data *me, int which, config_item *cfg)
|
||||||
{
|
{
|
||||||
char *error, *p;
|
char *error;
|
||||||
game_params *params;
|
game_params *params;
|
||||||
|
|
||||||
switch (which) {
|
switch (which) {
|
||||||
@ -393,36 +446,9 @@ char *midend_set_config(midend_data *me, int which, config_item *cfg)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case CFG_SEED:
|
case CFG_SEED:
|
||||||
|
error = midend_game_id(me, cfg[0].sval, TRUE);
|
||||||
/*
|
|
||||||
* The game ID will often (in fact, mostly) have a prefix
|
|
||||||
* containing a string-encoded parameter specification
|
|
||||||
* followed by a colon. So first find the colon, and then
|
|
||||||
* split the string up.
|
|
||||||
*/
|
|
||||||
p = strchr(cfg[0].sval, ':');
|
|
||||||
|
|
||||||
if (p) {
|
|
||||||
*p++ = '\0'; /* p now points to game seed */
|
|
||||||
params = decode_params(cfg[0].sval);
|
|
||||||
error = validate_params(params);
|
|
||||||
if (error) {
|
|
||||||
free_params(params);
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
free_params(me->params);
|
|
||||||
me->params = params;
|
|
||||||
} else
|
|
||||||
p = cfg[0].sval;
|
|
||||||
|
|
||||||
error = validate_seed(me->params, p);
|
|
||||||
if (error)
|
if (error)
|
||||||
return error;
|
return error;
|
||||||
|
|
||||||
sfree(me->seed);
|
|
||||||
me->seed = dupstr(p);
|
|
||||||
me->fresh_seed = TRUE;
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,6 +127,7 @@ int midend_wants_statusbar(midend_data *me);
|
|||||||
enum { CFG_SETTINGS, CFG_SEED };
|
enum { CFG_SETTINGS, CFG_SEED };
|
||||||
config_item *midend_get_config(midend_data *me, int which, char **wintitle);
|
config_item *midend_get_config(midend_data *me, int which, char **wintitle);
|
||||||
char *midend_set_config(midend_data *me, int which, config_item *cfg);
|
char *midend_set_config(midend_data *me, int which, config_item *cfg);
|
||||||
|
char *midend_game_id(midend_data *me, char *id, int def_seed);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* malloc.c
|
* malloc.c
|
||||||
|
23
windows.c
23
windows.c
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <ctype.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@ -309,7 +310,7 @@ void activate_timer(frontend *fe)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static frontend *new_window(HINSTANCE inst)
|
static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
|
||||||
{
|
{
|
||||||
frontend *fe;
|
frontend *fe;
|
||||||
int x, y;
|
int x, y;
|
||||||
@ -322,6 +323,15 @@ static frontend *new_window(HINSTANCE inst)
|
|||||||
time(&t);
|
time(&t);
|
||||||
fe->me = midend_new(fe, &t, sizeof(t));
|
fe->me = midend_new(fe, &t, sizeof(t));
|
||||||
|
|
||||||
|
if (game_id) {
|
||||||
|
*error = midend_game_id(fe->me, game_id, FALSE);
|
||||||
|
if (*error) {
|
||||||
|
midend_free(fe->me);
|
||||||
|
sfree(fe);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fe->inst = inst;
|
fe->inst = inst;
|
||||||
midend_new_game(fe->me);
|
midend_new_game(fe->me);
|
||||||
midend_size(fe->me, &x, &y);
|
midend_size(fe->me, &x, &y);
|
||||||
@ -1008,6 +1018,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
|
|||||||
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||||
{
|
{
|
||||||
MSG msg;
|
MSG msg;
|
||||||
|
char *error;
|
||||||
|
|
||||||
InitCommonControls();
|
InitCommonControls();
|
||||||
|
|
||||||
@ -1028,7 +1039,15 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
|||||||
RegisterClass(&wndclass);
|
RegisterClass(&wndclass);
|
||||||
}
|
}
|
||||||
|
|
||||||
new_window(inst);
|
while (*cmdline && isspace(*cmdline))
|
||||||
|
cmdline++;
|
||||||
|
|
||||||
|
if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
|
||||||
|
char buf[128];
|
||||||
|
sprintf(buf, "%.100s Error", game_name);
|
||||||
|
MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
while (GetMessage(&msg, NULL, 0, 0)) {
|
while (GetMessage(&msg, NULL, 0, 0)) {
|
||||||
DispatchMessage(&msg);
|
DispatchMessage(&msg);
|
||||||
|
Reference in New Issue
Block a user