libretro-nuklear-soft/libretro.c

303 lines
6.0 KiB
C
Raw Normal View History

2016-12-20 13:18:51 +00:00
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
2017-12-06 20:03:23 +00:00
#include <stdarg.h>
2016-12-20 13:18:51 +00:00
#include <string.h>
#include <math.h>
2017-12-06 20:59:43 +00:00
#include <stdio.h>
2017-12-06 20:03:23 +00:00
#if defined(_WIN32) && !defined(_XBOX)
#include <windows.h>
2016-12-27 15:55:06 +00:00
#endif
2017-12-06 20:59:43 +00:00
#include <libretro.h>
#include <gui.h>
2017-12-06 20:03:23 +00:00
2017-12-06 20:59:43 +00:00
#define VIDEO_WIDTH 1280
#define VIDEO_HEIGHT 720
2017-12-06 20:03:23 +00:00
#define VIDEO_PIXELS VIDEO_WIDTH * VIDEO_HEIGHT
static struct retro_log_callback logging;
2017-12-06 20:59:43 +00:00
retro_log_printf_t log_cb;
2017-12-06 20:03:23 +00:00
static bool use_audio_cb;
static float last_aspect;
static float last_sample_rate;
char retro_base_directory[4096];
char retro_game_path[4096];
2017-12-06 20:59:43 +00:00
static void fallback_log(enum retro_log_level level, const char *fmt, ...)
2016-12-20 13:18:51 +00:00
{
2017-12-06 20:03:23 +00:00
(void)level;
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
2016-12-20 13:18:51 +00:00
}
2017-12-06 20:03:23 +00:00
2017-12-06 20:59:43 +00:00
void context_reset(void)
{
rwidth = VIDEO_WIDTH;
rheight = VIDEO_HEIGHT;
log_cb(RETRO_LOG_INFO, "Context reset\n");
gui_init();
}
void context_destroy(void)
{
log_cb(RETRO_LOG_INFO, "Context destroy\n");
gui_free();
}
2017-12-06 20:03:23 +00:00
static retro_environment_t environ_cb;
2016-12-20 13:18:51 +00:00
void retro_init(void)
{
2017-12-06 20:03:23 +00:00
const char *dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
{
snprintf(retro_base_directory, sizeof(retro_base_directory), "%s", dir);
}
2016-12-20 13:18:51 +00:00
}
void retro_deinit(void)
{
2017-12-06 20:59:43 +00:00
context_destroy();
2017-12-06 20:03:23 +00:00
frame_buf = NULL;
2016-12-20 13:18:51 +00:00
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
void retro_set_controller_port_device(unsigned port, unsigned device)
{
2017-12-06 20:03:23 +00:00
log_cb(RETRO_LOG_INFO, "Plugging device %u into port %u.\n", device, port);
2016-12-20 13:18:51 +00:00
}
void retro_get_system_info(struct retro_system_info *info)
{
memset(info, 0, sizeof(*info));
2017-12-06 20:59:43 +00:00
info->library_name = "Nuklear";
2017-12-06 20:03:23 +00:00
info->library_version = "0.1";
2017-12-06 20:59:43 +00:00
info->need_fullpath = false;
info->valid_extensions = NULL;
2016-12-20 13:18:51 +00:00
}
2017-12-06 20:03:23 +00:00
static retro_video_refresh_t video_cb;
static retro_audio_sample_t audio_cb;
static retro_audio_sample_batch_t audio_batch_cb;
2017-12-06 20:59:43 +00:00
retro_input_poll_t input_poll_cb;
retro_input_state_t input_state_cb;
2016-12-20 13:18:51 +00:00
void retro_get_system_av_info(struct retro_system_av_info *info)
{
2017-12-06 20:03:23 +00:00
float aspect = 0.0f;
float sampling_rate = 30000.0f;
2016-12-20 13:18:51 +00:00
2017-12-06 20:03:23 +00:00
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;
2016-12-20 13:18:51 +00:00
2017-12-06 20:03:23 +00:00
last_aspect = aspect;
last_sample_rate = sampling_rate;
2016-12-20 13:18:51 +00:00
}
2017-12-06 20:03:23 +00:00
static struct retro_rumble_interface rumble;
2016-12-20 13:18:51 +00:00
void retro_set_environment(retro_environment_t cb)
{
environ_cb = cb;
2017-12-06 20:03:23 +00:00
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) },
};
2016-12-20 13:18:51 +00:00
2017-12-06 20:03:23 +00:00
static const struct retro_controller_info ports[] = {
{ controllers, 1 },
{ NULL, 0 },
2016-12-20 13:18:51 +00:00
};
2017-12-06 20:03:23 +00:00
cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
2017-12-06 20:59:43 +00:00
bool no_content = true;
cb(RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME, &no_content);
2016-12-20 13:18:51 +00:00
}
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;
}
2017-12-06 20:03:23 +00:00
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)
{
2016-12-20 13:18:51 +00:00
2017-12-06 20:03:23 +00:00
}
2016-12-20 13:18:51 +00:00
2017-12-06 20:03:23 +00:00
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;
}
2016-12-20 13:18:51 +00:00
void retro_run(void)
{
2017-12-06 20:03:23 +00:00
unsigned i;
update_input();
2016-12-20 13:18:51 +00:00
2017-12-06 20:03:23 +00:00
bool updated = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
2017-12-06 20:59:43 +00:00
gui_frame();
video_cb(frame_buf, rwidth, rheight, 0);
2016-12-20 13:18:51 +00:00
}
bool retro_load_game(const struct retro_game_info *info)
{
2017-12-06 20:03:23 +00:00
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 },
};
2017-12-06 20:59:43 +00:00
context_reset();
2017-12-06 20:03:23 +00:00
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
2016-12-20 13:18:51 +00:00
2017-12-06 20:59:43 +00:00
#ifdef M16B
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
#else
2017-12-06 20:03:23 +00:00
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
2017-12-06 20:59:43 +00:00
#endif
2016-12-20 13:18:51 +00:00
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{
2017-12-06 20:59:43 +00:00
#ifdef M16B
fprintf(stderr, "RGB565 is not supported.\n");
#else
fprintf(stderr, "XRGB8888 is not supported.\n");
#endif
2016-12-20 13:18:51 +00:00
return false;
}
2017-12-06 20:03:23 +00:00
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();
2016-12-20 13:18:51 +00:00
(void)info;
return true;
}
void retro_unload_game(void)
2017-12-06 20:03:23 +00:00
{
}
2016-12-20 13:18:51 +00:00
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)
{
2017-12-06 20:03:23 +00:00
return false;
2016-12-20 13:18:51 +00:00
}
2017-12-06 20:03:23 +00:00
bool retro_serialize(void *data_, size_t size)
2016-12-20 13:18:51 +00:00
{
return false;
}
2017-12-06 20:03:23 +00:00
bool retro_unserialize(const void *data_, size_t size)
2016-12-20 13:18:51 +00:00
{
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;
}