-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdl.diff
342 lines (327 loc) · 8.88 KB
/
sdl.diff
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
diff -uarN jcgo_Out/GameShll.c jcgo_Out_Patched/GameShll.c
--- jcgo_Out/GameShll.c 2021-07-17 14:14:57.909182969 -0500
+++ jcgo_Out_Patched/GameShll.c 2021-07-17 14:14:38.724958991 -0500
@@ -12,6 +12,128 @@
#include "jcgobchk.h"
#endif
+#include <stdio.h>
+
+void get_keycodes(jchar* charCode, jint* code) {
+ switch (event.key.keysym.scancode) {
+ case SDL_SCANCODE_LEFT:
+ *code = 37;
+ *charCode = 65535;
+ break;
+ case SDL_SCANCODE_RIGHT:
+ *code = 39;
+ *charCode = 65535;
+ break;
+ case SDL_SCANCODE_UP:
+ *code = 38;
+ *charCode = 65535;
+ break;
+ case SDL_SCANCODE_DOWN:
+ *code = 40;
+ *charCode = 65535;
+ break;
+ default:
+ *charCode = event.key.keysym.sym;
+ const char* keyName = SDL_GetKeyName(event.key.keysym.sym);
+
+ if (strlen(keyName) == 1) {
+ *code = keyName[0];
+ } else {
+ *code = *charCode;
+ }
+
+ // absolutely dumb hack but i don't want to use SDL's textinput
+ if (event.key.keysym.mod & KMOD_SHIFT) {
+ if (*charCode >= 'a' && *charCode <= 'z') {
+ *charCode -= 32;
+ } else {
+ switch (*charCode) {
+ case ';':
+ *charCode = ':';
+ break;
+ case '`':
+ *charCode = '~';
+ break;
+ case '1':
+ *charCode = '!';
+ break;
+ case '2':
+ *charCode = '@';
+ break;
+ case '3':
+ *charCode = '#';
+ break;
+ case '4':
+ *charCode = '$';
+ break;
+ case '5':
+ *charCode = '%';
+ break;
+ case '6':
+ *charCode = '^';
+ break;
+ case '7':
+ *charCode = '&';
+ break;
+ case '8':
+ *charCode = '*';
+ break;
+ case '9':
+ *charCode = '(';
+ break;
+ case '0':
+ *charCode = ')';
+ break;
+ case '-':
+ *charCode = '_';
+ break;
+ case '=':
+ *charCode = '+';
+ break;
+ }
+ }
+ }
+ break;
+ }
+}
+
+void handle_sdl_events(package_GameShell This) {
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ exit(0);
+ break;
+ case SDL_KEYDOWN: {
+ jchar charCode;
+ jint code;
+ get_keycodes(&charCode, &code);
+ (package_GameShell__keyPressed__CI(This, code, charCode));
+ break;
+ }
+ case SDL_KEYUP: {
+ jchar charCode;
+ jint code;
+ get_keycodes(&charCode, &code);
+ (package_GameShell__keyReleased__I(This, code));
+ break;
+ }
+ case SDL_MOUSEMOTION:
+ (package_GameShell__mouseMoved__II(This, event.motion.x, event.motion.y));
+ break;
+ case SDL_MOUSEBUTTONDOWN: {
+ jint button = event.button.button == 3 ? 2 : 1;
+ (package_GameShell__mousePressed__III(This, event.button.x, event.button.y,
+ button));
+ break;
+ }
+ case SDL_MOUSEBUTTONUP:
+ (package_GameShell__mouseReleased__II(This, event.button.x,
+ event.button.y));
+ break;
+ }
+ }
+}
+
JCGO_NOSEP_INLINE void CFASTCALL
package_GameShell__keyPressed__CI( package_GameShell This, jint code,
jchar chr )
@@ -398,8 +520,25 @@
{
{
;
- JCGO_FIELD_NZACCESS(This, appletWidth)= (jint)512;
- JCGO_FIELD_NZACCESS(This, appletHeight)= (jint)344;
+ jint width;
+ jint height;
+ char* env_width = getenv("MUDCLIENT_WIDTH");
+ char* env_height = getenv("MUDCLIENT_HEIGHT");
+
+ if (env_width) {
+ width = atoi(env_width);
+ } else {
+ width = 512;
+ }
+
+ if (env_height) {
+ height = atoi(env_height);
+ } else {
+ height = 344;
+ }
+
+ JCGO_FIELD_NZACCESS(This, appletWidth)= (jint)width;
+ JCGO_FIELD_NZACCESS(This, appletHeight)= (jint)height;
JCGO_FIELD_NZACCESS(This, targetFps)= (jint)20;
JCGO_FIELD_NZACCESS(This, maxDrawTime)= (jint)1000;
JCGO_FIELD_NZACCESS(This, timings)= (jlongArr)jcgo_newArray(JCGO_CORECLASS_FOR(OBJT_jlong),
@@ -507,6 +646,34 @@
package_GameShell__run__( package_GameShell This )
{
{
+ jint width = JCGO_FIELD_NZACCESS(This, appletWidth);
+ jint height = JCGO_FIELD_NZACCESS(This, appletHeight) + 2;
+
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
+ fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError());
+ exit(1);
+ }
+
+ wanted_audio.freq = 8000;
+ wanted_audio.format = AUDIO_S16;
+ wanted_audio.channels = 1;
+ wanted_audio.silence = 0;
+ wanted_audio.samples = 1024;
+ wanted_audio.callback = NULL;
+
+ if (SDL_OpenAudio(&wanted_audio, NULL) < 0) {
+ fprintf(stderr, "SDL_OpenAudio(): %s\n", SDL_GetError());
+ exit(1);
+ }
+
+ SDL_PauseAudio(0);
+
+ window = SDL_CreateWindow("Runescape by Andrew Gower", SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
+ screen = SDL_GetWindowSurface(window);
+ pixel_surface = SDL_CreateRGBSurface(0, width, height, 32, 0xff0000, 0x00ff00,
+ 0x0000ff, 0);
+
jint i;
jint j;
jint sleep;
@@ -626,6 +793,7 @@
while (i1 < (jint)256)
{
jint jcgo_rcvrI1;
+ handle_sdl_events(This);
(JCGO_CALL_NZVFUNC(This)->handleInputs__(
This));
i1+= j;
diff -uarN jcgo_Out/KeyEvent.h jcgo_Out_Patched/KeyEvent.h
--- jcgo_Out/KeyEvent.h 2021-07-17 14:14:57.981183810 -0500
+++ jcgo_Out_Patched/KeyEvent.h 2021-07-17 14:14:38.696958665 -0500
@@ -2,21 +2,21 @@
#ifdef JCGO_116
-#define package_KeyEvent__VK_LEFT (-(jint)12345678L)
+#define package_KeyEvent__VK_LEFT (37)
-#define package_KeyEvent__VK_RIGHT (-(jint)12345678L)
+#define package_KeyEvent__VK_RIGHT (39)
-#define package_KeyEvent__VK_UP (-(jint)12345678L)
+#define package_KeyEvent__VK_UP (38)
-#define package_KeyEvent__VK_DOWN (-(jint)12345678L)
+#define package_KeyEvent__VK_DOWN (40)
-#define package_KeyEvent__VK_SPACE (-(jint)12345678L)
+#define package_KeyEvent__VK_SPACE (SDLK_SPACE)
-#define package_KeyEvent__VK_F1 (-(jint)12345678L)
+#define package_KeyEvent__VK_F1 (SDL_SCANCODE_F1)
-#define package_KeyEvent__VK_BACK_SPACE (-(jint)12345678L)
+#define package_KeyEvent__VK_BACK_SPACE (SDLK_BACKSPACE)
-#define package_KeyEvent__VK_ENTER (-(jint)12345678L)
+#define package_KeyEvent__VK_ENTER (SDLK_RETURN)
JCGO_NOSEP_STATIC package_KeyEvent CFASTCALL
package_KeyEvent__this__( package_KeyEvent This );
diff -uarN jcgo_Out/Main.c jcgo_Out_Patched/Main.c
--- jcgo_Out/Main.c 2021-07-17 14:14:58.117185399 -0500
+++ jcgo_Out_Patched/Main.c 2021-07-17 14:14:38.708958804 -0500
@@ -1222,6 +1222,9 @@
MAINENTRY
( int argc, JCGO_MAIN_TCHAR **targv )
{
+#ifdef __MINGW32__
+ putenv("SDL_AUDIODRIVER=DirectSound");
+#endif
JCGO_MAIN_LAUNCH(argc, targv);
return 0;
}
diff -uarN jcgo_Out/Main.h jcgo_Out_Patched/Main.h
--- jcgo_Out/Main.h 2021-07-17 14:14:58.057184698 -0500
+++ jcgo_Out_Patched/Main.h 2021-07-17 14:14:38.724958991 -0500
@@ -1557,4 +1557,16 @@
JCGO_SEP_EXTERN CONST struct java_lang_Object_methods_s jObjectArr15_methods;
JCGO_SEP_EXTERN CONST struct java_lang_Object_methods_s jObjectArr16_methods;
+#define SDL_MAIN_HANDLED
+
+#include <SDL2/SDL.h>
+
+SDL_Window *window;
+SDL_Surface *screen;
+SDL_Surface *pixel_surface;
+SDL_Event event;
+SDL_AudioSpec wanted_audio;
+
+short pcm_out[1024 * 1024];
+
#endif
diff -uarN jcgo_Out/StrmAdPl.c jcgo_Out_Patched/StrmAdPl.c
--- jcgo_Out/StrmAdPl.c 2021-07-17 14:14:58.041184512 -0500
+++ jcgo_Out_Patched/StrmAdPl.c 2021-07-17 14:14:38.724958991 -0500
@@ -12,11 +12,33 @@
#include "jcgobchk.h"
#endif
+#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
+#define QUANT_MASK (0xf) /* Quantization field mask. */
+#define SEG_SHIFT (4) /* Left shift for segment number. */
+#define SEG_MASK (0x70) /* Segment field mask. */
+#define BIAS (0x84) /* Bias for linear code (for u-law). */
+
+int ulaw2linear(long size, unsigned char *u_ptr, short *out_ptr) {
+ short t;
+ unsigned char u_val;
+
+ for (long i=0; i< size; i++) {
+ u_val = ~(*u_ptr);
+ u_ptr++;
+ t = ((u_val & QUANT_MASK) << 3) + BIAS;
+ t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
+ *out_ptr++ = ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
+ }
+
+ return 0;
+}
+
JCGO_NOSEP_FRWINL void CFASTCALL
package_StreamAudioPlayer__stopPlayer__( package_StreamAudioPlayer
This )
{
{
+ SDL_PauseAudio(1);
}
}
@@ -26,6 +48,14 @@
{
JCGO_SYNC_BLOCKSAFENZ(This)
{
+ unsigned char ulaw[len];
+
+ for (int i = 0; i < len; i += 1) {
+ ulaw[i] = JCGO_ARRAY_BACCESS(buff, i + off);
+ }
+
+ ulaw2linear(len, ulaw, pcm_out);
+ SDL_QueueAudio(1, pcm_out, len * 2);
}
JCGO_SYNC_END
}
diff -uarN jcgo_Out/Surface.c jcgo_Out_Patched/Surface.c
--- jcgo_Out/Surface.c 2021-07-17 14:14:58.025184324 -0500
+++ jcgo_Out_Patched/Surface.c 2021-07-17 14:14:38.716958898 -0500
@@ -786,6 +786,17 @@
{
(package_Surface__setcomplete__(
This));
+
+ if (!window) {
+ return;
+ }
+
+ jint* pixel_bytes = (jint*) JCGO_FIELD_NZACCESS(This, pixels);
+ jint area = JCGO_FIELD_NZACCESS(This, width2) * JCGO_FIELD_NZACCESS(This,
+ height2);
+ memcpy(pixel_surface->pixels, pixel_bytes + 5, area * sizeof(jint));
+ SDL_BlitSurface(pixel_surface, NULL, screen, NULL);
+ SDL_UpdateWindowSurface(window);
}
}