Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/src/pc/controller/controller_keyboard.c
7861 views
1
#include <stdbool.h>
2
#include <ultra64.h>
3
4
#include "controller_api.h"
5
6
#ifdef TARGET_WEB
7
#include "controller_emscripten_keyboard.h"
8
#endif
9
10
#include "game/settings.h"
11
12
static int keyboard_buttons_down;
13
14
static long mouse_x;
15
static long mouse_y;
16
static s8 mouse_wheel;
17
18
static int keyboard_mapping[15][2];
19
20
static int keyboard_map_scancode(int scancode) {
21
int ret = 0;
22
for (size_t i = 0; i < sizeof(keyboard_mapping) / sizeof(keyboard_mapping[0]); i++) {
23
if (keyboard_mapping[i][0] == scancode) {
24
ret |= keyboard_mapping[i][1];
25
}
26
}
27
return ret;
28
}
29
30
bool keyboard_on_key_down(int scancode) {
31
int mapped = keyboard_map_scancode(scancode);
32
keyboard_buttons_down |= mapped;
33
return mapped != 0;
34
}
35
36
bool keyboard_on_key_up(int scancode) {
37
int mapped = keyboard_map_scancode(scancode);
38
keyboard_buttons_down &= ~mapped;
39
return mapped != 0;
40
}
41
42
void keyboard_on_all_keys_up(void) {
43
keyboard_buttons_down = 0;
44
}
45
46
void keyboard_on_mouse_move(long x, long y) {
47
mouse_x = x;
48
mouse_y = y;
49
}
50
51
void keyboard_on_mouse_press(s8 left, s8 right, s8 middle, s8 wheel) {
52
if (!configMouseCam)
53
return;
54
55
if (left > 0)
56
keyboard_buttons_down |= configMouseLeft;
57
if (left < 0)
58
keyboard_buttons_down &= ~configMouseLeft;
59
60
if (right > 0)
61
keyboard_buttons_down |= configMouseRight;
62
if (right < 0)
63
keyboard_buttons_down &= ~configMouseRight;
64
65
if (middle > 0)
66
keyboard_buttons_down |= configMouseMiddle;
67
if (middle < 0)
68
keyboard_buttons_down &= ~configMouseMiddle;
69
70
if (wheel < 0) {
71
keyboard_buttons_down |= configMouseWheelDown;
72
mouse_wheel = wheel;
73
}
74
if (wheel > 0) {
75
keyboard_buttons_down |= configMouseWheelUp;
76
mouse_wheel = wheel;
77
}
78
}
79
80
static void set_keyboard_mapping(int index, int mask, int scancode) {
81
keyboard_mapping[index][0] = scancode;
82
keyboard_mapping[index][1] = mask;
83
}
84
85
static void keyboard_init(void) {
86
int i = 0;
87
88
set_keyboard_mapping(i++, 0x80000, configKeyStickUp);
89
set_keyboard_mapping(i++, 0x10000, configKeyStickLeft);
90
set_keyboard_mapping(i++, 0x40000, configKeyStickDown);
91
set_keyboard_mapping(i++, 0x20000, configKeyStickRight);
92
set_keyboard_mapping(i++, A_BUTTON, configKeyA);
93
set_keyboard_mapping(i++, B_BUTTON, configKeyB);
94
set_keyboard_mapping(i++, Z_TRIG, configKeyZ);
95
if (configImprovedCamera) {
96
set_keyboard_mapping(i++, U_CBUTTONS, configKeyCUp);
97
set_keyboard_mapping(i++, 0x100000, configKeyCLeft);
98
set_keyboard_mapping(i++, D_CBUTTONS, configKeyCDown);
99
set_keyboard_mapping(i++, 0x200000, configKeyCRight);
100
}
101
else {
102
set_keyboard_mapping(i++, U_CBUTTONS, configKeyCUp);
103
set_keyboard_mapping(i++, L_CBUTTONS, configKeyCLeft);
104
set_keyboard_mapping(i++, D_CBUTTONS, configKeyCDown);
105
set_keyboard_mapping(i++, R_CBUTTONS, configKeyCRight);
106
}
107
set_keyboard_mapping(i++, L_TRIG, configKeyL);
108
set_keyboard_mapping(i++, R_TRIG, configKeyR);
109
set_keyboard_mapping(i++, START_BUTTON, configKeyStart);
110
set_keyboard_mapping(i++, 0x1000000, configKeyWalk);
111
112
#ifdef TARGET_WEB
113
controller_emscripten_keyboard_init();
114
#endif
115
}
116
117
static void keyboard_read(OSContPad *pad) {
118
// Camera movement with mouse
119
if (configMouseCam) {
120
if (mouse_x != 0)
121
pad->stick2_x = mouse_x*configMouseSensitivity;
122
if (mouse_y != 0)
123
pad->stick2_y = mouse_y*configMouseSensitivity;
124
if (mouse_wheel < -1) {
125
keyboard_buttons_down &= ~configMouseWheelDown;
126
mouse_wheel = 0;
127
}
128
if (mouse_wheel > 1) {
129
keyboard_buttons_down &= ~configMouseWheelUp;
130
mouse_wheel = 0;
131
}
132
mouse_wheel *= 2;
133
}
134
135
pad->button |= keyboard_buttons_down;
136
if ((keyboard_buttons_down & 0x30000) == 0x10000) {
137
pad->stick_x = (keyboard_buttons_down & 0x1000000) ? -32 : -128;
138
}
139
if ((keyboard_buttons_down & 0x30000) == 0x20000) {
140
pad->stick_x = (keyboard_buttons_down & 0x1000000) ? 32 : 127;
141
}
142
if ((keyboard_buttons_down & 0xc0000) == 0x40000) {
143
pad->stick_y = (keyboard_buttons_down & 0x1000000) ? -32 : -128;
144
}
145
if ((keyboard_buttons_down & 0xc0000) == 0x80000) {
146
pad->stick_y = (keyboard_buttons_down & 0x1000000) ? 32 : 127;
147
}
148
149
if ((keyboard_buttons_down & 0x300000) == 0x100000) {
150
pad->stick2_x = -80;
151
}
152
if ((keyboard_buttons_down & 0x300000) == 0x200000) {
153
pad->stick2_x = 80;
154
}
155
if ((keyboard_buttons_down & 0xc00000) == 0x400000) {
156
pad->stick2_y = -80;
157
}
158
if ((keyboard_buttons_down & 0xc00000) == 0x800000) {
159
pad->stick2_y = 80;
160
}
161
}
162
163
struct ControllerAPI controller_keyboard = {
164
keyboard_init,
165
keyboard_read,
166
NULL,
167
NULL
168
};
169