Digimon World 3 decomp
The European version, SLES_039.36
Loading...
Searching...
No Matches
pad.c
Go to the documentation of this file.
1#include "engine/game.h"
2#include <libpad.h>
3
4/* The vsyncs a held button takes to repeat, unless initPad is told otherwise */
5#define DEFAULT_REPEAT_RATE 0x10
6
7/* Opens the pads (PadInitMtap or PadInitDirect); repeatRate 0 means DEFAULT_REPEAT_RATE */
8void initPad(s32 multitap, s32 repeatRate) {
9 s32 i;
10 s32 j;
11 s16 count;
12
14 HEAP.fill(PAD.act, 0xFF, sizeof(PAD.act));
15 for (i = 0; i < 2; i++) {
16 for (j = 0; j < 4; j++) {
17 resetButtonMap((i * 16 + j) & 0xFF);
18 }
19 }
20 if (multitap != 0) {
21 PadInitMtap(PAD.buf[0].bytes, PAD.buf[1].bytes);
23 } else {
24 PadInitDirect(PAD.buf[0].bytes, PAD.buf[1].bytes);
25 }
26 repeatRate &= 0x7F;
27 PAD.repeatRate = (u8)repeatRate;
28 count = (u8)repeatRate;
30 if (count == 0) {
32 }
33 startPad();
34}
35
36/* Stops reading the pads and forgets every flag, so the next startPad opens them again */
37void shutdownPad(void) {
38 stopPad();
39 PAD.flags = 0;
40}
41
42/* Opens the pads with the defaults if needed, then starts reading them every vsync */
43void startPad(void) {
46 }
47 if (!(PAD.flags & PAD_FLAG_STARTED)) {
48 PadStartCom();
50 }
51}
52
53/* Stops reading the pads every vsync */
54void stopPad(void) {
56 PadStopCom();
57 }
58 PAD.flags &= ~PAD_FLAG_STARTED;
59}
60
61/* Reads every pad, or the demo data while a demo plays */
62void updatePad(void) {
64 s32 ret;
65 s32 i;
66 s32 j;
67 u16 port;
68
69 ret = PadChkVsync();
70 if (ret != 1) {
71 return;
72 }
75 return;
76 }
77 for (i = 0; i < 2; i++) {
78 port = i * 16;
79 if (PAD.buf[i].tap.id == PAD_MULTITAP_ID) {
80 for (j = 0; j < 4; j++) {
82 readPadButtons((u8)port, &PAD.buf[i].tap.pads[j], &record->tap.pads[j]);
83 } else {
84 readPad((u8)(port + j), &PAD.buf[i].tap.pads[j]);
85 }
86 }
87 } else {
89 readPadButtons((u8)port, &PAD.buf[i].pad, &record->pad);
90 } else {
91 readPad((u8)port, &PAD.buf[i].pad);
92 }
93 }
94 }
95}
96
97/* Starts a motor for `time` vsyncs (0 stops it) */
98s32 setVibration(u16 port, s32 motor, s16 time, u8 value) {
99 u8 id = port;
100 s32 mode;
101 PadSlot *slot;
102 s16 t;
103 s32 pad;
104
105 t = time;
106 if (!(PAD.flags & PAD_FLAG_VIBRATION)) {
107 return 0;
108 }
109 if (pollPadState(id) == 0) {
110 return 0;
111 }
112 mode = PadInfoMode(id, InfoModeCurExID, 0);
113 pad = id >> 4;
114 slot = &PAD.slots[pad][port & 3];
115 if (mode == PAD_ID_DIGITAL || mode == PAD_ID_ANALOG) {
116 PAD.act[pad][motor & 1] = value;
117 } else {
118 PAD.act[pad][0] = 0x40;
119 PAD.act[pad][1] = 1;
120 }
121 if (slot->vibrationTimers[motor] <= 0) {
122 slot->vibrationTimers[motor] = t;
123 } else if (t == 0) {
124 slot->vibrationTimers[motor] = 0;
125 }
126 PadSetAct(id, PAD.act[pad], 2);
127 return 1;
128}
129
130/* The logical buttons newly pressed on pad 1 or 2 this frame */
132 return PAD.slots[pad][0].pressed;
133}
134
135/* The logical buttons held on pad 1 or 2 */
137 return PAD.slots[pad][0].held;
138}
139
140/* The logical buttons pressed on pad 1 or 2, repeating while held */
142 return PAD.slots[pad][0].repeated;
143}
144
145/* Gives a pad slot the default button map, where button i is bit i */
146void resetButtonMap(u16 port) {
147 s32 i;
148
149 for (i = 0; i < 16; i++) {
150 PAD.slots[(u8)port >> 4][port & 3].buttonMap[i] = DEFAULT_BUTTON_MAP[i];
151 }
152}
153
154/* Swaps two logical buttons in a pad slot's button map */
155void swapButtons(u16 port, s32 a, s32 b) {
156 u8 bit = PAD.slots[(u8)port >> 4][port & 3].buttonMap[a];
157
158 PAD.slots[(u8)port >> 4][port & 3].buttonMap[a] = PAD.slots[(u8)port >> 4][port & 3].buttonMap[b];
159 PAD.slots[(u8)port >> 4][port & 3].buttonMap[b] = bit;
160}
161
162/* The bit that logical button `index` sets on pad 1 or 2 */
163s32 getButtonBit(s32 pad, s32 index) {
164 return PAD.slots[pad][0].buttonMap[index];
165}
166
167/* Controller input: cleared by initPad, then the methods */
176
177/* The identity: logical button i is bit i */
179 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
180 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
181};
#define NULL
Definition common.h:17
signed int s32
Definition common.h:11
signed short s16
Definition common.h:9
unsigned short u16
Definition common.h:10
unsigned char u8
Definition common.h:8
s32 startDemoRecording(void)
Demo recording was left out of the release: it never starts.
Definition demo.c:5
s32 isDemoRecording(s32 pad)
1 while pad pad records a demo, -1 while another pad does, 0 otherwise (never set)
Definition demo.c:14
void stopDemoRecording(void)
Does nothing: demo recording was left out.
Definition demo.c:10
void stopDemoPlayback(void)
Gives the pads back to the players.
Definition demo.c:40
s32 isDemoPlaying(s32 pad)
1 while pad pad replays a demo, -1 while another pad does, 0 otherwise
Definition demo.c:50
s32 startDemoPlayback(s16 pad, PadBuffer *data)
Replays data (one raw buffer a frame) as the input of pad.
Definition demo.c:25
Heap HEAP
The functions that return what a call left in v0 (allocMemHigh, findTask) and the ones that take othe...
Definition heap.c:184
s32 getButtonBit(s32 pad, s32 index)
The bit that logical button index sets on pad 1 or 2.
Definition pad.c:163
s32 setVibration(u16 port, s32 motor, s16 time, u8 value)
Starts a motor for time vsyncs (0 stops it)
Definition pad.c:98
void updatePad(void)
Reads every pad, or the demo data while a demo plays.
Definition pad.c:62
void shutdownPad(void)
Stops reading the pads and forgets every flag, so the next startPad opens them again.
Definition pad.c:37
s32 getPadHeld(s32 pad)
The logical buttons held on pad 1 or 2.
Definition pad.c:136
void initPad(s32 multitap, s32 repeatRate)
Opens the pads (PadInitMtap or PadInitDirect); repeatRate 0 means DEFAULT_REPEAT_RATE.
Definition pad.c:8
s32 getPadPressed(s32 pad)
The logical buttons newly pressed on pad 1 or 2 this frame.
Definition pad.c:131
PadState PAD
Controller input: cleared by initPad, then the methods.
Definition pad.c:168
void stopPad(void)
Stops reading the pads every vsync.
Definition pad.c:54
u8 DEFAULT_BUTTON_MAP[16]
The identity: logical button i is bit i.
Definition pad.c:178
s32 getPadRepeated(s32 pad)
The logical buttons pressed on pad 1 or 2, repeating while held.
Definition pad.c:141
void swapButtons(u16 port, s32 a, s32 b)
Swaps two logical buttons in a pad slot's button map.
Definition pad.c:155
void startPad(void)
Opens the pads with the defaults if needed, then starts reading them every vsync.
Definition pad.c:43
void resetButtonMap(u16 port)
Gives a pad slot the default button map, where button i is bit i.
Definition pad.c:146
#define DEFAULT_REPEAT_RATE
The vsyncs a held button takes to repeat, unless initPad is told otherwise.
Definition pad.c:5
#define PAD_FLAG_STARTED
PadStartCom done.
Definition pad.h:97
#define PAD_FLAG_MULTITAP
Definition pad.h:99
#define PAD_ID_ANALOG
Definition pad.h:37
#define PAD_FLAG_VIBRATION
the actuators are aligned
Definition pad.h:96
#define PAD_FLAG_DEMO_PLAYBACK
PadState.flags: bits 0-7 hold the mode lock of each port/slot (lockPadMode), the rest are these.
Definition pad.h:93
#define PAD_DATA_SIZE
The bytes PadState keeps before its methods, which initPad clears.
Definition pad.h:102
#define PAD_MULTITAP_ID
The second byte of the raw buffer when a multitap answers.
Definition pad.h:40
#define PAD_ID_DIGITAL
PadInfoMode(InfoModeCurExID) of a DualShock in digital and analog mode.
Definition pad.h:36
#define DEMO_FRAME_COUNT
A demo replays recorded pad data (one raw buffer a frame, up to DEMO_FRAME_COUNT frames) instead of t...
Definition pad.h:108
#define PAD_FLAG_INITIALIZED
Definition pad.h:98
s32 pollPadState(u32 port)
The libpad state of a port while it can be read (0 otherwise); a stable pad gets its actuators aligne...
Definition port.c:8
s32 readPad(u16 port, PadData *data)
Reads one pad (vibration included), or clears its buttons when it is missing.
Definition port.c:144
void readPadButtons(s32 port, PadData *data, PadData *record)
Builds held/pressed/repeated from the raw data (the demo record replaces all but Start)
Definition port.c:40
s32 lockPadMode(s32 port, s32 lock)
Locks or unlocks the analog button of a DualShock, keeping its current mode.
Definition port.c:163
void(* fill)(void *ptr, s32 value, s32 size)
Definition heap.h:33
void(* zero)(void *ptr, s32 size)
Definition heap.h:32
One controller: a port, or one of the four multitap slots behind it.
Definition pad.h:77
u16 repeated
pressed, or held long enough to repeat
Definition pad.h:79
u16 held
Definition pad.h:81
u16 pressed
this frame
Definition pad.h:78
s16 vibrationTimers[2]
Definition pad.h:86
u8 buttonMap[16]
Definition pad.h:85
Controller input (PAD)
Definition pad.h:111
PadBuffer buf[2]
Definition pad.h:113
s32 flags
Definition pad.h:112
u8 act[2][6]
Definition pad.h:115
s16 repeatRate
vsyncs
Definition pad.h:116
PadSlot slots[2][4]
Definition pad.h:114
PadBuffer * demoData
a buffer a frame
Definition pad.h:118
s16 demoPad
Definition pad.h:117
s16 demoFrame
Definition pad.h:119
A port's receive buffer, and a frame of a demo: a pad, or a multitap and the four pads behind it.
Definition pad.h:66
u8 bytes[0x22]
Definition pad.h:67
PadData pad
Definition pad.h:68
u8 id
PAD_MULTITAP_ID.
Definition pad.h:71
struct PadBuffer::@11 tap
PadData pads[4]
Definition pad.h:72