-
Notifications
You must be signed in to change notification settings - Fork 13
/
keyboard.c
154 lines (136 loc) · 3.82 KB
/
keyboard.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Digger Remastered
Copyright (c) Andrew Jenner 1998-2004 */
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "draw_api.h"
#include "drawing.h"
#include "hardware.h"
#include "input.h"
#include "ini.h"
#include "keyboard.h"
#include "main.h"
#include "game.h"
const char *keynames[NKEYS]={"Right","Up","Left","Down","Fire",
"Right","Up","Left","Down","Fire",
"Cheat","Accel","Brake","Music","Sound","Exit","Pause",
"Mode Change","Save DRF"};
#define FINDKEY_EX(i) {if (prockey(i) == -1) return;}
static int prockey(int kn)
{
int16_t key;
key = getkey(true);
if (kn != DKEY_EXT && key == keycodes[DKEY_EXT][0])
return -1;
keycodes[kn][0] = key;
return (0);
}
void redefkeyb(struct digger_draw_api *ddap, bool allf)
{
int i,j,k,l,z,y=0,x,savey;
bool f;
char kbuf[80],vbuf[80];
maininit();
outtext(ddap, "PRESS NEW KEY FOR",0,y,3);
y+=CHR_H;
if (dgstate.diggers==2) {
outtext(ddap, "PLAYER 1:",0,y,3);
y+=CHR_H;
}
/* Step one: redefine keys that are always redefined. */
savey = y;
for (i=0;i<5;i++) {
outtext(ddap, keynames[i],0,y,2); /* Red first */
FINDKEY_EX(i);
outtext(ddap, keynames[i],0,y,1); /* Green once got */
y+=CHR_H;
for (j=0;j<i;j++) { /* Note: only check keys just pressed (I hate it when
this is done wrong, and it often is.) */
if (keycodes[i][0]==keycodes[j][0] && keycodes[i][0]!=0) {
i--;
y-=CHR_H;
break;
}
for (k=2;k<5;k++)
for (l=2;l<5;l++)
if (keycodes[i][k]==keycodes[j][l] && keycodes[i][k]!=-2) {
j=i;
k=5;
i--;
y-=CHR_H;
break; /* Try again if this key already used */
}
}
}
if (dgstate.diggers==2) {
outtext(ddap, "PLAYER 2:",0,y,3);
y+=CHR_H;
for (i=5;i<10;i++) {
outtext(ddap, keynames[i],0,y,2); /* Red first */
FINDKEY_EX(i);
outtext(ddap, keynames[i],0,y,1); /* Green once got */
y+=CHR_H;
for (j=0;j<i;j++) { /* Note: only check keys just pressed (I hate it when
this is done wrong, and it often is.) */
if (keycodes[i][0]==keycodes[j][0] && keycodes[i][0]!=0) {
i--;
y-=CHR_H;
break;
}
for (k=2;k<5;k++)
for (l=2;l<5;l++)
if (keycodes[i][k]==keycodes[j][l] && keycodes[i][k]!=-2) {
j=i;
k=5;
i--;
y-=CHR_H;
break; /* Try again if this key already used */
}
}
}
}
/* Step two: redefine other keys which step one has caused to conflict */
if (allf) {
outtext(ddap, "OTHER:",0,y,3);
y+=CHR_H;
}
z=0;
x=0;
y-=CHR_H;
for (i=10;i<NKEYS;i++) {
f=false;
for (j=0;j<10;j++)
for (k=0;k<5;k++)
for (l=2;l<5;l++)
if (keycodes[i][k]==keycodes[j][l] && keycodes[i][k]!=-2)
f=true;
for (j=10;j<i;j++)
for (k=0;k<5;k++)
for (l=0;l<5;l++)
if (keycodes[i][k]==keycodes[j][l] && keycodes[i][k]!=-2)
f=true;
if (f || (allf && i!=z)) {
if (i!=z)
y+=CHR_H;
if (y >= MAX_H - CHR_H) {
y = savey;
x = (MAX_TEXT_LEN / 2) * CHR_W;
}
outtext(ddap, keynames[i],x,y,2); /* Red first */
FINDKEY_EX(i);
outtext(ddap, keynames[i],x,y,1); /* Green once got */
z=i;
i--;
}
}
/* Step three: save the INI file */
for (i=0;i<NKEYS;i++)
if (krdf[i]) {
sprintf(kbuf,"%s%c",keynames[i],(i>=5 && i<10) ? '2' : 0);
sprintf(vbuf,"%i/%i/%i/%i/%i",keycodes[i][0],keycodes[i][1],
keycodes[i][2],keycodes[i][3],keycodes[i][4]);
WriteINIString(INI_KEY_SETTINGS,kbuf,vbuf,ININAME);
}
}