summaryrefslogtreecommitdiffstats
path: root/prefs.c
blob: b53bf995491119d0a6372850e9517411c28afa81 (plain)
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
/*
 * prefs.c
 *
 * Select preferences
 */

#include "graphics.h"
#include "grv.h"

/*
 * Keyboard map
 */

SDLKey kbd_keys[NKEYS] = {
  SDLK_w,			/* Up */
  SDLK_a,			/* Left */
  SDLK_s,			/* Right */
  SDLK_z,			/* Down */
  SDLK_u,			/* Shoot up */
  SDLK_h,			/* Shoot left */
  SDLK_j,			/* Shoot right */
  SDLK_n,			/* Shoot down */
  SDLK_SPACE,			/* Stop */
  SDLK_ESCAPE,			/* Escape */
  SDLK_F1,			/* Pause */
};

static void load_keys(void)
{
  FILE *f;
  unsigned long keycode;
  int i;

  f = fopen("grvkeys.dat", "rt");
  if ( !f )
    return;

  for ( i = 0 ; i < NKEYS ; i++ ) {
    if ( fscanf(f, "%lu", &keycode) != 1 )
      break;
    kbd_keys[i] = keycode;
  }
  fclose(f);
}

static void save_keys(void)
{
  FILE *f;
  int i;

  f = fopen("grvkeys.dat", "wt");
  if ( !f )
    return;

  for ( i = 0 ; i < NKEYS ; i++ ) {
    fprintf(f, "%lu\n", (unsigned long)kbd_keys[i]);
  }
  fclose(f);
}

void load_prefs(void)
{
  load_keys();
}

void set_keys(void)
{
  int i, j;
  int bad;

  initscreen(40);

  lprint( 3, 6, "Move up . . . . . .");
  lprint( 4, 6, "Move left . . . . .");
  lprint( 5, 6, "Move right  . . . .");
  lprint( 6, 6, "Move down . . . . .");
  lprint( 7, 6, "Shoot up  . . . . .");
  lprint( 8, 6, "Shoot left  . . . .");
  lprint( 9, 6, "Shoot right . . . .");
  lprint(10, 6, "Shoot down  . . . .");
  lprint(11, 6, "Dead stop . . . . .");
  lprint(12, 6, "Quick escape  . . .");
  lprint(13, 6, "Pause game  . . . .");

  while ( 1 ) {
    bad = 0;

    color(0,gp.c);
    for ( i = 0 ; i < NKEYS ; i++ )
      lprintf(i+3, 26, "%-10s", SDL_GetKeyName(kbd_keys[i]));

    for ( i = 0 ; i < NKEYS ; i++ ) {
      color(0,gp.c2);
      lprintf(i+3, 26, "%-10s", SDL_GetKeyName(kbd_keys[i]));

      kbd_keys[i] = get_key()->keysym.sym;
      color(0,gp.c);
      lprintf(i+3, 26, "%-10s", SDL_GetKeyName(kbd_keys[i]));
      lprint(20,5,"                      ");

      for ( j = 0 ; j < i ; j++ )
	if ( kbd_keys[i] == kbd_keys[j] )
	  bad = 1;
    }

    if ( bad ) {
      color(15,gp.c);
      lprint(20,5,"ERROR: Duplicated keys");
    } else {
      save_keys();

      color(0,gp.c);
      lprint(20,5,"Edit again? (Y/N) ");

      while ( !bad ) {
	switch ( get_key()->keysym.sym ) {
	case SDLK_y:
	  bad = 1;
	  break;
	case SDLK_n:
	  return;
	default:
	  break;
	}
      }
    }
  }
}