Wing Commander II
Reconstructed Win32 source (1996)
Toggle main menu visibility
Loading...
Searching...
No Matches
joycal.c
Go to the documentation of this file.
1
/*
2
* Joystick calibration file and calibrated reads.
3
*
4
* Retail object 0x423480-0x423C5F (link order; see docs/ORDER.md).
5
*/
6
#include "
game.h
"
7
8
const
int
g_nJoystickUnavailableSample
= -1;
/* 0x0048e054 */
9
10
signed
char
g_acExpectedGraphicsModes
[5] = {
11
0x13, 0x0d, 4, 9, 7
12
};
/* 0x00493078 */
13
14
/* Function start: 0x423480 */
15
short
LoadJoystickCalibrationFile
(
short
horizontalRange,
16
short
verticalRange,
17
short
horizontalDeadZone,
18
short
verticalDeadZone)
19
{
20
InputDeviceSample
stickSamples[2];
21
unsigned
short
restX;
22
unsigned
short
restY;
23
unsigned
short
maxY;
24
unsigned
short
minY;
25
unsigned
short
minX;
26
int
fileError;
27
unsigned
short
maxX;
28
int
file;
29
short
activeDevice;
30
unsigned
short
passes;
31
32
activeDevice = -1;
33
/* The DOS polling calibration's starting pass count; unused here. */
34
passes = 0x40;
35
fileError = 1;
36
if
(horizontalRange == 0)
37
horizontalRange++;
38
if
(verticalRange == 0)
39
verticalRange++;
40
g_4_nControllerFailureValue
=
41
g_nJoystickUnavailableSample
;
42
SampleBothJoysticks
(stickSamples,
g_nJoystickUnavailableSample
);
43
if
(stickSamples[0].x !=
g_nJoystickUnavailableSample
&&
44
stickSamples[0].y !=
g_nJoystickUnavailableSample
)
45
activeDevice = 0;
46
else
if
(stickSamples[1].x !=
g_nJoystickUnavailableSample
&&
47
stickSamples[1].y !=
g_nJoystickUnavailableSample
)
48
activeDevice = 1;
49
50
if
(activeDevice != -1) {
51
file = _open(
"j.cal"
, 0x8000);
52
if
(file != -1) {
53
fileError = _read(file, &activeDevice, 2) <= 0;
54
if
(fileError == 0)
55
fileError = _read(file, &minX, 2) <= 0;
56
if
(fileError == 0)
57
fileError = _read(file, &minY, 2) <= 0;
58
if
(fileError == 0)
59
fileError = _read(file, &maxX, 2) <= 0;
60
if
(fileError == 0)
61
fileError = _read(file, &maxY, 2) <= 0;
62
if
(fileError == 0)
63
fileError = _read(file, &restX, 2) <= 0;
64
if
(fileError == 0)
65
fileError = _read(file, &restY, 2) <= 0;
66
g_1_nJoystickCentreXNumber
= (
unsigned
int)restX;
67
g_2_nJoystickCentreY
= (
unsigned
int)restY;
68
_close(file);
69
if
(fileError != 0)
70
_unlink(
"j.cal"
);
71
}
72
73
if
(fileError != 0) {
74
GetJoystickDevCaps
(activeDevice,
75
(
short
*)&minX,
76
(
short
*)&maxX,
77
(
short
*)&minY,
78
(
short
*)&maxY);
79
g_1_nJoystickCentreXNumber
=
80
((int)minX + (int)maxX) / 2;
81
g_2_nJoystickCentreY
=
82
((int)minY + (int)maxY) / 2;
83
}
84
85
if
(
g_1_nJoystickCentreXNumber
> 10) {
86
g_2_nJoystickCalibrationMinimumXNumber
=
87
g_1_nJoystickCentreXNumber
- 10;
88
g_2_nControllerCalibrationMaximumX
=
89
g_1_nJoystickCentreXNumber
+ 10;
90
}
else
{
91
g_2_nControllerCalibrationMaximumX
=
92
g_1_nJoystickCentreXNumber
;
93
g_2_nJoystickCalibrationMinimumXNumber
=
94
g_2_nControllerCalibrationMaximumX
;
95
}
96
if
(
g_2_nJoystickCentreY
> 10) {
97
g_nJoystickCalibrationMinimumY
=
98
g_2_nJoystickCentreY
- 10;
99
g_7_nJoystickTuningTopY
=
100
g_2_nJoystickCentreY
+ 10;
101
}
else
{
102
g_7_nJoystickTuningTopY
=
103
g_2_nJoystickCentreY
;
104
g_nJoystickCalibrationMinimumY
=
105
g_7_nJoystickTuningTopY
;
106
}
107
108
g_1_nJsRightScaleWord
=
109
g_2_nJoystickCalibrationMinimumXNumber
/
110
(int)horizontalRange;
111
g_12_nControllerLeftScaleValue
=
112
g_1_nJsRightScaleWord
;
113
g_12_nJoystickDownScaleWord
=
114
g_nJoystickCalibrationMinimumY
/
115
(int)verticalRange;
116
g_59_nJoystickUpScaleWord
=
117
g_12_nJoystickDownScaleWord
;
118
if
(
g_12_nControllerLeftScaleValue
== 0)
119
g_12_nControllerLeftScaleValue
++;
120
if
(
g_59_nJoystickUpScaleWord
== 0)
121
g_59_nJoystickUpScaleWord
++;
122
if
(
g_1_nJsRightScaleWord
== 0)
123
g_1_nJsRightScaleWord
++;
124
if
(
g_12_nJoystickDownScaleWord
== 0)
125
g_12_nJoystickDownScaleWord
++;
126
127
g_12_nJoystickMinimumXNumber
=
128
g_1_nJoystickCentreXNumber
-
129
horizontalRange *
g_12_nControllerLeftScaleValue
;
130
g_2_nJsMinimumY
=
131
g_2_nJoystickCentreY
-
132
verticalRange *
g_59_nJoystickUpScaleWord
;
133
g_4_nJoystickMaximumXValue
=
134
horizontalRange *
g_12_nControllerLeftScaleValue
+
135
g_1_nJoystickCentreXNumber
;
136
g_nJoystickMaximumY
=
137
verticalRange *
g_59_nJoystickUpScaleWord
+
138
g_2_nJoystickCentreY
;
139
g_nJoystickHorizDeadZone
= horizontalDeadZone;
140
g_nControllerVerticalDeadZoneWord
= verticalDeadZone;
141
}
142
g_8_nActiveInputDeviceWord
= activeDevice;
143
return
activeDevice;
144
}
145
146
/* Function start: 0x4238E9 */
147
short
ReadCalibratedJoystick
(
void
)
148
{
149
short
normalizedX;
150
short
normalizedY;
151
short
inputDevice;
152
153
inputDevice =
g_8_nActiveInputDeviceWord
;
154
if
(
g_8_nActiveInputDeviceWord
== -1)
155
return
0;
156
SampleJoystickDevice
(
157
&
g_aInputDeviceSamplesList
[inputDevice], inputDevice,
158
g_4_nControllerFailureValue
);
159
if
(
g_aInputDeviceSamplesList
[inputDevice].x ==
160
g_4_nControllerFailureValue
||
161
g_aInputDeviceSamplesList
[inputDevice].y ==
162
g_4_nControllerFailureValue
) {
163
g_aInputDeviceSamplesList
[inputDevice].
x
=
164
g_aInputDeviceSamplesList
[inputDevice].
y
=
165
g_aInputDeviceSamplesList
[inputDevice].
buttons
= 0;
166
g_8_nActiveInputDeviceWord
= -1;
167
return
0;
168
}
169
170
g_3_nJoystickRawX
=
171
g_aInputDeviceSamplesList
[inputDevice].
x
;
172
g_nStickRawY
=
173
g_aInputDeviceSamplesList
[inputDevice].
y
;
174
if
(
g_aInputDeviceSamplesList
[inputDevice].x <
175
g_12_nJoystickMinimumXNumber
)
176
g_aInputDeviceSamplesList
[inputDevice].
x
=
177
g_12_nJoystickMinimumXNumber
;
178
if
(
g_aInputDeviceSamplesList
[inputDevice].x >
179
g_4_nJoystickMaximumXValue
)
180
g_aInputDeviceSamplesList
[inputDevice].
x
=
181
g_4_nJoystickMaximumXValue
;
182
if
(
g_aInputDeviceSamplesList
[inputDevice].y <
183
g_2_nJsMinimumY
)
184
g_aInputDeviceSamplesList
[inputDevice].
y
=
185
g_2_nJsMinimumY
;
186
if
(
g_aInputDeviceSamplesList
[inputDevice].y >
187
g_nJoystickMaximumY
)
188
g_aInputDeviceSamplesList
[inputDevice].
y
=
189
g_nJoystickMaximumY
;
190
191
normalizedY = 0;
192
normalizedX = normalizedY;
193
if
(
g_aInputDeviceSamplesList
[inputDevice].x <
194
g_1_nJoystickCentreXNumber
) {
195
normalizedX = (short)((
g_1_nJoystickCentreXNumber
-
196
g_aInputDeviceSamplesList
[inputDevice].x) /
197
g_12_nControllerLeftScaleValue
);
198
if
(normalizedX <=
g_nJoystickHorizDeadZone
)
199
normalizedX = 0;
200
else
if
(normalizedX != 0)
201
normalizedX = (short)-normalizedX;
202
}
else
if
(
g_aInputDeviceSamplesList
[inputDevice].x >
203
g_1_nJoystickCentreXNumber
) {
204
normalizedX = (short)((
205
g_aInputDeviceSamplesList
[inputDevice].x -
206
g_1_nJoystickCentreXNumber
) /
207
g_1_nJsRightScaleWord
);
208
if
(normalizedX <=
g_nJoystickHorizDeadZone
)
209
normalizedX = 0;
210
}
211
212
if
(
g_aInputDeviceSamplesList
[inputDevice].y <
213
g_2_nJoystickCentreY
) {
214
normalizedY = (short)((
g_2_nJoystickCentreY
-
215
g_aInputDeviceSamplesList
[inputDevice].y) /
216
g_59_nJoystickUpScaleWord
);
217
if
(normalizedY <=
g_nControllerVerticalDeadZoneWord
)
218
normalizedY = 0;
219
else
if
(normalizedY != 0)
220
normalizedY = (short)-normalizedY;
221
}
else
if
(
g_aInputDeviceSamplesList
[inputDevice].y >
222
g_2_nJoystickCentreY
) {
223
normalizedY = (short)((
224
g_aInputDeviceSamplesList
[inputDevice].y -
225
g_2_nJoystickCentreY
) /
226
g_12_nJoystickDownScaleWord
);
227
if
(normalizedY <=
g_nControllerVerticalDeadZoneWord
)
228
normalizedY = 0;
229
}
230
g_aInputDeviceSamplesList
[inputDevice].
x
= (int)normalizedX;
231
g_aInputDeviceSamplesList
[inputDevice].
y
= (int)normalizedY;
232
return
1;
233
}
234
235
/* Unreferenced driver names; WC1's joystick unit has the same pair. */
236
static
char
g_szTMDriverName
[] =
"TM.DRV"
;
/* 0x00493080 */
237
static
char
g_szSuperTMDriverName
[] =
"SUPERTM.DRV"
;
/* 0x00493088 */
238
239
/* Linker commons defined here. LINK lays a unit's commons out by the
240
* identifiers' hash keys, so these spellings follow the original addresses. */
241
int
g_nJoystickCalibrationMinimumY
;
/* 0x005d2ff0 */
242
int
g_2_nJoystickCalibrationMinimumXNumber
;
/* 0x005d2ff4 */
243
int
g_3_nJoystickRawX
;
/* 0x005d2ff8 */
244
int
g_nStickRawY
;
/* 0x005d2ffc */
245
int
g_2_nControllerCalibrationMaximumX
;
/* 0x005d3000 */
246
int
g_7_nJoystickTuningTopY
;
/* 0x005d3004 */
247
char
g_nUnusedJoycalValue
;
/* 0x005d3008 */
/* never referenced */
248
unsigned
char
*
g_1_pCampaignDataBase
;
/* 0x005d300c */
ReadCalibratedJoystick
short ReadCalibratedJoystick(void)
Definition
joycal.c:147
SampleBothJoysticks
short SampleBothJoysticks(InputDeviceSample *samples, unsigned int fallback)
Definition
joystick.c:9
LoadJoystickCalibrationFile
short LoadJoystickCalibrationFile(short horizontalRange, short verticalRange, short horizontalDeadZone, short verticalDeadZone)
Definition
joycal.c:15
SampleJoystickDevice
void SampleJoystickDevice(InputDeviceSample *samples, short joystick, unsigned int fallback)
Definition
joystick.c:22
GetJoystickDevCaps
void GetJoystickDevCaps(short joystick, short *xMin, short *xMax, short *yMin, short *yMax)
Definition
winmain.c:716
game.h
Shared Win32 declarations, types and library interfaces.
g_nStickRawY
int g_nStickRawY
Definition
joycal.c:244
g_nControllerVerticalDeadZoneWord
int g_nControllerVerticalDeadZoneWord
Definition
joystick.c:58
g_12_nJoystickDownScaleWord
int g_12_nJoystickDownScaleWord
Definition
joystick.c:70
g_12_nControllerLeftScaleValue
int g_12_nControllerLeftScaleValue
Definition
joystick.c:62
g_4_nJoystickMaximumXValue
int g_4_nJoystickMaximumXValue
Definition
joystick.c:63
g_nJoystickCalibrationMinimumY
int g_nJoystickCalibrationMinimumY
Definition
joycal.c:241
g_1_nJoystickCentreXNumber
int g_1_nJoystickCentreXNumber
Definition
joystick.c:72
g_nJoystickUnavailableSample
const int g_nJoystickUnavailableSample
Definition
joycal.c:8
g_1_pCampaignDataBase
unsigned char * g_1_pCampaignDataBase
Definition
joycal.c:248
g_acExpectedGraphicsModes
signed char g_acExpectedGraphicsModes[5]
Definition
joycal.c:10
g_2_nJsMinimumY
int g_2_nJsMinimumY
Definition
joystick.c:66
g_2_nJoystickCalibrationMinimumXNumber
int g_2_nJoystickCalibrationMinimumXNumber
Definition
joycal.c:242
g_1_nJsRightScaleWord
int g_1_nJsRightScaleWord
Definition
joystick.c:69
g_aInputDeviceSamplesList
InputDeviceSample g_aInputDeviceSamplesList[2]
Definition
joystick.c:75
g_7_nJoystickTuningTopY
int g_7_nJoystickTuningTopY
Definition
joycal.c:246
g_2_nJoystickCentreY
int g_2_nJoystickCentreY
Definition
joystick.c:71
g_2_nControllerCalibrationMaximumX
int g_2_nControllerCalibrationMaximumX
Definition
joycal.c:245
g_8_nActiveInputDeviceWord
short g_8_nActiveInputDeviceWord
Definition
joystick.c:57
g_nJoystickHorizDeadZone
int g_nJoystickHorizDeadZone
Definition
joystick.c:59
g_4_nControllerFailureValue
int g_4_nControllerFailureValue
Definition
joystick.c:73
g_12_nJoystickMinimumXNumber
int g_12_nJoystickMinimumXNumber
Definition
joystick.c:65
g_3_nJoystickRawX
int g_3_nJoystickRawX
Definition
joycal.c:243
g_59_nJoystickUpScaleWord
int g_59_nJoystickUpScaleWord
Definition
joystick.c:61
g_nJoystickMaximumY
int g_nJoystickMaximumY
Definition
joystick.c:64
g_szSuperTMDriverName
static char g_szSuperTMDriverName[]
Definition
joycal.c:237
g_nUnusedJoycalValue
char g_nUnusedJoycalValue
Definition
joycal.c:247
g_szTMDriverName
static char g_szTMDriverName[]
Definition
joycal.c:236
InputDeviceSample
Definition
game.h:221
InputDeviceSample::x
int x
Definition
game.h:222
InputDeviceSample::y
int y
Definition
game.h:223
InputDeviceSample::buttons
unsigned int buttons
Definition
game.h:224
src
joycal.c
Generated by
1.18.0