Wing Commander
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
joycal.c
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup shell
4 * @brief Joystick calibration and graphics setup data.
5 *
6 * Address range 431a10-431eff. The rectangle helper at its end stays in
7 * this unit provisionally.
8 */
9#include <signal.h>
10#include "wc1.h"
11#include <sys/types.h>
12
13/* 0x0046b178 */
15 0x13, 0x0d, 4, 9, 7, 0, 0, 0
16};
17/* Unreferenced driver names retained in the original initialized data. */
18/* 0x0046b180 */
19static char szTMDriverName[] = "TM.DRV";
20/* 0x0046b188 */
21static char szSuperTMDriverName[] = "SUPERTM.DRV";
22
23
24/* Linker commons defined here; LINK orders them by identifier hash. */
25signed char acShipRating[16]; /* 0x0059cd80 */
26short asObjScreenAngles[SPACE_OBJECT_COUNT]; /* 0x0059cd90 */
27short nTgtRange; /* 0x0059ce10 */
28unsigned int anVduModeCache[2]; /* 0x0059ce18 */
29signed char acObjOwnersTable[64]; /* 0x0059ce20 */
30signed char acShipTargetTable[16]; /* 0x0059ce60 */
31/* The previous frame's control inputs (DOS g_prevInputPitch/Yaw/Roll). */
34signed char acPilotHitPointsArray[16]; /* 0x0059cf00 */
35signed char acTurnRegulatorsArray[16]; /* 0x0059cf10 */
36unsigned char abCollisionAlertTargets[0x10]; /* 0x0059cf20 */
37
38/* Function start: 0x431A10 */
39void LoadJoystickCalibrationFile(short horizontalRange,
40 short verticalRange,
41 short horizontalDeadZone,
42 short verticalDeadZone)
43{
44 int rangeY;
45 int rangeX;
46 int centreX;
47 unsigned short storedCentreX;
48 unsigned short storedCentreY;
49 int centreY;
50 unsigned short maximumX;
51 unsigned short minimumY;
52 short activeDevice;
53 InputDeviceSample samples[2];
54 int file;
55 int failed;
56 unsigned short minimumX;
57 unsigned short maximumY;
58 int leftScale;
59 int upScale;
60
61 activeDevice = -1;
62 failed = 1;
63 if (horizontalRange == 0)
64 horizontalRange++;
65 if (verticalRange == 0)
66 verticalRange++;
68 SampleBothJoysticks(samples, -1);
69 if (samples[0].x != -1 && samples[0].y != -1)
70 activeDevice = 0;
71 else if (samples[1].x != -1 && samples[1].y != -1)
72 activeDevice = 1;
73
74 if (activeDevice != -1) {
75 file = _open("j.cal", 0x8000);
76 if (file != -1) {
77 failed = _read(file, &activeDevice, 2) <= 0;
78 if (failed == 0)
79 failed = _read(file, &minimumX, 2) <= 0;
80 if (failed == 0)
81 failed = _read(file, &minimumY, 2) <= 0;
82 if (failed == 0)
83 failed = _read(file, &maximumX, 2) <= 0;
84 if (failed == 0)
85 failed = _read(file, &maximumY, 2) <= 0;
86 if (failed == 0)
87 failed = _read(file, &storedCentreX, 2) <= 0;
88 if (failed == 0)
89 failed = _read(file, &storedCentreY, 2) <= 0;
90 nJoystickCentreX = (unsigned int)storedCentreX;
91 nJoystickCentreY = (unsigned int)storedCentreY;
92 _close(file);
93 if (failed != 0)
94 _unlink("j.cal");
95 }
96
97 if (failed != 0) {
98 GetJoystickDevCaps(activeDevice,
99 &minimumX,
100 &maximumX,
101 &minimumY,
102 &maximumY);
103 centreX = ((int)minimumX + (int)maximumX) / 2;
104 centreY = ((int)maximumY + (int)minimumY) / 2;
105 } else {
106 centreX = nJoystickCentreX;
107 centreY = nJoystickCentreY;
108 }
109
110 if (centreX > 10) {
111 nJoystickCalibrationMinimumX = centreX - 10;
112 nJoystickCalibrationMaximumX = centreX + 10;
113 } else {
116 }
117 if (centreY > 10) {
118 nJoystickCalibrationMinimumY = centreY - 10;
119 nJoystickCalibrationMaxY = centreY + 10;
120 } else {
121 nJoystickCalibrationMaxY = centreY;
123 }
124
125 rangeX = horizontalRange;
126 rangeY = verticalRange;
127 leftScale = nJoystickCalibrationMinimumX / rangeX;
128 upScale = nJoystickCalibrationMinimumY / rangeY;
129 nCurrentJoystickLeftScale = leftScale;
130 nJoystickUpScale = upScale;
133 if (nJoystickUpScale == 0)
135 nJoystickRightScale = leftScale;
136 if (nJoystickRightScale == 0)
138 nJoystickDownScaleValue = upScale;
141
142 nJoystickCentreX = centreX;
143 leftScale = nCurrentJoystickLeftScale;
144 rangeX = horizontalRange * leftScale;
145 upScale = nJoystickUpScale;
146 rangeY = verticalRange * upScale;
147 nJoystickCentreY = centreY;
148 nStickMinimumPositionX = centreX - rangeX;
149 nStickMinimumPositionY = centreY - rangeY;
150 nStickMaxX = rangeX + centreX;
151 nStickMaxY = rangeY + centreY;
152 nJoystickHorizontalDeadZone = horizontalDeadZone;
153 nJoystickVertDeadZoneValue = verticalDeadZone;
154 }
155 nCurrentActiveInputDevice = activeDevice;
156}
157
158/* Function start: 0x431D20 */
160{
161 short normalizedX;
162 short normalizedY;
163 short device;
164 int result;
165
167 if (device == -1)
168 return 0;
169 result = SampleJoystickDevice(&aInputDevSamples[device], device,
173 result != 0) {
175 aInputDevSamples[device].x =
176 aInputDevSamples[device].y =
177 aInputDevSamples[device].buttons = 0;
178 return 0;
179 }
180
185 if (aInputDevSamples[device].x > nStickMaxX)
186 aInputDevSamples[device].x = nStickMaxX;
189 if (aInputDevSamples[device].y > nStickMaxY)
190 aInputDevSamples[device].y = nStickMaxY;
191
192 normalizedY = 0;
193 normalizedX = normalizedY;
194 if (aInputDevSamples[device].x < nJoystickCentreX) {
195 int sample;
196 int centre;
197 int scale;
198
199 sample = aInputDevSamples[device].x;
200 centre = nJoystickCentreX;
202 normalizedX = (short)((centre - sample) / scale);
203 if (nJoystickHorizontalDeadZone < normalizedX) {
204 if (normalizedX != 0)
205 normalizedX = (short)-normalizedX;
206 } else {
207 normalizedX = 0;
208 }
209 } else if (aInputDevSamples[device].x > nJoystickCentreX) {
210 int sample;
211 int centre;
212 int scale;
213
214 sample = aInputDevSamples[device].x;
215 centre = nJoystickCentreX;
216 scale = nJoystickRightScale;
217 normalizedX = (short)((sample - centre) / scale);
218 if (normalizedX <= nJoystickHorizontalDeadZone)
219 normalizedX = 0;
220 }
221
222 if (aInputDevSamples[device].y < nJoystickCentreY) {
223 int sample;
224 int centre;
225 int scale;
226
227 sample = aInputDevSamples[device].y;
228 centre = nJoystickCentreY;
229 scale = nJoystickUpScale;
230 normalizedY = (short)((centre - sample) / scale);
231 if (nJoystickVertDeadZoneValue < normalizedY) {
232 if (normalizedY != 0)
233 normalizedY = (short)-normalizedY;
234 } else {
235 normalizedY = 0;
236 }
237 } else if (aInputDevSamples[device].y > nJoystickCentreY) {
238 int sample;
239 int centre;
240 int scale;
241
242 sample = aInputDevSamples[device].y;
243 centre = nJoystickCentreY;
245 normalizedY = (short)((sample - centre) / scale);
246 if (normalizedY <= nJoystickVertDeadZoneValue)
247 normalizedY = 0;
248 }
249 aInputDevSamples[device].x = (int)normalizedX;
250 aInputDevSamples[device].y = (int)normalizedY;
251 return 1;
252}
253
254/* Function start: 0x431EA0 */
255void __stdcall UnionRectBounds(ShortRect *destination,
256 const ShortRect *first,
257 const ShortRect *second)
258{
259 destination->left = first->left < second->left ?
260 first->left : second->left;
261 destination->top = first->top < second->top ?
262 first->top : second->top;
263 destination->right = first->right > second->right ?
264 first->right : second->right;
265 destination->bottom = first->bottom > second->bottom ?
266 first->bottom : second->bottom;
267}
InputDeviceSample aInputDevSamples[2]
Definition nav.c:78
signed char acShipRating[16]
Definition joycal.c:25
int nJoystickCalibrationMinimumY
Definition music.c:91
int nJoystickCentreY
Definition nav.c:75
int nJoystickCalibrationMinimumX
Definition music.c:92
short asObjScreenAngles[SPACE_OBJECT_COUNT]
Definition joycal.c:26
short anObjectYawRotation[SPACE_OBJECT_COUNT]
Definition joycal.c:33
unsigned int anVduModeCache[2]
Definition joycal.c:28
int nCurrentJoystickFailureValue
Definition nav.c:77
int nJoystickCentreX
Definition nav.c:76
short nTgtRange
Definition joycal.c:27
int nStickMaxX
Definition nav.c:65
int nJoystickDownScaleValue
Definition nav.c:74
ShortVector stPreviousRotationInput
Definition joycal.c:32
int nStickMinimumPositionX
Definition nav.c:67
int nJoystickRightScale
Definition nav.c:73
int nCurrentJoystickLeftScale
Definition nav.c:64
int nJoystickCalibrationMaxY
Definition music.c:94
int nJoystickHorizontalDeadZone
Definition nav.c:62
int nStickMinimumPositionY
Definition nav.c:68
int nCurrentJoystickRawCoordX
Definition nav.c:69
signed char acGraphicsModeByAnimationSpeed[8]
Definition joycal.c:14
int nJoystickUpScale
Definition nav.c:63
signed char acObjOwnersTable[64]
Definition joycal.c:29
short nCurrentActiveInputDevice
Definition nav.c:60
unsigned char abCollisionAlertTargets[0x10]
Definition joycal.c:36
int nJoystickVertDeadZoneValue
Definition nav.c:61
int nCurrentJoystickRawCoordY
Definition nav.c:70
signed char acShipTargetTable[16]
Definition joycal.c:30
int nStickMaxY
Definition nav.c:66
signed char acPilotHitPointsArray[16]
Definition joycal.c:34
int nJoystickCalibrationMaximumX
Definition music.c:93
signed char acTurnRegulatorsArray[16]
Definition joycal.c:35
static char szTMDriverName[]
Definition joycal.c:19
static char szSuperTMDriverName[]
Definition joycal.c:21
unsigned int buttons
Definition wc1base.h:152
short top
Definition wcdata.h:441
short left
Definition wcdata.h:440
short right
Definition wcdata.h:442
short bottom
Definition wcdata.h:443
Umbrella header for the game core.
short __stdcall SampleBothJoysticks(InputDeviceSample *samples, short fallback)
Definition mission.c:945
void GetJoystickDevCaps(short joystick, unsigned short *xMin, unsigned short *xMax, unsigned short *yMin, unsigned short *yMax)
Definition winmain.c:1012
void LoadJoystickCalibrationFile(short horizontalRange, short verticalRange, short horizontalDeadZone, short verticalDeadZone)
Definition joycal.c:39
short ReadCalibratedJoystick(void)
Definition joycal.c:159
void __stdcall UnionRectBounds(ShortRect *destination, const ShortRect *first, const ShortRect *second)
Definition joycal.c:255
int __stdcall SampleJoystickDevice(InputDeviceSample *samples, short joystick, unsigned short fallback)
Definition mission.c:958
#define SPACE_OBJECT_COUNT
Definition wcdata.h:1004