-
Notifications
You must be signed in to change notification settings - Fork 0
/
libretro.c
executable file
·285 lines (223 loc) · 5.86 KB
/
libretro.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#if defined(_WIN32) && !defined(_XBOX)
#include <windows.h>
#endif
#include "libretro.h"
#include <switch.h>
#define VIDEO_WIDTH 256
#define VIDEO_HEIGHT 384
#define VIDEO_PIXELS VIDEO_WIDTH * VIDEO_HEIGHT
static uint8_t *frame_buf;
static struct retro_log_callback logging;
static retro_log_printf_t log_cb;
static bool use_audio_cb;
static float last_aspect;
static float last_sample_rate;
char retro_base_directory[4096];
char retro_game_path[4096];
static void fallback_log(enum retro_log_level level, const char *fmt, ...)
{
(void)level;
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
}
static retro_environment_t environ_cb;
void retro_init(void)
{
frame_buf = (uint8_t*)malloc(VIDEO_PIXELS * sizeof(uint32_t));
const char *dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
{
snprintf(retro_base_directory, sizeof(retro_base_directory), "%s", dir);
}
}
void retro_deinit(void)
{
free(frame_buf);
frame_buf = NULL;
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
void retro_set_controller_port_device(unsigned port, unsigned device)
{
log_cb(RETRO_LOG_INFO, "Plugging device %u into port %u.\n", device, port);
}
void retro_get_system_info(struct retro_system_info *info)
{
memset(info, 0, sizeof(*info));
info->library_name = "trampoline";
info->library_version = "1.0";
info->need_fullpath = true;
info->valid_extensions = "nro";
}
static retro_video_refresh_t video_cb;
static retro_audio_sample_t audio_cb;
static retro_audio_sample_batch_t audio_batch_cb;
static retro_input_poll_t input_poll_cb;
static retro_input_state_t input_state_cb;
void retro_get_system_av_info(struct retro_system_av_info *info)
{
float aspect = 0.0f;
float sampling_rate = 30000.0f;
info->geometry.base_width = VIDEO_WIDTH;
info->geometry.base_height = VIDEO_HEIGHT;
info->geometry.max_width = VIDEO_WIDTH;
info->geometry.max_height = VIDEO_HEIGHT;
info->geometry.aspect_ratio = aspect;
last_aspect = aspect;
last_sample_rate = sampling_rate;
}
static struct retro_rumble_interface rumble;
void retro_set_environment(retro_environment_t cb)
{
environ_cb = cb;
if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
log_cb = logging.log;
else
log_cb = fallback_log;
static const struct retro_controller_description controllers[] = {
{ "Nintendo DS", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0) },
};
static const struct retro_controller_info ports[] = {
{ controllers, 1 },
{ NULL, 0 },
};
cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
}
void retro_set_audio_sample(retro_audio_sample_t cb)
{
audio_cb = cb;
}
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
{
audio_batch_cb = cb;
}
void retro_set_input_poll(retro_input_poll_t cb)
{
input_poll_cb = cb;
}
void retro_set_input_state(retro_input_state_t cb)
{
input_state_cb = cb;
}
void retro_set_video_refresh(retro_video_refresh_t cb)
{
video_cb = cb;
}
static unsigned x_coord;
static unsigned y_coord;
static unsigned phase;
static int mouse_rel_x;
static int mouse_rel_y;
void retro_reset(void)
{
x_coord = 0;
y_coord = 0;
}
static void update_input(void)
{
}
static void check_variables(void)
{
}
static void audio_callback(void)
{
for (unsigned i = 0; i < 30000 / 60; i++, phase++)
{
int16_t val = 0x800 * sinf(2.0f * M_PI * phase * 300.0f / 30000.0f);
audio_cb(val, val);
}
phase %= 100;
}
static void audio_set_state(bool enable)
{
(void)enable;
}
void retro_run(void)
{
unsigned i;
update_input();
bool updated = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
printf("Shutting down...\n");
environ_cb(RETRO_ENVIRONMENT_SHUTDOWN, NULL);
}
bool retro_load_game(const struct retro_game_info *info)
{
struct retro_input_descriptor desc[] = {
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 0 },
};
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{
log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported.\n");
return false;
}
snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path);
struct retro_audio_callback audio_cb = { audio_callback, audio_set_state };
use_audio_cb = environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_CALLBACK, &audio_cb);
check_variables();
(void)info;
char path[255];
snprintf(path, sizeof(path), "sdmc:%s", info->path, info->path);
envSetNextLoad(path, info->path);
printf("Set %s as path, %s as args\n", path, info->path);
return true;
}
void retro_unload_game(void)
{
}
unsigned retro_get_region(void)
{
return RETRO_REGION_NTSC;
}
bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num)
{
return false;
}
size_t retro_serialize_size(void)
{
return false;
}
bool retro_serialize(void *data_, size_t size)
{
return false;
}
bool retro_unserialize(const void *data_, size_t size)
{
return false;
}
void *retro_get_memory_data(unsigned id)
{
(void)id;
return NULL;
}
size_t retro_get_memory_size(unsigned id)
{
(void)id;
return 0;
}
void retro_cheat_reset(void)
{}
void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
(void)index;
(void)enabled;
(void)code;
}