/
retromapper.c
executable file
·387 lines (321 loc) · 10.4 KB
/
retromapper.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
void retro_poll_mame_input();
static int rtwi=320,rthe=240,topw=1024; // DEFAULT TEXW/TEXH/PITCH
int SHIFTON=-1;
char RPATH[512];
extern "C" int mmain(int argc, const char *argv);
extern bool draw_this_frame;
#if !defined(HAVE_OPENGL) && !defined(HAVE_OPENGLES) && !defined(HAVE_RGB32)
#define M16B
#endif
#ifdef M16B
uint16_t videoBuffer[1024*1024];
#define PITCH 1
#else
unsigned int videoBuffer[1024*1024];
#define PITCH 2*1
#endif
retro_video_refresh_t video_cb = NULL;
retro_environment_t environ_cb = NULL;
const char *retro_save_directory;
const char *retro_system_directory;
const char *retro_content_directory;
retro_log_printf_t log_cb;
static retro_input_state_t input_state_cb = NULL;
static retro_audio_sample_batch_t audio_batch_cb = NULL;
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
#include "retroogl.c"
#endif
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
static retro_input_poll_t input_poll_cb;
void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
void retro_set_audio_sample(retro_audio_sample_t cb) { }
void retro_set_environment(retro_environment_t cb)
{
static const struct retro_variable vars[] = {
{ "mame_current_mouse_enable", "Mouse supported; disabled|enabled" },
{ "mame_current_videoapproach1_enable", "Video approach 1 Enabled; disabled|enabled" },
{ "mame_current_skip_nagscreen", "Hide nag screen; disabled|enabled" },
{ "mame_current_skip_gameinfo", "Hide game info screen; disabled|enabled" },
{ "mame_current_skip_warnings", "Hide warning screen; disabled|enabled" },
{ "mame_current_turbo_button", "Enable autofire; disabled|button 1|button 2|R2 to button 1 mapping|R2 to button 2 mapping" },
{ "mame_current_turbo_delay", "Set autofire pulse speed; medium|slow|fast" },
{ NULL, NULL },
};
environ_cb = cb;
cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
}
static void check_variables(void)
{
struct retro_variable var = {0};
var.key = "mame_current_mouse_enable";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
fprintf(stderr, "value: %s\n", var.value);
if (!strcmp(var.value, "disabled"))
mouse_enable = false;
if (!strcmp(var.value, "enabled"))
mouse_enable = true;
}
var.key = "mame_current_skip_nagscreen";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
fprintf(stderr, "value: %s\n", var.value);
if (!strcmp(var.value, "disabled"))
hide_nagscreen = false;
if (!strcmp(var.value, "enabled"))
hide_nagscreen = true;
}
var.key = "mame_current_skip_gameinfo";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
fprintf(stderr, "value: %s\n", var.value);
if (!strcmp(var.value, "disabled"))
hide_gameinfo = false;
if (!strcmp(var.value, "enabled"))
hide_gameinfo = true;
}
var.key = "mame_current_skip_warnings";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
fprintf(stderr, "value: %s\n", var.value);
if (!strcmp(var.value, "disabled"))
hide_warnings = false;
if (!strcmp(var.value, "enabled"))
hide_warnings = true;
}
var.key = "mame_current_videoapproach1_enable";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
fprintf(stderr, "value: %s\n", var.value);
if (!strcmp(var.value, "disabled"))
videoapproach1_enable = false;
if (!strcmp(var.value, "enabled"))
videoapproach1_enable = true;
}
var.key = "mame_current_turbo_button";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp(var.value, "button 1"))
turbo_enable = 1;
else if (!strcmp(var.value, "button 2"))
turbo_enable = 2;
else if (!strcmp(var.value, "R2 to button 1 mapping"))
turbo_enable = 3;
else if (!strcmp(var.value, "R2 to button 2 mapping"))
turbo_enable = 4;
else
turbo_enable = 0;
}
var.key = "mame_current_turbo_delay";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp(var.value, "medium"))
turbo_delay = 5;
else if (!strcmp(var.value, "slow"))
turbo_delay = 7;
else
turbo_delay = 3;
}
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
static void update_geometry()
{
struct retro_system_av_info av_info;
retro_get_system_av_info( &av_info);
environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &av_info);
}
void retro_get_system_info(struct retro_system_info *info)
{
memset(info, 0, sizeof(*info));
info->library_name = "MAME 2010";
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
info->library_version = "0.139" GIT_VERSION;
info->valid_extensions = "zip|chd|7z";
info->need_fullpath = true;
info->block_extract = true;
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
info->geometry.base_width = rtwi;
info->geometry.base_height = rthe;
info->geometry.max_width = 1024;
info->geometry.max_height = 768;
float display_ratio = vertical ? (float)rthe / (float)rtwi : (float)rtwi / (float)rthe;
info->geometry.aspect_ratio = display_ratio;
write_log("display aspect ratio = %f \n", display_ratio);
info->timing.fps = 60;
info->timing.sample_rate = 48000.0;
}
void retro_init (void)
{
struct retro_log_callback log;
const char *system_dir = NULL;
const char *content_dir = NULL;
const char *save_dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
log_cb = log.log;
else
log_cb = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_dir) && system_dir)
{
/* if defined, use the system directory */
retro_system_directory = system_dir;
}
if (log_cb)
log_cb(RETRO_LOG_INFO, "SYSTEM_DIRECTORY: %s", retro_system_directory);
if (environ_cb(RETRO_ENVIRONMENT_GET_CONTENT_DIRECTORY, &content_dir) && content_dir)
{
// if defined, use the system directory
retro_content_directory=content_dir;
}
if (log_cb)
log_cb(RETRO_LOG_INFO, "CONTENT_DIRECTORY: %s", retro_content_directory);
if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_dir) && save_dir)
{
/* If save directory is defined use it,
* otherwise use system directory. */
retro_save_directory = *save_dir ? save_dir : retro_system_directory;
}
else
{
/* make retro_save_directory the same,
* in case RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY
* is not implemented by the frontend. */
retro_save_directory=retro_system_directory;
}
if (log_cb)
log_cb(RETRO_LOG_INFO, "SAVE_DIRECTORY: %s", retro_save_directory);
}
extern void retro_finish();
extern void retro_main_loop();
int RLOOP=1;
void retro_deinit(void)
{
if(retro_load_ok)retro_finish();
LOGI("Retro DeInit\n");
}
void retro_reset (void)
{
mame_reset = 1;
}
void retro_run (void)
{
bool updated = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
retro_main_loop();
retro_poll_mame_input();
RLOOP=1;
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
do_gl2d();
#else
if (draw_this_frame)
video_cb(videoBuffer,rtwi, rthe, topw << PITCH);
else
video_cb(NULL,rtwi, rthe, topw << PITCH);
#endif
turbo_state > turbo_delay ? turbo_state = 0 : turbo_state++;
}
void prep_retro_rotation(int rot)
{
LOGI("Rotation:%d\n",rot);
environ_cb(RETRO_ENVIRONMENT_SET_ROTATION, &rot);
}
/*
static void keyboard_cb(bool down, unsigned keycode, uint32_t character, uint16_t mod)
{
#ifdef KEYDBG
printf( "Down: %s, Code: %d, Char: %u, Mod: %u. \n",
down ? "yes" : "no", keycode, character, mod);
#endif
if (keycode>=320);
else
{
if(down && keycode==RETROK_LSHIFT)
{
SHIFTON=-SHIFTON;
if(SHIFTON == 1)
retrokbd_state[keycode]=1;
else
retrokbd_state[keycode]=0;
}
else if(keycode!=RETROK_LSHIFT)
{
if (down)
retrokbd_state[keycode]=1;
else if (!down)
retrokbd_state[keycode]=0;
}
}
}
*/
bool retro_load_game(const struct retro_game_info *info)
{
char basename[128];
#if 0
struct retro_keyboard_callback cb = { keyboard_cb };
environ_cb(RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK, &cb);
#endif
#ifndef M16B
enum retro_pixel_format fmt =RETRO_PIXEL_FORMAT_XRGB8888;
#else
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
#endif
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{
fprintf(stderr, "RGB pixel format is not supported.\n");
exit(0);
}
check_variables();
#ifdef M16B
memset(videoBuffer,0,1024*1024*2);
#else
memset(videoBuffer,0,1024*1024*2*2);
#endif
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
#ifdef HAVE_OPENGLES
hw_render.context_type = RETRO_HW_CONTEXT_OPENGLES2;
#else
hw_render.context_type = RETRO_HW_CONTEXT_OPENGL;
#endif
hw_render.context_reset = context_reset;
hw_render.context_destroy = context_destroy;
if (!environ_cb(RETRO_ENVIRONMENT_SET_HW_RENDER, &hw_render))
return false;
#endif
basename[0] = '\0';
extract_basename(basename, info->path, sizeof(basename));
extract_directory(g_rom_dir, info->path, sizeof(g_rom_dir));
strcpy(RPATH,info->path);
mmain(1,RPATH);
retro_load_ok = true;
return 1;
}
void retro_unload_game(void)
{
if(pauseg==0)
pauseg=-1;
LOGI("Retro unload_game\n");
}
// Stubs
size_t retro_serialize_size(void){ return 0; }
bool retro_serialize(void *data, size_t size){ return false; }
bool retro_unserialize(const void * data, size_t size){ return false; }
unsigned retro_get_region (void) {return RETRO_REGION_NTSC;}
void *retro_get_memory_data(unsigned type) {return 0;}
size_t retro_get_memory_size(unsigned type) {return 0;}
bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info){return false;}
void retro_cheat_reset(void){}
void retro_cheat_set(unsigned unused, bool unused1, const char* unused2){}
void retro_set_controller_port_device(unsigned in_port, unsigned device){}